From 09e823e304074da2eacccafa4aa8405ff1be9f66 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 May 2023 14:52:21 +0100 Subject: [PATCH] BlockStateDictionary: remove useless indirection --- .../mcpe/convert/BlockStateDictionary.php | 32 +++++++- .../mcpe/convert/BlockStateLookupCache.php | 78 ------------------- 2 files changed, 29 insertions(+), 81 deletions(-) delete mode 100644 src/network/mcpe/convert/BlockStateLookupCache.php diff --git a/src/network/mcpe/convert/BlockStateDictionary.php b/src/network/mcpe/convert/BlockStateDictionary.php index 834390538..e52c7d3d3 100644 --- a/src/network/mcpe/convert/BlockStateDictionary.php +++ b/src/network/mcpe/convert/BlockStateDictionary.php @@ -28,7 +28,10 @@ use pocketmine\data\bedrock\block\BlockTypeNames; use pocketmine\nbt\NbtDataException; use pocketmine\nbt\TreeRoot; use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; +use pocketmine\utils\Utils; +use function array_key_first; use function array_map; +use function count; use function get_debug_type; use function is_array; use function is_int; @@ -40,8 +43,12 @@ use const JSON_THROW_ON_ERROR; * Handles translation of network block runtime IDs into blockstate data, and vice versa */ final class BlockStateDictionary{ + /** + * @var int[][]|int[] + * @phpstan-var array|int> + */ + private array $stateDataToStateIdLookup = []; - private BlockStateLookupCache $stateDataToStateIdLookupCache; /** * @var int[][]|null * @phpstan-var array>|null @@ -56,7 +63,19 @@ final class BlockStateDictionary{ public function __construct( private array $states ){ - $this->stateDataToStateIdLookupCache = new BlockStateLookupCache($this->states); + $table = []; + foreach($this->states as $stateId => $stateNbt){ + $table[$stateNbt->getStateName()][$stateNbt->getRawStateProperties()] = $stateId; + } + + //setup fast path for stateless blocks + foreach(Utils::stringifyKeys($table) as $name => $stateIds){ + if(count($stateIds) === 1){ + $this->stateDataToStateIdLookup[$name] = $stateIds[array_key_first($stateIds)]; + }else{ + $this->stateDataToStateIdLookup[$name] = $stateIds; + } + } } /** @@ -85,7 +104,14 @@ final class BlockStateDictionary{ * Returns null if there were no matches. */ public function lookupStateIdFromData(BlockStateData $data) : ?int{ - return $this->stateDataToStateIdLookupCache->lookupStateId($data); + $name = $data->getName(); + + $lookup = $this->stateDataToStateIdLookup[$name] ?? null; + return match(true){ + $lookup === null => null, + is_int($lookup) => $lookup, + is_array($lookup) => $lookup[BlockStateDictionaryEntry::encodeStateProperties($data->getStates())] ?? null + }; } /** diff --git a/src/network/mcpe/convert/BlockStateLookupCache.php b/src/network/mcpe/convert/BlockStateLookupCache.php deleted file mode 100644 index 16cf0ef0e..000000000 --- a/src/network/mcpe/convert/BlockStateLookupCache.php +++ /dev/null @@ -1,78 +0,0 @@ -|int> - */ - private array $nameToNetworkIdsLookup = []; - - /** - * @param BlockStateDictionaryEntry[] $blockStates - * @phpstan-param list $blockStates - */ - public function __construct(array $blockStates){ - $table = []; - foreach($blockStates as $stateId => $stateNbt){ - $table[$stateNbt->getStateName()][$stateNbt->getRawStateProperties()] = $stateId; - } - - //setup fast path for stateless blocks - foreach(Utils::stringifyKeys($table) as $name => $stateIds){ - if(count($stateIds) === 1){ - $this->nameToNetworkIdsLookup[$name] = $stateIds[array_key_first($stateIds)]; - }else{ - $this->nameToNetworkIdsLookup[$name] = $stateIds; - } - } - } - - /** - * Searches for the appropriate state ID which matches the given blockstate NBT. - * Returns null if there were no matches. - */ - public function lookupStateId(BlockStateData $data) : ?int{ - $name = $data->getName(); - - $lookup = $this->nameToNetworkIdsLookup[$name] ?? null; - return match(true){ - $lookup === null => null, - is_int($lookup) => $lookup, - is_array($lookup) => $lookup[BlockStateDictionaryEntry::encodeStateProperties($data->getStates())] ?? null - }; - } -}