From 95fa1824c8a4a6e5e24ad0b3b3591ab4d3410acc Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 16 Oct 2017 12:32:10 +0100 Subject: [PATCH] Use a trait for nameable tiles instead of repeating code --- src/pocketmine/tile/Chest.php | 24 ++------- src/pocketmine/tile/EnchantTable.php | 22 +++------ src/pocketmine/tile/Furnace.php | 22 +++------ src/pocketmine/tile/Nameable.php | 4 ++ src/pocketmine/tile/NameableTrait.php | 71 +++++++++++++++++++++++++++ src/pocketmine/tile/Tile.php | 6 ++- 6 files changed, 96 insertions(+), 53 deletions(-) create mode 100644 src/pocketmine/tile/NameableTrait.php diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index f9834d2ed..d9eb9da0a 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -38,6 +38,7 @@ use pocketmine\nbt\tag\StringTag; use pocketmine\Player; class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ + use NameableTrait; /** @var ChestInventory */ protected $inventory; @@ -188,27 +189,8 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ /** * @return string */ - public function getName() : string{ - return isset($this->namedtag->CustomName) ? $this->namedtag->CustomName->getValue() : "Chest"; - } - - /** - * @return bool - */ - public function hasName() : bool{ - return isset($this->namedtag->CustomName); - } - - /** - * @param string $str - */ - public function setName(string $str){ - if($str === ""){ - unset($this->namedtag->CustomName); - return; - } - - $this->namedtag->CustomName = new StringTag("CustomName", $str); + public function getDefaultName() : string{ + return "Chest"; } public function isPaired(){ diff --git a/src/pocketmine/tile/EnchantTable.php b/src/pocketmine/tile/EnchantTable.php index 7ddb8b1c5..863e02c85 100644 --- a/src/pocketmine/tile/EnchantTable.php +++ b/src/pocketmine/tile/EnchantTable.php @@ -30,23 +30,13 @@ use pocketmine\nbt\tag\StringTag; use pocketmine\Player; class EnchantTable extends Spawnable implements Nameable{ + use NameableTrait; - - public function getName() : string{ - return isset($this->namedtag->CustomName) ? $this->namedtag->CustomName->getValue() : "Enchanting Table"; - } - - public function hasName() : bool{ - return isset($this->namedtag->CustomName); - } - - public function setName(string $str){ - if($str === ""){ - unset($this->namedtag->CustomName); - return; - } - - $this->namedtag->CustomName = new StringTag("CustomName", $str); + /** + * @return string + */ + public function getDefaultName() : string{ + return "Enchanting Table"; } public function addAdditionalSpawnData(CompoundTag $nbt){ diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index 1aa8af256..13b33d4c9 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -43,6 +43,8 @@ use pocketmine\network\mcpe\protocol\ContainerSetDataPacket; use pocketmine\Player; class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ + use NameableTrait; + /** @var FurnaceInventory */ protected $inventory; @@ -78,21 +80,11 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ } } - public function getName() : string{ - return isset($this->namedtag->CustomName) ? $this->namedtag->CustomName->getValue() : "Furnace"; - } - - public function hasName() : bool{ - return isset($this->namedtag->CustomName); - } - - public function setName(string $str){ - if($str === ""){ - unset($this->namedtag->CustomName); - return; - } - - $this->namedtag->CustomName = new StringTag("CustomName", $str); + /** + * @return string + */ + public function getDefaultName() : string{ + return "Furnace"; } public function close(){ diff --git a/src/pocketmine/tile/Nameable.php b/src/pocketmine/tile/Nameable.php index 4008705ee..3be91b360 100644 --- a/src/pocketmine/tile/Nameable.php +++ b/src/pocketmine/tile/Nameable.php @@ -25,6 +25,10 @@ namespace pocketmine\tile; interface Nameable{ + /** + * @return string + */ + public function getDefaultName() : string; /** * @return string diff --git a/src/pocketmine/tile/NameableTrait.php b/src/pocketmine/tile/NameableTrait.php new file mode 100644 index 000000000..0777e77e4 --- /dev/null +++ b/src/pocketmine/tile/NameableTrait.php @@ -0,0 +1,71 @@ +getNBT(); + return isset($nbt->CustomName) ? ((string) $nbt->CustomName->getValue()) : $this->getDefaultName(); + } + + /** + * @param string $name + */ + public function setName(string $name) : void{ + $nbt = $this->getNBT(); + if($name === ""){ + unset($nbt->CustomName); + return; + } + + $nbt->CustomName = new StringTag("CustomName", $name); + } + + /** + * @return bool + */ + public function hasName() : bool{ + return isset($this->getNBT()->CustomName); + } +} \ No newline at end of file diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index c011fb73c..17e32374e 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -163,6 +163,10 @@ abstract class Tile extends Position{ $this->namedtag->z->setValue($this->z); } + public function getNBT() : CompoundTag{ + return $this->namedtag; + } + public function getCleanedNBT(){ $this->saveNBT(); $tag = clone $this->namedtag; @@ -264,7 +268,7 @@ abstract class Tile extends Position{ } } - public function getName(){ + public function getName() : string{ return $this->name; }