mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 10:53:05 +00:00
Make Block(De)Serializer much less nasty to interact with
this makes it a lot less inconvenient to access the primary blockstate serializer/deserializer, which is necessary for registering new blocks.
This commit is contained in:
@ -179,6 +179,12 @@ final class BlockObjectToBlockStateSerializer implements BlockStateSerializer{
|
||||
*/
|
||||
private array $serializers = [];
|
||||
|
||||
/**
|
||||
* @var BlockStateData[]
|
||||
* @phpstan-var array<int, BlockStateData>
|
||||
*/
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct(){
|
||||
$this->registerCandleSerializers();
|
||||
$this->registerCauldronSerializers();
|
||||
@ -188,7 +194,8 @@ final class BlockObjectToBlockStateSerializer implements BlockStateSerializer{
|
||||
|
||||
public function serialize(int $stateId) : BlockStateData{
|
||||
//TODO: singleton usage not ideal
|
||||
return $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId));
|
||||
//TODO: we may want to deduplicate cache entries to avoid wasting memory
|
||||
return $this->cache[$stateId] ??= $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,6 +51,7 @@ use pocketmine\data\bedrock\block\convert\BlockStateReader as Reader;
|
||||
use pocketmine\math\Axis;
|
||||
use pocketmine\math\Facing;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
use function min;
|
||||
|
||||
final class BlockStateToBlockObjectDeserializer implements BlockStateDeserializer{
|
||||
@ -61,6 +62,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize
|
||||
*/
|
||||
private array $deserializeFuncs = [];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<string, int>
|
||||
*/
|
||||
private array $simpleCache = [];
|
||||
|
||||
public function __construct(){
|
||||
$this->registerCandleDeserializers();
|
||||
$this->registerCauldronDeserializers();
|
||||
@ -69,6 +76,12 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize
|
||||
}
|
||||
|
||||
public function deserialize(BlockStateData $stateData) : int{
|
||||
if(count($stateData->getStates()) === 0){
|
||||
//if a block has zero properties, we can keep a map of string ID -> internal blockstate ID
|
||||
return $this->simpleCache[$stateData->getName()] ??= $this->deserializeBlock($stateData)->getStateId();
|
||||
}
|
||||
|
||||
//we can't cache blocks that have properties - go ahead and deserialize the slow way
|
||||
return $this->deserializeBlock($stateData)->getStateId();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user