BlockStateDictionary: remove useless indirection

This commit is contained in:
Dylan K. Taylor 2023-05-05 14:52:21 +01:00
parent 289ede669d
commit 09e823e304
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 29 additions and 81 deletions

View File

@ -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<string, array<string, int>|int>
*/
private array $stateDataToStateIdLookup = [];
private BlockStateLookupCache $stateDataToStateIdLookupCache;
/**
* @var int[][]|null
* @phpstan-var array<int, array<string, int>>|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
};
}
/**

View File

@ -1,78 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\convert;
use pocketmine\data\bedrock\block\BlockStateData;
use pocketmine\utils\Utils;
use function array_key_first;
use function count;
use function is_array;
use function is_int;
/**
* Facilitates quickly looking up a block's state ID based on its NBT.
*/
final class BlockStateLookupCache{
/**
* @var int[][]|int[]
* @phpstan-var array<string, array<string, int>|int>
*/
private array $nameToNetworkIdsLookup = [];
/**
* @param BlockStateDictionaryEntry[] $blockStates
* @phpstan-param list<BlockStateDictionaryEntry> $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
};
}
}