mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-15 18:29:46 +00:00
Allow Tiles to decide how to copy data from an item
This commit is contained in:
parent
f6983efec1
commit
2c6381632c
@ -84,16 +84,7 @@ class StandingBanner extends Transparent{
|
||||
}
|
||||
|
||||
if($ret){
|
||||
/** @var TileBanner $tile */
|
||||
$tile = Tile::createFromItem(TileBanner::class, $this->getLevel(), $this->asVector3(), $item);
|
||||
if($item instanceof ItemBanner){
|
||||
$tile->setBaseColor($item->getBaseColor());
|
||||
if(($patterns = $item->getPatterns()) !== null){
|
||||
$tile->setPatterns($patterns);
|
||||
}
|
||||
}
|
||||
|
||||
$this->level->addTile($tile);
|
||||
$this->level->addTile(Tile::createFromItem(TileBanner::class, $this->getLevel(), $this->asVector3(), $item));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\tile;
|
||||
|
||||
use pocketmine\item\Banner as ItemBanner;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\level\Level;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
@ -33,6 +35,7 @@ use pocketmine\nbt\tag\StringTag;
|
||||
class Banner extends Spawnable implements Nameable{
|
||||
use NameableTrait {
|
||||
addAdditionalSpawnData as addNameSpawnData;
|
||||
copyDataFromItem as copyNameFromItem;
|
||||
}
|
||||
|
||||
public const TAG_BASE = "Base";
|
||||
@ -127,6 +130,17 @@ class Banner extends Spawnable implements Nameable{
|
||||
$this->addNameSpawnData($nbt);
|
||||
}
|
||||
|
||||
protected function copyDataFromItem(Item $item) : void{
|
||||
parent::copyDataFromItem($item);
|
||||
$this->copyNameFromItem($item);
|
||||
if($item instanceof ItemBanner){
|
||||
$this->setBaseColor($item->getBaseColor());
|
||||
if(($patterns = $item->getPatterns()) !== null){
|
||||
$this->setPatterns($patterns);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color of the banner base.
|
||||
*
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\tile;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\nbt\tag\StringTag;
|
||||
|
||||
@ -80,4 +81,15 @@ trait NameableTrait{
|
||||
$tag->setString(Nameable::TAG_CUSTOM_NAME, $this->customName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
* @see Tile::copyDataFromItem()
|
||||
*/
|
||||
protected function copyDataFromItem(Item $item) : void{
|
||||
parent::copyDataFromItem($item);
|
||||
if($item->hasCustomName()){ //this should take precedence over saved NBT
|
||||
$this->setName($item->getCustomName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,12 +105,7 @@ abstract class Tile extends Position{
|
||||
*/
|
||||
public static function createFromItem(string $baseClass, Level $level, Vector3 $pos, Item $item) : Tile{
|
||||
$tile = self::create($baseClass, $level, $pos);
|
||||
if($item->hasCustomBlockData()){
|
||||
$tile->readSaveData($item->getCustomBlockData());
|
||||
}
|
||||
if($tile instanceof Nameable and $item->hasCustomName()){ //this should take precedence over saved NBT
|
||||
$tile->setName($item->getCustomName());
|
||||
}
|
||||
$tile->copyDataFromItem($item);
|
||||
|
||||
return $tile;
|
||||
}
|
||||
@ -222,6 +217,12 @@ abstract class Tile extends Position{
|
||||
return $tag->getCount() > 0 ? $tag : null;
|
||||
}
|
||||
|
||||
protected function copyDataFromItem(Item $item) : void{
|
||||
if($item->hasCustomBlockData()){ //TODO: check item root tag (MCPE doesn't use BlockEntityTag)
|
||||
$this->readSaveData($item->getCustomBlockData());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Block
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user