22declare(strict_types=1);
24namespace pocketmine\data\bedrock\item\upgrade;
42 private const TAG_LEGACY_ID =
"id";
44 public function __construct(
60 if(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !== null){
62 $blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
68 $blockStateData =
null;
71 [$newNameId, $newMeta] = $this->idMetaUpgrader->upgrade($rawNameId, $meta);
76 new SavedItemData($newNameId, $newMeta, $blockStateData, $nbt),
93 $rawNameId = $this->legacyIntToStringIdMap->legacyToString($legacyNumericId);
94 if($rawNameId ===
null){
97 return $this->upgradeItemTypeDataString($rawNameId, $meta, $count, $nbt);
107 $rawNameId = $nameIdTag->getValue();
108 }elseif(($idTag = $tag->
getTag(self::TAG_LEGACY_ID)) instanceof ShortTag){
111 if($idTag->getValue() === 0){
116 $rawNameId = $this->legacyIntToStringIdMap->legacyToString($idTag->getValue());
117 if($rawNameId ===
null){
120 }elseif($idTag instanceof StringTag){
123 $rawNameId = $idTag->getValue();
125 throw new SavedDataLoadingException(
"Item stack data should have either a name ID or a legacy ID");
128 $meta = $tag->getShort(SavedItemData::TAG_DAMAGE, 0);
131 if($blockStateNbt !==
null){
133 $blockStateData = $this->blockDataUpgrader->upgradeBlockStateNbt($blockStateNbt);
134 }
catch(BlockStateDeserializeException $e){
135 throw new SavedDataLoadingException(
"Failed to deserialize blockstate for blockitem: " . $e->getMessage(), 0, $e);
137 }elseif(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !==
null){
140 $blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
141 }
catch(BlockStateDeserializeException $e){
142 throw new SavedDataLoadingException(
"Failed to deserialize blockstate for legacy blockitem: " . $e->getMessage(), 0, $e);
146 $blockStateData =
null;
149 [$newNameId, $newMeta] = $this->idMetaUpgrader->upgrade($rawNameId, $meta);
154 return new SavedItemData($newNameId, $newMeta, $blockStateData, $tag->
getCompoundTag(SavedItemData::TAG_TAG));
161 private static function deserializeListOfStrings(?ListTag $list,
string $tagName) : array{
165 if($list->getTagType() !== NBT::TAG_String){
166 throw new SavedDataLoadingException(
"Unexpected type of list for tag '$tagName', expected TAG_String");
169 foreach($list as $item){
170 assert($item instanceof StringTag);
171 $result[] = $item->getValue();
181 $savedItemData = $this->upgradeItemTypeNbt($tag);
182 if($savedItemData ===
null){
188 $count = Binary::unsignByte($tag->getByte(SavedItemStackData::TAG_COUNT));
191 $slot = ($slotTag = $tag->
getTag(SavedItemStackData::TAG_SLOT)) instanceof
ByteTag ? Binary::unsignByte($slotTag->getValue()) :
null;
192 $wasPickedUp = ($wasPickedUpTag = $tag->
getTag(SavedItemStackData::TAG_WAS_PICKED_UP)) instanceof
ByteTag ? $wasPickedUpTag->
getValue() :
null;
193 $canPlaceOnList = $tag->
getListTag(SavedItemStackData::TAG_CAN_PLACE_ON);
194 $canDestroyList = $tag->
getListTag(SavedItemStackData::TAG_CAN_DESTROY);
199 return new SavedItemStackData(
204 self::deserializeListOfStrings($canPlaceOnList, SavedItemStackData::TAG_CAN_PLACE_ON),
205 self::deserializeListOfStrings($canDestroyList, SavedItemStackData::TAG_CAN_DESTROY)
209 public function getIdMetaUpgrader() : ItemIdMetaUpgrader{ return $this->idMetaUpgrader; }
upgradeItemStackNbt(CompoundTag $tag)
upgradeItemTypeDataString(string $rawNameId, int $meta, int $count, ?CompoundTag $nbt)
upgradeItemTypeDataInt(int $legacyNumericId, int $meta, int $count, ?CompoundTag $nbt)
getCompoundTag(string $name)