Updated NBT library to get new ListTag handling features

This commit is contained in:
Dylan K. Taylor
2025-09-19 23:40:04 +01:00
parent 6d2d23a210
commit bbcc0c08b5
17 changed files with 72 additions and 115 deletions

View File

@@ -44,7 +44,7 @@
"pocketmine/locale-data": "~2.25.0", "pocketmine/locale-data": "~2.25.0",
"pocketmine/log": "^0.4.0", "pocketmine/log": "^0.4.0",
"pocketmine/math": "~1.0.0", "pocketmine/math": "~1.0.0",
"pocketmine/nbt": "~1.1.0", "pocketmine/nbt": "~1.2.0",
"pocketmine/raklib": "~1.2.0", "pocketmine/raklib": "~1.2.0",
"pocketmine/raklib-ipc": "~1.0.0", "pocketmine/raklib-ipc": "~1.0.0",
"pocketmine/snooze": "^0.5.0", "pocketmine/snooze": "^0.5.0",

17
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "9a4fa406f66a2f57be87c9ac955191b2", "content-hash": "1e7545f6cc226b31d54238602143ba78",
"packages": [ "packages": [
{ {
"name": "adhocore/json-comment", "name": "adhocore/json-comment",
@@ -576,16 +576,16 @@
}, },
{ {
"name": "pocketmine/nbt", "name": "pocketmine/nbt",
"version": "1.1.1", "version": "1.2.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/NBT.git", "url": "https://github.com/pmmp/NBT.git",
"reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1" "reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/NBT/zipball/c3c7b0a7295daeaf7873d90fed5c5d10381d12e1", "url": "https://api.github.com/repos/pmmp/NBT/zipball/51b8d6a97065fb93e0b4f660b65164b6e1ed2fff",
"reference": "c3c7b0a7295daeaf7873d90fed5c5d10381d12e1", "reference": "51b8d6a97065fb93e0b4f660b65164b6e1ed2fff",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -595,7 +595,8 @@
}, },
"require-dev": { "require-dev": {
"phpstan/extension-installer": "^1.0", "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", "phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.5" "phpunit/phpunit": "^9.5"
}, },
@@ -612,9 +613,9 @@
"description": "PHP library for working with Named Binary Tags", "description": "PHP library for working with Named Binary Tags",
"support": { "support": {
"issues": "https://github.com/pmmp/NBT/issues", "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", "name": "pocketmine/raklib",

View File

@@ -69,9 +69,8 @@ class Banner extends Spawnable{
$patternTypeIdMap = BannerPatternTypeIdMap::getInstance(); $patternTypeIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $nbt->getListTag(self::TAG_PATTERNS); $patterns = $nbt->getListTag(self::TAG_PATTERNS, CompoundTag::class);
if($patterns !== null){ if($patterns !== null){
/** @var CompoundTag $pattern */
foreach($patterns as $pattern){ foreach($patterns as $pattern){
$patternColor = $colorIdMap->fromInvertedId($pattern->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error $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)); $patternType = $patternTypeIdMap->fromId($pattern->getString(self::TAG_PATTERN_NAME));

View File

@@ -34,6 +34,7 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\world\World; use pocketmine\world\World;
use function count; use function count;
@@ -86,13 +87,18 @@ class ChiseledBookshelf extends Tile implements Container{
} }
protected function loadItems(CompoundTag $tag) : void{ 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(); $inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray(); $listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
$newContents = []; $newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $slot => $itemNBT){ foreach($inventoryTag as $slot => $itemNBT){
try{ try{
$count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT); $count = $itemNBT->getByte(SavedItemStackData::TAG_COUNT);

View File

@@ -31,6 +31,7 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\world\Position; use pocketmine\world\Position;
/** /**
@@ -43,13 +44,18 @@ trait ContainerTrait{
abstract public function getRealInventory() : Inventory; abstract public function getRealInventory() : Inventory;
protected function loadItems(CompoundTag $tag) : void{ 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(); $inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray(); $listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
$newContents = []; $newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $itemNBT){ foreach($inventoryTag as $itemNBT){
try{ try{
$newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT); $newContents[$itemNBT->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($itemNBT);

View File

@@ -29,16 +29,14 @@ use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\SavedItemData; use pocketmine\data\bedrock\item\SavedItemData;
use pocketmine\data\bedrock\item\SavedItemStackData; use pocketmine\data\bedrock\item\SavedItemStackData;
use pocketmine\data\SavedDataLoadingException; use pocketmine\data\SavedDataLoadingException;
use pocketmine\nbt\NBT;
use pocketmine\nbt\NbtException; use pocketmine\nbt\NbtException;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\convert\BlockStateDictionary; use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use function assert; use function array_map;
final class ItemDataUpgrader{ final class ItemDataUpgrader{
private const TAG_LEGACY_ID = "id"; //TAG_Short (or TAG_String for Java itemstacks) 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 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 * @throws SavedDataLoadingException
*/ */
@@ -205,8 +183,8 @@ final class ItemDataUpgrader{
//optional //optional
$slot = ($slotTag = $tag->getTag(SavedItemStackData::TAG_SLOT)) instanceof ByteTag ? Binary::unsignByte($slotTag->getValue()) : null; $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; $wasPickedUp = ($wasPickedUpTag = $tag->getTag(SavedItemStackData::TAG_WAS_PICKED_UP)) instanceof ByteTag ? $wasPickedUpTag->getValue() : null;
$canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON); $canPlaceOnList = $tag->getListTag(SavedItemStackData::TAG_CAN_PLACE_ON, StringTag::class);
$canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY); $canDestroyList = $tag->getListTag(SavedItemStackData::TAG_CAN_DESTROY, StringTag::class);
}catch(NbtException $e){ }catch(NbtException $e){
throw new SavedDataLoadingException($e->getMessage(), 0, $e); throw new SavedDataLoadingException($e->getMessage(), 0, $e);
} }
@@ -216,8 +194,8 @@ final class ItemDataUpgrader{
$count, $count,
$slot, $slot,
$wasPickedUp !== 0, $wasPickedUp !== 0,
self::deserializeListOfStrings($canPlaceOnList, SavedItemStackData::TAG_CAN_PLACE_ON), $canPlaceOnList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canPlaceOnList->getValue()),
self::deserializeListOfStrings($canDestroyList, SavedItemStackData::TAG_CAN_DESTROY) $canDestroyList === null ? [] : array_map(fn(StringTag $t) => $t->getValue(), $canDestroyList->getValue())
); );
} }

View File

@@ -25,7 +25,6 @@ namespace pocketmine\entity;
use pocketmine\data\SavedDataLoadingException; use pocketmine\data\SavedDataLoadingException;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag; use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\FloatTag;
@@ -59,11 +58,10 @@ final class EntityDataHelper{
public static function parseLocation(CompoundTag $nbt, World $world) : Location{ public static function parseLocation(CompoundTag $nbt, World $world) : Location{
$pos = self::parseVec3($nbt, Entity::TAG_POS, false); $pos = self::parseVec3($nbt, Entity::TAG_POS, false);
$yawPitch = $nbt->getTag(Entity::TAG_ROTATION); $generic = $nbt->getTag(Entity::TAG_ROTATION);
if(!($yawPitch instanceof ListTag) || $yawPitch->getTagType() !== NBT::TAG_Float){ if(!($generic instanceof ListTag) || ($yawPitch = $generic->cast(FloatTag::class)) === null){
throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List<Float>"); throw new SavedDataLoadingException("'" . Entity::TAG_ROTATION . "' should be a List<Float>");
} }
/** @var FloatTag[] $values */
$values = $yawPitch->getValue(); $values = $yawPitch->getValue();
if(count($values) !== 2){ if(count($values) !== 2){
throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'"); throw new SavedDataLoadingException("Expected exactly 2 entries for 'Rotation'");
@@ -78,14 +76,13 @@ final class EntityDataHelper{
* @throws SavedDataLoadingException * @throws SavedDataLoadingException
*/ */
public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{ public static function parseVec3(CompoundTag $nbt, string $tagName, bool $optional) : Vector3{
$pos = $nbt->getTag($tagName); $generic = $nbt->getTag($tagName);
if($pos === null && $optional){ if($generic === null && $optional){
return Vector3::zero(); 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<Double> or List<Float>"); throw new SavedDataLoadingException("'$tagName' should be a List<Double> or List<Float>");
} }
/** @var DoubleTag[]|FloatTag[] $values */
$values = $pos->getValue(); $values = $pos->getValue();
if(count($values) !== 3){ if(count($values) !== 3){
throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag"); throw new SavedDataLoadingException("Expected exactly 3 entries in '$tagName' tag");

View File

@@ -299,12 +299,11 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
$this->enderInventory = new PlayerEnderInventory($this); $this->enderInventory = new PlayerEnderInventory($this);
$this->initHumanData($nbt); $this->initHumanData($nbt);
$inventoryTag = $nbt->getListTag(self::TAG_INVENTORY); $inventoryTag = $nbt->getListTag(self::TAG_INVENTORY, CompoundTag::class);
if($inventoryTag !== null){ if($inventoryTag !== null){
$inventoryItems = []; $inventoryItems = [];
$armorInventoryItems = []; $armorInventoryItems = [];
/** @var CompoundTag $item */
foreach($inventoryTag as $i => $item){ foreach($inventoryTag as $i => $item){
$slot = $item->getByte(SavedItemStackData::TAG_SLOT); $slot = $item->getByte(SavedItemStackData::TAG_SLOT);
if($slot >= 0 && $slot < 9){ //Hotbar 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) 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){ if($enderChestInventoryTag !== null){
$enderChestInventoryItems = []; $enderChestInventoryItems = [];
/** @var CompoundTag $item */
foreach($enderChestInventoryTag as $i => $item){ foreach($enderChestInventoryTag as $i => $item){
$enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item); $enderChestInventoryItems[$item->getByte(SavedItemStackData::TAG_SLOT)] = Item::nbtDeserialize($item);
} }

View File

@@ -181,8 +181,7 @@ abstract class Living extends Entity{
$this->setAirSupplyTicks($nbt->getShort(self::TAG_BREATH_TICKS, self::DEFAULT_BREATH_TICKS)); $this->setAirSupplyTicks($nbt->getShort(self::TAG_BREATH_TICKS, self::DEFAULT_BREATH_TICKS));
/** @var CompoundTag[]|ListTag|null $activeEffectsTag */ $activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS, CompoundTag::class);
$activeEffectsTag = $nbt->getListTag(self::TAG_ACTIVE_EFFECTS);
if($activeEffectsTag !== null){ if($activeEffectsTag !== null){
foreach($activeEffectsTag as $e){ foreach($activeEffectsTag as $e){
$effect = EffectIdMap::getInstance()->fromId($e->getByte(self::TAG_EFFECT_ID)); $effect = EffectIdMap::getInstance()->fromId($e->getByte(self::TAG_EFFECT_ID));

View File

@@ -39,7 +39,6 @@ use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\math\RayTraceResult; use pocketmine\math\RayTraceResult;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace; use pocketmine\math\VoxelRayTrace;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
@@ -81,12 +80,11 @@ abstract class Projectile extends Entity{
$this->setHealth(1); $this->setHealth(1);
$this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage); $this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage);
if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS)) !== null){ if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS, IntTag::class)) !== null){
if($stuckOnBlockPosTag->getTagType() !== NBT::TAG_Int || count($stuckOnBlockPosTag) !== 3){ if(count($stuckOnBlockPosTag) !== 3){
throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int"); throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int");
} }
/** @var IntTag[] $values */
$values = $stuckOnBlockPosTag->getValue(); $values = $stuckOnBlockPosTag->getValue();
$this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue()); $this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue());

View File

@@ -29,7 +29,6 @@ use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\BannerPatternTypeIdMap; use pocketmine\data\bedrock\BannerPatternTypeIdMap;
use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use function count; use function count;
@@ -92,9 +91,8 @@ class Banner extends ItemBlockWallOrFloor{
$colorIdMap = DyeColorIdMap::getInstance(); $colorIdMap = DyeColorIdMap::getInstance();
$patternIdMap = BannerPatternTypeIdMap::getInstance(); $patternIdMap = BannerPatternTypeIdMap::getInstance();
$patterns = $tag->getListTag(self::TAG_PATTERNS); $patterns = $tag->getListTag(self::TAG_PATTERNS, CompoundTag::class);
if($patterns !== null && $patterns->getTagType() === NBT::TAG_Compound){ if($patterns !== null){
/** @var CompoundTag $t */
foreach($patterns as $t){ foreach($patterns as $t){
$patternColor = $colorIdMap->fromInvertedId($t->getInt(self::TAG_PATTERN_COLOR)) ?? DyeColor::BLACK; //TODO: missing pattern colour should be an error $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)); $patternType = $patternIdMap->fromId($t->getString(self::TAG_PATTERN_NAME));

View File

@@ -293,9 +293,8 @@ class Item implements \JsonSerializable{
$display = $tag->getCompoundTag(self::TAG_DISPLAY); $display = $tag->getCompoundTag(self::TAG_DISPLAY);
if($display !== null){ if($display !== null){
$this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName); $this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName);
$lore = $display->getListTag(self::TAG_DISPLAY_LORE); $lore = $display->getListTag(self::TAG_DISPLAY_LORE, StringTag::class);
if($lore !== null && $lore->getTagType() === NBT::TAG_String){ if($lore !== null){
/** @var StringTag $t */
foreach($lore as $t){ foreach($lore as $t){
$this->lore[] = $t->getValue(); $this->lore[] = $t->getValue();
} }
@@ -303,9 +302,8 @@ class Item implements \JsonSerializable{
} }
$this->removeEnchantments(); $this->removeEnchantments();
$enchantments = $tag->getListTag(self::TAG_ENCH); $enchantments = $tag->getListTag(self::TAG_ENCH, CompoundTag::class);
if($enchantments !== null && $enchantments->getTagType() === NBT::TAG_Compound){ if($enchantments !== null){
/** @var CompoundTag $enchantment */
foreach($enchantments as $enchantment){ foreach($enchantments as $enchantment){
$magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1); $magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1);
$level = $enchantment->getShort(self::TAG_ENCH_LVL, 0); $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->blockEntityTag = $tag->getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
$this->canPlaceOn = []; $this->canPlaceOn = [];
$canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON); $canPlaceOn = $tag->getListTag(self::TAG_CAN_PLACE_ON, StringTag::class);
if($canPlaceOn !== null && $canPlaceOn->getTagType() === NBT::TAG_String){ if($canPlaceOn !== null){
/** @var StringTag $entry */
foreach($canPlaceOn as $entry){ foreach($canPlaceOn as $entry){
$this->canPlaceOn[$entry->getValue()] = $entry->getValue(); $this->canPlaceOn[$entry->getValue()] = $entry->getValue();
} }
} }
$this->canDestroy = []; $this->canDestroy = [];
$canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY); $canDestroy = $tag->getListTag(self::TAG_CAN_DESTROY, StringTag::class);
if($canDestroy !== null && $canDestroy->getTagType() === NBT::TAG_String){ if($canDestroy !== null){
/** @var StringTag $entry */
foreach($canDestroy as $entry){ foreach($canDestroy as $entry){
$this->canDestroy[$entry->getValue()] = $entry->getValue(); $this->canDestroy[$entry->getValue()] = $entry->getValue();
} }

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\item; namespace pocketmine\item;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
@@ -167,14 +166,12 @@ abstract class WritableBookBase extends Item{
$pages = $tag->getListTag(self::TAG_PAGES); $pages = $tag->getListTag(self::TAG_PAGES);
if($pages !== null){ if($pages !== null){
if($pages->getTagType() === NBT::TAG_Compound){ //PE format if(($compoundPages = $pages->cast(CompoundTag::class)) !== null){ //PE format
/** @var CompoundTag $page */ foreach($compoundPages as $page){
foreach($pages as $page){
$this->pages[] = new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT), 'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME, "")); $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 }elseif(($stringPages = $pages->cast(StringTag::class)) !== null){ //PC format
/** @var StringTag $page */ foreach($stringPages as $page){
foreach($pages as $page){
$this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8')); $this->pages[] = new WritableBookPage(mb_scrub($page->getValue(), 'UTF-8'));
} }
} }

View File

@@ -42,6 +42,7 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\Tag; use pocketmine\nbt\tag\Tag;
use pocketmine\nbt\TreeRoot; use pocketmine\nbt\TreeRoot;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary; use pocketmine\network\mcpe\protocol\serializer\ItemTypeDictionary;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\GameMode as ProtocolGameMode; 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. * We don't need to, and should not allow, sending nested inventories across the network.
*/ */
protected function stripContainedItemNonVisualNBT(CompoundTag $tag) : bool{ protected function stripContainedItemNonVisualNBT(CompoundTag $tag) : bool{
if( try{
($blockEntityInventoryTag = $tag->getTag(Container::TAG_ITEMS)) !== null && $blockEntityInventoryTag = $tag->getListTag(Container::TAG_ITEMS, CompoundTag::class);
$blockEntityInventoryTag instanceof ListTag && }catch(UnexpectedTagTypeException){
$blockEntityInventoryTag->getTagType() === NBT::TAG_Compound && return false;
$blockEntityInventoryTag->count() > 0 }
){ if($blockEntityInventoryTag !== null && $blockEntityInventoryTag->count() > 0){
$stripped = new ListTag(); $stripped = new ListTag();
/** @var CompoundTag $itemTag */
foreach($blockEntityInventoryTag as $itemTag){ foreach($blockEntityInventoryTag as $itemTag){
try{ try{
$containedItem = Item::nbtDeserialize($itemTag); $containedItem = Item::nbtDeserialize($itemTag);

View File

@@ -87,12 +87,10 @@ trait LegacyAnvilChunkTrait{
} }
$subChunks = []; $subChunks = [];
$subChunksTag = $chunk->getListTag("Sections") ?? []; $subChunksTag = $chunk->getListTag("Sections", CompoundTag::class) ?? [];
foreach($subChunksTag as $subChunk){ foreach($subChunksTag as $subChunk){
if($subChunk instanceof CompoundTag){ $y = $subChunk->getByte("Y");
$y = $subChunk->getByte("Y"); $subChunks[$y] = $this->deserializeSubChunk($subChunk, clone $biomes3d, new \PrefixedLogger($logger, "Subchunk y=$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){ for($y = Chunk::MIN_SUBCHUNK_INDEX; $y <= Chunk::MAX_SUBCHUNK_INDEX; ++$y){
if(!isset($subChunks[$y])){ if(!isset($subChunks[$y])){

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\world\format\io\region; namespace pocketmine\world\format\io\region;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\ByteArrayTag; use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
@@ -164,21 +163,11 @@ abstract class RegionWorldProvider extends BaseWorldProvider{
* @throws CorruptedChunkException * @throws CorruptedChunkException
*/ */
protected static function getCompoundList(string $context, ListTag $list) : array{ protected static function getCompoundList(string $context, ListTag $list) : array{
if($list->count() === 0){ //empty lists might have wrong types, we don't care $compoundList = $list->cast(CompoundTag::class);
return []; if($compoundList === null){
}
if($list->getTagType() !== NBT::TAG_Compound){
throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'"); throw new CorruptedChunkException("Expected TAG_List<TAG_Compound> for '$context'");
} }
$result = []; return $compoundList->getValue();
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<TAG_Compound> for '$context'");
}
$result[] = $tag;
}
return $result;
} }
protected static function readFixedSizeByteArray(CompoundTag $chunk, string $tagName, int $length) : string{ protected static function readFixedSizeByteArray(CompoundTag $chunk, string $tagName, int $length) : string{

View File

@@ -37,10 +37,10 @@ use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\ItemTypeNames; use pocketmine\data\bedrock\item\ItemTypeNames;
use pocketmine\inventory\json\CreativeGroupData; use pocketmine\inventory\json\CreativeGroupData;
use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\TreeRoot; use pocketmine\nbt\TreeRoot;
use pocketmine\nbt\UnexpectedTagTypeException;
use pocketmine\network\mcpe\convert\BlockStateDictionary; use pocketmine\network\mcpe\convert\BlockStateDictionary;
use pocketmine\network\mcpe\convert\BlockTranslator; use pocketmine\network\mcpe\convert\BlockTranslator;
use pocketmine\network\mcpe\convert\ItemTranslator; use pocketmine\network\mcpe\convert\ItemTranslator;
@@ -554,8 +554,8 @@ class ParserPacketHandler extends PacketHandler{
if(!($tag instanceof CompoundTag)){ if(!($tag instanceof CompoundTag)){
throw new AssumptionFailedError(); throw new AssumptionFailedError();
} }
$idList = $tag->getTag("idlist"); $generic = $tag->getTag("idlist");
if(!($idList instanceof ListTag) || $idList->getTagType() !== NBT::TAG_Compound){ if(!($generic instanceof ListTag) || ($idList = $generic->cast(CompoundTag::class)) === null){
echo $tag . "\n"; echo $tag . "\n";
throw new \RuntimeException("expected TAG_List<TAG_Compound>(\"idlist\") tag inside root TAG_Compound"); throw new \RuntimeException("expected TAG_List<TAG_Compound>(\"idlist\") tag inside root TAG_Compound");
} }
@@ -565,9 +565,6 @@ class ParserPacketHandler extends PacketHandler{
} }
echo "updating legacy => string entity ID mapping table\n"; echo "updating legacy => string entity ID mapping table\n";
$map = []; $map = [];
/**
* @var CompoundTag $thing
*/
foreach($idList as $thing){ foreach($idList as $thing){
$map[$thing->getString("id")] = $thing->getInt("rid"); $map[$thing->getString("id")] = $thing->getInt("rid");
} }