Tile: Improved Nameable and NameableTrait to cut down code duplication

This commit is contained in:
Dylan K. Taylor 2018-06-02 15:17:32 +01:00
parent b6b0bbde18
commit 1bb0337420
6 changed files with 38 additions and 50 deletions

View File

@ -33,7 +33,10 @@ use pocketmine\nbt\tag\StringTag;
use pocketmine\Player; use pocketmine\Player;
class Banner extends Spawnable implements Nameable{ class Banner extends Spawnable implements Nameable{
use NameableTrait; use NameableTrait {
addAdditionalSpawnData as addNameSpawnData;
createAdditionalNBT as createNameNBT;
}
public const TAG_BASE = "Base"; public const TAG_BASE = "Base";
public const TAG_PATTERNS = "Patterns"; public const TAG_PATTERNS = "Patterns";
@ -109,6 +112,7 @@ class Banner extends Spawnable implements Nameable{
public function addAdditionalSpawnData(CompoundTag $nbt) : void{ public function addAdditionalSpawnData(CompoundTag $nbt) : void{
$nbt->setTag($this->namedtag->getTag(self::TAG_PATTERNS)); $nbt->setTag($this->namedtag->getTag(self::TAG_PATTERNS));
$nbt->setTag($this->namedtag->getTag(self::TAG_BASE)); $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)){ if($item->getNamedTag()->hasTag(self::TAG_PATTERNS, ListTag::class)){
$nbt->setTag($item->getNamedTag()->getListTag(self::TAG_PATTERNS)); $nbt->setTag($item->getNamedTag()->getListTag(self::TAG_PATTERNS));
} }
if($item->hasCustomName()){
$nbt->setString("CustomName", $item->getCustomName()); self::createNameNBT($nbt, $pos, $face, $item, $player);
}
} }
} }

View File

@ -26,14 +26,14 @@ namespace pocketmine\tile;
use pocketmine\inventory\ChestInventory; use pocketmine\inventory\ChestInventory;
use pocketmine\inventory\DoubleChestInventory; use pocketmine\inventory\DoubleChestInventory;
use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\Player;
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{ 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_PAIRX = "pairx";
public const TAG_PAIRZ = "pairz"; 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)); $nbt->setTag($this->namedtag->getTag(self::TAG_PAIRZ));
} }
if($this->hasName()){ $this->addNameSpawnData($nbt);
$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());
}
} }
} }

View File

@ -23,11 +23,6 @@ declare(strict_types=1);
namespace pocketmine\tile; 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{ class EnchantTable extends Spawnable implements Nameable{
use NameableTrait; use NameableTrait;
@ -37,16 +32,4 @@ class EnchantTable extends Spawnable implements Nameable{
public function getDefaultName() : string{ public function getDefaultName() : string{
return "Enchanting Table"; 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());
}
}
} }

View File

@ -33,13 +33,14 @@ use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket; use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
use pocketmine\Player;
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ 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_BURN_TIME = "BurnTime";
public const TAG_COOK_TIME = "CookTime"; 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_BURN_TIME, $this->burnTime);
$nbt->setShort(self::TAG_COOK_TIME, $this->cookTime); $nbt->setShort(self::TAG_COOK_TIME, $this->cookTime);
if($this->hasName()){ $this->addNameSpawnData($nbt);
$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());
}
} }
} }

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
interface Nameable{ interface Nameable{
public const TAG_CUSTOM_NAME = "CustomName";
/** /**
* @return string * @return string

View File

@ -23,7 +23,10 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag; 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. * 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{ public function getName() : string{
$nbt = $this->getNBT(); $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{ public function setName(string $name) : void{
$nbt = $this->getNBT(); $nbt = $this->getNBT();
if($name === ""){ if($name === ""){
$nbt->removeTag("CustomName"); $nbt->removeTag(Nameable::TAG_CUSTOM_NAME);
return; return;
} }
$nbt->setString("CustomName", $name); $nbt->setString(Nameable::TAG_CUSTOM_NAME, $name);
} }
/** /**
* @return bool * @return bool
*/ */
public function hasName() : 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());
}
} }
} }