From bbcc0c08b5a17f56d1e363c12d6ab6d6883e9d7c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 19 Sep 2025 23:40:04 +0100 Subject: [PATCH] Updated NBT library to get new ListTag handling features --- composer.json | 2 +- composer.lock | 17 +++++----- src/block/tile/Banner.php | 3 +- src/block/tile/ChiseledBookshelf.php | 10 ++++-- src/block/tile/ContainerTrait.php | 10 ++++-- .../bedrock/item/upgrade/ItemDataUpgrader.php | 32 +++---------------- src/entity/EntityDataHelper.php | 13 +++----- src/entity/Human.php | 6 ++-- src/entity/Living.php | 3 +- src/entity/projectile/Projectile.php | 6 ++-- src/item/Banner.php | 6 ++-- src/item/Item.php | 20 +++++------- src/item/WritableBookBase.php | 11 +++---- src/network/mcpe/convert/TypeConverter.php | 14 ++++---- .../io/region/LegacyAnvilChunkTrait.php | 8 ++--- .../format/io/region/RegionWorldProvider.php | 17 ++-------- tools/generate-bedrock-data-from-packets.php | 9 ++---- 17 files changed, 72 insertions(+), 115 deletions(-) diff --git a/composer.json b/composer.json index d77203389..0f2ffe95f 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "pocketmine/locale-data": "~2.25.0", "pocketmine/log": "^0.4.0", "pocketmine/math": "~1.0.0", - "pocketmine/nbt": "~1.1.0", + "pocketmine/nbt": "~1.2.0", "pocketmine/raklib": "~1.2.0", "pocketmine/raklib-ipc": "~1.0.0", "pocketmine/snooze": "^0.5.0", diff --git a/composer.lock b/composer.lock index fec0d3a29..ece3c5070 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9a4fa406f66a2f57be87c9ac955191b2", + "content-hash": "1e7545f6cc226b31d54238602143ba78", "packages": [ { "name": "adhocore/json-comment", @@ -576,16 +576,16 @@ }, { "name": "pocketmine/nbt", - "version": "1.1.1", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/pmmp/NBT.git", - "reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1" + "reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pmmp/NBT/zipball/c3c7b0a7295daeaf7873d90fed5c5d10381d12e1", - "reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1", + "url": "https://api.github.com/repos/pmmp/NBT/zipball/51b8d6a97065fb93e0b4f660b65164b6e1ed2fff", + "reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff", "shasum": "" }, "require": { @@ -595,7 +595,8 @@ }, "require-dev": { "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "2.1.0", + "phpstan/phpstan": "2.1.27", + "phpstan/phpstan-phpunit": "^2.0", "phpstan/phpstan-strict-rules": "^2.0", "phpunit/phpunit": "^9.5" }, @@ -612,9 +613,9 @@ "description": "PHP library for working with Named Binary Tags", "support": { "issues": "https://github.com/pmmp/NBT/issues", - "source": "https://github.com/pmmp/NBT/tree/1.1.1" + "source": "https://github.com/pmmp/NBT/tree/1.2.0" }, - "time": "2025-03-09T01:46:03+00:00" + "time": "2025-09-19T18:09:30+00:00" }, { "name": "pocketmine/raklib", diff --git a/src/block/tile/Banner.php b/src/block/tile/Banner.php index b6a143fe7..ac19d9835 100644 --- a/src/block/tile/Banner.php +++ b/src/block/tile/Banner.php @@ -69,9 +69,8 @@ class Banner extends Spawnable{ $patternTypeIdMap = BannerPatternTypeIdMap::getInstance(); - $patterns = $nbt->getListTag(self::TAG_PATTERNS); + $patterns = $nbt->getListTag(self::TAG_PATTERNS, CompoundTag::class); if($patterns !== null){ - /** @var CompoundTag $pattern */ foreach($patterns as $pattern){ $patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error $patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME)); diff --git a/src/block/tile/ChiseledBookshelf.php b/src/block/tile/ChiseledBookshelf.php index 06175e27f..90bf8f29b 100644 --- a/src/block/tile/ChiseledBookshelf.php +++ b/src/block/tile/ChiseledBookshelf.php @@ -34,6 +34,7 @@ use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; +use pocketmine\nbt\UnexpectedTagTypeException; use pocketmine\world\World; use function count; @@ -86,13 +87,18 @@ class ChiseledBookshelf extends Tile implements Container{ } protected function loadItems(CompoundTag $tag) : void{ - if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){ + try{ + $inventoryTag = $tag->getListTag(Container::TAG_ITEMS, CompoundTag::class); + }catch(UnexpectedTagTypeException){ + //preserve the old behaviour of not throwing on wrong types + $inventoryTag = null; + } + if($inventoryTag !== null){ $inventory = $this->getRealInventory(); $listeners = $inventory->getListeners()->toArray(); $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization $newContents = []; - /** @var CompoundTag $itemNBT */ foreach($inventoryTag as $slot => $itemNBT){ try{ $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT); diff --git a/src/block/tile/ContainerTrait.php b/src/block/tile/ContainerTrait.php index fdd050a41..6b9158d7a 100644 --- a/src/block/tile/ContainerTrait.php +++ b/src/block/tile/ContainerTrait.php @@ -31,6 +31,7 @@ use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; +use pocketmine\nbt\UnexpectedTagTypeException; use pocketmine\world\Position; /** @@ -43,13 +44,18 @@ trait ContainerTrait{ abstract public function getRealInventory() : Inventory; protected function loadItems(CompoundTag $tag) : void{ - if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){ + try{ + $inventoryTag = $tag->getListTag(Container::TAG_ITEMS, CompoundTag::class); + }catch(UnexpectedTagTypeException){ + //preserve the old behaviour of not throwing on wrong types + $inventoryTag = null; + } + if($inventoryTag !== null){ $inventory = $this->getRealInventory(); $listeners = $inventory->getListeners()->toArray(); $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization $newContents = []; - /** @var CompoundTag $itemNBT */ foreach($inventoryTag as $itemNBT){ try{ $newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT); diff --git a/src/data/bedrock/item/upgrade/ItemDataUpgrader.php b/src/data/bedrock/item/upgrade/ItemDataUpgrader.php index 7b34ffcb6..98af7ffb0 100644 --- a/src/data/bedrock/item/upgrade/ItemDataUpgrader.php +++ b/src/data/bedrock/item/upgrade/ItemDataUpgrader.php @@ -29,16 +29,14 @@ use pocketmine\data\bedrock\item\BlockItemIdMap; use pocketmine\data\bedrock\item\SavedItemData; use pocketmine\data\bedrock\item\SavedItemStackData; use pocketmine\data\SavedDataLoadingException; -use pocketmine\nbt\NBT; use pocketmine\nbt\NbtException; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\StringTag; use pocketmine\network\mcpe\convert\BlockStateDictionary; use pocketmine\utils\Binary; -use function assert; +use function array_map; final class ItemDataUpgrader{ private const TAG_LEGACY_ID = "id"; //TAG_Short (or TAG_String for Java itemstacks) @@ -169,26 +167,6 @@ final class ItemDataUpgrader{ return new SavedItemData($newNameId, $newMeta, $blockStateData, $tag->getCompoundTag(SavedItemData::TAG_TAG)); } - /** - * @return string[] - * @throws SavedDataLoadingException - */ - private static function deserializeListOfStrings(?ListTag $list, string $tagName) : array{ - if($list === null){ - return []; - } - if($list->getTagType() !== NBT::TAG_String){ - throw new SavedDataLoadingException("Unexpected type of list for tag '$tagName', expected TAG_String"); - } - $result = []; - foreach($list as $item){ - assert($item instanceof StringTag); - $result[] = $item->getValue(); - } - - return $result; - } - /** * @throws SavedDataLoadingException */ @@ -205,8 +183,8 @@ final class ItemDataUpgrader{ //optional $slot = ($slotTag = $tag->getTag(SavedItemStackData::TAG_SLOT)) instanceof ByteTag ? Binary::unsignByte($slotTag->getValue()) : null; $wasPickedUp = ($wasPickedUpTag = $tag->getTag(SavedItemStackData::TAG_WAS_PICKED_UP)) instanceof ByteTag ? $wasPickedUpTag->getValue() : null; - $canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON); - $canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY); + $canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON, StringTag::class); + $canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY, StringTag::class); }catch(NbtException $e){ throw new SavedDataLoadingException($e->getMessage(), 0, $e); } @@ -216,8 +194,8 @@ final class ItemDataUpgrader{ $count, $slot, $wasPickedUp !== 0, - self::deserializeListOfStrings($canPlaceOnList, SavedItemStackData::TAG_CAN_PLACE_ON), - self::deserializeListOfStrings($canDestroyList, SavedItemStackData::TAG_CAN_DESTROY) + $canPlaceOnList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canPlaceOnList->getValue()), + $canDestroyList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canDestroyList->getValue()) ); } diff --git a/src/entity/EntityDataHelper.php b/src/entity/EntityDataHelper.php index 60e45e535..4cce47127 100644 --- a/src/entity/EntityDataHelper.php +++ b/src/entity/EntityDataHelper.php @@ -25,7 +25,6 @@ namespace pocketmine\entity; use pocketmine\data\SavedDataLoadingException; use pocketmine\math\Vector3; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\DoubleTag; use pocketmine\nbt\tag\FloatTag; @@ -59,11 +58,10 @@ final class EntityDataHelper{ public static function parseLocation(CompoundTag $nbt, World $world) : Location{ $pos = self::parseVec3($nbt, Entity::TAG_POS, false); - $yawPitch = $nbt->getTag(Entity::TAG_ROTATION); - if(!($yawPitch instanceof ListTag) || $yawPitch->getTagType() !== NBT::TAG_Float){ + $generic = $nbt->getTag(Entity::TAG_ROTATION); + if(!($generic instanceof ListTag) || ($yawPitch = $generic->cast(FloatTag::class)) === null){ throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List"); } - /** @var FloatTag[] $values */ $values = $yawPitch->getValue(); if(count($values) !== 2){ throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'"); @@ -78,14 +76,13 @@ final class EntityDataHelper{ * @throws SavedDataLoadingException */ public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{ - $pos = $nbt->getTag($tagName); - if($pos === null && $optional){ + $generic = $nbt->getTag($tagName); + if($generic === null && $optional){ return Vector3::zero(); } - if(!($pos instanceof ListTag) || ($pos->getTagType() !== NBT::TAG_Double && $pos->getTagType() !== NBT::TAG_Float)){ + if(!($generic instanceof ListTag) || ($pos = $generic->cast(DoubleTag::class) ?? $generic->cast(FloatTag::class)) === null){ throw new SavedDataLoadingException("'$tagName' should be a List or List"); } - /** @var DoubleTag[]|FloatTag[] $values */ $values = $pos->getValue(); if(count($values) !== 3){ throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag"); diff --git a/src/entity/Human.php b/src/entity/Human.php index d2637a3f9..97ebdefca 100644 --- a/src/entity/Human.php +++ b/src/entity/Human.php @@ -299,12 +299,11 @@ class Human extends Living implements ProjectileSource, InventoryHolder{ $this->enderInventory = new PlayerEnderInventory($this); $this->initHumanData($nbt); - $inventoryTag = $nbt->getListTag(self::TAG_INVENTORY); + $inventoryTag = $nbt->getListTag(self::TAG_INVENTORY, CompoundTag::class); if($inventoryTag !== null){ $inventoryItems = []; $armorInventoryItems = []; - /** @var CompoundTag $item */ foreach($inventoryTag as $i => $item){ $slot = $item->getByte(SavedItemStackData::TAG_SLOT); if($slot >= 0 && $slot < 9){ //Hotbar @@ -328,11 +327,10 @@ class Human extends Living implements ProjectileSource, InventoryHolder{ fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobOffHandItemChange($recipients, $this) ))); - $enderChestInventoryTag = $nbt->getListTag(self::TAG_ENDER_CHEST_INVENTORY); + $enderChestInventoryTag = $nbt->getListTag(self::TAG_ENDER_CHEST_INVENTORY, CompoundTag::class); if($enderChestInventoryTag !== null){ $enderChestInventoryItems = []; - /** @var CompoundTag $item */ foreach($enderChestInventoryTag as $i => $item){ $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item); } diff --git a/src/entity/Living.php b/src/entity/Living.php index 5ae1fd4e6..e24e07b00 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -181,8 +181,7 @@ abstract class Living extends Entity{ $this->setAirSupplyTicks($nbt->getShort(self::TAG_BREATH_TICKS, self::DEFAULT_BREATH_TICKS)); - /** @var CompoundTag[]|ListTag|null $activeEffectsTag */ - $activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS); + $activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS, CompoundTag::class); if($activeEffectsTag !== null){ foreach($activeEffectsTag as $e){ $effect = EffectIdMap::getInstance()->fromId($e->getByte(self::TAG_EFFECT_ID)); diff --git a/src/entity/projectile/Projectile.php b/src/entity/projectile/Projectile.php index f8c8f45a5..09529b6b3 100644 --- a/src/entity/projectile/Projectile.php +++ b/src/entity/projectile/Projectile.php @@ -39,7 +39,6 @@ use pocketmine\event\entity\ProjectileHitEvent; use pocketmine\math\RayTraceResult; use pocketmine\math\Vector3; use pocketmine\math\VoxelRayTrace; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\ListTag; @@ -81,12 +80,11 @@ abstract class Projectile extends Entity{ $this->setHealth(1); $this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage); - if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS)) !== null){ - if($stuckOnBlockPosTag->getTagType() !== NBT::TAG_Int || count($stuckOnBlockPosTag) !== 3){ + if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS, IntTag::class)) !== null){ + if(count($stuckOnBlockPosTag) !== 3){ throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int"); } - /** @var IntTag[] $values */ $values = $stuckOnBlockPosTag->getValue(); $this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue()); diff --git a/src/item/Banner.php b/src/item/Banner.php index 2fc53f5ae..f955a8a12 100644 --- a/src/item/Banner.php +++ b/src/item/Banner.php @@ -29,7 +29,6 @@ use pocketmine\block\utils\DyeColor; use pocketmine\data\bedrock\BannerPatternTypeIdMap; use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\runtime\RuntimeDataDescriber; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use function count; @@ -92,9 +91,8 @@ class Banner extends ItemBlockWallOrFloor{ $colorIdMap = DyeColorIdMap::getInstance(); $patternIdMap = BannerPatternTypeIdMap::getInstance(); - $patterns = $tag->getListTag(self::TAG_PATTERNS); - if($patterns !== null && $patterns->getTagType() === NBT::TAG_Compound){ - /** @var CompoundTag $t */ + $patterns = $tag->getListTag(self::TAG_PATTERNS, CompoundTag::class); + if($patterns !== null){ foreach($patterns as $t){ $patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error $patternType = $patternIdMap->fromId($t->getString(self::TAG_PATTERN_NAME)); diff --git a/src/item/Item.php b/src/item/Item.php index af7cab433..e7c86e167 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -293,9 +293,8 @@ class Item implements \JsonSerializable{ $display = $tag->getCompoundTag(self::TAG_DISPLAY); if($display !== null){ $this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName); - $lore = $display->getListTag(self::TAG_DISPLAY_LORE); - if($lore !== null && $lore->getTagType() === NBT::TAG_String){ - /** @var StringTag $t */ + $lore = $display->getListTag(self::TAG_DISPLAY_LORE, StringTag::class); + if($lore !== null){ foreach($lore as $t){ $this->lore[] = $t->getValue(); } @@ -303,9 +302,8 @@ class Item implements \JsonSerializable{ } $this->removeEnchantments(); - $enchantments = $tag->getListTag(self::TAG_ENCH); - if($enchantments !== null && $enchantments->getTagType() === NBT::TAG_Compound){ - /** @var CompoundTag $enchantment */ + $enchantments = $tag->getListTag(self::TAG_ENCH, CompoundTag::class); + if($enchantments !== null){ foreach($enchantments as $enchantment){ $magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1); $level = $enchantment->getShort(self::TAG_ENCH_LVL, 0); @@ -322,17 +320,15 @@ class Item implements \JsonSerializable{ $this->blockEntityTag = $tag->getCompoundTag(self::TAG_BLOCK_ENTITY_TAG); $this->canPlaceOn = []; - $canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON); - if($canPlaceOn !== null && $canPlaceOn->getTagType() === NBT::TAG_String){ - /** @var StringTag $entry */ + $canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON, StringTag::class); + if($canPlaceOn !== null){ foreach($canPlaceOn as $entry){ $this->canPlaceOn[$entry->getValue()] = $entry->getValue(); } } $this->canDestroy = []; - $canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY); - if($canDestroy !== null && $canDestroy->getTagType() === NBT::TAG_String){ - /** @var StringTag $entry */ + $canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY, StringTag::class); + if($canDestroy !== null){ foreach($canDestroy as $entry){ $this->canDestroy[$entry->getValue()] = $entry->getValue(); } diff --git a/src/item/WritableBookBase.php b/src/item/WritableBookBase.php index d3b9b7061..de6d5414c 100644 --- a/src/item/WritableBookBase.php +++ b/src/item/WritableBookBase.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\item; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; @@ -167,14 +166,12 @@ abstract class WritableBookBase extends Item{ $pages = $tag->getListTag(self::TAG_PAGES); if($pages !== null){ - if($pages->getTagType() === NBT::TAG_Compound){ //PE format - /** @var CompoundTag $page */ - foreach($pages as $page){ + if(($compoundPages = $pages->cast(CompoundTag::class)) !== null){ //PE format + foreach($compoundPages as $page){ $this->pages[] = new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT), 'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME, "")); } - }elseif($pages->getTagType() === NBT::TAG_String){ //PC format - /** @var StringTag $page */ - foreach($pages as $page){ + }elseif(($stringPages = $pages->cast(StringTag::class)) !== null){ //PC format + foreach($stringPages as $page){ $this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8')); } } diff --git a/src/network/mcpe/convert/TypeConverter.php b/src/network/mcpe/convert/TypeConverter.php index a6520ce12..2a3a4e8f3 100644 --- a/src/network/mcpe/convert/TypeConverter.php +++ b/src/network/mcpe/convert/TypeConverter.php @@ -42,6 +42,7 @@ use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\Tag; use pocketmine\nbt\TreeRoot; +use pocketmine\nbt\UnexpectedTagTypeException; use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary; use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use pocketmine\network\mcpe\protocol\types\GameMode as ProtocolGameMode; @@ -225,15 +226,14 @@ class TypeConverter{ * We don't need to, and should not allow, sending nested inventories across the network. */ protected function stripContainedItemNonVisualNBT(CompoundTag $tag) : bool{ - if( - ($blockEntityInventoryTag = $tag->getTag(Container::TAG_ITEMS)) !== null && - $blockEntityInventoryTag instanceof ListTag && - $blockEntityInventoryTag->getTagType() === NBT::TAG_Compound && - $blockEntityInventoryTag->count() > 0 - ){ + try{ + $blockEntityInventoryTag = $tag->getListTag(Container::TAG_ITEMS, CompoundTag::class); + }catch(UnexpectedTagTypeException){ + return false; + } + if($blockEntityInventoryTag !== null && $blockEntityInventoryTag->count() > 0){ $stripped = new ListTag(); - /** @var CompoundTag $itemTag */ foreach($blockEntityInventoryTag as $itemTag){ try{ $containedItem = Item::nbtDeserialize($itemTag); diff --git a/src/world/format/io/region/LegacyAnvilChunkTrait.php b/src/world/format/io/region/LegacyAnvilChunkTrait.php index 6e2f4c8f8..05c79ea63 100644 --- a/src/world/format/io/region/LegacyAnvilChunkTrait.php +++ b/src/world/format/io/region/LegacyAnvilChunkTrait.php @@ -87,12 +87,10 @@ trait LegacyAnvilChunkTrait{ } $subChunks = []; - $subChunksTag = $chunk->getListTag("Sections") ?? []; + $subChunksTag = $chunk->getListTag("Sections", CompoundTag::class) ?? []; foreach($subChunksTag as $subChunk){ - if($subChunk instanceof CompoundTag){ - $y = $subChunk->getByte("Y"); - $subChunks[$y] = $this->deserializeSubChunk($subChunk, clone $biomes3d, new \PrefixedLogger($logger, "Subchunk y=$y")); - } + $y = $subChunk->getByte("Y"); + $subChunks[$y] = $this->deserializeSubChunk($subChunk, clone $biomes3d, new \PrefixedLogger($logger, "Subchunk y=$y")); } for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; ++$y){ if(!isset($subChunks[$y])){ diff --git a/src/world/format/io/region/RegionWorldProvider.php b/src/world/format/io/region/RegionWorldProvider.php index 8fe7928b8..9ce0a35de 100644 --- a/src/world/format/io/region/RegionWorldProvider.php +++ b/src/world/format/io/region/RegionWorldProvider.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\world\format\io\region; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\ByteArrayTag; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; @@ -164,21 +163,11 @@ abstract class RegionWorldProvider extends BaseWorldProvider{ * @throws CorruptedChunkException */ protected static function getCompoundList(string $context, ListTag $list) : array{ - if($list->count() === 0){ //empty lists might have wrong types, we don't care - return []; - } - if($list->getTagType() !== NBT::TAG_Compound){ + $compoundList = $list->cast(CompoundTag::class); + if($compoundList === null){ throw new CorruptedChunkException("Expected TAG_List for '$context'"); } - $result = []; - foreach($list as $tag){ - if(!($tag instanceof CompoundTag)){ - //this should never happen, but it's still possible due to lack of native type safety - throw new CorruptedChunkException("Expected TAG_List for '$context'"); - } - $result[] = $tag; - } - return $result; + return $compoundList->getValue(); } protected static function readFixedSizeByteArray(CompoundTag $chunk, string $tagName, int $length) : string{ diff --git a/tools/generate-bedrock-data-from-packets.php b/tools/generate-bedrock-data-from-packets.php index f40029365..11b73e86d 100644 --- a/tools/generate-bedrock-data-from-packets.php +++ b/tools/generate-bedrock-data-from-packets.php @@ -37,10 +37,10 @@ use pocketmine\data\bedrock\item\BlockItemIdMap; use pocketmine\data\bedrock\item\ItemTypeNames; use pocketmine\inventory\json\CreativeGroupData; use pocketmine\nbt\LittleEndianNbtSerializer; -use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\TreeRoot; +use pocketmine\nbt\UnexpectedTagTypeException; use pocketmine\network\mcpe\convert\BlockStateDictionary; use pocketmine\network\mcpe\convert\BlockTranslator; use pocketmine\network\mcpe\convert\ItemTranslator; @@ -554,8 +554,8 @@ class ParserPacketHandler extends PacketHandler{ if(!($tag instanceof CompoundTag)){ throw new AssumptionFailedError(); } - $idList = $tag->getTag("idlist"); - if(!($idList instanceof ListTag) || $idList->getTagType() !== NBT::TAG_Compound){ + $generic = $tag->getTag("idlist"); + if(!($generic instanceof ListTag) || ($idList = $generic->cast(CompoundTag::class)) === null){ echo $tag . "\n"; throw new \RuntimeException("expected TAG_List(\"idlist\") tag inside root TAG_Compound"); } @@ -565,9 +565,6 @@ class ParserPacketHandler extends PacketHandler{ } echo "updating legacy => string entity ID mapping table\n"; $map = []; - /** - * @var CompoundTag $thing - */ foreach($idList as $thing){ $map[$thing->getString("id")] = $thing->getInt("rid"); }