diff --git a/src/data/bedrock/item/ItemDeserializer.php b/src/data/bedrock/item/ItemDeserializer.php index 6bbef3246..42a42ce94 100644 --- a/src/data/bedrock/item/ItemDeserializer.php +++ b/src/data/bedrock/item/ItemDeserializer.php @@ -29,6 +29,7 @@ use pocketmine\block\utils\SkullType; use pocketmine\block\utils\TreeType; use pocketmine\block\VanillaBlocks as Blocks; use pocketmine\data\bedrock\block\BlockStateDeserializeException; +use pocketmine\data\bedrock\block\BlockStateDeserializer; use pocketmine\data\bedrock\CompoundTypeIds; use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\bedrock\EntityLegacyIds; @@ -43,16 +44,15 @@ use pocketmine\utils\SingletonTrait; use pocketmine\world\format\io\GlobalBlockStateHandlers; final class ItemDeserializer{ - - use SingletonTrait; - /** * @var \Closure[] * @phpstan-var array */ private array $deserializers = []; - public function __construct(){ + public function __construct( + private BlockStateDeserializer $blockStateDeserializer + ){ $this->registerDeserializers(); } @@ -70,7 +70,7 @@ final class ItemDeserializer{ if(($blockData = $data->getBlock()) !== null){ //TODO: this is rough duct tape; we need a better way to deal with this try{ - $block = GlobalBlockStateHandlers::getDeserializer()->deserialize($blockData); + $block = $this->blockStateDeserializer->deserialize($blockData); }catch(BlockStateDeserializeException $e){ throw new ItemTypeDeserializeException("Failed to deserialize item data: " . $e->getMessage(), 0, $e); } diff --git a/src/data/bedrock/item/ItemSerializer.php b/src/data/bedrock/item/ItemSerializer.php index d5b87e110..49cf012fc 100644 --- a/src/data/bedrock/item/ItemSerializer.php +++ b/src/data/bedrock/item/ItemSerializer.php @@ -27,6 +27,7 @@ use pocketmine\block\Block; use pocketmine\block\utils\DyeColor; use pocketmine\block\utils\SkullType; use pocketmine\block\VanillaBlocks as Blocks; +use pocketmine\data\bedrock\block\BlockStateSerializer; use pocketmine\data\bedrock\item\BlockItemIdMap; use pocketmine\data\bedrock\block\BlockStateSerializeException; use pocketmine\data\bedrock\CompoundTypeIds; @@ -61,7 +62,9 @@ final class ItemSerializer{ */ private array $blockItemSerializers = []; - public function __construct(){ + public function __construct( + private BlockStateSerializer $blockStateSerializer + ){ $this->registerSerializers(); } @@ -170,7 +173,7 @@ final class ItemSerializer{ $serializer = $locatedSerializer; $data = $serializer($block); }else{ - $data = self::standardBlock($block); + $data = $this->standardBlock($block); } return $data; @@ -179,9 +182,9 @@ final class ItemSerializer{ /** * @throws ItemTypeSerializeException */ - private static function standardBlock(Block $block) : Data{ + private function standardBlock(Block $block) : Data{ try{ - $blockStateData = GlobalBlockStateHandlers::getSerializer()->serialize($block->getFullId()); + $blockStateData = $this->blockStateSerializer->serialize($block->getFullId()); }catch(BlockStateSerializeException $e){ throw new ItemTypeSerializeException($e->getMessage(), 0, $e); } @@ -261,8 +264,8 @@ final class ItemSerializer{ //these are encoded as regular blocks, but they have to be accounted for explicitly since they don't use ItemBlock //Bamboo->getBlock() returns BambooSapling :( - $this->map(Items::BAMBOO(), fn() => self::standardBlock(Blocks::BAMBOO())); - $this->map(Items::CORAL_FAN(), fn(CoralFan $item) => self::standardBlock($item->getBlock())); + $this->map(Items::BAMBOO(), fn() => $this->standardBlock(Blocks::BAMBOO())); + $this->map(Items::CORAL_FAN(), fn(CoralFan $item) => $this->standardBlock($item->getBlock())); $this->map(Items::ACACIA_BOAT(), self::id(Ids::ACACIA_BOAT)); $this->map(Items::ACACIA_SIGN(), self::id(Ids::ACACIA_SIGN)); diff --git a/src/network/mcpe/convert/ItemTranslator.php b/src/network/mcpe/convert/ItemTranslator.php index 4243971c8..6ada7f372 100644 --- a/src/network/mcpe/convert/ItemTranslator.php +++ b/src/network/mcpe/convert/ItemTranslator.php @@ -32,6 +32,7 @@ use pocketmine\item\Item; use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary; use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\SingletonTrait; +use pocketmine\world\format\io\GlobalBlockStateHandlers; /** * This class handles translation between network item ID+metadata to PocketMine-MP internal ID+metadata and vice versa. @@ -45,8 +46,8 @@ final class ItemTranslator{ return new self( GlobalItemTypeDictionary::getInstance()->getDictionary(), RuntimeBlockMapping::getInstance()->getBlockStateDictionary(), - new ItemSerializer(), - new ItemDeserializer() + new ItemSerializer(GlobalBlockStateHandlers::getSerializer()), + new ItemDeserializer(GlobalBlockStateHandlers::getDeserializer()) ); } diff --git a/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php b/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php index cc73321e7..ebc7dafc8 100644 --- a/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php +++ b/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php @@ -26,6 +26,7 @@ namespace pocketmine\data\bedrock\item; use PHPUnit\Framework\TestCase; use pocketmine\block\BlockFactory; use pocketmine\item\ItemFactory; +use pocketmine\world\format\io\GlobalBlockStateHandlers; final class ItemSerializerDeserializerTest extends TestCase{ @@ -33,8 +34,8 @@ final class ItemSerializerDeserializerTest extends TestCase{ private ItemSerializer $serializer; public function setUp() : void{ - $this->deserializer = new ItemDeserializer(); - $this->serializer = new ItemSerializer(); + $this->deserializer = new ItemDeserializer(GlobalBlockStateHandlers::getDeserializer()); + $this->serializer = new ItemSerializer(GlobalBlockStateHandlers::getSerializer()); } public function testAllVanillaItemsSerializableAndDeserializable() : void{