Improve BlockFactory initialization performance

as expected, expanding data range unconditionally resulted in some performance issues ...
This commit is contained in:
Dylan K. Taylor 2022-06-27 15:37:05 +01:00
parent bedc9cf518
commit 0afb67be7d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
75 changed files with 169 additions and 3 deletions

View File

@ -51,6 +51,8 @@ class Anvil extends Transparent implements Fallable{
return $this->damage << 2; return $this->damage << 2;
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->setDamage($r->readBoundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED)); $this->setDamage($r->readBoundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED));
$this->setFacing($r->readHorizontalFacing()); $this->setFacing($r->readHorizontalFacing());

View File

@ -55,6 +55,8 @@ class Bamboo extends Transparent{
protected bool $ready = false; protected bool $ready = false;
protected int $leafSize = self::NO_LEAVES; protected int $leafSize = self::NO_LEAVES;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->setLeafSize($r->readBoundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES)); $this->setLeafSize($r->readBoundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES));
$this->setThick($r->readBool()); $this->setThick($r->readBool());

View File

@ -36,6 +36,8 @@ use pocketmine\world\BlockTransaction;
final class BambooSapling extends Flowable{ final class BambooSapling extends Flowable{
private bool $ready = false; private bool $ready = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->setReady($r->readBool()); $this->setReady($r->readBool());
} }

View File

@ -39,6 +39,8 @@ class Barrel extends Opaque{
protected bool $open = false; protected bool $open = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->setFacing($r->readFacing()); $this->setFacing($r->readFacing());
$this->setOpen($r->readBool()); $this->setOpen($r->readBool());

View File

@ -55,6 +55,9 @@ abstract class BaseBanner extends Transparent{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 0; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
//TODO: we currently purposely don't read or write colour (it's stored on the tile) //TODO: we currently purposely don't read or write colour (it's stored on the tile)
} }

View File

@ -54,6 +54,8 @@ class Bed extends Transparent{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->occupied = $r->readBool(); $this->occupied = $r->readBool();

View File

@ -29,6 +29,8 @@ use pocketmine\block\utils\BlockDataWriter;
class Bedrock extends Opaque{ class Bedrock extends Opaque{
private bool $burnsForever = false; private bool $burnsForever = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->burnsForever = $r->readBool(); $this->burnsForever = $r->readBool();
} }

View File

@ -49,6 +49,8 @@ final class Bell extends Transparent{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->attachmentType = BlockDataReaderHelper::readBellAttachmentType($r); $this->attachmentType = BlockDataReaderHelper::readBellAttachmentType($r);
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();

View File

@ -100,9 +100,16 @@ class Block{
return 0; return 0;
} }
public function getRequiredStateDataBits() : int{ return 0; }
public function decodeStateData(int $data) : void{ public function decodeStateData(int $data) : void{
$reader = new BlockDataReader(self::INTERNAL_STATE_DATA_BITS, $data); $givenBits = $this->getRequiredStateDataBits();
$reader = new BlockDataReader($givenBits, $data);
$this->decodeState($reader); $this->decodeState($reader);
$readBits = $reader->getOffset();
if($givenBits !== $readBits){
throw new \LogicException("Exactly $givenBits bits of state data were provided, but only $readBits were read");
}
} }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
@ -113,8 +120,14 @@ class Block{
* @internal * @internal
*/ */
public function computeStateData() : int{ public function computeStateData() : int{
$writer = new BlockDataWriter(self::INTERNAL_STATE_DATA_BITS); $requiredBits = $this->getRequiredStateDataBits();
$writer = new BlockDataWriter($requiredBits);
$this->encodeState($writer); $this->encodeState($writer);
$writtenBits = $writer->getOffset();
if($requiredBits !== $writtenBits){
throw new \LogicException("Exactly $requiredBits bits of state data were expected, but only $writtenBits were written");
}
return $writer->getValue(); return $writer->getValue();
} }

View File

@ -864,7 +864,11 @@ class BlockFactory{
//TODO: this bruteforce approach to discovering all valid states is very inefficient for larger state data sizes //TODO: this bruteforce approach to discovering all valid states is very inefficient for larger state data sizes
//at some point we'll need to find a better way to do this //at some point we'll need to find a better way to do this
for($stateData = 0; $stateData < (1 << Block::INTERNAL_STATE_DATA_BITS); ++$stateData){ $bits = $block->getRequiredStateDataBits();
if($bits > Block::INTERNAL_STATE_DATA_BITS){
throw new \InvalidArgumentException("Block state data cannot use more than " . Block::INTERNAL_STATE_DATA_BITS . " bits");
}
for($stateData = 0; $stateData < (1 << $bits); ++$stateData){
$v = clone $block; $v = clone $block;
try{ try{
$v->decodeStateData($stateData); $v->decodeStateData($stateData);

View File

@ -46,6 +46,8 @@ class BrewingStand extends Transparent{
*/ */
protected array $slots = []; protected array $slots = [];
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->setSlots(BlockDataReaderHelper::readBrewingStandSlotKeySet($r)); $this->setSlots(BlockDataReaderHelper::readBrewingStandSlotKeySet($r));
} }

View File

@ -39,6 +39,8 @@ abstract class Button extends Flowable{
protected bool $pressed = false; protected bool $pressed = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readFacing(); $this->facing = $r->readFacing();
$this->pressed = $r->readBool(); $this->pressed = $r->readBool();

View File

@ -42,6 +42,8 @@ class Cactus extends Transparent{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(4, 0, self::MAX_AGE);
} }

View File

@ -41,6 +41,8 @@ class Cake extends Transparent implements FoodSource{
protected int $bites = 0; protected int $bites = 0;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->bites = $r->readBoundedInt(3, 0, self::MAX_BITES); $this->bites = $r->readBoundedInt(3, 0, self::MAX_BITES);
} }

View File

@ -47,6 +47,8 @@ class CocoaBlock extends Transparent{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(2, 0, self::MAX_AGE);

View File

@ -39,6 +39,8 @@ abstract class Crops extends Flowable{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(3, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(3, 0, self::MAX_AGE);
} }

View File

@ -42,6 +42,8 @@ class DaylightSensor extends Transparent{
protected bool $inverted = false; protected bool $inverted = false;
public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->signalStrength = $r->readBoundedInt(4, 0, 15); $this->signalStrength = $r->readBoundedInt(4, 0, 15);
$this->inverted = $r->readBool(); $this->inverted = $r->readBool();

View File

@ -29,6 +29,8 @@ use pocketmine\block\utils\BlockDataWriter;
class DetectorRail extends StraightOnlyRail{ class DetectorRail extends StraightOnlyRail{
protected bool $activated = false; protected bool $activated = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->activated = $r->readBool(); $this->activated = $r->readBool();

View File

@ -39,6 +39,8 @@ class Dirt extends Opaque{
return $this->coarse ? BlockLegacyMetadata::DIRT_FLAG_COARSE : 0; return $this->coarse ? BlockLegacyMetadata::DIRT_FLAG_COARSE : 0;
} }
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->coarse = $r->readBool(); $this->coarse = $r->readBool();
} }

View File

@ -42,6 +42,9 @@ class Door extends Transparent{
protected bool $hingeRight = false; protected bool $hingeRight = false;
protected bool $open = false; protected bool $open = false;
public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->top = $r->readBool(); $this->top = $r->readBool();

View File

@ -34,6 +34,8 @@ use pocketmine\world\BlockTransaction;
class DoublePlant extends Flowable{ class DoublePlant extends Flowable{
protected bool $top = false; protected bool $top = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->top = $r->readBool(); $this->top = $r->readBool();
} }

View File

@ -36,6 +36,8 @@ class EndPortalFrame extends Opaque{
protected bool $eye = false; protected bool $eye = false;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->eye = $r->readBool(); $this->eye = $r->readBool();

View File

@ -38,6 +38,8 @@ class Farmland extends Transparent{
protected int $wetness = 0; //"moisture" blockstate property in PC protected int $wetness = 0; //"moisture" blockstate property in PC
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->wetness = $r->readBoundedInt(3, 0, self::MAX_WETNESS); $this->wetness = $r->readBoundedInt(3, 0, self::MAX_WETNESS);
} }

View File

@ -41,6 +41,8 @@ class FenceGate extends Transparent{
protected bool $open = false; protected bool $open = false;
protected bool $inWall = false; protected bool $inWall = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->open = $r->readBool(); $this->open = $r->readBool();

View File

@ -46,6 +46,8 @@ class Fire extends Flowable{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(4, 0, self::MAX_AGE);
} }

View File

@ -38,6 +38,8 @@ final class FloorBanner extends BaseBanner{
encodeState as encodeRotation; encodeState as encodeRotation;
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->decodeRotation($r); $this->decodeRotation($r);

View File

@ -52,6 +52,8 @@ final class FloorCoralFan extends BaseCoral{
return CoralTypeIdMap::getInstance()->toId($this->coralType); return CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->axis = $r->readHorizontalAxis(); $this->axis = $r->readHorizontalAxis();

View File

@ -33,6 +33,8 @@ class FrostedIce extends Ice{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(2, 0, self::MAX_AGE);
} }

View File

@ -39,6 +39,8 @@ class Furnace extends Opaque{
protected bool $lit = false; protected bool $lit = false;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->lit = $r->readBool(); $this->lit = $r->readBool();

View File

@ -41,6 +41,8 @@ class Hopper extends Transparent{
private int $facing = Facing::DOWN; private int $facing = Facing::DOWN;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$facing = $r->readFacing(); $facing = $r->readFacing();
if($facing === Facing::UP){ if($facing === Facing::UP){

View File

@ -47,6 +47,8 @@ class ItemFrame extends Flowable{
protected int $itemRotation = 0; protected int $itemRotation = 0;
protected float $itemDropChance = 1.0; protected float $itemDropChance = 1.0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readFacing(); $this->facing = $r->readFacing();
$this->hasMap = $r->readBool(); $this->hasMap = $r->readBool();

View File

@ -37,6 +37,8 @@ use pocketmine\world\BlockTransaction;
class Lantern extends Transparent{ class Lantern extends Transparent{
protected bool $hanging = false; protected bool $hanging = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->hanging = $r->readBool(); $this->hanging = $r->readBool();
} }

View File

@ -49,6 +49,8 @@ class Leaves extends Transparent{
$this->treeType = $treeType; $this->treeType = $treeType;
} }
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->noDecay = $r->readBool(); $this->noDecay = $r->readBool();
$this->checkDecay = $r->readBool(); $this->checkDecay = $r->readBool();

View File

@ -47,6 +47,8 @@ class Lectern extends Transparent{
protected bool $producingSignal = false; protected bool $producingSignal = false;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->producingSignal = $r->readBool(); $this->producingSignal = $r->readBool();

View File

@ -47,6 +47,8 @@ class Lever extends Flowable{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = BlockDataReaderHelper::readLeverFacing($r); $this->facing = BlockDataReaderHelper::readLeverFacing($r);
$this->activated = $r->readBool(); $this->activated = $r->readBool();

View File

@ -49,6 +49,8 @@ abstract class Liquid extends Transparent{
protected int $decay = 0; //PC "level" property protected int $decay = 0; //PC "level" property
protected bool $still = false; protected bool $still = false;
public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->decay = $r->readBoundedInt(3, 0, self::MAX_DECAY); $this->decay = $r->readBoundedInt(3, 0, self::MAX_DECAY);
$this->falling = $r->readBool(); $this->falling = $r->readBool();

View File

@ -35,6 +35,8 @@ class NetherPortal extends Transparent{
protected int $axis = Axis::X; protected int $axis = Axis::X;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->axis = $r->readHorizontalAxis(); $this->axis = $r->readHorizontalAxis();
} }

View File

@ -38,6 +38,8 @@ class NetherWartPlant extends Flowable{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(2, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(2, 0, self::MAX_AGE);
} }

View File

@ -35,6 +35,8 @@ class Rail extends BaseRail{
private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH; private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$railShape = $r->readInt(4); $railShape = $r->readInt(4);
if(!isset(RailConnectionInfo::CONNECTIONS[$railShape]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$railShape])){ if(!isset(RailConnectionInfo::CONNECTIONS[$railShape]) && !isset(RailConnectionInfo::CURVE_CONNECTIONS[$railShape])){

View File

@ -39,6 +39,8 @@ class RedMushroomBlock extends Opaque{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->mushroomBlockType = BlockDataReaderHelper::readMushroomBlockType($r); $this->mushroomBlockType = BlockDataReaderHelper::readMushroomBlockType($r);
} }

View File

@ -45,6 +45,8 @@ class RedstoneComparator extends Flowable{
protected bool $isSubtractMode = false; protected bool $isSubtractMode = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->isSubtractMode = $r->readBool(); $this->isSubtractMode = $r->readBool();

View File

@ -30,6 +30,8 @@ use pocketmine\block\utils\PoweredByRedstoneTrait;
class RedstoneLamp extends Opaque{ class RedstoneLamp extends Opaque{
use PoweredByRedstoneTrait; use PoweredByRedstoneTrait;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->powered = $r->readBool(); $this->powered = $r->readBool();
} }

View File

@ -34,6 +34,8 @@ use function mt_rand;
class RedstoneOre extends Opaque{ class RedstoneOre extends Opaque{
protected bool $lit = false; protected bool $lit = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->lit = $r->readBool(); $this->lit = $r->readBool();
} }

View File

@ -44,6 +44,8 @@ class RedstoneRepeater extends Flowable{
protected int $delay = self::MIN_DELAY; protected int $delay = self::MIN_DELAY;
public function getRequiredStateDataBits() : int{ return 5; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->delay = $r->readBoundedInt(2, self::MIN_DELAY - 1, self::MAX_DELAY - 1) + 1; $this->delay = $r->readBoundedInt(2, self::MIN_DELAY - 1, self::MAX_DELAY - 1) + 1;

View File

@ -29,6 +29,8 @@ use pocketmine\block\utils\BlockDataWriter;
class RedstoneTorch extends Torch{ class RedstoneTorch extends Torch{
protected bool $lit = true; protected bool $lit = true;
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->lit = $r->readBool(); $this->lit = $r->readBool();

View File

@ -47,6 +47,8 @@ class Sapling extends Flowable{
$this->treeType = $treeType; $this->treeType = $treeType;
} }
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->ready = $r->readBool(); $this->ready = $r->readBool();
} }

View File

@ -39,6 +39,8 @@ class SeaPickle extends Transparent{
protected int $count = self::MIN_COUNT; protected int $count = self::MIN_COUNT;
protected bool $underwater = false; protected bool $underwater = false;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->count = $r->readBoundedInt(2, self::MIN_COUNT - 1, self::MAX_COUNT - 1) + 1; $this->count = $r->readBoundedInt(2, self::MIN_COUNT - 1, self::MAX_COUNT - 1) + 1;
$this->underwater = $r->readBool(); $this->underwater = $r->readBool();

View File

@ -35,6 +35,8 @@ use pocketmine\world\BlockTransaction;
class ShulkerBox extends Opaque{ class ShulkerBox extends Opaque{
use AnyFacingTrait; use AnyFacingTrait;
public function getRequiredStateDataBits() : int{ return 0; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : 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

@ -29,6 +29,8 @@ use pocketmine\block\utils\BlockDataWriter;
abstract class SimplePressurePlate extends PressurePlate{ abstract class SimplePressurePlate extends PressurePlate{
protected bool $pressed = false; protected bool $pressed = false;
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->pressed = $r->readBool(); $this->pressed = $r->readBool();
} }

View File

@ -51,6 +51,8 @@ class Skull extends Flowable{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$facing = $r->readFacing(); $facing = $r->readFacing();
if($facing === Facing::DOWN){ if($facing === Facing::DOWN){

View File

@ -44,6 +44,8 @@ class Slab extends Transparent{
$this->slabType = SlabType::BOTTOM(); $this->slabType = SlabType::BOTTOM();
} }
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->slabType = BlockDataReaderHelper::readSlabType($r); $this->slabType = BlockDataReaderHelper::readSlabType($r);
} }

View File

@ -47,6 +47,8 @@ class SnowLayer extends Flowable implements Fallable{
protected int $layers = self::MIN_LAYERS; protected int $layers = self::MIN_LAYERS;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->layers = $r->readBoundedInt(3, self::MIN_LAYERS - 1, self::MAX_LAYERS - 1) + 1; $this->layers = $r->readBoundedInt(3, self::MIN_LAYERS - 1, self::MAX_LAYERS - 1) + 1;
} }

View File

@ -33,6 +33,8 @@ class Sponge extends Opaque{
return $this->wet ? BlockLegacyMetadata::SPONGE_FLAG_WET : 0; return $this->wet ? BlockLegacyMetadata::SPONGE_FLAG_WET : 0;
} }
public function getRequiredStateDataBits() : int{ return 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->wet = $r->readBool(); $this->wet = $r->readBool();
} }

View File

@ -47,6 +47,8 @@ class Stair extends Transparent{
parent::__construct($idInfo, $name, $breakInfo); parent::__construct($idInfo, $name, $breakInfo);
} }
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->upsideDown = $r->readBool(); $this->upsideDown = $r->readBool();

View File

@ -37,6 +37,8 @@ class StraightOnlyRail extends BaseRail{
private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH; private int $railShape = BlockLegacyMetadata::RAIL_STRAIGHT_NORTH_SOUTH;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$railShape = $r->readInt(3); $railShape = $r->readInt(3);
if(!isset(RailConnectionInfo::CONNECTIONS[$railShape])){ if(!isset(RailConnectionInfo::CONNECTIONS[$railShape])){

View File

@ -38,6 +38,8 @@ class Sugarcane extends Flowable{
protected int $age = 0; protected int $age = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(4, 0, self::MAX_AGE); $this->age = $r->readBoundedInt(4, 0, self::MAX_AGE);
} }

View File

@ -46,6 +46,8 @@ class SweetBerryBush extends Flowable{
protected int $age = self::STAGE_SAPLING; protected int $age = self::STAGE_SAPLING;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->age = $r->readBoundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE); $this->age = $r->readBoundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE);
} }

View File

@ -49,6 +49,8 @@ class TNT extends Opaque{
return $this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0; return $this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0;
} }
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->unstable = $r->readBool(); $this->unstable = $r->readBool();
$this->worksUnderwater = $r->readBool(); $this->worksUnderwater = $r->readBool();

View File

@ -38,6 +38,8 @@ class Torch extends Flowable{
protected int $facing = Facing::UP; protected int $facing = Facing::UP;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$facing = $r->readFacing(); $facing = $r->readFacing();
if($facing === Facing::DOWN){ if($facing === Facing::DOWN){

View File

@ -41,6 +41,8 @@ class Trapdoor extends Transparent{
protected bool $open = false; protected bool $open = false;
protected bool $top = false; protected bool $top = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->top = $r->readBool(); $this->top = $r->readBool();

View File

@ -32,6 +32,8 @@ class Tripwire extends Flowable{
protected bool $connected = false; protected bool $connected = false;
protected bool $disarmed = false; protected bool $disarmed = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->triggered = $r->readBool(); $this->triggered = $r->readBool();
$this->suspended = $r->readBool(); $this->suspended = $r->readBool();

View File

@ -39,6 +39,8 @@ class TripwireHook extends Flowable{
protected bool $connected = false; protected bool $connected = false;
protected bool $powered = false; protected bool $powered = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
$this->connected = $r->readBool(); $this->connected = $r->readBool();

View File

@ -40,6 +40,8 @@ class Vine extends Flowable{
/** @var int[] */ /** @var int[] */
protected array $faces = []; protected array $faces = [];
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
foreach(Facing::HORIZONTAL as $facing){ foreach(Facing::HORIZONTAL as $facing){
$this->setFace($facing, $r->readBool()); $this->setFace($facing, $r->readBool());

View File

@ -43,6 +43,8 @@ class Wall extends Transparent{
protected array $connections = []; protected array $connections = [];
protected bool $post = false; protected bool $post = false;
public function getRequiredStateDataBits() : int{ return 9; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->connections = $r->readWallConnections(); $this->connections = $r->readWallConnections();
$this->post = $r->readBool(); $this->post = $r->readBool();

View File

@ -39,6 +39,8 @@ final class WallBanner extends BaseBanner{
encodeState as encodeFacing; encodeState as encodeFacing;
} }
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->decodeFacing($r); $this->decodeFacing($r);

View File

@ -43,6 +43,8 @@ final class WallCoralFan extends BaseCoral{
return CoralTypeIdMap::getInstance()->toId($this->coralType); return CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();

View File

@ -26,6 +26,8 @@ namespace pocketmine\block\utils;
trait AnalogRedstoneSignalEmitterTrait{ trait AnalogRedstoneSignalEmitterTrait{
protected int $signalStrength = 0; protected int $signalStrength = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->signalStrength = $r->readBoundedInt(4, 0, 15); $this->signalStrength = $r->readBoundedInt(4, 0, 15);
} }

View File

@ -28,6 +28,9 @@ use pocketmine\math\Facing;
trait AnyFacingTrait{ trait AnyFacingTrait{
protected int $facing = Facing::DOWN; protected int $facing = Facing::DOWN;
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readFacing(); $this->facing = $r->readFacing();
} }

View File

@ -118,4 +118,6 @@ final class BlockDataReader{
return $connections; return $connections;
} }
public function getOffset() : int{ return $this->offset; }
} }

View File

@ -37,6 +37,8 @@ trait ColoredTrait{
return DyeColorIdMap::getInstance()->toId($this->color); return DyeColorIdMap::getInstance()->toId($this->color);
} }
public function getRequiredStateDataBits() : int{ return 4; }
/** @see Block::decodeState() */ /** @see Block::decodeState() */
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->color = BlockDataReaderHelper::readDyeColor($r); $this->color = BlockDataReaderHelper::readDyeColor($r);

View File

@ -27,6 +27,8 @@ trait CoralTypeTrait{
protected CoralType $coralType; protected CoralType $coralType;
protected bool $dead = false; protected bool $dead = false;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->coralType = BlockDataReaderHelper::readCoralType($r); $this->coralType = BlockDataReaderHelper::readCoralType($r);
$this->dead = $r->readBool(); $this->dead = $r->readBool();

View File

@ -29,6 +29,8 @@ use pocketmine\math\Facing;
trait HorizontalFacingTrait{ trait HorizontalFacingTrait{
protected int $facing = Facing::NORTH; protected int $facing = Facing::NORTH;
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->facing = $r->readHorizontalFacing(); $this->facing = $r->readHorizontalFacing();
} }

View File

@ -34,6 +34,8 @@ use pocketmine\world\BlockTransaction;
trait PillarRotationTrait{ trait PillarRotationTrait{
protected int $axis = Axis::Y; protected int $axis = Axis::Y;
public function getRequiredStateDataBits() : int{ return 2; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->axis = $r->readAxis(); $this->axis = $r->readAxis();
} }

View File

@ -26,6 +26,8 @@ namespace pocketmine\block\utils;
trait RailPoweredByRedstoneTrait{ trait RailPoweredByRedstoneTrait{
use PoweredByRedstoneTrait; use PoweredByRedstoneTrait;
public function getRequiredStateDataBits() : int{ return parent::getRequiredStateDataBits() + 1; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
parent::decodeState($r); parent::decodeState($r);
$this->powered = $r->readBool(); $this->powered = $r->readBool();

View File

@ -29,6 +29,8 @@ trait SignLikeRotationTrait{
/** @var int */ /** @var int */
private $rotation = 0; private $rotation = 0;
public function getRequiredStateDataBits() : int{ return 4; }
protected function decodeState(BlockDataReader $r) : void{ protected function decodeState(BlockDataReader $r) : void{
$this->rotation = $r->readBoundedInt(4, 0, 15); $this->rotation = $r->readBoundedInt(4, 0, 15);
} }