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.
This commit is contained in:
Muqsit Rayyan 2017-08-06 17:05:37 +05:30 committed by Dylan K. Taylor
parent 3fdbcee10f
commit 7d3fca83f0
11 changed files with 107 additions and 165 deletions

View File

@ -27,8 +27,6 @@ namespace pocketmine\tile;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
class Bed extends Spawnable{ class Bed extends Spawnable{
@ -44,17 +42,11 @@ class Bed extends Spawnable{
} }
public function setColor(int $color){ public function setColor(int $color){
$this->namedtag["color"] = $color & 0x0f; $this->namedtag->color->setValue($color & 0x0f);
$this->onChanged(); $this->onChanged();
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
return new CompoundTag("", [ $nbt->color = $this->namedtag->color;
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
]);
} }
} }

View File

@ -74,7 +74,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
} }
public function saveNBT(){ public function saveNBT(){
$this->namedtag->Items = new ListTag("Items", []); $this->namedtag->Items->setValue([]);
$this->namedtag->Items->setTagType(NBT::TAG_Compound); $this->namedtag->Items->setTagType(NBT::TAG_Compound);
for($index = 0; $index < $this->getSize(); ++$index){ for($index = 0; $index < $this->getSize(); ++$index){
$this->setItem($index, $this->inventory->getItem($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){ protected function getSlotIndex(int $index){
foreach($this->namedtag->Items as $i => $slot){ foreach($this->namedtag->Items as $i => $slot){
if((int) $slot["Slot"] === $index){ if($slot->Slot->getValue() === $index){
return (int) $i; return (int) $i;
} }
} }
@ -225,7 +225,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
*/ */
public function getPair(){ public function getPair(){
if($this->isPaired()){ 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){ if($tile instanceof Chest){
return $tile; return $tile;
} }
@ -276,29 +276,14 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
return true; return true;
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
if($this->isPaired()){ if($this->isPaired()){
$c = new CompoundTag("", [ $nbt->pairx = $this->namedtag->pairx;
new StringTag("id", Tile::CHEST), $nbt->pairz = $this->namedtag->pairz;
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)
]);
} }
if($this->hasName()){ if($this->hasName()){
$c->CustomName = $this->namedtag->CustomName; $nbt->CustomName = $this->namedtag->CustomName;
} }
return $c;
} }
} }

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
class EnchantTable extends Spawnable implements Nameable{ class EnchantTable extends Spawnable implements Nameable{
@ -47,18 +46,9 @@ class EnchantTable extends Spawnable implements Nameable{
$this->namedtag->CustomName = new StringTag("CustomName", $str); $this->namedtag->CustomName = new StringTag("CustomName", $str);
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
$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)
]);
if($this->hasName()){ if($this->hasName()){
$c->CustomName = $this->namedtag->CustomName; $nbt->CustomName = $this->namedtag->CustomName;
} }
return $c;
} }
} }

View File

@ -28,7 +28,6 @@ use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
class FlowerPot extends Spawnable{ class FlowerPot extends Spawnable{
@ -66,12 +65,12 @@ class FlowerPot extends Spawnable{
} }
public function getItem() : Item{ 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){ public function setItem(Item $item){
$this->namedtag["item"] = $item->getId(); $this->namedtag->item->setValue($item->getId());
$this->namedtag["mData"] = $item->getDamage(); $this->namedtag->mData->setValue($item->getDamage());
$this->onChanged(); $this->onChanged();
} }
@ -83,14 +82,8 @@ class FlowerPot extends Spawnable{
return $this->getItem()->getId() === Item::AIR; return $this->getItem()->getId() === Item::AIR;
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
return new CompoundTag("", [ $nbt->item = $this->namedtag->item;
new StringTag("id", Tile::FLOWER_POT), $nbt->mData = $this->namedtag->mData;
new IntTag("x", (int) $this->x),
new IntTag("y", (int) $this->y),
new IntTag("z", (int) $this->z),
$this->namedtag->item,
$this->namedtag->mData
]);
} }
} }

View File

@ -33,7 +33,6 @@ use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\NBT; use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag; 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;
@ -44,14 +43,14 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
protected $inventory; protected $inventory;
public function __construct(Level $level, CompoundTag $nbt){ 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); $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); $nbt->CookTime = new ShortTag("CookTime", 0);
} }
if(!isset($nbt->MaxTime)){ 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); $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)); $this->inventory->setItem($i, $this->getItem($i));
} }
if($this->namedtag["BurnTime"] > 0){ if($this->namedtag->BurnTime->getValue() > 0){
$this->scheduleUpdate(); $this->scheduleUpdate();
} }
} }
@ -102,7 +101,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
} }
public function saveNBT(){ public function saveNBT(){
$this->namedtag->Items = new ListTag("Items", []); $this->namedtag->Items->setValue([]);
$this->namedtag->Items->setTagType(NBT::TAG_Compound); $this->namedtag->Items->setTagType(NBT::TAG_Compound);
for($index = 0; $index < $this->getSize(); ++$index){ for($index = 0; $index < $this->getSize(); ++$index){
$this->setItem($index, $this->inventory->getItem($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{ protected function getSlotIndex(int $index) : int{
foreach($this->namedtag->Items as $i => $slot){ foreach($this->namedtag->Items as $i => $slot){
if((int) $slot["Slot"] === $index){ if($slot->Slot->getValue() === $index){
return (int) $i; return (int) $i;
} }
} }
@ -188,14 +187,14 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
return; return;
} }
$this->namedtag->MaxTime = new ShortTag("MaxTime", $ev->getBurnTime()); $this->namedtag->MaxTime->setValue($ev->getBurnTime());
$this->namedtag->BurnTime = new ShortTag("BurnTime", $ev->getBurnTime()); $this->namedtag->BurnTime->setValue($ev->getBurnTime());
$this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); $this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0);
if($this->getBlock()->getId() === Item::FURNACE){ if($this->getBlock()->getId() === Item::FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $this->getBlock()->getDamage()), true); $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); $fuel->setCount($fuel->getCount() - 1);
if($fuel->getCount() === 0){ if($fuel->getCount() === 0){
$fuel = Item::get(Item::AIR, 0, 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); $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)); $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); $this->checkFuel($fuel);
} }
if($this->namedtag["BurnTime"] > 0){ if($this->namedtag->BurnTime->getValue() > 0){
$this->namedtag->BurnTime = new ShortTag("BurnTime", ((int) $this->namedtag["BurnTime"]) - 1); $this->namedtag->BurnTime->setValue($this->namedtag->BurnTime->getValue() - 1);
$this->namedtag->BurnTicks = new ShortTag("BurnTicks", (int) ceil($this->namedtag["BurnTime"] / $this->namedtag["MaxTime"] * 200)); $this->namedtag->BurnTicks = new ShortTag("BurnTicks", (int) ceil($this->namedtag->BurnTime->getValue() / $this->namedtag->MaxTime->getValue() * 200));
if($smelt instanceof FurnaceRecipe and $canSmelt){ if($smelt instanceof FurnaceRecipe and $canSmelt){
$this->namedtag->CookTime = new ShortTag("CookTime", ((int) $this->namedtag["CookTime"]) + 1); $this->namedtag->CookTime->setValue($this->namedtag->CookTime->getValue() + 1);
if($this->namedtag["CookTime"] >= 200){ //10 seconds if($this->namedtag->CookTime->getValue() >= 200){ //10 seconds
$product = Item::get($smelt->getResult()->getId(), $smelt->getResult()->getDamage(), $product->getCount() + 1); $product = Item::get($smelt->getResult()->getId(), $smelt->getResult()->getDamage(), $product->getCount() + 1);
$this->server->getPluginManager()->callEvent($ev = new FurnaceSmeltEvent($this, $raw, $product)); $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->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){ }elseif($this->namedtag->BurnTime->getValue() <= 0){
$this->namedtag->BurnTime = new ShortTag("BurnTime", 0); $this->namedtag->BurnTime->setValue(0);
$this->namedtag->CookTime = new ShortTag("CookTime", 0); $this->namedtag->CookTime->setValue(0);
$this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); $this->namedtag->BurnTicks->setValue(0);
}else{ }else{
$this->namedtag->CookTime = new ShortTag("CookTime", 0); $this->namedtag->CookTime->setValue(0);
} }
$ret = true; $ret = true;
}else{ }else{
if($this->getBlock()->getId() === Item::BURNING_FURNACE){ if($this->getBlock()->getId() === Item::BURNING_FURNACE){
$this->getLevel()->setBlock($this, Block::get(Item::FURNACE, $this->getBlock()->getDamage()), true); $this->getLevel()->setBlock($this, Block::get(Item::FURNACE, $this->getBlock()->getDamage()), true);
} }
$this->namedtag->BurnTime = new ShortTag("BurnTime", 0); $this->namedtag->BurnTime->setValue(0);
$this->namedtag->CookTime = new ShortTag("CookTime", 0); $this->namedtag->CookTime->setValue(0);
$this->namedtag->BurnTicks = new ShortTag("BurnTicks", 0); $this->namedtag->BurnTicks->setValue(0);
} }
foreach($this->getInventory()->getViewers() as $player){ foreach($this->getInventory()->getViewers() as $player){
@ -268,13 +267,13 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
$pk = new ContainerSetDataPacket(); $pk = new ContainerSetDataPacket();
$pk->windowid = $windowId; $pk->windowid = $windowId;
$pk->property = 0; //Smelting $pk->property = 0; //Smelting
$pk->value = $this->namedtag["CookTime"]; $pk->value = $this->namedtag->CookTime->getValue();
$player->dataPacket($pk); $player->dataPacket($pk);
$pk = new ContainerSetDataPacket(); $pk = new ContainerSetDataPacket();
$pk->windowid = $windowId; $pk->windowid = $windowId;
$pk->property = 1; //Fire icon $pk->property = 1; //Fire icon
$pk->value = $this->namedtag["BurnTicks"]; $pk->value = $this->namedtag->BurnTicks->getValue();
$player->dataPacket($pk); $player->dataPacket($pk);
} }
@ -287,19 +286,12 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
return $ret; return $ret;
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
$nbt = new CompoundTag("", [ $nbt->BurnTime = $this->namedtag->BurnTime;
new StringTag("id", Tile::FURNACE), $nbt->CookTime = $this->namedtag->CookTime;
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"])
]);
if($this->hasName()){ if($this->hasName()){
$nbt->CustomName = $this->namedtag->CustomName; $nbt->CustomName = $this->namedtag->CustomName;
} }
return $nbt;
} }
} }

View File

@ -28,8 +28,6 @@ use pocketmine\level\Level;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\FloatTag; use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
class ItemFrame extends Spawnable{ class ItemFrame extends Spawnable{
@ -71,7 +69,7 @@ class ItemFrame extends Spawnable{
} }
public function setItemRotation(int $rotation){ public function setItemRotation(int $rotation){
$this->namedtag->ItemRotation = new ByteTag("ItemRotation", $rotation); $this->namedtag->ItemRotation->setValue($rotation);
$this->onChanged(); $this->onChanged();
} }
@ -80,23 +78,17 @@ class ItemFrame extends Spawnable{
} }
public function setItemDropChance(float $chance){ public function setItemDropChance(float $chance){
$this->namedtag->ItemDropChance = new FloatTag("ItemDropChance", $chance); $this->namedtag->ItemDropChance->setValue($chance);
$this->onChanged(); $this->onChanged();
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
$tag = new CompoundTag("", [ $nbt->ItemDropChance = $this->namedtag->ItemDropChance;
new StringTag("id", Tile::ITEM_FRAME), $nbt->ItemRotation = $this->namedtag->ItemRotation;
new IntTag("x", (int) $this->x),
new IntTag("y", (int) $this->y),
new IntTag("z", (int) $this->z),
$this->namedtag->ItemDropChance,
$this->namedtag->ItemRotation,
]);
if($this->hasItem()){ if($this->hasItem()){
$tag->Item = $this->namedtag->Item; $nbt->Item = $this->namedtag->Item;
} }
return $tag;
} }
} }

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
interface Nameable{ interface Nameable{

View File

@ -26,7 +26,6 @@ namespace pocketmine\tile;
use pocketmine\event\block\SignChangeEvent; use pocketmine\event\block\SignChangeEvent;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
@ -56,13 +55,11 @@ class Sign extends Spawnable{
} }
public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){ public function setText($line1 = "", $line2 = "", $line3 = "", $line4 = ""){
$this->namedtag->Text1 = new StringTag("Text1", $line1); $this->namedtag->Text1->setValue($line1);
$this->namedtag->Text2 = new StringTag("Text2", $line2); $this->namedtag->Text2->setValue($line2);
$this->namedtag->Text3 = new StringTag("Text3", $line3); $this->namedtag->Text3->setValue($line3);
$this->namedtag->Text4 = new StringTag("Text4", $line4); $this->namedtag->Text4->setValue($line4);
$this->onChanged(); $this->onChanged();
return true;
} }
/** /**
@ -74,7 +71,7 @@ class Sign extends Spawnable{
if($index < 0 or $index > 3){ if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-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){ if($update){
$this->onChanged(); $this->onChanged();
} }
@ -89,29 +86,24 @@ class Sign extends Spawnable{
if($index < 0 or $index > 3){ if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-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(){ public function getText(){
return [ return [
$this->namedtag["Text1"], $this->namedtag->Text1->getValue(),
$this->namedtag["Text2"], $this->namedtag->Text2->getValue(),
$this->namedtag["Text3"], $this->namedtag->Text3->getValue(),
$this->namedtag["Text4"] $this->namedtag->Text4->getValue()
]; ];
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
return new CompoundTag("", [ for($i = 1; $i <= 4; $i++){
new StringTag("id", Tile::SIGN), $textKey = "Text" . $i;
$this->namedtag->Text1, $nbt->$textKey = $this->namedtag->$textKey;
$this->namedtag->Text2, }
$this->namedtag->Text3, return $nbt;
$this->namedtag->Text4,
new IntTag("x", (int) $this->x),
new IntTag("y", (int) $this->y),
new IntTag("z", (int) $this->z)
]);
} }
public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{ public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{
@ -120,13 +112,13 @@ class Sign extends Spawnable{
} }
$ev = new SignChangeEvent($this->getBlock(), $player, [ $ev = new SignChangeEvent($this->getBlock(), $player, [
TextFormat::clean($nbt["Text1"], ($removeFormat = $player->getRemoveFormat())), TextFormat::clean($nbt->Text1->getValue(), ($removeFormat = $player->getRemoveFormat())),
TextFormat::clean($nbt["Text2"], $removeFormat), TextFormat::clean($nbt->Text2->getValue(), $removeFormat),
TextFormat::clean($nbt["Text3"], $removeFormat), TextFormat::clean($nbt->Text3->getValue(), $removeFormat),
TextFormat::clean($nbt["Text4"], $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(); $ev->setCancelled();
} }

View File

@ -26,8 +26,6 @@ namespace pocketmine\tile;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
class Skull extends Spawnable{ class Skull extends Spawnable{
const TYPE_SKELETON = 0; const TYPE_SKELETON = 0;
@ -48,22 +46,16 @@ class Skull extends Spawnable{
} }
public function setType(int $type){ public function setType(int $type){
$this->namedtag->SkullType = new ByteTag("SkullType", $type); $this->namedtag->SkullType->setValue($type);
$this->onChanged(); $this->onChanged();
} }
public function getType(){ public function getType() : int{
return $this->namedtag["SkullType"]; return $this->namedtag->SkullType->getValue();
} }
public function getSpawnCompound() : CompoundTag{ public function addAdditionalSpawnData(CompoundTag $nbt){
return new CompoundTag("", [ $nbt->SkullType = $this->namedtag->SkullType;
new StringTag("id", Tile::SKULL), $nbt->Rot = $this->namedtag->Rot;
$this->namedtag->SkullType,
$this->namedtag->Rot,
new IntTag("x", (int) $this->x),
new IntTag("y", (int) $this->y),
new IntTag("z", (int) $this->z)
]);
} }
} }

View File

@ -77,7 +77,24 @@ abstract class Spawnable extends Tile{
/** /**
* @return CompoundTag * @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 * Called when a player updates a block entity's NBT data

View File

@ -33,8 +33,6 @@ use pocketmine\level\format\Chunk;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\level\Position; use pocketmine\level\Position;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
abstract class Tile extends Position{ abstract class Tile extends Position{
@ -127,15 +125,15 @@ abstract class Tile extends Position{
$this->namedtag = $nbt; $this->namedtag = $nbt;
$this->server = $level->getServer(); $this->server = $level->getServer();
$this->setLevel($level); $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); assert($this->chunk !== null);
$this->name = ""; $this->name = "";
$this->lastUpdate = microtime(true); $this->lastUpdate = microtime(true);
$this->id = Tile::$tileCount++; $this->id = Tile::$tileCount++;
$this->x = (int) $this->namedtag["x"]; $this->x = $this->namedtag->x->getValue();
$this->y = (int) $this->namedtag["y"]; $this->y = $this->namedtag->y->getValue();
$this->z = (int) $this->namedtag["z"]; $this->z = $this->namedtag->z->getValue();
$this->chunk->addTile($this); $this->chunk->addTile($this);
$this->getLevel()->addTile($this); $this->getLevel()->addTile($this);
@ -147,10 +145,10 @@ abstract class Tile extends Position{
} }
public function saveNBT(){ public function saveNBT(){
$this->namedtag->id = new StringTag("id", $this->getSaveId()); $this->namedtag->id->setValue($this->getSaveId());
$this->namedtag->x = new IntTag("x", $this->x); $this->namedtag->x->setValue($this->x);
$this->namedtag->y = new IntTag("y", $this->y); $this->namedtag->y->setValue($this->y);
$this->namedtag->z = new IntTag("z", $this->z); $this->namedtag->z->setValue($this->z);
} }
public function getCleanedNBT(){ public function getCleanedNBT(){