diff --git a/src/block/BlockFactory.php b/src/block/BlockFactory.php index 31b2404d8..36e618068 100644 --- a/src/block/BlockFactory.php +++ b/src/block/BlockFactory.php @@ -46,7 +46,7 @@ use pocketmine\block\tile\Note as TileNote; use pocketmine\block\tile\Skull as TileSkull; use pocketmine\block\utils\DyeColor; use pocketmine\block\utils\InvalidBlockStateException; -use pocketmine\block\utils\PillarRotationTrait; +use pocketmine\block\utils\PillarRotationInMetadataTrait; use pocketmine\block\utils\TreeType; use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\item\Item; @@ -293,7 +293,7 @@ class BlockFactory{ $purpurBreakInfo = new BlockBreakInfo(1.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 30.0); $this->register(new Opaque(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_NORMAL), "Purpur Block", $purpurBreakInfo)); $this->register(new class(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_PILLAR), "Purpur Pillar", $purpurBreakInfo) extends Opaque{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; }); $this->register(new Stair(new BID(Ids::PURPUR_STAIRS), "Purpur Stairs", $purpurBreakInfo)); @@ -301,10 +301,10 @@ class BlockFactory{ $this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_NORMAL), "Quartz Block", $quartzBreakInfo)); $this->register(new Stair(new BID(Ids::QUARTZ_STAIRS), "Quartz Stairs", $quartzBreakInfo)); $this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_CHISELED), "Chiseled Quartz Block", $quartzBreakInfo) extends Opaque{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; }); $this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_PILLAR), "Quartz Pillar", $quartzBreakInfo) extends Opaque{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; }); $this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_SMOOTH), "Smooth Quartz Block", $quartzBreakInfo)); //TODO: this has axis rotation in 1.9, unsure if a bug (https://bugs.mojang.com/browse/MCPE-39074) $this->register(new Stair(new BID(Ids::SMOOTH_QUARTZ_STAIRS), "Smooth Quartz Stairs", $quartzBreakInfo)); diff --git a/src/block/BoneBlock.php b/src/block/BoneBlock.php index 458103852..49ca51f9b 100644 --- a/src/block/BoneBlock.php +++ b/src/block/BoneBlock.php @@ -23,11 +23,11 @@ declare(strict_types=1); namespace pocketmine\block; -use pocketmine\block\utils\PillarRotationTrait; +use pocketmine\block\utils\PillarRotationInMetadataTrait; use pocketmine\item\ToolTier; class BoneBlock extends Opaque{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(2.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel())); diff --git a/src/block/HayBale.php b/src/block/HayBale.php index d6c1be286..6d70a8230 100644 --- a/src/block/HayBale.php +++ b/src/block/HayBale.php @@ -23,10 +23,10 @@ declare(strict_types=1); namespace pocketmine\block; -use pocketmine\block\utils\PillarRotationTrait; +use pocketmine\block\utils\PillarRotationInMetadataTrait; class HayBale extends Opaque{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.5)); diff --git a/src/block/Log.php b/src/block/Log.php index ca9346cab..06364c746 100644 --- a/src/block/Log.php +++ b/src/block/Log.php @@ -23,8 +23,8 @@ declare(strict_types=1); namespace pocketmine\block; -use pocketmine\block\utils\PillarRotationTrait; +use pocketmine\block\utils\PillarRotationInMetadataTrait; class Log extends Wood{ - use PillarRotationTrait; + use PillarRotationInMetadataTrait; } diff --git a/src/block/utils/PillarRotationInMetadataTrait.php b/src/block/utils/PillarRotationInMetadataTrait.php new file mode 100644 index 000000000..42adf7120 --- /dev/null +++ b/src/block/utils/PillarRotationInMetadataTrait.php @@ -0,0 +1,76 @@ +writeAxisToMeta(); + } + + /** + * @see Block::readStateFromData() + */ + public function readStateFromData(int $id, int $stateMeta) : void{ + $this->readAxisFromMeta($stateMeta); + } + + /** + * @see Block::getStateBitmask() + */ + public function getStateBitmask() : int{ + return 0b11 << $this->getAxisMetaShift(); + } + + protected function readAxisFromMeta(int $meta) : void{ + $axis = $meta >> $this->getAxisMetaShift(); + $mapped = [ + 0 => Axis::Y, + 1 => Axis::X, + 2 => Axis::Z + ][$axis] ?? null; + if($mapped === null){ + throw new InvalidBlockStateException("Invalid axis meta $axis"); + } + $this->axis = $mapped; + } + + protected function writeAxisToMeta() : int{ + return [ + Axis::Y => 0, + Axis::Z => 2, + Axis::X => 1 + ][$this->axis] << $this->getAxisMetaShift(); + } +} diff --git a/src/block/utils/PillarRotationTrait.php b/src/block/utils/PillarRotationTrait.php index 77144c4eb..806782db8 100644 --- a/src/block/utils/PillarRotationTrait.php +++ b/src/block/utils/PillarRotationTrait.php @@ -36,52 +36,6 @@ trait PillarRotationTrait{ /** @var int */ protected $axis = Axis::Y; - protected function getAxisMetaShift() : int{ - return 2; //default - } - - /** - * @see Block::writeStateToMeta() - */ - protected function writeStateToMeta() : int{ - return $this->writeAxisToMeta(); - } - - /** - * @see Block::readStateFromData() - */ - public function readStateFromData(int $id, int $stateMeta) : void{ - $this->readAxisFromMeta($stateMeta); - } - - /** - * @see Block::getStateBitmask() - */ - public function getStateBitmask() : int{ - return 0b11 << $this->getAxisMetaShift(); - } - - protected function readAxisFromMeta(int $meta) : void{ - $axis = $meta >> $this->getAxisMetaShift(); - $mapped = [ - 0 => Axis::Y, - 1 => Axis::X, - 2 => Axis::Z - ][$axis] ?? null; - if($mapped === null){ - throw new InvalidBlockStateException("Invalid axis meta $axis"); - } - $this->axis = $mapped; - } - - protected function writeAxisToMeta() : int{ - return [ - Axis::Y => 0, - Axis::Z => 2, - Axis::X => 1 - ][$this->axis] << $this->getAxisMetaShift(); - } - /** @see Axis */ public function getAxis() : int{ return $this->axis; }