From 7d3fca83f090f181bb1096e59fa48b59b987f5df Mon Sep 17 00:00:00 2001 From: Muqsit Rayyan Date: Sun, 6 Aug 2017 17:05:37 +0530 Subject: [PATCH] Tile NBT usage enhancements (#1259) * Do not create new NBT objects on Tile::getSpawnCompound() * PocketMine's string formatting * Remove more useless array indices and create lesser new NBT objects. * Remove unused imports and type-hint Sign::setText() params * Do not mess with Sign::setText() params due to #1204 * Fix formatting * Make getSpawnCompound() final and add abstract addAdditionalSpawnData() * Make the same changes for Bed tile * Fix a missing "->" and remove some unneeded int casting. --- src/pocketmine/tile/Bed.php | 14 ++---- src/pocketmine/tile/Chest.php | 29 +++--------- src/pocketmine/tile/EnchantTable.php | 14 +----- src/pocketmine/tile/FlowerPot.php | 19 +++----- src/pocketmine/tile/Furnace.php | 66 ++++++++++++---------------- src/pocketmine/tile/ItemFrame.php | 22 +++------- src/pocketmine/tile/Nameable.php | 1 - src/pocketmine/tile/Sign.php | 50 +++++++++------------ src/pocketmine/tile/Skull.php | 20 +++------ src/pocketmine/tile/Spawnable.php | 19 +++++++- src/pocketmine/tile/Tile.php | 18 ++++---- 11 files changed, 107 insertions(+), 165 deletions(-) diff --git a/src/pocketmine/tile/Bed.php b/src/pocketmine/tile/Bed.php index 68bd4ba20..c6eb67750 100644 --- a/src/pocketmine/tile/Bed.php +++ b/src/pocketmine/tile/Bed.php @@ -27,8 +27,6 @@ namespace pocketmine\tile; use pocketmine\level\Level; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; -use pocketmine\nbt\tag\StringTag; class Bed extends Spawnable{ @@ -44,17 +42,11 @@ class Bed extends Spawnable{ } public function setColor(int $color){ - $this->namedtag["color"] = $color & 0x0f; + $this->namedtag->color->setValue($color & 0x0f); $this->onChanged(); } - public function getSpawnCompound() : CompoundTag{ - return new CompoundTag("", [ - new StringTag("id", Tile::BED), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z), - $this->namedtag->color - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + $nbt->color = $this->namedtag->color; } } \ No newline at end of file diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index 6f1c84064..5e3a88285 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -74,7 +74,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ } public function saveNBT(){ - $this->namedtag->Items = new ListTag("Items", []); + $this->namedtag->Items->setValue([]); $this->namedtag->Items->setTagType(NBT::TAG_Compound); for($index = 0; $index < $this->getSize(); ++$index){ $this->setItem($index, $this->inventory->getItem($index)); @@ -95,7 +95,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ */ protected function getSlotIndex(int $index){ foreach($this->namedtag->Items as $i => $slot){ - if((int) $slot["Slot"] === $index){ + if($slot->Slot->getValue() === $index){ return (int) $i; } } @@ -225,7 +225,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ */ public function getPair(){ if($this->isPaired()){ - $tile = $this->getLevel()->getTile(new Vector3((int) $this->namedtag["pairx"], $this->y, (int) $this->namedtag["pairz"])); + $tile = $this->getLevel()->getTile(new Vector3($this->namedtag->pairx->getValue(), $this->y, $this->namedtag->pairz->getValue())); if($tile instanceof Chest){ return $tile; } @@ -276,29 +276,14 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ return true; } - public function getSpawnCompound() : CompoundTag{ + public function addAdditionalSpawnData(CompoundTag $nbt){ if($this->isPaired()){ - $c = new CompoundTag("", [ - new StringTag("id", Tile::CHEST), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z), - new IntTag("pairx", (int) $this->namedtag["pairx"]), - new IntTag("pairz", (int) $this->namedtag["pairz"]) - ]); - }else{ - $c = new CompoundTag("", [ - new StringTag("id", Tile::CHEST), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z) - ]); + $nbt->pairx = $this->namedtag->pairx; + $nbt->pairz = $this->namedtag->pairz; } if($this->hasName()){ - $c->CustomName = $this->namedtag->CustomName; + $nbt->CustomName = $this->namedtag->CustomName; } - - return $c; } } diff --git a/src/pocketmine/tile/EnchantTable.php b/src/pocketmine/tile/EnchantTable.php index e867bfcc8..7a2592526 100644 --- a/src/pocketmine/tile/EnchantTable.php +++ b/src/pocketmine/tile/EnchantTable.php @@ -24,7 +24,6 @@ declare(strict_types=1); namespace pocketmine\tile; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\StringTag; class EnchantTable extends Spawnable implements Nameable{ @@ -47,18 +46,9 @@ class EnchantTable extends Spawnable implements Nameable{ $this->namedtag->CustomName = new StringTag("CustomName", $str); } - public function getSpawnCompound() : CompoundTag{ - $c = new CompoundTag("", [ - new StringTag("id", Tile::ENCHANT_TABLE), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z) - ]); - + public function addAdditionalSpawnData(CompoundTag $nbt){ if($this->hasName()){ - $c->CustomName = $this->namedtag->CustomName; + $nbt->CustomName = $this->namedtag->CustomName; } - - return $c; } } diff --git a/src/pocketmine/tile/FlowerPot.php b/src/pocketmine/tile/FlowerPot.php index 5897918c0..f5950186c 100644 --- a/src/pocketmine/tile/FlowerPot.php +++ b/src/pocketmine/tile/FlowerPot.php @@ -28,7 +28,6 @@ use pocketmine\level\Level; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\ShortTag; -use pocketmine\nbt\tag\StringTag; class FlowerPot extends Spawnable{ @@ -66,12 +65,12 @@ class FlowerPot extends Spawnable{ } public function getItem() : Item{ - return Item::get((int) ($this->namedtag["item"] ?? 0), (int) ($this->namedtag["mData"] ?? 0), 1); + return Item::get($this->namedtag->item->getValue(), $this->namedtag->mData->getValue(), 1); } public function setItem(Item $item){ - $this->namedtag["item"] = $item->getId(); - $this->namedtag["mData"] = $item->getDamage(); + $this->namedtag->item->setValue($item->getId()); + $this->namedtag->mData->setValue($item->getDamage()); $this->onChanged(); } @@ -83,14 +82,8 @@ class FlowerPot extends Spawnable{ return $this->getItem()->getId() === Item::AIR; } - public function getSpawnCompound() : CompoundTag{ - return new CompoundTag("", [ - new StringTag("id", Tile::FLOWER_POT), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z), - $this->namedtag->item, - $this->namedtag->mData - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + $nbt->item = $this->namedtag->item; + $nbt->mData = $this->namedtag->mData; } } \ No newline at end of file diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 0e0a2508b..912a47be6 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -33,7 +33,6 @@ use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\StringTag; @@ -44,14 +43,14 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ protected $inventory; public function __construct(Level $level, CompoundTag $nbt){ - if(!isset($nbt->BurnTime) or $nbt["BurnTime"] < 0){ + if(!isset($nbt->BurnTime) or $nbt->BurnTime->getValue() < 0){ $nbt->BurnTime = new ShortTag("BurnTime", 0); } - if(!isset($nbt->CookTime) or $nbt["CookTime"] < 0 or ($nbt["BurnTime"] === 0 and $nbt["CookTime"] > 0)){ + if(!isset($nbt->CookTime) or $nbt->CookTime->getValue() < 0 or ($nbt->BurnTime->getValue() === 0 and $nbt->CookTime->getValue() > 0)){ $nbt->CookTime = new ShortTag("CookTime", 0); } if(!isset($nbt->MaxTime)){ - $nbt->MaxTime = new ShortTag("BurnTime", $nbt["BurnTime"]); + $nbt->MaxTime = new ShortTag("BurnTime", $nbt->BurnTime->getValue()); $nbt->BurnTicks = new ShortTag("BurnTicks", 0); } @@ -67,7 +66,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $this->inventory->setItem($i, $this->getItem($i)); } - if($this->namedtag["BurnTime"] > 0){ + if($this->namedtag->BurnTime->getValue() > 0){ $this->scheduleUpdate(); } } @@ -102,7 +101,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ } public function saveNBT(){ - $this->namedtag->Items = new ListTag("Items", []); + $this->namedtag->Items->setValue([]); $this->namedtag->Items->setTagType(NBT::TAG_Compound); for($index = 0; $index < $this->getSize(); ++$index){ $this->setItem($index, $this->inventory->getItem($index)); @@ -123,7 +122,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ */ protected function getSlotIndex(int $index) : int{ foreach($this->namedtag->Items as $i => $slot){ - if((int) $slot["Slot"] === $index){ + if($slot->Slot->getValue() === $index){ return (int) $i; } } @@ -188,14 +187,14 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ return; } - $this->namedtag->MaxTime = new ShortTag("MaxTime", $ev->getBurnTime()); - $this->namedtag->BurnTime = new ShortTag("BurnTime", $ev->getBurnTime()); + $this->namedtag->MaxTime->setValue($ev->getBurnTime()); + $this->namedtag->BurnTime->setValue($ev->getBurnTime()); $this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); if($this->getBlock()->getId() === Item::FURNACE){ $this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $this->getBlock()->getDamage()), true); } - if($this->namedtag["BurnTime"] > 0 and $ev->isBurning()){ + if($this->namedtag->BurnTime->getValue() > 0 and $ev->isBurning()){ $fuel->setCount($fuel->getCount() - 1); if($fuel->getCount() === 0){ $fuel = Item::get(Item::AIR, 0, 0); @@ -219,17 +218,17 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $smelt = $this->server->getCraftingManager()->matchFurnaceRecipe($raw); $canSmelt = ($smelt instanceof FurnaceRecipe and $raw->getCount() > 0 and (($smelt->getResult()->equals($product) and $product->getCount() < $product->getMaxStackSize()) or $product->getId() === Item::AIR)); - if($this->namedtag["BurnTime"] <= 0 and $canSmelt and $fuel->getFuelTime() !== null and $fuel->getCount() > 0){ + if($this->namedtag->BurnTime->getValue() <= 0 and $canSmelt and $fuel->getFuelTime() !== null and $fuel->getCount() > 0){ $this->checkFuel($fuel); } - if($this->namedtag["BurnTime"] > 0){ - $this->namedtag->BurnTime = new ShortTag("BurnTime", ((int) $this->namedtag["BurnTime"]) - 1); - $this->namedtag->BurnTicks = new ShortTag("BurnTicks", (int) ceil($this->namedtag["BurnTime"] / $this->namedtag["MaxTime"] * 200)); + if($this->namedtag->BurnTime->getValue() > 0){ + $this->namedtag->BurnTime->setValue($this->namedtag->BurnTime->getValue() - 1); + $this->namedtag->BurnTicks = new ShortTag("BurnTicks", (int) ceil($this->namedtag->BurnTime->getValue() / $this->namedtag->MaxTime->getValue() * 200)); if($smelt instanceof FurnaceRecipe and $canSmelt){ - $this->namedtag->CookTime = new ShortTag("CookTime", ((int) $this->namedtag["CookTime"]) + 1); - if($this->namedtag["CookTime"] >= 200){ //10 seconds + $this->namedtag->CookTime->setValue($this->namedtag->CookTime->getValue() + 1); + if($this->namedtag->CookTime->getValue() >= 200){ //10 seconds $product = Item::get($smelt->getResult()->getId(), $smelt->getResult()->getDamage(), $product->getCount() + 1); $this->server->getPluginManager()->callEvent($ev = new FurnaceSmeltEvent($this, $raw, $product)); @@ -243,23 +242,23 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $this->inventory->setSmelting($raw); } - $this->namedtag->CookTime = new ShortTag("CookTime", ((int) $this->namedtag["CookTime"]) - 200); + $this->namedtag->CookTime->setValue($this->namedtag->CookTime->getValue() - 200); } - }elseif($this->namedtag["BurnTime"] <= 0){ - $this->namedtag->BurnTime = new ShortTag("BurnTime", 0); - $this->namedtag->CookTime = new ShortTag("CookTime", 0); - $this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); + }elseif($this->namedtag->BurnTime->getValue() <= 0){ + $this->namedtag->BurnTime->setValue(0); + $this->namedtag->CookTime->setValue(0); + $this->namedtag->BurnTicks->setValue(0); }else{ - $this->namedtag->CookTime = new ShortTag("CookTime", 0); + $this->namedtag->CookTime->setValue(0); } $ret = true; }else{ if($this->getBlock()->getId() === Item::BURNING_FURNACE){ $this->getLevel()->setBlock($this, Block::get(Item::FURNACE, $this->getBlock()->getDamage()), true); } - $this->namedtag->BurnTime = new ShortTag("BurnTime", 0); - $this->namedtag->CookTime = new ShortTag("CookTime", 0); - $this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); + $this->namedtag->BurnTime->setValue(0); + $this->namedtag->CookTime->setValue(0); + $this->namedtag->BurnTicks->setValue(0); } foreach($this->getInventory()->getViewers() as $player){ @@ -268,13 +267,13 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $pk = new ContainerSetDataPacket(); $pk->windowid = $windowId; $pk->property = 0; //Smelting - $pk->value = $this->namedtag["CookTime"]; + $pk->value = $this->namedtag->CookTime->getValue(); $player->dataPacket($pk); $pk = new ContainerSetDataPacket(); $pk->windowid = $windowId; $pk->property = 1; //Fire icon - $pk->value = $this->namedtag["BurnTicks"]; + $pk->value = $this->namedtag->BurnTicks->getValue(); $player->dataPacket($pk); } @@ -287,19 +286,12 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ return $ret; } - public function getSpawnCompound() : CompoundTag{ - $nbt = new CompoundTag("", [ - new StringTag("id", Tile::FURNACE), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z), - new ShortTag("BurnTime", (int) $this->namedtag["BurnTime"]), - new ShortTag("CookTime", (int) $this->namedtag["CookTime"]) - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + $nbt->BurnTime = $this->namedtag->BurnTime; + $nbt->CookTime = $this->namedtag->CookTime; if($this->hasName()){ $nbt->CustomName = $this->namedtag->CustomName; } - return $nbt; } } diff --git a/src/pocketmine/tile/ItemFrame.php b/src/pocketmine/tile/ItemFrame.php index e057f937f..388c8efe9 100644 --- a/src/pocketmine/tile/ItemFrame.php +++ b/src/pocketmine/tile/ItemFrame.php @@ -28,8 +28,6 @@ use pocketmine\level\Level; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\FloatTag; -use pocketmine\nbt\tag\IntTag; -use pocketmine\nbt\tag\StringTag; class ItemFrame extends Spawnable{ @@ -71,7 +69,7 @@ class ItemFrame extends Spawnable{ } public function setItemRotation(int $rotation){ - $this->namedtag->ItemRotation = new ByteTag("ItemRotation", $rotation); + $this->namedtag->ItemRotation->setValue($rotation); $this->onChanged(); } @@ -80,23 +78,17 @@ class ItemFrame extends Spawnable{ } public function setItemDropChance(float $chance){ - $this->namedtag->ItemDropChance = new FloatTag("ItemDropChance", $chance); + $this->namedtag->ItemDropChance->setValue($chance); $this->onChanged(); } - public function getSpawnCompound() : CompoundTag{ - $tag = new CompoundTag("", [ - new StringTag("id", Tile::ITEM_FRAME), - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z), - $this->namedtag->ItemDropChance, - $this->namedtag->ItemRotation, - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + $nbt->ItemDropChance = $this->namedtag->ItemDropChance; + $nbt->ItemRotation = $this->namedtag->ItemRotation; + if($this->hasItem()){ - $tag->Item = $this->namedtag->Item; + $nbt->Item = $this->namedtag->Item; } - return $tag; } } \ No newline at end of file diff --git a/src/pocketmine/tile/Nameable.php b/src/pocketmine/tile/Nameable.php index 43a1a9154..4008705ee 100644 --- a/src/pocketmine/tile/Nameable.php +++ b/src/pocketmine/tile/Nameable.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace pocketmine\tile; - interface Nameable{ diff --git a/src/pocketmine/tile/Sign.php b/src/pocketmine/tile/Sign.php index 88e668e49..49688588a 100644 --- a/src/pocketmine/tile/Sign.php +++ b/src/pocketmine/tile/Sign.php @@ -26,7 +26,6 @@ namespace pocketmine\tile; use pocketmine\event\block\SignChangeEvent; use pocketmine\level\Level; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\StringTag; use pocketmine\Player; use pocketmine\utils\TextFormat; @@ -56,13 +55,11 @@ class Sign extends Spawnable{ } public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ - $this->namedtag->Text1 = new StringTag("Text1", $line1); - $this->namedtag->Text2 = new StringTag("Text2", $line2); - $this->namedtag->Text3 = new StringTag("Text3", $line3); - $this->namedtag->Text4 = new StringTag("Text4", $line4); + $this->namedtag->Text1->setValue($line1); + $this->namedtag->Text2->setValue($line2); + $this->namedtag->Text3->setValue($line3); + $this->namedtag->Text4->setValue($line4); $this->onChanged(); - - return true; } /** @@ -74,7 +71,7 @@ class Sign extends Spawnable{ if($index < 0 or $index > 3){ throw new \InvalidArgumentException("Index must be in the range 0-3!"); } - $this->namedtag["Text" . ($index + 1)] = $line; + $this->namedtag->{"Text" . ($index + 1)}->setValue($line); if($update){ $this->onChanged(); } @@ -89,29 +86,24 @@ class Sign extends Spawnable{ if($index < 0 or $index > 3){ throw new \InvalidArgumentException("Index must be in the range 0-3!"); } - return (string) $this->namedtag["Text" . ($index + 1)]; + return $this->namedtag->{"Text" . ($index + 1)}->getValue(); } public function getText(){ return [ - $this->namedtag["Text1"], - $this->namedtag["Text2"], - $this->namedtag["Text3"], - $this->namedtag["Text4"] + $this->namedtag->Text1->getValue(), + $this->namedtag->Text2->getValue(), + $this->namedtag->Text3->getValue(), + $this->namedtag->Text4->getValue() ]; } - public function getSpawnCompound() : CompoundTag{ - return new CompoundTag("", [ - new StringTag("id", Tile::SIGN), - $this->namedtag->Text1, - $this->namedtag->Text2, - $this->namedtag->Text3, - $this->namedtag->Text4, - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z) - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + for($i = 1; $i <= 4; $i++){ + $textKey = "Text" . $i; + $nbt->$textKey = $this->namedtag->$textKey; + } + return $nbt; } public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{ @@ -120,13 +112,13 @@ class Sign extends Spawnable{ } $ev = new SignChangeEvent($this->getBlock(), $player, [ - TextFormat::clean($nbt["Text1"], ($removeFormat = $player->getRemoveFormat())), - TextFormat::clean($nbt["Text2"], $removeFormat), - TextFormat::clean($nbt["Text3"], $removeFormat), - TextFormat::clean($nbt["Text4"], $removeFormat) + TextFormat::clean($nbt->Text1->getValue(), ($removeFormat = $player->getRemoveFormat())), + TextFormat::clean($nbt->Text2->getValue(), $removeFormat), + TextFormat::clean($nbt->Text3->getValue(), $removeFormat), + TextFormat::clean($nbt->Text4->getValue(), $removeFormat) ]); - if(!isset($this->namedtag->Creator) or $this->namedtag["Creator"] !== $player->getRawUniqueId()){ + if(!isset($this->namedtag->Creator) or $this->namedtag->Creator->getValue() !== $player->getRawUniqueId()){ $ev->setCancelled(); } diff --git a/src/pocketmine/tile/Skull.php b/src/pocketmine/tile/Skull.php index b16b3407f..ea7f021da 100644 --- a/src/pocketmine/tile/Skull.php +++ b/src/pocketmine/tile/Skull.php @@ -26,8 +26,6 @@ namespace pocketmine\tile; use pocketmine\level\Level; use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; -use pocketmine\nbt\tag\StringTag; class Skull extends Spawnable{ const TYPE_SKELETON = 0; @@ -48,22 +46,16 @@ class Skull extends Spawnable{ } public function setType(int $type){ - $this->namedtag->SkullType = new ByteTag("SkullType", $type); + $this->namedtag->SkullType->setValue($type); $this->onChanged(); } - public function getType(){ - return $this->namedtag["SkullType"]; + public function getType() : int{ + return $this->namedtag->SkullType->getValue(); } - public function getSpawnCompound() : CompoundTag{ - return new CompoundTag("", [ - new StringTag("id", Tile::SKULL), - $this->namedtag->SkullType, - $this->namedtag->Rot, - new IntTag("x", (int) $this->x), - new IntTag("y", (int) $this->y), - new IntTag("z", (int) $this->z) - ]); + public function addAdditionalSpawnData(CompoundTag $nbt){ + $nbt->SkullType = $this->namedtag->SkullType; + $nbt->Rot = $this->namedtag->Rot; } } \ No newline at end of file diff --git a/src/pocketmine/tile/Spawnable.php b/src/pocketmine/tile/Spawnable.php index 7d580023d..f77e81d71 100644 --- a/src/pocketmine/tile/Spawnable.php +++ b/src/pocketmine/tile/Spawnable.php @@ -77,7 +77,24 @@ abstract class Spawnable extends Tile{ /** * @return CompoundTag */ - abstract public function getSpawnCompound() : CompoundTag; + final public function getSpawnCompound() : CompoundTag{ + $nbt = new CompoundTag("", [ + $this->namedtag->id, + $this->namedtag->x, + $this->namedtag->y, + $this->namedtag->z + ]); + $this->addAdditionalSpawnData($nbt); + return $nbt; + } + + /** + * An extension to getSpawnCompound() for + * further modifying the generic tile NBT. + * + * @param CompoundTag $nbt + */ + abstract public function addAdditionalSpawnData(CompoundTag $nbt); /** * Called when a player updates a block entity's NBT data diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index f7b0a8d3c..ecb93639e 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -33,8 +33,6 @@ use pocketmine\level\format\Chunk; use pocketmine\level\Level; use pocketmine\level\Position; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\nbt\tag\IntTag; -use pocketmine\nbt\tag\StringTag; abstract class Tile extends Position{ @@ -127,15 +125,15 @@ abstract class Tile extends Position{ $this->namedtag = $nbt; $this->server = $level->getServer(); $this->setLevel($level); - $this->chunk = $level->getChunk($this->namedtag["x"] >> 4, $this->namedtag["z"] >> 4, false); + $this->chunk = $level->getChunk($this->namedtag->x->getValue() >> 4, $this->namedtag->z->getValue() >> 4, false); assert($this->chunk !== null); $this->name = ""; $this->lastUpdate = microtime(true); $this->id = Tile::$tileCount++; - $this->x = (int) $this->namedtag["x"]; - $this->y = (int) $this->namedtag["y"]; - $this->z = (int) $this->namedtag["z"]; + $this->x = $this->namedtag->x->getValue(); + $this->y = $this->namedtag->y->getValue(); + $this->z = $this->namedtag->z->getValue(); $this->chunk->addTile($this); $this->getLevel()->addTile($this); @@ -147,10 +145,10 @@ abstract class Tile extends Position{ } public function saveNBT(){ - $this->namedtag->id = new StringTag("id", $this->getSaveId()); - $this->namedtag->x = new IntTag("x", $this->x); - $this->namedtag->y = new IntTag("y", $this->y); - $this->namedtag->z = new IntTag("z", $this->z); + $this->namedtag->id->setValue($this->getSaveId()); + $this->namedtag->x->setValue($this->x); + $this->namedtag->y->setValue($this->y); + $this->namedtag->z->setValue($this->z); } public function getCleanedNBT(){