mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
ItemTranslator now operates directly using Item, rather than using item ID/meta + ItemFactory
in almost all cases where ItemTranslator is used, an Item already exists, so it doesn't make any sense to make ItemTranslator go and create another item instance just to pass to ItemSerializer.
This commit is contained in:
@ -27,7 +27,7 @@ use pocketmine\data\bedrock\item\ItemDeserializer;
|
||||
use pocketmine\data\bedrock\item\ItemSerializer;
|
||||
use pocketmine\data\bedrock\item\ItemTypeSerializeException;
|
||||
use pocketmine\data\bedrock\item\SavedItemData;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary;
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
@ -54,16 +54,24 @@ final class ItemTranslator{
|
||||
* @return int[]|null
|
||||
* @phpstan-return array{int, int, int}|null
|
||||
*/
|
||||
public function toNetworkIdQuiet(int $internalId, int $internalMeta) : ?array{
|
||||
//TODO: we should probably come up with a cache for this
|
||||
|
||||
public function toNetworkIdQuiet(Item $item) : ?array{
|
||||
try{
|
||||
$itemData = $this->itemSerializer->serialize(ItemFactory::getInstance()->get($internalId, $internalMeta));
|
||||
return $this->toNetworkId($item);
|
||||
}catch(ItemTypeSerializeException){
|
||||
//TODO: this will swallow any serializer error; this is not ideal, but it should be OK since unit tests
|
||||
//should cover this
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
* @phpstan-return array{int, int, int}
|
||||
*
|
||||
* @throws ItemTypeSerializeException
|
||||
*/
|
||||
public function toNetworkId(Item $item) : array{
|
||||
//TODO: we should probably come up with a cache for this
|
||||
|
||||
$itemData = $this->itemSerializer->serialize($item);
|
||||
|
||||
$numericId = $this->dictionary->fromStringId($itemData->getName());
|
||||
$blockStateData = $itemData->getBlock();
|
||||
@ -81,40 +89,15 @@ final class ItemTranslator{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
* @phpstan-return array{int, int, int}
|
||||
*/
|
||||
public function toNetworkId(int $internalId, int $internalMeta) : array{
|
||||
return $this->toNetworkIdQuiet($internalId, $internalMeta) ??
|
||||
throw new \InvalidArgumentException("Unmapped ID/metadata combination $internalId:$internalMeta");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
* @phpstan-return array{int, int}
|
||||
* @throws TypeConversionException
|
||||
*/
|
||||
public function fromNetworkId(int $networkId, int $networkMeta, int $networkBlockRuntimeId) : array{
|
||||
public function fromNetworkId(int $networkId, int $networkMeta, int $networkBlockRuntimeId) : Item{
|
||||
$stringId = $this->dictionary->fromIntId($networkId);
|
||||
|
||||
$blockStateData = $networkBlockRuntimeId !== self::NO_BLOCK_RUNTIME_ID ?
|
||||
RuntimeBlockMapping::getInstance()->getBlockStateDictionary()->getDataFromStateId($networkBlockRuntimeId) :
|
||||
null;
|
||||
|
||||
$item = $this->itemDeserializer->deserialize(new SavedItemData($stringId, $networkMeta, $blockStateData));
|
||||
return [$item->getId(), $item->getMeta()];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
* @phpstan-return array{int, int}
|
||||
* @throws TypeConversionException
|
||||
*/
|
||||
public function fromNetworkIdWithWildcardHandling(int $networkId, int $networkMeta) : array{
|
||||
if($networkMeta !== 0x7fff){
|
||||
return $this->fromNetworkId($networkId, $networkMeta, 0);
|
||||
}
|
||||
[$id, ] = $this->fromNetworkId($networkId, 0, 0);
|
||||
return [$id, -1];
|
||||
return $this->itemDeserializer->deserialize(new SavedItemData($stringId, $networkMeta, $blockStateData));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user