mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Tile: Improved Nameable and NameableTrait to cut down code duplication
This commit is contained in:
parent
b6b0bbde18
commit
1bb0337420
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\tile;
|
||||
|
||||
interface Nameable{
|
||||
public const TAG_CUSTOM_NAME = "CustomName";
|
||||
|
||||
/**
|
||||
* @return string
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user