mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Allow Item (de)serializer to accept dynamic BlockState(De)Serializer
This commit is contained in:
@ -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<string, \Closure(Data) : Item>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@ -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));
|
||||
|
Reference in New Issue
Block a user