44 private const TAG_LEGACY_ID =
"id";
46 public function __construct(
64 if(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !== null){
66 $blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
72 $blockStateData =
null;
75 [$newNameId, $newMeta] = $this->idMetaUpgrader->upgrade($rawNameId, $meta);
80 new SavedItemData($newNameId, $newMeta, $blockStateData, $nbt),
97 $rawNameId = $this->legacyIntToStringIdMap->legacyToString($legacyNumericId);
98 if($rawNameId ===
null){
101 return $this->upgradeItemTypeDataString($rawNameId, $meta, $count, $nbt);
111 $rawNameId = $nameIdTag->getValue();
112 }elseif(($idTag = $tag->
getTag(self::TAG_LEGACY_ID)) instanceof ShortTag){
115 if($idTag->getValue() === 0){
120 $rawNameId = $this->legacyIntToStringIdMap->legacyToString($idTag->getValue());
121 if($rawNameId ===
null){
124 }elseif($idTag instanceof StringTag){
127 $rawNameId = $idTag->getValue();
129 throw new SavedDataLoadingException(
"Item stack data should have either a name ID or a legacy ID");
132 $meta = $tag->getShort(SavedItemData::TAG_DAMAGE, 0);
135 if($blockStateNbt !==
null){
137 $blockStateData = $this->blockDataUpgrader->upgradeBlockStateNbt($blockStateNbt);
138 }
catch(BlockStateDeserializeException $e){
139 throw new SavedDataLoadingException(
"Failed to deserialize blockstate for blockitem: " . $e->getMessage(), 0, $e);
141 }elseif(($r12BlockId = $this->r12ItemIdToBlockIdMap->itemIdToBlockId($rawNameId)) !==
null){
144 $blockStateData = $this->blockDataUpgrader->upgradeStringIdMeta($r12BlockId, $meta);
145 }
catch(BlockStateDeserializeException $e){
146 throw new SavedDataLoadingException(
"Failed to deserialize blockstate for legacy blockitem: " . $e->getMessage(), 0, $e);
150 $blockStateData =
null;
153 [$newNameId, $newMeta] = $this->idMetaUpgrader->upgrade($rawNameId, $meta);
156 if($blockStateData ===
null && ($blockId = $this->blockItemIdMap->lookupBlockId($newNameId)) !==
null){
157 $networkRuntimeId = $this->blockStateDictionary->lookupStateIdFromIdMeta($blockId, 0);
159 if($networkRuntimeId ===
null){
160 throw new SavedDataLoadingException(
"Failed to find blockstate for blockitem $newNameId");
163 $blockStateData = $this->blockStateDictionary->generateDataFromStateId($networkRuntimeId);
169 return new SavedItemData($newNameId, $newMeta, $blockStateData, $tag->
getCompoundTag(SavedItemData::TAG_TAG));
176 private static function deserializeListOfStrings(?ListTag $list,
string $tagName) : array{
180 if($list->getTagType() !== NBT::TAG_String){
181 throw new SavedDataLoadingException(
"Unexpected type of list for tag '$tagName', expected TAG_String");
184 foreach($list as $item){
185 assert($item instanceof StringTag);
186 $result[] = $item->getValue();
196 $savedItemData = $this->upgradeItemTypeNbt($tag);
197 if($savedItemData ===
null){
203 $count = Binary::unsignByte($tag->getByte(SavedItemStackData::TAG_COUNT));
206 $slot = ($slotTag = $tag->
getTag(SavedItemStackData::TAG_SLOT)) instanceof
ByteTag ? Binary::unsignByte($slotTag->getValue()) : null;
207 $wasPickedUp = ($wasPickedUpTag = $tag->
getTag(SavedItemStackData::TAG_WAS_PICKED_UP)) instanceof
ByteTag ? $wasPickedUpTag->
getValue() :
null;
208 $canPlaceOnList = $tag->
getListTag(SavedItemStackData::TAG_CAN_PLACE_ON);
209 $canDestroyList = $tag->
getListTag(SavedItemStackData::TAG_CAN_DESTROY);
214 return new SavedItemStackData(
219 self::deserializeListOfStrings($canPlaceOnList, SavedItemStackData::TAG_CAN_PLACE_ON),
220 self::deserializeListOfStrings($canDestroyList, SavedItemStackData::TAG_CAN_DESTROY)
224 public function getIdMetaUpgrader() : ItemIdMetaUpgrader{ return $this->idMetaUpgrader; }