Item: added getStateId(), removed state data from public API

state data was only used for indexing stuff along with state ID anyway, so it makes more sense to lock it away in here instead.
This commit is contained in:
Dylan K. Taylor 2023-05-16 14:14:18 +01:00
parent 015c668885
commit c9bb4335a1
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
6 changed files with 18 additions and 18 deletions

View File

@ -29,7 +29,6 @@ use pocketmine\nbt\TreeRoot;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\DestructorCallbackTrait;
use pocketmine\utils\ObjectSet;
use function morton2d_encode;
use function usort;
class CraftingManager{
@ -103,7 +102,7 @@ class CraftingManager{
*/
public static function sort(Item $i1, Item $i2) : int{
//Use spaceship operator to compare each property, then try the next one if they are equivalent.
($retval = $i1->getTypeId() <=> $i2->getTypeId()) === 0 && ($retval = $i1->computeStateData() <=> $i2->computeStateData()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
($retval = $i1->getStateId() <=> $i2->getStateId()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount()) === 0;
return $retval;
}
@ -142,7 +141,7 @@ class CraftingManager{
foreach($outputs as $o){
//count is not written because the outputs might be from multiple repetitions of a single recipe
//this reduces the accuracy of the hash, but it won't matter in most cases.
$result->putVarInt(morton2d_encode($o->getTypeId(), $o->computeStateData()));
$result->putVarInt($o->getStateId());
$result->put((new LittleEndianNbtSerializer())->write(new TreeRoot($o->getNamedTag())));
}
@ -283,8 +282,8 @@ class CraftingManager{
}
public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
$inputHash = morton2d_encode($input->getTypeId(), $input->computeStateData());
$ingredientHash = morton2d_encode($ingredient->getTypeId(), $ingredient->computeStateData());
$inputHash = $input->getStateId();
$ingredientHash = $ingredient->getStateId();
$cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ?? null;
if($cached !== null){
return $cached;

View File

@ -25,7 +25,6 @@ namespace pocketmine\crafting;
use pocketmine\item\Item;
use pocketmine\utils\ObjectSet;
use function morton2d_encode;
final class FurnaceRecipeManager{
/** @var FurnaceRecipe[] */
@ -66,7 +65,7 @@ final class FurnaceRecipeManager{
}
public function match(Item $input) : ?FurnaceRecipe{
$index = morton2d_encode($input->getTypeId(), $input->computeStateData());
$index = $input->getStateId();
$simpleRecipe = $this->lookupCache[$index] ?? null;
if($simpleRecipe !== null){
return $simpleRecipe;

View File

@ -55,6 +55,7 @@ use function count;
use function gettype;
use function hex2bin;
use function is_string;
use function morton2d_encode;
class Item implements \JsonSerializable{
use ItemEnchantmentHandlingTrait;
@ -469,7 +470,11 @@ class Item implements \JsonSerializable{
return $this->identifier->getTypeId();
}
final public function computeStateData() : int{
final public function getStateId() : int{
return morton2d_encode($this->identifier->getTypeId(), $this->computeStateData());
}
private function computeStateData() : int{
$writer = new RuntimeDataWriter(16); //TODO: max bits should be a constant instead of being hardcoded all over the place
$this->describeState($writer);
return $writer->getValue();
@ -630,8 +635,7 @@ class Item implements \JsonSerializable{
* @param bool $checkCompound Whether to verify that the items' NBT match.
*/
final public function equals(Item $item, bool $checkDamage = true, bool $checkCompound = true) : bool{
return $this->getTypeId() === $item->getTypeId() &&
$this->computeStateData() === $item->computeStateData() &&
return $this->getStateId() === $item->getStateId() &&
(!$checkCompound || $this->getNamedTag()->equals($item->getNamedTag()));
}

View File

@ -1540,7 +1540,7 @@ final class StringToItemParser extends StringToTParser{
public function register(string $alias, \Closure $callback) : void{
parent::register($alias, $callback);
$item = $callback($alias);
$this->reverseMap[$item->getTypeId()][$item->computeStateData()][$alias] = true;
$this->reverseMap[$item->getStateId()][$alias] = true;
}
/** @phpstan-param \Closure(string $input) : Block $callback */
@ -1559,7 +1559,7 @@ final class StringToItemParser extends StringToTParser{
* @phpstan-return list<string>
*/
public function lookupAliases(Item $item) : array{
$aliases = $this->reverseMap[$item->getTypeId()][$item->computeStateData()] ?? [];
$aliases = $this->reverseMap[$item->getStateId()] ?? [];
return array_keys($aliases);
}

View File

@ -48,7 +48,6 @@ use pocketmine\utils\SingletonTrait;
use pocketmine\world\format\io\GlobalBlockStateHandlers;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use function get_class;
use function morton2d_encode;
class TypeConverter{
use SingletonTrait;
@ -222,7 +221,7 @@ class TypeConverter{
if($nbt === null){
$nbt = new CompoundTag();
}
$nbt->setLong(self::PM_ID_TAG, morton2d_encode($itemStack->getTypeId(), $itemStack->computeStateData()));
$nbt->setLong(self::PM_ID_TAG, $itemStack->getStateId());
}else{
[$id, $meta, $blockRuntimeId] = $idMeta;
}

View File

@ -146,7 +146,6 @@ use function max;
use function mb_strlen;
use function microtime;
use function min;
use function morton2d_encode;
use function preg_match;
use function spl_object_id;
use function sqrt;
@ -687,7 +686,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
*/
public function getItemCooldownExpiry(Item $item) : int{
$this->checkItemCooldowns();
return $this->usedItemsCooldown[morton2d_encode($item->getTypeId(), $item->computeStateData())] ?? 0;
return $this->usedItemsCooldown[$item->getStateId()] ?? 0;
}
/**
@ -695,7 +694,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
*/
public function hasItemCooldown(Item $item) : bool{
$this->checkItemCooldowns();
return isset($this->usedItemsCooldown[morton2d_encode($item->getTypeId(), $item->computeStateData())]);
return isset($this->usedItemsCooldown[$item->getStateId()]);
}
/**
@ -704,7 +703,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
public function resetItemCooldown(Item $item, ?int $ticks = null) : void{
$ticks = $ticks ?? $item->getCooldownTicks();
if($ticks > 0){
$this->usedItemsCooldown[morton2d_encode($item->getTypeId(), $item->computeStateData())] = $this->server->getTick() + $ticks;
$this->usedItemsCooldown[$item->getStateId()] = $this->server->getTick() + $ticks;
}
}