diff --git a/src/pocketmine/tile/Banner.php b/src/pocketmine/tile/Banner.php index b1837aaad..dde23c8c2 100644 --- a/src/pocketmine/tile/Banner.php +++ b/src/pocketmine/tile/Banner.php @@ -33,7 +33,10 @@ use pocketmine\nbt\tag\StringTag; use pocketmine\Player; class Banner extends Spawnable implements Nameable{ - use NameableTrait; + use NameableTrait { + addAdditionalSpawnData as addNameSpawnData; + createAdditionalNBT as createNameNBT; + } public const TAG_BASE = "Base"; public const TAG_PATTERNS = "Patterns"; @@ -109,6 +112,7 @@ class Banner extends Spawnable implements Nameable{ public function addAdditionalSpawnData(CompoundTag $nbt) : void{ $nbt->setTag($this->namedtag->getTag(self::TAG_PATTERNS)); $nbt->setTag($this->namedtag->getTag(self::TAG_BASE)); + $this->addNameSpawnData($nbt); } /** @@ -265,9 +269,8 @@ class Banner extends Spawnable implements Nameable{ if($item->getNamedTag()->hasTag(self::TAG_PATTERNS, ListTag::class)){ $nbt->setTag($item->getNamedTag()->getListTag(self::TAG_PATTERNS)); } - if($item->hasCustomName()){ - $nbt->setString("CustomName", $item->getCustomName()); - } + + self::createNameNBT($nbt, $pos, $face, $item, $player); } } diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index da0bf1995..4f08f1ff3 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -26,14 +26,14 @@ namespace pocketmine\tile; use pocketmine\inventory\ChestInventory; use pocketmine\inventory\DoubleChestInventory; use pocketmine\inventory\InventoryHolder; -use pocketmine\item\Item; use pocketmine\level\Level; -use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; -use pocketmine\Player; class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ - use NameableTrait, ContainerTrait; + use NameableTrait { + addAdditionalSpawnData as addNameSpawnData; + } + use ContainerTrait; public const TAG_PAIRX = "pairx"; public const TAG_PAIRZ = "pairz"; @@ -184,14 +184,6 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ $nbt->setTag($this->namedtag->getTag(self::TAG_PAIRZ)); } - if($this->hasName()){ - $nbt->setTag($this->namedtag->getTag("CustomName")); - } - } - - protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ - if($item !== null and $item->hasCustomName()){ - $nbt->setString("CustomName", $item->getCustomName()); - } + $this->addNameSpawnData($nbt); } } diff --git a/src/pocketmine/tile/EnchantTable.php b/src/pocketmine/tile/EnchantTable.php index 0bfa36806..723018e69 100644 --- a/src/pocketmine/tile/EnchantTable.php +++ b/src/pocketmine/tile/EnchantTable.php @@ -23,11 +23,6 @@ declare(strict_types=1); namespace pocketmine\tile; -use pocketmine\item\Item; -use pocketmine\math\Vector3; -use pocketmine\nbt\tag\CompoundTag; -use pocketmine\Player; - class EnchantTable extends Spawnable implements Nameable{ use NameableTrait; @@ -37,16 +32,4 @@ class EnchantTable extends Spawnable implements Nameable{ public function getDefaultName() : string{ return "Enchanting Table"; } - - public function addAdditionalSpawnData(CompoundTag $nbt) : void{ - if($this->hasName()){ - $nbt->setTag($this->namedtag->getTag("CustomName")); - } - } - - protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ - if($item !== null and $item->hasCustomName()){ - $nbt->setString("CustomName", $item->getCustomName()); - } - } } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 56f3cb2e5..4c1b95373 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -33,13 +33,14 @@ use pocketmine\inventory\InventoryHolder; use pocketmine\item\Item; use pocketmine\item\ItemFactory; use pocketmine\level\Level; -use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\ContainerSetDataPacket; -use pocketmine\Player; class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ - use NameableTrait, ContainerTrait; + use NameableTrait { + addAdditionalSpawnData as addNameSpawnData; + } + use ContainerTrait; public const TAG_BURN_TIME = "BurnTime"; public const TAG_COOK_TIME = "CookTime"; @@ -230,14 +231,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ $nbt->setShort(self::TAG_BURN_TIME, $this->burnTime); $nbt->setShort(self::TAG_COOK_TIME, $this->cookTime); - if($this->hasName()){ - $nbt->setTag($this->namedtag->getTag("CustomName")); - } - } - - protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ - if($item !== null and $item->hasCustomName()){ - $nbt->setString("CustomName", $item->getCustomName()); - } + $this->addNameSpawnData($nbt); } } diff --git a/src/pocketmine/tile/Nameable.php b/src/pocketmine/tile/Nameable.php index 3be91b360..ab66b101c 100644 --- a/src/pocketmine/tile/Nameable.php +++ b/src/pocketmine/tile/Nameable.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\tile; interface Nameable{ + public const TAG_CUSTOM_NAME = "CustomName"; /** * @return string diff --git a/src/pocketmine/tile/NameableTrait.php b/src/pocketmine/tile/NameableTrait.php index 5b584d029..28ff2b260 100644 --- a/src/pocketmine/tile/NameableTrait.php +++ b/src/pocketmine/tile/NameableTrait.php @@ -23,7 +23,10 @@ declare(strict_types=1); namespace pocketmine\tile; +use pocketmine\item\Item; +use pocketmine\math\Vector3; use pocketmine\nbt\tag\CompoundTag; +use pocketmine\Player; /** * This trait implements most methods in the {@link Nameable} interface. It should only be used by Tiles. @@ -45,7 +48,7 @@ trait NameableTrait{ */ public function getName() : string{ $nbt = $this->getNBT(); - return $nbt->getString("CustomName") ?? $this->getDefaultName(); + return $nbt->getString(Nameable::TAG_CUSTOM_NAME) ?? $this->getDefaultName(); } /** @@ -54,17 +57,30 @@ trait NameableTrait{ public function setName(string $name) : void{ $nbt = $this->getNBT(); if($name === ""){ - $nbt->removeTag("CustomName"); + $nbt->removeTag(Nameable::TAG_CUSTOM_NAME); + return; } - $nbt->setString("CustomName", $name); + $nbt->setString(Nameable::TAG_CUSTOM_NAME, $name); } /** * @return bool */ public function hasName() : bool{ - return $this->getNBT()->hasTag("CustomName"); + return $this->getNBT()->hasTag(Nameable::TAG_CUSTOM_NAME); + } + + protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{ + if($item !== null and $item->hasCustomName()){ + $nbt->setString(Nameable::TAG_CUSTOM_NAME, $item->getCustomName()); + } + } + + public function addAdditionalSpawnData(CompoundTag $nbt) : void{ + if($this->hasName()){ + $nbt->setString(Nameable::TAG_CUSTOM_NAME, $this->getName()); + } } }