A giant hack to cut down code needed for runtime block serialization by 50%

this also avoids repeated information and inconsistencies.
This commit is contained in:
Dylan K. Taylor 2022-07-18 18:25:41 +01:00
parent cf34f88a67
commit 6d4279671e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
90 changed files with 380 additions and 717 deletions

View File

@ -41,6 +41,7 @@ use function dirname;
use function file_put_contents; use function file_put_contents;
use function implode; use function implode;
use function ksort; use function ksort;
use function lcfirst;
use function log; use function log;
use function ob_get_clean; use function ob_get_clean;
use function ob_start; use function ob_start;
@ -58,9 +59,9 @@ function buildWriterFunc(string $virtualTypeName, string $nativeTypeName, array
$bits = getBitsRequired($memberNames); $bits = getBitsRequired($memberNames);
$lines = []; $lines = [];
$functionName = "write$virtualTypeName"; $functionName = lcfirst($virtualTypeName);
$lines[] = "public function $functionName(\\$nativeTypeName \$value) : void{"; $lines[] = "public function $functionName(\\$nativeTypeName \$value) : void{";
$lines[] = "\t\$this->writeInt($bits, match(\$value){"; $lines[] = "\t\$this->int($bits, match(\$value){";
foreach($memberNames as $key => $memberName){ foreach($memberNames as $key => $memberName){
$lines[] = "\t\t$memberName => $key,"; $lines[] = "\t\t$memberName => $key,";
@ -83,9 +84,9 @@ function buildReaderFunc(string $virtualTypeName, string $nativeTypeName, array
$bits = getBitsRequired($memberNames); $bits = getBitsRequired($memberNames);
$lines = []; $lines = [];
$functionName = "read$virtualTypeName"; $functionName = lcfirst($virtualTypeName);
$lines[] = "public function $functionName() : \\$nativeTypeName{"; $lines[] = "public function $functionName(\\$nativeTypeName &\$value) : void{";
$lines[] = "\treturn match(\$this->readInt($bits)){"; $lines[] = "\t\$value = match(\$this->readInt($bits)){";
foreach($memberNames as $key => $memberName){ foreach($memberNames as $key => $memberName){
$lines[] = "\t\t$key => $memberName,"; $lines[] = "\t\t$key => $memberName,";
@ -167,12 +168,12 @@ $enumsUsed = [
$readerFuncs = [ $readerFuncs = [
"" => [ "" => [
"abstract public function readInt(int \$bits) : int;" "abstract protected function readInt(int \$bits) : int;"
] ]
]; ];
$writerFuncs = [ $writerFuncs = [
"" => [ "" => [
"abstract public function writeInt(int \$bits, int \$value) : self;" "abstract public function int(int \$bits, int \$value) : void;"
] ]
]; ];
$functionName = ""; $functionName = "";

View File

@ -49,22 +49,14 @@ class Anvil extends Transparent implements Fallable{
public function getRequiredTypeDataBits() : int{ return 2; } public function getRequiredTypeDataBits() : int{ return 2; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setDamage($r->readBoundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED)); $w->boundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED, $this->damage);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED, $this->getDamage());
} }
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setFacing($r->readHorizontalFacing()); $w->horizontalFacing($this->facing);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->getFacing());
} }
public function getDamage() : int{ return $this->damage; } public function getDamage() : int{ return $this->damage; }

View File

@ -58,16 +58,10 @@ class Bamboo extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setLeafSize($r->readBoundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES)); $w->boundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES, $this->leafSize);
$this->setThick($r->readBool()); $w->bool($this->thick);
$this->setReady($r->readBool()); $w->bool($this->ready);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES, $this->getLeafSize());
$w->writeBool($this->isThick());
$w->writeBool($this->isReady());
} }
public function isThick() : bool{ return $this->thick; } public function isThick() : bool{ return $this->thick; }

View File

@ -39,12 +39,8 @@ final class BambooSapling extends Flowable{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setReady($r->readBool()); $w->bool($this->ready);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->isReady());
} }
public function isReady() : bool{ return $this->ready; } public function isReady() : bool{ return $this->ready; }

View File

@ -41,14 +41,9 @@ class Barrel extends Opaque{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setFacing($r->readFacing()); $w->facing($this->facing);
$this->setOpen($r->readBool()); $w->bool($this->open);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->getFacing());
$w->writeBool($this->isOpen());
} }
public function isOpen() : bool{ public function isOpen() : bool{

View File

@ -56,16 +56,10 @@ class Bed extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->occupied = $r->readBool(); $w->bool($this->occupied);
$this->head = $r->readBool(); $w->bool($this->head);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->occupied);
$w->writeBool($this->head);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -31,12 +31,8 @@ class Bedrock extends Opaque{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->burnsForever = $r->readBool(); $w->bool($this->burnsForever);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->burnsForever);
} }
public function burnsForever() : bool{ public function burnsForever() : bool{

View File

@ -49,14 +49,9 @@ final class Bell extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->attachmentType = $r->readBellAttachmentType(); $w->bellAttachmentType($this->attachmentType);
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBellAttachmentType($this->attachmentType);
$w->writeHorizontalFacing($this->facing);
} }
protected function recalculateCollisionBoxes() : array{ protected function recalculateCollisionBoxes() : array{

View File

@ -107,7 +107,7 @@ class Block{
$givenBits = $typeBits; $givenBits = $typeBits;
$reader = new RuntimeDataReader($givenBits, $data); $reader = new RuntimeDataReader($givenBits, $data);
$this->decodeType($reader); $this->describeType($reader);
$readBits = $reader->getOffset(); $readBits = $reader->getOffset();
if($typeBits !== $readBits){ if($typeBits !== $readBits){
throw new \LogicException(get_class($this) . ": Exactly $typeBits bits of type data were provided, but $readBits were read"); throw new \LogicException(get_class($this) . ": Exactly $typeBits bits of type data were provided, but $readBits were read");
@ -124,21 +124,13 @@ class Block{
$reader = new RuntimeDataReader($givenBits, $data); $reader = new RuntimeDataReader($givenBits, $data);
$this->decodeTypeData($reader->readInt($typeBits)); $this->decodeTypeData($reader->readInt($typeBits));
$this->decodeState($reader); $this->describeState($reader);
$readBits = $reader->getOffset() - $typeBits; $readBits = $reader->getOffset() - $typeBits;
if($stateBits !== $readBits){ if($stateBits !== $readBits){
throw new \LogicException(get_class($this) . ": Exactly $stateBits bits of state data were provided, but $readBits were read"); throw new \LogicException(get_class($this) . ": Exactly $stateBits bits of state data were provided, but $readBits were read");
} }
} }
protected function decodeType(RuntimeDataReader $r) : void{
//NOOP
}
protected function decodeState(RuntimeDataReader $r) : void{
//NOOP
}
/** /**
* @internal * @internal
*/ */
@ -147,7 +139,7 @@ class Block{
$requiredBits = $typeBits; $requiredBits = $typeBits;
$writer = new RuntimeDataWriter($requiredBits); $writer = new RuntimeDataWriter($requiredBits);
$this->encodeType($writer); $this->describeType($writer);
$writtenBits = $writer->getOffset(); $writtenBits = $writer->getOffset();
if($typeBits !== $writtenBits){ if($typeBits !== $writtenBits){
throw new \LogicException(get_class($this) . ": Exactly $typeBits bits of type data were expected, but $writtenBits were written"); throw new \LogicException(get_class($this) . ": Exactly $typeBits bits of type data were expected, but $writtenBits were written");
@ -164,9 +156,9 @@ class Block{
$stateBits = $this->getRequiredStateDataBits(); $stateBits = $this->getRequiredStateDataBits();
$requiredBits = $typeBits + $stateBits; $requiredBits = $typeBits + $stateBits;
$writer = new RuntimeDataWriter($requiredBits); $writer = new RuntimeDataWriter($requiredBits);
$writer->writeInt($typeBits, $this->computeTypeData()); $writer->int($typeBits, $this->computeTypeData());
$this->encodeState($writer); $this->describeState($writer);
$writtenBits = $writer->getOffset() - $typeBits; $writtenBits = $writer->getOffset() - $typeBits;
if($stateBits !== $writtenBits){ if($stateBits !== $writtenBits){
throw new \LogicException(get_class($this) . ": Exactly $stateBits bits of state data were expected, but $writtenBits were written"); throw new \LogicException(get_class($this) . ": Exactly $stateBits bits of state data were expected, but $writtenBits were written");
@ -175,11 +167,11 @@ class Block{
return $writer->getValue(); return $writer->getValue();
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
//NOOP //NOOP
} }
protected function encodeState(RuntimeDataWriter $w) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
//NOOP //NOOP
} }

View File

@ -46,12 +46,8 @@ class BrewingStand extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->setSlots($r->readBrewingStandSlots()); $w->brewingStandSlots($this->slots);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBrewingStandSlots($this->getSlots());
} }
protected function recalculateCollisionBoxes() : array{ protected function recalculateCollisionBoxes() : array{

View File

@ -41,14 +41,9 @@ abstract class Button extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readFacing(); $w->facing($this->facing);
$this->pressed = $r->readBool(); $w->bool($this->pressed);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
$w->writeBool($this->pressed);
} }
public function isPressed() : bool{ return $this->pressed; } public function isPressed() : bool{ return $this->pressed; }

View File

@ -44,12 +44,8 @@ class Cactus extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $w->boundedInt(4, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -39,12 +39,8 @@ class Cake extends BaseCake{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->bites = $r->readBoundedInt(3, 0, self::MAX_BITES); $w->boundedInt(3, 0, self::MAX_BITES, $this->bites);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, 0, self::MAX_BITES, $this->bites);
} }
/** /**

View File

@ -24,10 +24,16 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\ColoredTrait; use pocketmine\block\utils\ColoredTrait;
use pocketmine\block\utils\DyeColor;
class CakeWithDyedCandle extends CakeWithCandle{ class CakeWithDyedCandle extends CakeWithCandle{
use ColoredTrait; use ColoredTrait;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->color = DyeColor::WHITE();
parent::__construct($idInfo, $name, $breakInfo);
}
public function getCandle() : Candle{ public function getCandle() : Candle{
return VanillaBlocks::DYED_CANDLE()->setColor($this->color); return VanillaBlocks::DYED_CANDLE()->setColor($this->color);
} }

View File

@ -37,8 +37,7 @@ use pocketmine\world\BlockTransaction;
class Candle extends Transparent{ class Candle extends Transparent{
use CandleTrait { use CandleTrait {
decodeState as decodeLitState; describeState as encodeLitState;
encodeState as encodeLitState;
getLightLevel as getBaseLightLevel; getLightLevel as getBaseLightLevel;
} }
@ -51,14 +50,9 @@ class Candle extends Transparent{
return 3; return 3;
} }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->decodeLitState($r);
$this->count = $r->readBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$this->encodeLitState($w); $this->encodeLitState($w);
$w->writeBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT, $this->count); $w->boundedInt(2, self::MIN_COUNT, self::MAX_COUNT, $this->count);
} }
public function getCount() : int{ return $this->count; } public function getCount() : int{ return $this->count; }

View File

@ -49,14 +49,9 @@ class CocoaBlock extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $w->boundedInt(2, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -23,8 +23,14 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\CopperOxidation;
use pocketmine\block\utils\CopperTrait; use pocketmine\block\utils\CopperTrait;
class CopperStairs extends Stair{ class CopperStairs extends Stair{
use CopperTrait; use CopperTrait;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->oxidation = CopperOxidation::NONE();
parent::__construct($idInfo, $name, $breakInfo);
}
} }

View File

@ -41,12 +41,8 @@ abstract class Crops extends Flowable{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(3, 0, self::MAX_AGE); $w->boundedInt(3, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -44,14 +44,9 @@ class DaylightSensor extends Transparent{
public function getRequiredStateDataBits() : int{ return 5; } public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->signalStrength = $r->readBoundedInt(4, 0, 15); $w->boundedInt(4, 0, 15, $this->signalStrength);
$this->inverted = $r->readBool(); $w->bool($this->inverted);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, 15, $this->signalStrength);
$w->writeBool($this->inverted);
} }
public function isInverted() : bool{ public function isInverted() : bool{

View File

@ -31,14 +31,9 @@ class DetectorRail extends StraightOnlyRail{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
parent::decodeState($r); parent::describeState($w);
$this->activated = $r->readBool(); $w->bool($this->activated);
}
protected function encodeState(RuntimeDataWriter $w) : void{
parent::encodeState($w);
$w->writeBool($this->activated);
} }
public function isActivated() : bool{ return $this->activated; } public function isActivated() : bool{ return $this->activated; }

View File

@ -37,12 +37,8 @@ class Dirt extends Opaque{
public function getRequiredTypeDataBits() : int{ return 1; } public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->coarse = $r->readBool(); $w->bool($this->coarse);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBool($this->coarse);
} }
public function isCoarse() : bool{ return $this->coarse; } public function isCoarse() : bool{ return $this->coarse; }

View File

@ -44,18 +44,11 @@ class Door extends Transparent{
public function getRequiredStateDataBits() : int{ return 5; } public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->top = $r->readBool(); $w->bool($this->top);
$this->hingeRight = $r->readBool(); $w->bool($this->hingeRight);
$this->open = $r->readBool(); $w->bool($this->open);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->top);
$w->writeBool($this->hingeRight);
$w->writeBool($this->open);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -36,12 +36,8 @@ class DoublePlant extends Flowable{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->top = $r->readBool(); $w->bool($this->top);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->top);
} }
public function isTop() : bool{ return $this->top; } public function isTop() : bool{ return $this->top; }

View File

@ -24,10 +24,16 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\ColoredTrait; use pocketmine\block\utils\ColoredTrait;
use pocketmine\block\utils\DyeColor;
class DyedCandle extends Candle{ class DyedCandle extends Candle{
use ColoredTrait; use ColoredTrait;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->color = DyeColor::WHITE();
parent::__construct($idInfo, $name, $breakInfo);
}
protected function getCandleIfCompatibleType(Block $block) : ?Candle{ protected function getCandleIfCompatibleType(Block $block) : ?Candle{
$result = parent::getCandleIfCompatibleType($block); $result = parent::getCandleIfCompatibleType($block);
//different coloured candles can't be combined in the same block //different coloured candles can't be combined in the same block

View File

@ -38,14 +38,9 @@ class EndPortalFrame extends Opaque{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->eye = $r->readBool(); $w->bool($this->eye);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->eye);
} }
public function hasEye() : bool{ return $this->eye; } public function hasEye() : bool{ return $this->eye; }

View File

@ -40,12 +40,8 @@ class Farmland extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->wetness = $r->readBoundedInt(3, 0, self::MAX_WETNESS); $w->boundedInt(3, 0, self::MAX_WETNESS, $this->wetness);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, 0, self::MAX_WETNESS, $this->wetness);
} }
public function getWetness() : int{ return $this->wetness; } public function getWetness() : int{ return $this->wetness; }

View File

@ -45,16 +45,10 @@ class FenceGate extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->open = $r->readBool(); $w->bool($this->open);
$this->inWall = $r->readBool(); $w->bool($this->inWall);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->open);
$w->writeBool($this->inWall);
} }
public function isOpen() : bool{ return $this->open; } public function isOpen() : bool{ return $this->open; }

View File

@ -42,12 +42,8 @@ class Fire extends BaseFire{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $w->boundedInt(4, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -40,12 +40,8 @@ final class FloorCoralFan extends BaseCoral{
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; } public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->axis = $r->readHorizontalAxis(); $w->horizontalAxis($this->axis);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalAxis($this->axis);
} }
public function getAxis() : int{ return $this->axis; } public function getAxis() : int{ return $this->axis; }

View File

@ -35,12 +35,8 @@ class FrostedIce extends Ice{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $w->boundedInt(2, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -41,14 +41,9 @@ class Furnace extends Opaque{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->lit = $r->readBool(); $w->bool($this->lit);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->lit);
} }
public function getLightLevel() : int{ public function getLightLevel() : int{

View File

@ -26,7 +26,6 @@ namespace pocketmine\block;
use pocketmine\block\tile\Hopper as TileHopper; use pocketmine\block\tile\Hopper as TileHopper;
use pocketmine\block\utils\PoweredByRedstoneTrait; use pocketmine\block\utils\PoweredByRedstoneTrait;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -43,18 +42,9 @@ class Hopper extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$facing = $r->readFacing(); $w->facingExcept($this->facing, Facing::UP);
if($facing === Facing::UP){ $w->bool($this->powered);
throw new InvalidSerializedRuntimeDataException("Hopper may not face upward");
}
$this->facing = $facing;
$this->powered = $r->readBool();
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
$w->writeBool($this->powered);
} }
public function getFacing() : int{ return $this->facing; } public function getFacing() : int{ return $this->facing; }

View File

@ -51,24 +51,15 @@ class ItemFrame extends Flowable{
public function getRequiredTypeDataBits() : int{ return 1; } public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->glowing = $r->readBool(); $w->bool($this->glowing);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBool($this->glowing);
} }
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readFacing(); $w->facing($this->facing);
$this->hasMap = $r->readBool(); $w->bool($this->hasMap);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
$w->writeBool($this->hasMap);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -46,12 +46,8 @@ class Lantern extends Transparent{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->hanging = $r->readBool(); $w->bool($this->hanging);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->hanging);
} }
public function isHanging() : bool{ return $this->hanging; } public function isHanging() : bool{ return $this->hanging; }

View File

@ -50,14 +50,9 @@ class Leaves extends Transparent{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->noDecay = $r->readBool(); $w->bool($this->noDecay);
$this->checkDecay = $r->readBool(); $w->bool($this->checkDecay);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->noDecay);
$w->writeBool($this->checkDecay);
} }
public function isNoDecay() : bool{ return $this->noDecay; } public function isNoDecay() : bool{ return $this->noDecay; }

View File

@ -49,14 +49,9 @@ class Lectern extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->producingSignal = $r->readBool(); $w->bool($this->producingSignal);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->producingSignal);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -47,14 +47,9 @@ class Lever extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readLeverFacing(); $w->leverFacing($this->facing);
$this->activated = $r->readBool(); $w->bool($this->activated);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeLeverFacing($this->facing);
$w->writeBool($this->activated);
} }
public function getFacing() : LeverFacing{ return $this->facing; } public function getFacing() : LeverFacing{ return $this->facing; }

View File

@ -37,12 +37,8 @@ final class Light extends Flowable{
public function getRequiredTypeDataBits() : int{ return 4; } public function getRequiredTypeDataBits() : int{ return 4; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->level = $r->readBoundedInt(4, self::MIN_LIGHT_LEVEL, self::MAX_LIGHT_LEVEL); $w->boundedInt(4, self::MIN_LIGHT_LEVEL, self::MAX_LIGHT_LEVEL, $this->level);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, self::MIN_LIGHT_LEVEL, self::MAX_LIGHT_LEVEL, $this->level);
} }
public function getLightLevel() : int{ return $this->level; } public function getLightLevel() : int{ return $this->level; }

View File

@ -51,16 +51,10 @@ abstract class Liquid extends Transparent{
public function getRequiredStateDataBits() : int{ return 5; } public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->decay = $r->readBoundedInt(3, 0, self::MAX_DECAY); $w->boundedInt(3, 0, self::MAX_DECAY, $this->decay);
$this->falling = $r->readBool(); $w->bool($this->falling);
$this->still = $r->readBool(); $w->bool($this->still);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, 0, self::MAX_DECAY, $this->decay);
$w->writeBool($this->falling);
$w->writeBool($this->still);
} }
public function isFalling() : bool{ return $this->falling; } public function isFalling() : bool{ return $this->falling; }

View File

@ -37,12 +37,8 @@ class NetherPortal extends Transparent{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->axis = $r->readHorizontalAxis(); $w->horizontalAxis($this->axis);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalAxis($this->axis);
} }
public function getAxis() : int{ public function getAxis() : int{

View File

@ -40,12 +40,8 @@ class NetherWartPlant extends Flowable{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $w->boundedInt(2, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -25,7 +25,6 @@ namespace pocketmine\block;
use pocketmine\block\utils\RailConnectionInfo; use pocketmine\block\utils\RailConnectionInfo;
use pocketmine\data\bedrock\block\BlockLegacyMetadata; use pocketmine\data\bedrock\block\BlockLegacyMetadata;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\math\Facing; use pocketmine\math\Facing;
@ -38,16 +37,8 @@ class Rail extends BaseRail{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$railShape = $r->readInt(4); $w->railShape($this->railShape);
if(!isset(RailConnectionInfo::CONNECTIONS[$railShape]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$railShape])){
throw new InvalidSerializedRuntimeDataException("Invalid rail shape $railShape");
}
$this->railShape = $railShape;
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->railShape);
} }
protected function setShapeFromConnections(array $connections) : void{ protected function setShapeFromConnections(array $connections) : void{

View File

@ -39,12 +39,8 @@ class RedMushroomBlock extends Opaque{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->mushroomBlockType = $r->readMushroomBlockType(); $w->mushroomBlockType($this->mushroomBlockType);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeMushroomBlockType($this->mushroomBlockType);
} }
public function getMushroomBlockType() : MushroomBlockType{ return $this->mushroomBlockType; } public function getMushroomBlockType() : MushroomBlockType{ return $this->mushroomBlockType; }

View File

@ -47,17 +47,10 @@ class RedstoneComparator extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->isSubtractMode = $r->readBool(); $w->bool($this->isSubtractMode);
$this->powered = $r->readBool(); $w->bool($this->powered);
//TODO: this doesn't call the decoder from AnalogRedstoneSignalEmitter
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->isSubtractMode);
$w->writeBool($this->powered);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -32,12 +32,8 @@ class RedstoneLamp extends Opaque{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->powered = $r->readBool(); $w->bool($this->powered);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->powered);
} }
public function getLightLevel() : int{ public function getLightLevel() : int{

View File

@ -36,12 +36,8 @@ class RedstoneOre extends Opaque{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->lit = $r->readBool(); $w->bool($this->lit);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->lit);
} }
public function isLit() : bool{ public function isLit() : bool{

View File

@ -46,16 +46,10 @@ class RedstoneRepeater extends Flowable{
public function getRequiredStateDataBits() : int{ return 5; } public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->delay = $r->readBoundedInt(2, self::MIN_DELAY, self::MAX_DELAY); $w->boundedInt(2, self::MIN_DELAY, self::MAX_DELAY, $this->delay);
$this->powered = $r->readBool(); $w->bool($this->powered);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBoundedInt(2, self::MIN_DELAY, self::MAX_DELAY, $this->delay);
$w->writeBool($this->powered);
} }
public function getDelay() : int{ return $this->delay; } public function getDelay() : int{ return $this->delay; }

View File

@ -31,14 +31,9 @@ class RedstoneTorch extends Torch{
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; } public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
parent::decodeState($r); parent::describeState($w);
$this->lit = $r->readBool(); $w->bool($this->lit);
}
protected function encodeState(RuntimeDataWriter $w) : void{
parent::encodeState($w);
$w->writeBool($this->lit);
} }
public function isLit() : bool{ public function isLit() : bool{

View File

@ -49,12 +49,8 @@ class Sapling extends Flowable{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->ready = $r->readBool(); $w->bool($this->ready);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->ready);
} }
public function isReady() : bool{ return $this->ready; } public function isReady() : bool{ return $this->ready; }

View File

@ -41,14 +41,9 @@ class SeaPickle extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->count = $r->readBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT); $w->boundedInt(2, self::MIN_COUNT, self::MAX_COUNT, $this->count);
$this->underwater = $r->readBool(); $w->bool($this->underwater);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT, $this->count);
$w->writeBool($this->underwater);
} }
public function getCount() : int{ return $this->count; } public function getCount() : int{ return $this->count; }

View File

@ -37,11 +37,7 @@ class ShulkerBox extends Opaque{
public function getRequiredStateDataBits() : int{ return 0; } public function getRequiredStateDataBits() : int{ return 0; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
//NOOP - we don't read or write facing here, because the tile persists it
}
protected function encodeState(RuntimeDataWriter $w) : void{
//NOOP - we don't read or write facing here, because the tile persists it //NOOP - we don't read or write facing here, because the tile persists it
} }

View File

@ -31,12 +31,8 @@ abstract class SimplePressurePlate extends PressurePlate{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->pressed = $r->readBool(); $w->bool($this->pressed);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->pressed);
} }
public function isPressed() : bool{ return $this->pressed; } public function isPressed() : bool{ return $this->pressed; }

View File

@ -25,7 +25,6 @@ namespace pocketmine\block;
use pocketmine\block\tile\Skull as TileSkull; use pocketmine\block\tile\Skull as TileSkull;
use pocketmine\block\utils\SkullType; use pocketmine\block\utils\SkullType;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -53,26 +52,14 @@ class Skull extends Flowable{
public function getRequiredTypeDataBits() : int{ return 3; } public function getRequiredTypeDataBits() : int{ return 3; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->skullType = $r->readSkullType(); $w->skullType($this->skullType);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeSkullType($this->skullType);
} }
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$facing = $r->readFacing(); $w->facingExcept($this->facing, Facing::DOWN);
if($facing === Facing::DOWN){
throw new InvalidSerializedRuntimeDataException("Skull may not face down");
}
$this->facing = $facing;
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -44,12 +44,8 @@ class Slab extends Transparent{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->slabType = $r->readSlabType(); $w->slabType($this->slabType);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeSlabType($this->slabType);
} }
public function isTransparent() : bool{ public function isTransparent() : bool{

View File

@ -49,12 +49,8 @@ class SnowLayer extends Flowable implements Fallable{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->layers = $r->readBoundedInt(3, self::MIN_LAYERS, self::MAX_LAYERS); $w->boundedInt(3, self::MIN_LAYERS, self::MAX_LAYERS, $this->layers);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, self::MIN_LAYERS, self::MAX_LAYERS, $this->layers);
} }
public function getLayers() : int{ return $this->layers; } public function getLayers() : int{ return $this->layers; }

View File

@ -31,12 +31,8 @@ class Sponge extends Opaque{
public function getRequiredTypeDataBits() : int{ return 1; } public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->wet = $r->readBool(); $w->bool($this->wet);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBool($this->wet);
} }
public function isWet() : bool{ return $this->wet; } public function isWet() : bool{ return $this->wet; }

View File

@ -49,14 +49,9 @@ class Stair extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->upsideDown = $r->readBool(); $w->bool($this->upsideDown);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->upsideDown);
} }
public function readStateFromWorld() : Block{ public function readStateFromWorld() : Block{

View File

@ -25,7 +25,6 @@ namespace pocketmine\block;
use pocketmine\block\utils\RailConnectionInfo; use pocketmine\block\utils\RailConnectionInfo;
use pocketmine\data\bedrock\block\BlockLegacyMetadata; use pocketmine\data\bedrock\block\BlockLegacyMetadata;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use function array_keys; use function array_keys;
@ -40,16 +39,8 @@ class StraightOnlyRail extends BaseRail{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$railShape = $r->readInt(3); $w->straightOnlyRailShape($this->railShape);
if(!isset(RailConnectionInfo::CONNECTIONS[$railShape])){
throw new InvalidSerializedRuntimeDataException("No rail shape matches meta $railShape");
}
$this->railShape = $railShape;
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->railShape);
} }
protected function setShapeFromConnections(array $connections) : void{ protected function setShapeFromConnections(array $connections) : void{

View File

@ -40,12 +40,8 @@ class Sugarcane extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $w->boundedInt(4, 0, self::MAX_AGE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
} }
private function grow() : bool{ private function grow() : bool{

View File

@ -48,12 +48,8 @@ class SweetBerryBush extends Flowable{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->age = $r->readBoundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE); $w->boundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE, $this->age);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE, $this->age);
} }
public function getAge() : int{ return $this->age; } public function getAge() : int{ return $this->age; }

View File

@ -47,22 +47,14 @@ class TNT extends Opaque{
public function getRequiredTypeDataBits() : int{ return 1; } public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->worksUnderwater = $r->readBool(); $w->bool($this->worksUnderwater);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBool($this->worksUnderwater);
} }
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->unstable = $r->readBool(); $w->bool($this->unstable);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->unstable);
} }
public function isUnstable() : bool{ return $this->unstable; } public function isUnstable() : bool{ return $this->unstable; }

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\InvalidSerializedRuntimeDataException;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -40,16 +39,8 @@ class Torch extends Flowable{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$facing = $r->readFacing(); $w->facingExcept($this->facing, Facing::DOWN);
if($facing === Facing::DOWN){
throw new InvalidSerializedRuntimeDataException("Torch cannot have a DOWN facing");
}
$this->facing = $facing;
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
} }
public function getFacing() : int{ return $this->facing; } public function getFacing() : int{ return $this->facing; }

View File

@ -43,16 +43,10 @@ class Trapdoor extends Transparent{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->top = $r->readBool(); $w->bool($this->top);
$this->open = $r->readBool(); $w->bool($this->open);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->top);
$w->writeBool($this->open);
} }
public function isOpen() : bool{ return $this->open; } public function isOpen() : bool{ return $this->open; }

View File

@ -36,18 +36,11 @@ class Tripwire extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->triggered = $r->readBool(); $w->bool($this->triggered);
$this->suspended = $r->readBool(); $w->bool($this->suspended);
$this->connected = $r->readBool(); $w->bool($this->connected);
$this->disarmed = $r->readBool(); $w->bool($this->disarmed);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->triggered);
$w->writeBool($this->suspended);
$w->writeBool($this->connected);
$w->writeBool($this->disarmed);
} }
public function isTriggered() : bool{ return $this->triggered; } public function isTriggered() : bool{ return $this->triggered; }

View File

@ -41,16 +41,10 @@ class TripwireHook extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
$this->connected = $r->readBool(); $w->bool($this->connected);
$this->powered = $r->readBool(); $w->bool($this->powered);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeBool($this->connected);
$w->writeBool($this->powered);
} }
public function isConnected() : bool{ return $this->connected; } public function isConnected() : bool{ return $this->connected; }

View File

@ -36,14 +36,10 @@ class UnknownBlock extends Transparent{
$this->stateData = $stateData; $this->stateData = $stateData;
} }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
//use type instead of state, so we don't lose any information like colour //use type instead of state, so we don't lose any information like colour
//this might be an improperly registered plugin block //this might be an improperly registered plugin block
$this->stateData = $r->readInt(Block::INTERNAL_STATE_DATA_BITS); $w->int(Block::INTERNAL_STATE_DATA_BITS, $this->stateData);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeInt(Block::INTERNAL_STATE_DATA_BITS, $this->stateData);
} }
public function canBePlaced() : bool{ public function canBePlaced() : bool{

View File

@ -42,12 +42,8 @@ class Vine extends Flowable{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->faces = $r->readHorizontalFacingFlags(); $w->horizontalFacingFlags($this->faces);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacingFlags($this->faces);
} }
/** @return int[] */ /** @return int[] */

View File

@ -45,14 +45,9 @@ class Wall extends Transparent{
public function getRequiredStateDataBits() : int{ return 9; } public function getRequiredStateDataBits() : int{ return 9; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->connections = $r->readWallConnections(); $w->wallConnections($this->connections);
$this->post = $r->readBool(); $w->bool($this->post);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeWallConnections($this->connections);
$w->writeBool($this->post);
} }
/** /**

View File

@ -39,12 +39,8 @@ final class WallCoralFan extends BaseCoral{
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 2; } public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
} }
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{

View File

@ -41,12 +41,8 @@ class Wood extends Opaque{
public function getRequiredTypeDataBits() : int{ return 1; } public function getRequiredTypeDataBits() : int{ return 1; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->stripped = $r->readBool(); $w->bool($this->stripped);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeBool($this->stripped);
} }
public function isStripped() : bool{ return $this->stripped; } public function isStripped() : bool{ return $this->stripped; }

View File

@ -31,12 +31,8 @@ trait AnalogRedstoneSignalEmitterTrait{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->signalStrength = $r->readBoundedInt(4, 0, 15); $w->boundedInt(4, 0, 15, $this->signalStrength);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, 15, $this->signalStrength);
} }
public function getOutputSignalStrength() : int{ return $this->signalStrength; } public function getOutputSignalStrength() : int{ return $this->signalStrength; }

View File

@ -32,12 +32,8 @@ trait AnyFacingTrait{
public function getRequiredStateDataBits() : int{ return 3; } public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readFacing(); $w->facing($this->facing);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeFacing($this->facing);
} }
public function getFacing() : int{ return $this->facing; } public function getFacing() : int{ return $this->facing; }

View File

@ -42,12 +42,8 @@ trait CandleTrait{
public function getRequiredStateDataBits() : int{ return 1; } public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->lit = $r->readBool(); $w->bool($this->lit);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBool($this->lit);
} }
public function getLightLevel() : int{ public function getLightLevel() : int{

View File

@ -33,14 +33,9 @@ trait ColoredTrait{
public function getRequiredTypeDataBits() : int{ return 4; } public function getRequiredTypeDataBits() : int{ return 4; }
/** @see Block::decodeType() */ /** @see Block::describeType() */
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->color = $r->readDyeColor(); $w->dyeColor($this->color);
}
/** @see Block::encodeType() */
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeDyeColor($this->color);
} }
public function getColor() : DyeColor{ return $this->color; } public function getColor() : DyeColor{ return $this->color; }

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block\utils; namespace pocketmine\block\utils;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockIdentifier;
use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter; use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\item\Axe; use pocketmine\item\Axe;
@ -36,18 +38,18 @@ use pocketmine\world\sound\ItemUseOnBlockSound;
trait CopperTrait{ trait CopperTrait{
private CopperOxidation $oxidation; private CopperOxidation $oxidation;
private bool $waxed; private bool $waxed = false;
public function __construct(BlockIdentifier $identifier, string $name, BlockBreakInfo $breakInfo){
$this->oxidation = CopperOxidation::NONE();
parent::__construct($identifier, $name, $breakInfo);
}
public function getRequiredTypeDataBits() : int{ return 3; } public function getRequiredTypeDataBits() : int{ return 3; }
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->oxidation = $r->readCopperOxidation(); $w->copperOxidation($this->oxidation);
$this->waxed = $r->readBool(); $w->bool($this->waxed);
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeCopperOxidation($this->oxidation);
$w->writeBool($this->waxed);
} }
public function getOxidation() : CopperOxidation{ return $this->oxidation; } public function getOxidation() : CopperOxidation{ return $this->oxidation; }

View File

@ -33,16 +33,10 @@ trait CoralTypeTrait{
public function getRequiredTypeDataBits() : int{ return 4; } public function getRequiredTypeDataBits() : int{ return 4; }
/** @see Block::decodeType() */ /** @see Block::describeType() */
protected function decodeType(RuntimeDataReader $r) : void{ protected function describeType(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->coralType = $r->readCoralType(); $w->coralType($this->coralType);
$this->dead = $r->readBool(); $w->bool($this->dead);
}
/** @see Block::encodeType() */
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeCoralType($this->coralType);
$w->writeBool($this->dead);
} }
public function getCoralType() : CoralType{ return $this->coralType; } public function getCoralType() : CoralType{ return $this->coralType; }

View File

@ -33,12 +33,8 @@ trait HorizontalFacingTrait{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->facing = $r->readHorizontalFacing(); $w->horizontalFacing($this->facing);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
} }
public function getFacing() : int{ return $this->facing; } public function getFacing() : int{ return $this->facing; }

View File

@ -38,12 +38,8 @@ trait PillarRotationTrait{
public function getRequiredStateDataBits() : int{ return 2; } public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->axis = $r->readAxis(); $w->axis($this->axis);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeAxis($this->axis);
} }
/** @see Axis */ /** @see Axis */

View File

@ -31,13 +31,8 @@ trait RailPoweredByRedstoneTrait{
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; } public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
parent::decodeState($r); parent::describeState($w);
$this->powered = $r->readBool(); $w->bool($this->powered);
}
protected function encodeState(RuntimeDataWriter $w) : void{
parent::encodeState($w);
$w->writeBool($this->powered);
} }
} }

View File

@ -33,12 +33,8 @@ trait SignLikeRotationTrait{
public function getRequiredStateDataBits() : int{ return 4; } public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(RuntimeDataReader $r) : void{ protected function describeState(RuntimeDataReader|RuntimeDataWriter $w) : void{
$this->rotation = $r->readBoundedInt(4, 0, 15); $w->boundedInt(4, 0, 15, $this->rotation);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeBoundedInt(4, 0, 15, $this->rotation);
} }
public function getRotation() : int{ return $this->rotation; } public function getRotation() : int{ return $this->rotation; }

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\data\runtime; namespace pocketmine\data\runtime;
use pocketmine\block\utils\BrewingStandSlot; use pocketmine\block\utils\BrewingStandSlot;
use pocketmine\block\utils\RailConnectionInfo;
use pocketmine\block\utils\WallConnectionType; use pocketmine\block\utils\WallConnectionType;
use pocketmine\math\Axis; use pocketmine\math\Axis;
use pocketmine\math\Facing; use pocketmine\math\Facing;
@ -50,20 +51,28 @@ final class RuntimeDataReader{
return $value; return $value;
} }
public function readBoundedInt(int $bits, int $min, int $max) : int{ public function int(int $bits, int &$value) : void{
$value = $this->readInt($bits);
}
public function boundedInt(int $bits, int $min, int $max, int &$value) : void{
$result = $this->readInt($bits) + $min; $result = $this->readInt($bits) + $min;
if($result < $min || $result > $max){ if($result < $min || $result > $max){
throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max"); throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max");
} }
return $result; $value = $result;
} }
public function readBool() : bool{ protected function readBool() : bool{
return $this->readInt(1) === 1; return $this->readInt(1) === 1;
} }
public function readHorizontalFacing() : int{ public function bool(bool &$value) : void{
return match($this->readInt(2)){ $value = $this->readBool();
}
public function horizontalFacing(int &$facing) : void{
$facing = match($this->readInt(2)){
0 => Facing::NORTH, 0 => Facing::NORTH,
1 => Facing::EAST, 1 => Facing::EAST,
2 => Facing::SOUTH, 2 => Facing::SOUTH,
@ -73,9 +82,9 @@ final class RuntimeDataReader{
} }
/** /**
* @return int[] * @param int[] $faces
*/ */
public function readHorizontalFacingFlags() : array{ public function horizontalFacingFlags(array &$faces) : void{
$result = []; $result = [];
foreach(Facing::HORIZONTAL as $facing){ foreach(Facing::HORIZONTAL as $facing){
if($this->readBool()){ if($this->readBool()){
@ -83,11 +92,11 @@ final class RuntimeDataReader{
} }
} }
return $result; $faces = $result;
} }
public function readFacing() : int{ public function facing(int &$facing) : void{
return match($this->readInt(3)){ $facing = match($this->readInt(3)){
0 => Facing::DOWN, 0 => Facing::DOWN,
1 => Facing::UP, 1 => Facing::UP,
2 => Facing::NORTH, 2 => Facing::NORTH,
@ -98,8 +107,18 @@ final class RuntimeDataReader{
}; };
} }
public function readAxis() : int{ public function facingExcept(int &$facing, int $except) : void{
return match($this->readInt(2)){ $result = 0;
$this->facing($result);
if($result === $except){
throw new InvalidSerializedRuntimeDataException("Illegal facing value");
}
$facing = $result;
}
public function axis(int &$axis) : void{
$axis = match($this->readInt(2)){
0 => Axis::X, 0 => Axis::X,
1 => Axis::Z, 1 => Axis::Z,
2 => Axis::Y, 2 => Axis::Y,
@ -107,8 +126,8 @@ final class RuntimeDataReader{
}; };
} }
public function readHorizontalAxis() : int{ public function horizontalAxis(int &$axis) : void{
return match($this->readInt(1)){ $axis = match($this->readInt(1)){
0 => Axis::X, 0 => Axis::X,
1 => Axis::Z, 1 => Axis::Z,
default => throw new AssumptionFailedError("Unreachable") default => throw new AssumptionFailedError("Unreachable")
@ -116,16 +135,17 @@ final class RuntimeDataReader{
} }
/** /**
* @return WallConnectionType[] * @param WallConnectionType[] $connections
* @phpstan-return array<Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST, WallConnectionType> * @phpstan-param array<Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST, WallConnectionType> $connections
*/ */
public function readWallConnections() : array{ public function wallConnections(array &$connections) : void{
$connections = []; $result = [];
//TODO: we can pack this into 7 bits instead of 8 //TODO: we can pack this into 7 bits instead of 8
foreach(Facing::HORIZONTAL as $facing){ foreach(Facing::HORIZONTAL as $facing){
$type = $this->readBoundedInt(2, 0, 2); $type = 0;
$this->boundedInt(2, 0, 2, $type);
if($type !== 0){ if($type !== 0){
$connections[$facing] = match($type){ $result[$facing] = match($type){
1 => WallConnectionType::SHORT(), 1 => WallConnectionType::SHORT(),
2 => WallConnectionType::TALL(), 2 => WallConnectionType::TALL(),
default => throw new AssumptionFailedError("Unreachable") default => throw new AssumptionFailedError("Unreachable")
@ -133,14 +153,14 @@ final class RuntimeDataReader{
} }
} }
return $connections; $connections = $result;
} }
/** /**
* @return BrewingStandSlot[] * @param BrewingStandSlot[] $slots
* @phpstan-return array<int, BrewingStandSlot> * @phpstan-param array<int, BrewingStandSlot> $slots
*/ */
public function readBrewingStandSlots() : array{ public function brewingStandSlots(array &$slots) : void{
$result = []; $result = [];
foreach([ foreach([
BrewingStandSlot::EAST(), BrewingStandSlot::EAST(),
@ -152,7 +172,25 @@ final class RuntimeDataReader{
} }
} }
return $result; $slots = $result;
}
public function railShape(int &$railShape) : void{
$result = $this->readInt(4);
if(!isset(RailConnectionInfo::CONNECTIONS[$result]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$result])){
throw new InvalidSerializedRuntimeDataException("Invalid rail shape $result");
}
$railShape = $result;
}
public function straightOnlyRailShape(int &$railShape) : void{
$result = $this->readInt(3);
if(!isset(RailConnectionInfo::CONNECTIONS[$result])){
throw new InvalidSerializedRuntimeDataException("No rail shape matches meta $result");
}
$railShape = $result;
} }
public function getOffset() : int{ return $this->offset; } public function getOffset() : int{ return $this->offset; }

View File

@ -40,8 +40,7 @@ final class RuntimeDataWriter{
private int $maxBits private int $maxBits
){} ){}
/** @return $this */ public function int(int $bits, int $value) : void{
public function writeInt(int $bits, int $value) : self{
if($this->offset + $bits > $this->maxBits){ if($this->offset + $bits > $this->maxBits){
throw new \InvalidArgumentException("Bit buffer cannot be larger than $this->maxBits bits (already have $this->offset bits)"); throw new \InvalidArgumentException("Bit buffer cannot be larger than $this->maxBits bits (already have $this->offset bits)");
} }
@ -51,27 +50,21 @@ final class RuntimeDataWriter{
$this->value |= ($value << $this->offset); $this->value |= ($value << $this->offset);
$this->offset += $bits; $this->offset += $bits;
return $this;
} }
/** @return $this */ public function boundedInt(int $bits, int $min, int $max, int $value) : void{
public function writeBoundedInt(int $bits, int $min, int $max, int $value) : self{
if($value < $min || $value > $max){ if($value < $min || $value > $max){
throw new \InvalidArgumentException("Value $value is outside the range $min - $max"); throw new \InvalidArgumentException("Value $value is outside the range $min - $max");
} }
$this->writeInt($bits, $value - $min); $this->int($bits, $value - $min);
return $this;
} }
/** @return $this */ public function bool(bool $value) : void{
public function writeBool(bool $value) : self{ $this->int(1, $value ? 1 : 0);
return $this->writeInt(1, $value ? 1 : 0);
} }
/** @return $this */ public function horizontalFacing(int $facing) : void{
public function writeHorizontalFacing(int $facing) : self{ $this->int(2, match($facing){
return $this->writeInt(2, match($facing){
Facing::NORTH => 0, Facing::NORTH => 0,
Facing::EAST => 1, Facing::EAST => 1,
Facing::SOUTH => 2, Facing::SOUTH => 2,
@ -82,20 +75,16 @@ final class RuntimeDataWriter{
/** /**
* @param int[] $faces * @param int[] $faces
*
* @return $this
*/ */
public function writeHorizontalFacingFlags(array $faces) : self{ public function horizontalFacingFlags(array $faces) : void{
$uniqueFaces = array_flip($faces); $uniqueFaces = array_flip($faces);
foreach(Facing::HORIZONTAL as $facing){ foreach(Facing::HORIZONTAL as $facing){
$this->writeBool(isset($uniqueFaces[$facing])); $this->bool(isset($uniqueFaces[$facing]));
} }
return $this;
} }
public function writeFacing(int $facing) : self{ public function facing(int $facing) : void{
return $this->writeInt(3, match($facing){ $this->int(3, match($facing){
0 => Facing::DOWN, 0 => Facing::DOWN,
1 => Facing::UP, 1 => Facing::UP,
2 => Facing::NORTH, 2 => Facing::NORTH,
@ -106,8 +95,12 @@ final class RuntimeDataWriter{
}); });
} }
public function writeAxis(int $axis) : self{ public function facingExcept(int $facing, int $except) : void{
return $this->writeInt(2, match($axis){ $this->facing($facing);
}
public function axis(int $axis) : void{
$this->int(2, match($axis){
Axis::X => 0, Axis::X => 0,
Axis::Z => 1, Axis::Z => 1,
Axis::Y => 2, Axis::Y => 2,
@ -115,8 +108,8 @@ final class RuntimeDataWriter{
}); });
} }
public function writeHorizontalAxis(int $axis) : self{ public function horizontalAxis(int $axis) : void{
return $this->writeInt(1, match($axis){ $this->int(1, match($axis){
Axis::X => 0, Axis::X => 0,
Axis::Z => 1, Axis::Z => 1,
default => throw new \InvalidArgumentException("Invalid horizontal axis $axis") default => throw new \InvalidArgumentException("Invalid horizontal axis $axis")
@ -127,36 +120,38 @@ final class RuntimeDataWriter{
* @param WallConnectionType[] $connections * @param WallConnectionType[] $connections
* @phpstan-param array<Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST, WallConnectionType> $connections * @phpstan-param array<Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST, WallConnectionType> $connections
*/ */
public function writeWallConnections(array $connections) : self{ public function wallConnections(array $connections) : void{
//TODO: we can pack this into 7 bits instead of 8 //TODO: we can pack this into 7 bits instead of 8
foreach(Facing::HORIZONTAL as $facing){ foreach(Facing::HORIZONTAL as $facing){
$this->writeBoundedInt(2, 0, 2, match($connections[$facing] ?? null){ $this->boundedInt(2, 0, 2, match($connections[$facing] ?? null){
null => 0, null => 0,
WallConnectionType::SHORT() => 1, WallConnectionType::SHORT() => 1,
WallConnectionType::TALL() => 2, WallConnectionType::TALL() => 2,
default => throw new AssumptionFailedError("Unreachable") default => throw new AssumptionFailedError("Unreachable")
}); });
} }
return $this;
} }
/** /**
* @param BrewingStandSlot[] $slots * @param BrewingStandSlot[] $slots
* @phpstan-param array<int, BrewingStandSlot> $slots * @phpstan-param array<int, BrewingStandSlot> $slots
*
* @return $this
*/ */
public function writeBrewingStandSlots(array $slots) : self{ public function brewingStandSlots(array $slots) : void{
foreach([ foreach([
BrewingStandSlot::EAST(), BrewingStandSlot::EAST(),
BrewingStandSlot::NORTHWEST(), BrewingStandSlot::NORTHWEST(),
BrewingStandSlot::SOUTHWEST(), BrewingStandSlot::SOUTHWEST(),
] as $member){ ] as $member){
$this->writeBool(isset($slots[$member->id()])); $this->bool(isset($slots[$member->id()]));
} }
}
return $this; public function railShape(int $railShape) : void{
$this->int(4, $railShape);
}
public function straightOnlyRailShape(int $railShape) : void{
$this->int(3, $railShape);
} }
public function getValue() : int{ return $this->value; } public function getValue() : int{ return $this->value; }

View File

@ -29,10 +29,10 @@ namespace pocketmine\data\runtime;
*/ */
trait RuntimeEnumDeserializerTrait{ trait RuntimeEnumDeserializerTrait{
abstract public function readInt(int $bits) : int; abstract protected function readInt(int $bits) : int;
public function readBellAttachmentType() : \pocketmine\block\utils\BellAttachmentType{ public function bellAttachmentType(\pocketmine\block\utils\BellAttachmentType &$value) : void{
return match($this->readInt(2)){ $value = match($this->readInt(2)){
0 => \pocketmine\block\utils\BellAttachmentType::CEILING(), 0 => \pocketmine\block\utils\BellAttachmentType::CEILING(),
1 => \pocketmine\block\utils\BellAttachmentType::FLOOR(), 1 => \pocketmine\block\utils\BellAttachmentType::FLOOR(),
2 => \pocketmine\block\utils\BellAttachmentType::ONE_WALL(), 2 => \pocketmine\block\utils\BellAttachmentType::ONE_WALL(),
@ -41,8 +41,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readCopperOxidation() : \pocketmine\block\utils\CopperOxidation{ public function copperOxidation(\pocketmine\block\utils\CopperOxidation &$value) : void{
return match($this->readInt(2)){ $value = match($this->readInt(2)){
0 => \pocketmine\block\utils\CopperOxidation::EXPOSED(), 0 => \pocketmine\block\utils\CopperOxidation::EXPOSED(),
1 => \pocketmine\block\utils\CopperOxidation::NONE(), 1 => \pocketmine\block\utils\CopperOxidation::NONE(),
2 => \pocketmine\block\utils\CopperOxidation::OXIDIZED(), 2 => \pocketmine\block\utils\CopperOxidation::OXIDIZED(),
@ -51,8 +51,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readCoralType() : \pocketmine\block\utils\CoralType{ public function coralType(\pocketmine\block\utils\CoralType &$value) : void{
return match($this->readInt(3)){ $value = match($this->readInt(3)){
0 => \pocketmine\block\utils\CoralType::BRAIN(), 0 => \pocketmine\block\utils\CoralType::BRAIN(),
1 => \pocketmine\block\utils\CoralType::BUBBLE(), 1 => \pocketmine\block\utils\CoralType::BUBBLE(),
2 => \pocketmine\block\utils\CoralType::FIRE(), 2 => \pocketmine\block\utils\CoralType::FIRE(),
@ -62,8 +62,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readDyeColor() : \pocketmine\block\utils\DyeColor{ public function dyeColor(\pocketmine\block\utils\DyeColor &$value) : void{
return match($this->readInt(4)){ $value = match($this->readInt(4)){
0 => \pocketmine\block\utils\DyeColor::BLACK(), 0 => \pocketmine\block\utils\DyeColor::BLACK(),
1 => \pocketmine\block\utils\DyeColor::BLUE(), 1 => \pocketmine\block\utils\DyeColor::BLUE(),
2 => \pocketmine\block\utils\DyeColor::BROWN(), 2 => \pocketmine\block\utils\DyeColor::BROWN(),
@ -84,8 +84,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readLeverFacing() : \pocketmine\block\utils\LeverFacing{ public function leverFacing(\pocketmine\block\utils\LeverFacing &$value) : void{
return match($this->readInt(3)){ $value = match($this->readInt(3)){
0 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X(), 0 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X(),
1 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z(), 1 => \pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z(),
2 => \pocketmine\block\utils\LeverFacing::EAST(), 2 => \pocketmine\block\utils\LeverFacing::EAST(),
@ -98,8 +98,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readMushroomBlockType() : \pocketmine\block\utils\MushroomBlockType{ public function mushroomBlockType(\pocketmine\block\utils\MushroomBlockType &$value) : void{
return match($this->readInt(4)){ $value = match($this->readInt(4)){
0 => \pocketmine\block\utils\MushroomBlockType::ALL_CAP(), 0 => \pocketmine\block\utils\MushroomBlockType::ALL_CAP(),
1 => \pocketmine\block\utils\MushroomBlockType::CAP_EAST(), 1 => \pocketmine\block\utils\MushroomBlockType::CAP_EAST(),
2 => \pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE(), 2 => \pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE(),
@ -115,8 +115,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readPotionType() : \pocketmine\item\PotionType{ public function potionType(\pocketmine\item\PotionType &$value) : void{
return match($this->readInt(6)){ $value = match($this->readInt(6)){
0 => \pocketmine\item\PotionType::AWKWARD(), 0 => \pocketmine\item\PotionType::AWKWARD(),
1 => \pocketmine\item\PotionType::FIRE_RESISTANCE(), 1 => \pocketmine\item\PotionType::FIRE_RESISTANCE(),
2 => \pocketmine\item\PotionType::HARMING(), 2 => \pocketmine\item\PotionType::HARMING(),
@ -163,8 +163,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readSkullType() : \pocketmine\block\utils\SkullType{ public function skullType(\pocketmine\block\utils\SkullType &$value) : void{
return match($this->readInt(3)){ $value = match($this->readInt(3)){
0 => \pocketmine\block\utils\SkullType::CREEPER(), 0 => \pocketmine\block\utils\SkullType::CREEPER(),
1 => \pocketmine\block\utils\SkullType::DRAGON(), 1 => \pocketmine\block\utils\SkullType::DRAGON(),
2 => \pocketmine\block\utils\SkullType::PLAYER(), 2 => \pocketmine\block\utils\SkullType::PLAYER(),
@ -175,8 +175,8 @@ trait RuntimeEnumDeserializerTrait{
}; };
} }
public function readSlabType() : \pocketmine\block\utils\SlabType{ public function slabType(\pocketmine\block\utils\SlabType &$value) : void{
return match($this->readInt(2)){ $value = match($this->readInt(2)){
0 => \pocketmine\block\utils\SlabType::BOTTOM(), 0 => \pocketmine\block\utils\SlabType::BOTTOM(),
1 => \pocketmine\block\utils\SlabType::DOUBLE(), 1 => \pocketmine\block\utils\SlabType::DOUBLE(),
2 => \pocketmine\block\utils\SlabType::TOP(), 2 => \pocketmine\block\utils\SlabType::TOP(),

View File

@ -29,10 +29,10 @@ namespace pocketmine\data\runtime;
*/ */
trait RuntimeEnumSerializerTrait{ trait RuntimeEnumSerializerTrait{
abstract public function writeInt(int $bits, int $value) : self; abstract public function int(int $bits, int $value) : void;
public function writeBellAttachmentType(\pocketmine\block\utils\BellAttachmentType $value) : void{ public function bellAttachmentType(\pocketmine\block\utils\BellAttachmentType $value) : void{
$this->writeInt(2, match($value){ $this->int(2, match($value){
\pocketmine\block\utils\BellAttachmentType::CEILING() => 0, \pocketmine\block\utils\BellAttachmentType::CEILING() => 0,
\pocketmine\block\utils\BellAttachmentType::FLOOR() => 1, \pocketmine\block\utils\BellAttachmentType::FLOOR() => 1,
\pocketmine\block\utils\BellAttachmentType::ONE_WALL() => 2, \pocketmine\block\utils\BellAttachmentType::ONE_WALL() => 2,
@ -41,8 +41,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeCopperOxidation(\pocketmine\block\utils\CopperOxidation $value) : void{ public function copperOxidation(\pocketmine\block\utils\CopperOxidation $value) : void{
$this->writeInt(2, match($value){ $this->int(2, match($value){
\pocketmine\block\utils\CopperOxidation::EXPOSED() => 0, \pocketmine\block\utils\CopperOxidation::EXPOSED() => 0,
\pocketmine\block\utils\CopperOxidation::NONE() => 1, \pocketmine\block\utils\CopperOxidation::NONE() => 1,
\pocketmine\block\utils\CopperOxidation::OXIDIZED() => 2, \pocketmine\block\utils\CopperOxidation::OXIDIZED() => 2,
@ -51,8 +51,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeCoralType(\pocketmine\block\utils\CoralType $value) : void{ public function coralType(\pocketmine\block\utils\CoralType $value) : void{
$this->writeInt(3, match($value){ $this->int(3, match($value){
\pocketmine\block\utils\CoralType::BRAIN() => 0, \pocketmine\block\utils\CoralType::BRAIN() => 0,
\pocketmine\block\utils\CoralType::BUBBLE() => 1, \pocketmine\block\utils\CoralType::BUBBLE() => 1,
\pocketmine\block\utils\CoralType::FIRE() => 2, \pocketmine\block\utils\CoralType::FIRE() => 2,
@ -62,8 +62,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeDyeColor(\pocketmine\block\utils\DyeColor $value) : void{ public function dyeColor(\pocketmine\block\utils\DyeColor $value) : void{
$this->writeInt(4, match($value){ $this->int(4, match($value){
\pocketmine\block\utils\DyeColor::BLACK() => 0, \pocketmine\block\utils\DyeColor::BLACK() => 0,
\pocketmine\block\utils\DyeColor::BLUE() => 1, \pocketmine\block\utils\DyeColor::BLUE() => 1,
\pocketmine\block\utils\DyeColor::BROWN() => 2, \pocketmine\block\utils\DyeColor::BROWN() => 2,
@ -84,8 +84,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeLeverFacing(\pocketmine\block\utils\LeverFacing $value) : void{ public function leverFacing(\pocketmine\block\utils\LeverFacing $value) : void{
$this->writeInt(3, match($value){ $this->int(3, match($value){
\pocketmine\block\utils\LeverFacing::DOWN_AXIS_X() => 0, \pocketmine\block\utils\LeverFacing::DOWN_AXIS_X() => 0,
\pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z() => 1, \pocketmine\block\utils\LeverFacing::DOWN_AXIS_Z() => 1,
\pocketmine\block\utils\LeverFacing::EAST() => 2, \pocketmine\block\utils\LeverFacing::EAST() => 2,
@ -98,8 +98,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeMushroomBlockType(\pocketmine\block\utils\MushroomBlockType $value) : void{ public function mushroomBlockType(\pocketmine\block\utils\MushroomBlockType $value) : void{
$this->writeInt(4, match($value){ $this->int(4, match($value){
\pocketmine\block\utils\MushroomBlockType::ALL_CAP() => 0, \pocketmine\block\utils\MushroomBlockType::ALL_CAP() => 0,
\pocketmine\block\utils\MushroomBlockType::CAP_EAST() => 1, \pocketmine\block\utils\MushroomBlockType::CAP_EAST() => 1,
\pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE() => 2, \pocketmine\block\utils\MushroomBlockType::CAP_MIDDLE() => 2,
@ -115,8 +115,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writePotionType(\pocketmine\item\PotionType $value) : void{ public function potionType(\pocketmine\item\PotionType $value) : void{
$this->writeInt(6, match($value){ $this->int(6, match($value){
\pocketmine\item\PotionType::AWKWARD() => 0, \pocketmine\item\PotionType::AWKWARD() => 0,
\pocketmine\item\PotionType::FIRE_RESISTANCE() => 1, \pocketmine\item\PotionType::FIRE_RESISTANCE() => 1,
\pocketmine\item\PotionType::HARMING() => 2, \pocketmine\item\PotionType::HARMING() => 2,
@ -163,8 +163,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeSkullType(\pocketmine\block\utils\SkullType $value) : void{ public function skullType(\pocketmine\block\utils\SkullType $value) : void{
$this->writeInt(3, match($value){ $this->int(3, match($value){
\pocketmine\block\utils\SkullType::CREEPER() => 0, \pocketmine\block\utils\SkullType::CREEPER() => 0,
\pocketmine\block\utils\SkullType::DRAGON() => 1, \pocketmine\block\utils\SkullType::DRAGON() => 1,
\pocketmine\block\utils\SkullType::PLAYER() => 2, \pocketmine\block\utils\SkullType::PLAYER() => 2,
@ -175,8 +175,8 @@ trait RuntimeEnumSerializerTrait{
}); });
} }
public function writeSlabType(\pocketmine\block\utils\SlabType $value) : void{ public function slabType(\pocketmine\block\utils\SlabType $value) : void{
$this->writeInt(2, match($value){ $this->int(2, match($value){
\pocketmine\block\utils\SlabType::BOTTOM() => 0, \pocketmine\block\utils\SlabType::BOTTOM() => 0,
\pocketmine\block\utils\SlabType::DOUBLE() => 1, \pocketmine\block\utils\SlabType::DOUBLE() => 1,
\pocketmine\block\utils\SlabType::TOP() => 2, \pocketmine\block\utils\SlabType::TOP() => 2,

View File

@ -64,7 +64,7 @@ class Banner extends ItemBlockWallOrFloor{
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeDyeColor($this->color); $w->dyeColor($this->color);
} }
/** /**

View File

@ -33,7 +33,7 @@ use pocketmine\math\Facing;
final class CoralFan extends Item{ final class CoralFan extends Item{
use CoralTypeTrait { use CoralTypeTrait {
encodeType as encodeCoralType; describeType as encodeCoralType;
} }
public function __construct(ItemIdentifier $identifier){ public function __construct(ItemIdentifier $identifier){

View File

@ -35,7 +35,7 @@ class Dye extends Item{
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeDyeColor($this->color); $w->dyeColor($this->color);
} }
public function getColor() : DyeColor{ public function getColor() : DyeColor{

View File

@ -45,7 +45,7 @@ final class ItemBlock extends Item{
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeInt(Block::INTERNAL_STATE_DATA_BITS, $this->blockTypeData); $w->int(Block::INTERNAL_STATE_DATA_BITS, $this->blockTypeData);
} }
public function getBlock(?int $clickedFace = null) : Block{ public function getBlock(?int $clickedFace = null) : Block{

View File

@ -37,7 +37,7 @@ class Potion extends Item implements ConsumableItem{
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function encodeType(RuntimeDataWriter $w) : void{
$w->writePotionType($this->potionType); $w->potionType($this->potionType);
} }
public function getType() : PotionType{ return $this->potionType; } public function getType() : PotionType{ return $this->potionType; }

View File

@ -39,7 +39,7 @@ class SplashPotion extends ProjectileItem{
} }
protected function encodeType(RuntimeDataWriter $w) : void{ protected function encodeType(RuntimeDataWriter $w) : void{
$w->writePotionType($this->potionType); $w->potionType($this->potionType);
} }
public function getType() : PotionType{ return $this->potionType; } public function getType() : PotionType{ return $this->potionType; }