mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 16:51:42 +00:00
Use a trait for nameable tiles instead of repeating code
This commit is contained in:
parent
251d5d7946
commit
95fa1824c8
@ -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(){
|
||||
|
@ -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){
|
||||
|
@ -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(){
|
||||
|
@ -25,6 +25,10 @@ namespace pocketmine\tile;
|
||||
|
||||
interface Nameable{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultName() : string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
|
71
src/pocketmine/tile/NameableTrait.php
Normal file
71
src/pocketmine/tile/NameableTrait.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\tile;
|
||||
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
|
||||
/**
|
||||
* This trait implements most methods in the {@link Nameable} interface. It should only be used by Tiles.
|
||||
*/
|
||||
trait NameableTrait{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getDefaultName() : string;
|
||||
|
||||
/**
|
||||
* @return CompoundTag
|
||||
*/
|
||||
abstract public function getNBT() : CompoundTag;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName() : string{
|
||||
$nbt = $this->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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user