From 8fafef2f7fc8bb8938cc46e30e905132cf945e71 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 8 Oct 2017 13:28:01 +0100 Subject: [PATCH] Added tagType parameter to ListTag constructor, remove some boilerplate code --- src/pocketmine/Server.php | 12 ++++-------- src/pocketmine/block/BurningFurnace.php | 7 +++---- src/pocketmine/block/Chest.php | 6 ++---- src/pocketmine/entity/Human.php | 3 +-- src/pocketmine/item/Item.php | 6 ++---- src/pocketmine/level/format/io/region/Anvil.php | 9 +++------ src/pocketmine/level/format/io/region/McRegion.php | 6 ++---- src/pocketmine/level/format/io/region/PMAnvil.php | 10 ++++------ src/pocketmine/nbt/tag/ListTag.php | 6 ++++-- 9 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 1cf004dc6..b6dcd88c2 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -764,25 +764,25 @@ class Server{ new DoubleTag("", $spawn->x), new DoubleTag("", $spawn->y), new DoubleTag("", $spawn->z) - ]), + ], NBT::TAG_Double), new StringTag("Level", $this->getDefaultLevel()->getName()), //new StringTag("SpawnLevel", $this->getDefaultLevel()->getName()), //new IntTag("SpawnX", (int) $spawn->x), //new IntTag("SpawnY", (int) $spawn->y), //new IntTag("SpawnZ", (int) $spawn->z), //new ByteTag("SpawnForced", 1), //TODO - new ListTag("Inventory", []), + new ListTag("Inventory", [], NBT::TAG_Compound), new CompoundTag("Achievements", []), new IntTag("playerGameType", $this->getGamemode()), new ListTag("Motion", [ new DoubleTag("", 0.0), new DoubleTag("", 0.0), new DoubleTag("", 0.0) - ]), + ], NBT::TAG_Double), new ListTag("Rotation", [ new FloatTag("", 0.0), new FloatTag("", 0.0) - ]), + ], NBT::TAG_Float), new FloatTag("FallDistance", 0.0), new ShortTag("Fire", 0), new ShortTag("Air", 300), @@ -790,10 +790,6 @@ class Server{ new ByteTag("Invulnerable", 0), new StringTag("NameTag", $name) ]); - $nbt->Pos->setTagType(NBT::TAG_Double); - $nbt->Inventory->setTagType(NBT::TAG_Compound); - $nbt->Motion->setTagType(NBT::TAG_Double); - $nbt->Rotation->setTagType(NBT::TAG_Float); return $nbt; diff --git a/src/pocketmine/block/BurningFurnace.php b/src/pocketmine/block/BurningFurnace.php index 806ac3702..e6828df5b 100644 --- a/src/pocketmine/block/BurningFurnace.php +++ b/src/pocketmine/block/BurningFurnace.php @@ -69,13 +69,12 @@ class BurningFurnace extends Solid{ $this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0]; $this->getLevel()->setBlock($blockReplace, $this, true, true); $nbt = new CompoundTag("", [ - new ListTag("Items", []), + new ListTag("Items", [], NBT::TAG_Compound), new StringTag("id", Tile::FURNACE), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z) ]); - $nbt->Items->setTagType(NBT::TAG_Compound); if($item->hasCustomName()){ $nbt->CustomName = new StringTag("CustomName", $item->getCustomName()); @@ -97,13 +96,13 @@ class BurningFurnace extends Solid{ $furnace = $this->getLevel()->getTile($this); if(!($furnace instanceof TileFurnace)){ $nbt = new CompoundTag("", [ - new ListTag("Items", []), + new ListTag("Items", [], NBT::TAG_Compound), new StringTag("id", Tile::FURNACE), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z) ]); - $nbt->Items->setTagType(NBT::TAG_Compound); + $furnace = Tile::createTile("Furnace", $this->getLevel(), $nbt); } diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index eca854726..8a0b7b528 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -96,13 +96,12 @@ class Chest extends Transparent{ $this->getLevel()->setBlock($blockReplace, $this, true, true); $nbt = new CompoundTag("", [ - new ListTag("Items", []), + new ListTag("Items", [], NBT::TAG_Compound), new StringTag("id", Tile::CHEST), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z) ]); - $nbt->Items->setTagType(NBT::TAG_Compound); if($item->hasCustomName()){ $nbt->CustomName = new StringTag("CustomName", $item->getCustomName()); @@ -143,13 +142,12 @@ class Chest extends Transparent{ $chest = $t; }else{ $nbt = new CompoundTag("", [ - new ListTag("Items", []), + new ListTag("Items", [], NBT::TAG_Compound), new StringTag("id", Tile::CHEST), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z) ]); - $nbt->Items->setTagType(NBT::TAG_Compound); $chest = Tile::createTile("Chest", $this->getLevel(), $nbt); } diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 528b11344..46f58eb8f 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -470,8 +470,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ $this->namedtag->foodSaturationLevel = new FloatTag("foodSaturationLevel", $this->getSaturation()); $this->namedtag->foodTickTimer = new IntTag("foodTickTimer", $this->foodTickTimer); - $this->namedtag->Inventory = new ListTag("Inventory", []); - $this->namedtag->Inventory->setTagType(NBT::TAG_Compound); + $this->namedtag->Inventory = new ListTag("Inventory", [], NBT::TAG_Compound); if($this->inventory !== null){ //Normal inventory $slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize(); diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index fcf10c754..26c38c261 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -376,8 +376,7 @@ class Item implements ItemIds, \JsonSerializable{ $found = false; if(!isset($tag->ench)){ - $tag->ench = new ListTag("ench", []); - $tag->ench->setTagType(NBT::TAG_Compound); + $tag->ench = new ListTag("ench", [], NBT::TAG_Compound); }else{ foreach($tag->ench as $k => $entry){ if($entry["id"] === $ench->getId()){ @@ -517,8 +516,7 @@ class Item implements ItemIds, \JsonSerializable{ if(!isset($tag->display)){ $tag->display = new CompoundTag("display", []); } - $tag->display->Lore = new ListTag("Lore"); - $tag->display->Lore->setTagType(NBT::TAG_String); + $tag->display->Lore = new ListTag("Lore", [], NBT::TAG_String); $count = 0; foreach($lines as $line){ $tag->display->Lore[$count++] = new StringTag("", $line); diff --git a/src/pocketmine/level/format/io/region/Anvil.php b/src/pocketmine/level/format/io/region/Anvil.php index 243f70fcb..a9d83741e 100644 --- a/src/pocketmine/level/format/io/region/Anvil.php +++ b/src/pocketmine/level/format/io/region/Anvil.php @@ -49,8 +49,7 @@ class Anvil extends McRegion{ $nbt->TerrainPopulated = new ByteTag("TerrainPopulated", $chunk->isPopulated() ? 1 : 0); $nbt->LightPopulated = new ByteTag("LightPopulated", $chunk->isLightPopulated() ? 1 : 0); - $nbt->Sections = new ListTag("Sections", []); - $nbt->Sections->setTagType(NBT::TAG_Compound); + $nbt->Sections = new ListTag("Sections", [], NBT::TAG_Compound); $subChunks = -1; foreach($chunk->getSubChunks() as $y => $subChunk){ if($subChunk->isEmpty()){ @@ -77,8 +76,7 @@ class Anvil extends McRegion{ } } - $nbt->Entities = new ListTag("Entities", $entities); - $nbt->Entities->setTagType(NBT::TAG_Compound); + $nbt->Entities = new ListTag("Entities", $entities, NBT::TAG_Compound); $tiles = []; foreach($chunk->getTiles() as $tile){ @@ -86,8 +84,7 @@ class Anvil extends McRegion{ $tiles[] = $tile->namedtag; } - $nbt->TileEntities = new ListTag("TileEntities", $tiles); - $nbt->TileEntities->setTagType(NBT::TAG_Compound); + $nbt->TileEntities = new ListTag("TileEntities", $tiles, NBT::TAG_Compound); //TODO: TileTicks diff --git a/src/pocketmine/level/format/io/region/McRegion.php b/src/pocketmine/level/format/io/region/McRegion.php index b44ea0204..b3c739108 100644 --- a/src/pocketmine/level/format/io/region/McRegion.php +++ b/src/pocketmine/level/format/io/region/McRegion.php @@ -95,8 +95,7 @@ class McRegion extends BaseLevelProvider{ } } - $nbt->Entities = new ListTag("Entities", $entities); - $nbt->Entities->setTagType(NBT::TAG_Compound); + $nbt->Entities = new ListTag("Entities", $entities, NBT::TAG_Compound); $tiles = []; foreach($chunk->getTiles() as $tile){ @@ -104,8 +103,7 @@ class McRegion extends BaseLevelProvider{ $tiles[] = $tile->namedtag; } - $nbt->TileEntities = new ListTag("TileEntities", $tiles); - $nbt->TileEntities->setTagType(NBT::TAG_Compound); + $nbt->TileEntities = new ListTag("TileEntities", $tiles, NBT::TAG_Compound); $writer = new NBT(NBT::BIG_ENDIAN); $nbt->setName("Level"); diff --git a/src/pocketmine/level/format/io/region/PMAnvil.php b/src/pocketmine/level/format/io/region/PMAnvil.php index db65e4073..b6fe2728b 100644 --- a/src/pocketmine/level/format/io/region/PMAnvil.php +++ b/src/pocketmine/level/format/io/region/PMAnvil.php @@ -52,8 +52,8 @@ class PMAnvil extends Anvil{ $nbt->TerrainPopulated = new ByteTag("TerrainPopulated", $chunk->isPopulated() ? 1 : 0); $nbt->LightPopulated = new ByteTag("LightPopulated", $chunk->isLightPopulated() ? 1 : 0); - $nbt->Sections = new ListTag("Sections", []); - $nbt->Sections->setTagType(NBT::TAG_Compound); + $nbt->Sections = new ListTag("Sections", [], NBT::TAG_Compound); + $subChunks = -1; foreach($chunk->getSubChunks() as $y => $subChunk){ if($subChunk->isEmpty()){ @@ -80,8 +80,7 @@ class PMAnvil extends Anvil{ } } - $nbt->Entities = new ListTag("Entities", $entities); - $nbt->Entities->setTagType(NBT::TAG_Compound); + $nbt->Entities = new ListTag("Entities", $entities, NBT::TAG_Compound); $tiles = []; foreach($chunk->getTiles() as $tile){ @@ -89,8 +88,7 @@ class PMAnvil extends Anvil{ $tiles[] = $tile->namedtag; } - $nbt->TileEntities = new ListTag("TileEntities", $tiles); - $nbt->TileEntities->setTagType(NBT::TAG_Compound); + $nbt->TileEntities = new ListTag("TileEntities", $tiles, NBT::TAG_Compound); //TODO: TileTicks diff --git a/src/pocketmine/nbt/tag/ListTag.php b/src/pocketmine/nbt/tag/ListTag.php index ac6d709fd..b15ff8b19 100644 --- a/src/pocketmine/nbt/tag/ListTag.php +++ b/src/pocketmine/nbt/tag/ListTag.php @@ -29,16 +29,18 @@ use pocketmine\nbt\NBT; class ListTag extends NamedTag implements \ArrayAccess, \Countable{ - private $tagType = NBT::TAG_End; + private $tagType; /** * ListTag constructor. * * @param string $name * @param NamedTag[] $value + * @param int $tagType */ - public function __construct(string $name = "", array $value = []){ + public function __construct(string $name = "", array $value = [], int $tagType = NBT::TAG_End){ parent::__construct($name, $value); + $this->tagType = $tagType; } /**