mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 16:29:40 +00:00
Use DyeColor instead of ints for banners
This commit is contained in:
parent
01e7ebeb5c
commit
55be0716d8
@ -104,7 +104,7 @@ class StandingBanner extends Transparent{
|
|||||||
public function getDropsForCompatibleTool(Item $item) : array{
|
public function getDropsForCompatibleTool(Item $item) : array{
|
||||||
$tile = $this->level->getTile($this);
|
$tile = $this->level->getTile($this);
|
||||||
|
|
||||||
$drop = ItemFactory::get(Item::BANNER, ($tile instanceof TileBanner ? $tile->getBaseColor() : 0));
|
$drop = ItemFactory::get(Item::BANNER, ($tile instanceof TileBanner ? $tile->getBaseColor()->getInvertedMagicNumber() : 0));
|
||||||
if($tile instanceof TileBanner and $drop instanceof ItemBanner and !($patterns = $tile->getPatterns())->empty()){
|
if($tile instanceof TileBanner and $drop instanceof ItemBanner and !($patterns = $tile->getPatterns())->empty()){
|
||||||
$drop->setPatterns($patterns);
|
$drop->setPatterns($patterns);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ namespace pocketmine\item;
|
|||||||
|
|
||||||
use pocketmine\block\Block;
|
use pocketmine\block\Block;
|
||||||
use pocketmine\block\BlockFactory;
|
use pocketmine\block\BlockFactory;
|
||||||
|
use pocketmine\block\utils\DyeColor;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use pocketmine\nbt\tag\IntTag;
|
use pocketmine\nbt\tag\IntTag;
|
||||||
use pocketmine\nbt\tag\ListTag;
|
use pocketmine\nbt\tag\ListTag;
|
||||||
@ -53,21 +54,21 @@ class Banner extends Item{
|
|||||||
/**
|
/**
|
||||||
* Returns the color of the banner base.
|
* Returns the color of the banner base.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return DyeColor
|
||||||
*/
|
*/
|
||||||
public function getBaseColor() : int{
|
public function getBaseColor() : DyeColor{
|
||||||
return $this->getNamedTag()->getInt(self::TAG_BASE, $this->getDamage());
|
return DyeColor::fromMagicNumber($this->getNamedTag()->getInt(self::TAG_BASE, $this->getDamage()), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the color of the banner base.
|
* Sets the color of the banner base.
|
||||||
* Banner items have to be resent to see the changes in the inventory.
|
* Banner items have to be resent to see the changes in the inventory.
|
||||||
*
|
*
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*/
|
*/
|
||||||
public function setBaseColor(int $color) : void{
|
public function setBaseColor(DyeColor $color) : void{
|
||||||
$namedTag = $this->getNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
$namedTag->setInt(self::TAG_BASE, $color & 0x0f);
|
$namedTag->setInt(self::TAG_BASE, $color->getInvertedMagicNumber());
|
||||||
$this->setNamedTag($namedTag);
|
$this->setNamedTag($namedTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,17 +76,17 @@ class Banner extends Item{
|
|||||||
* Applies a new pattern on the banner with the given color.
|
* Applies a new pattern on the banner with the given color.
|
||||||
* Banner items have to be resent to see the changes in the inventory.
|
* Banner items have to be resent to see the changes in the inventory.
|
||||||
*
|
*
|
||||||
* @param string $pattern
|
* @param string $pattern
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*
|
*
|
||||||
* @return int ID of pattern.
|
* @return int ID of pattern.
|
||||||
*/
|
*/
|
||||||
public function addPattern(string $pattern, int $color) : int{
|
public function addPattern(string $pattern, DyeColor $color) : int{
|
||||||
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
|
$patternsTag = $this->getNamedTag()->getListTag(self::TAG_PATTERNS);
|
||||||
assert($patternsTag !== null);
|
assert($patternsTag !== null);
|
||||||
|
|
||||||
$patternsTag->push(new CompoundTag("", [
|
$patternsTag->push(new CompoundTag("", [
|
||||||
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
|
new IntTag(self::TAG_PATTERN_COLOR, $color->getInvertedMagicNumber()),
|
||||||
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
@ -124,7 +125,7 @@ class Banner extends Item{
|
|||||||
assert($pattern instanceof CompoundTag);
|
assert($pattern instanceof CompoundTag);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
self::TAG_PATTERN_COLOR => $pattern->getInt(self::TAG_PATTERN_COLOR),
|
self::TAG_PATTERN_COLOR => DyeColor::fromMagicNumber($pattern->getInt(self::TAG_PATTERN_COLOR), true),
|
||||||
self::TAG_PATTERN_NAME => $pattern->getString(self::TAG_PATTERN_NAME)
|
self::TAG_PATTERN_NAME => $pattern->getString(self::TAG_PATTERN_NAME)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -133,13 +134,13 @@ class Banner extends Item{
|
|||||||
* Changes the pattern of a previously existing pattern.
|
* Changes the pattern of a previously existing pattern.
|
||||||
* Banner items have to be resent to see the changes in the inventory.
|
* Banner items have to be resent to see the changes in the inventory.
|
||||||
*
|
*
|
||||||
* @param int $patternId
|
* @param int $patternId
|
||||||
* @param string $pattern
|
* @param string $pattern
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*
|
*
|
||||||
* @return bool indicating success.
|
* @return bool indicating success.
|
||||||
*/
|
*/
|
||||||
public function changePattern(int $patternId, string $pattern, int $color) : bool{
|
public function changePattern(int $patternId, string $pattern, DyeColor $color) : bool{
|
||||||
if(!$this->patternExists($patternId)){
|
if(!$this->patternExists($patternId)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -148,7 +149,7 @@ class Banner extends Item{
|
|||||||
assert($patternsTag !== null);
|
assert($patternsTag !== null);
|
||||||
|
|
||||||
$patternsTag->set($patternId, new CompoundTag("", [
|
$patternsTag->set($patternId, new CompoundTag("", [
|
||||||
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
|
new IntTag(self::TAG_PATTERN_COLOR, $color->getInvertedMagicNumber()),
|
||||||
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
@ -224,7 +225,7 @@ class Banner extends Item{
|
|||||||
public function correctNBT() : void{
|
public function correctNBT() : void{
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
if(!$tag->hasTag(self::TAG_BASE, IntTag::class)){
|
if(!$tag->hasTag(self::TAG_BASE, IntTag::class)){
|
||||||
$tag->setInt(self::TAG_BASE, 0);
|
$tag->setInt(self::TAG_BASE, DyeColor::$BLACK->getInvertedMagicNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$tag->hasTag(self::TAG_PATTERNS, ListTag::class)){
|
if(!$tag->hasTag(self::TAG_PATTERNS, ListTag::class)){
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\tile;
|
namespace pocketmine\tile;
|
||||||
|
|
||||||
|
use pocketmine\block\utils\DyeColor;
|
||||||
use pocketmine\item\Banner as ItemBanner;
|
use pocketmine\item\Banner as ItemBanner;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\level\Level;
|
use pocketmine\level\Level;
|
||||||
@ -83,25 +84,8 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
public const PATTERN_FLOWER = "flo";
|
public const PATTERN_FLOWER = "flo";
|
||||||
public const PATTERN_MOJANG = "moj";
|
public const PATTERN_MOJANG = "moj";
|
||||||
|
|
||||||
public const COLOR_BLACK = 0;
|
/** @var DyeColor */
|
||||||
public const COLOR_RED = 1;
|
private $baseColor;
|
||||||
public const COLOR_GREEN = 2;
|
|
||||||
public const COLOR_BROWN = 3;
|
|
||||||
public const COLOR_BLUE = 4;
|
|
||||||
public const COLOR_PURPLE = 5;
|
|
||||||
public const COLOR_CYAN = 6;
|
|
||||||
public const COLOR_LIGHT_GRAY = 7;
|
|
||||||
public const COLOR_GRAY = 8;
|
|
||||||
public const COLOR_PINK = 9;
|
|
||||||
public const COLOR_LIME = 10;
|
|
||||||
public const COLOR_YELLOW = 11;
|
|
||||||
public const COLOR_LIGHT_BLUE = 12;
|
|
||||||
public const COLOR_MAGENTA = 13;
|
|
||||||
public const COLOR_ORANGE = 14;
|
|
||||||
public const COLOR_WHITE = 15;
|
|
||||||
|
|
||||||
/** @var int */
|
|
||||||
private $baseColor = self::COLOR_BLACK;
|
|
||||||
/**
|
/**
|
||||||
* @var ListTag
|
* @var ListTag
|
||||||
* TODO: break this down further and remove runtime NBT from here entirely
|
* TODO: break this down further and remove runtime NBT from here entirely
|
||||||
@ -109,24 +93,28 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
private $patterns;
|
private $patterns;
|
||||||
|
|
||||||
public function __construct(Level $level, Vector3 $pos){
|
public function __construct(Level $level, Vector3 $pos){
|
||||||
|
$this->baseColor = DyeColor::$BLACK;
|
||||||
$this->patterns = new ListTag(self::TAG_PATTERNS);
|
$this->patterns = new ListTag(self::TAG_PATTERNS);
|
||||||
parent::__construct($level, $pos);
|
parent::__construct($level, $pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readSaveData(CompoundTag $nbt) : void{
|
public function readSaveData(CompoundTag $nbt) : void{
|
||||||
$this->baseColor = $nbt->getInt(self::TAG_BASE, $this->baseColor, true);
|
if($nbt->hasTag(self::TAG_BASE, IntTag::class)){
|
||||||
|
$this->baseColor = DyeColor::fromMagicNumber($nbt->getInt(self::TAG_BASE), true);
|
||||||
|
}
|
||||||
|
|
||||||
$this->patterns = $nbt->getListTag(self::TAG_PATTERNS) ?? $this->patterns;
|
$this->patterns = $nbt->getListTag(self::TAG_PATTERNS) ?? $this->patterns;
|
||||||
$this->loadName($nbt);
|
$this->loadName($nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function writeSaveData(CompoundTag $nbt) : void{
|
protected function writeSaveData(CompoundTag $nbt) : void{
|
||||||
$nbt->setInt(self::TAG_BASE, $this->baseColor);
|
$nbt->setInt(self::TAG_BASE, $this->baseColor->getInvertedMagicNumber());
|
||||||
$nbt->setTag($this->patterns);
|
$nbt->setTag($this->patterns);
|
||||||
$this->saveName($nbt);
|
$this->saveName($nbt);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
|
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
|
||||||
$nbt->setInt(self::TAG_BASE, $this->baseColor);
|
$nbt->setInt(self::TAG_BASE, $this->baseColor->getInvertedMagicNumber());
|
||||||
$nbt->setTag($this->patterns);
|
$nbt->setTag($this->patterns);
|
||||||
$this->addNameSpawnData($nbt);
|
$this->addNameSpawnData($nbt);
|
||||||
}
|
}
|
||||||
@ -145,18 +133,18 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
/**
|
/**
|
||||||
* Returns the color of the banner base.
|
* Returns the color of the banner base.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return DyeColor
|
||||||
*/
|
*/
|
||||||
public function getBaseColor() : int{
|
public function getBaseColor() : DyeColor{
|
||||||
return $this->baseColor;
|
return $this->baseColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the color of the banner base.
|
* Sets the color of the banner base.
|
||||||
*
|
*
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*/
|
*/
|
||||||
public function setBaseColor(int $color) : void{
|
public function setBaseColor(DyeColor $color) : void{
|
||||||
$this->baseColor = $color;
|
$this->baseColor = $color;
|
||||||
$this->onChanged();
|
$this->onChanged();
|
||||||
}
|
}
|
||||||
@ -164,14 +152,14 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
/**
|
/**
|
||||||
* Applies a new pattern on the banner with the given color.
|
* Applies a new pattern on the banner with the given color.
|
||||||
*
|
*
|
||||||
* @param string $pattern
|
* @param string $pattern
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*
|
*
|
||||||
* @return int ID of pattern.
|
* @return int ID of pattern.
|
||||||
*/
|
*/
|
||||||
public function addPattern(string $pattern, int $color) : int{
|
public function addPattern(string $pattern, DyeColor $color) : int{
|
||||||
$this->patterns->push(new CompoundTag("", [
|
$this->patterns->push(new CompoundTag("", [
|
||||||
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
|
new IntTag(self::TAG_PATTERN_COLOR, $color->getInvertedMagicNumber()),
|
||||||
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
@ -206,7 +194,7 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
assert($patternTag instanceof CompoundTag);
|
assert($patternTag instanceof CompoundTag);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
self::TAG_PATTERN_COLOR => $patternTag->getInt(self::TAG_PATTERN_COLOR),
|
self::TAG_PATTERN_COLOR => DyeColor::fromMagicNumber($patternTag->getInt(self::TAG_PATTERN_COLOR), true),
|
||||||
self::TAG_PATTERN_NAME => $patternTag->getString(self::TAG_PATTERN_NAME)
|
self::TAG_PATTERN_NAME => $patternTag->getString(self::TAG_PATTERN_NAME)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -214,19 +202,19 @@ class Banner extends Spawnable implements Nameable{
|
|||||||
/**
|
/**
|
||||||
* Changes the pattern of a previously existing pattern.
|
* Changes the pattern of a previously existing pattern.
|
||||||
*
|
*
|
||||||
* @param int $patternId
|
* @param int $patternId
|
||||||
* @param string $pattern
|
* @param string $pattern
|
||||||
* @param int $color
|
* @param DyeColor $color
|
||||||
*
|
*
|
||||||
* @return bool indicating success.
|
* @return bool indicating success.
|
||||||
*/
|
*/
|
||||||
public function changePattern(int $patternId, string $pattern, int $color) : bool{
|
public function changePattern(int $patternId, string $pattern, DyeColor $color) : bool{
|
||||||
if(!$this->patternExists($patternId)){
|
if(!$this->patternExists($patternId)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->patterns->set($patternId, new CompoundTag("", [
|
$this->patterns->set($patternId, new CompoundTag("", [
|
||||||
new IntTag(self::TAG_PATTERN_COLOR, $color & 0x0f),
|
new IntTag(self::TAG_PATTERN_COLOR, $color->getInvertedMagicNumber()),
|
||||||
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
new StringTag(self::TAG_PATTERN_NAME, $pattern)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user