Tile: make ContainerTrait and NameableTrait non-dependent on context-retained NBT

This commit is contained in:
Dylan K. Taylor
2018-06-03 16:32:05 +01:00
parent 7b7917939a
commit b1cb63ebd6
6 changed files with 53 additions and 30 deletions

View File

@ -26,50 +26,44 @@ namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
/**
* This trait implements most methods in the {@link Nameable} interface. It should only be used by Tiles.
*/
trait NameableTrait{
/** @var string|null */
private $customName;
/**
* @return string
*/
abstract public function getDefaultName() : string;
/**
* @return CompoundTag
*/
abstract public function getNBT() : CompoundTag;
/**
* @return string
*/
public function getName() : string{
$nbt = $this->getNBT();
return $nbt->getString(Nameable::TAG_CUSTOM_NAME) ?? $this->getDefaultName();
return $this->customName ?? $this->getDefaultName();
}
/**
* @param string $name
*/
public function setName(string $name) : void{
$nbt = $this->getNBT();
if($name === ""){
$nbt->removeTag(Nameable::TAG_CUSTOM_NAME);
return;
$this->customName = null;
}else{
$this->customName = $name;
}
$nbt->setString(Nameable::TAG_CUSTOM_NAME, $name);
}
/**
* @return bool
*/
public function hasName() : bool{
return $this->getNBT()->hasTag(Nameable::TAG_CUSTOM_NAME);
return $this->customName !== null;
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
@ -79,8 +73,20 @@ trait NameableTrait{
}
public function addAdditionalSpawnData(CompoundTag $nbt) : void{
if($this->hasName()){
$nbt->setString(Nameable::TAG_CUSTOM_NAME, $this->getName());
if($this->customName !== null){
$nbt->setString(Nameable::TAG_CUSTOM_NAME, $this->customName);
}
}
protected function loadName(CompoundTag $tag) : void{
if($tag->hasTag(Nameable::TAG_CUSTOM_NAME, StringTag::class)){
$this->customName = $tag->getString(Nameable::TAG_CUSTOM_NAME);
}
}
protected function saveName(CompoundTag $tag) : void{
if($this->customName !== null){
$tag->setString(Nameable::TAG_CUSTOM_NAME, $this->customName);
}
}
}