mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 16:24:05 +00:00
Allow Item (de)serializer to accept dynamic BlockState(De)Serializer
This commit is contained in:
parent
c8e318df8c
commit
301b0aba82
@ -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));
|
||||
|
@ -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())
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user