Item: Remove get/set/removeNamedTagEntry()

This commit is contained in:
Dylan K. Taylor 2019-03-17 17:49:09 +00:00
parent 5830ca958b
commit 8de9e61651
6 changed files with 30 additions and 57 deletions

View File

@ -85,7 +85,7 @@ abstract class Armor extends Durable{
* @param Color $color * @param Color $color
*/ */
public function setCustomColor(Color $color) : void{ public function setCustomColor(Color $color) : void{
$this->setNamedTagEntry(new IntTag(self::TAG_CUSTOM_COLOR, Binary::signInt($color->toARGB()))); $this->getNamedTag()->setInt(self::TAG_CUSTOM_COLOR, Binary::signInt($color->toARGB()));
} }
/** /**

View File

@ -89,7 +89,7 @@ class Banner extends Item{
new IntTag(self::TAG_PATTERN_COLOR, $pattern->getColor()->getInvertedMagicNumber()) new IntTag(self::TAG_PATTERN_COLOR, $pattern->getColor()->getInvertedMagicNumber())
])); ]));
} }
$this->setNamedTagEntry($tag); $this->getNamedTag()->setTag($tag);
} }
public function getFuelTime() : int{ public function getFuelTime() : int{

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\item; namespace pocketmine\item;
use pocketmine\item\enchantment\Enchantment; use pocketmine\item\enchantment\Enchantment;
use pocketmine\nbt\tag\ByteTag;
use function lcg_value; use function lcg_value;
use function min; use function min;
@ -51,7 +50,7 @@ abstract class Durable extends Item{
* @param bool $value * @param bool $value
*/ */
public function setUnbreakable(bool $value = true) : void{ public function setUnbreakable(bool $value = true) : void{
$this->setNamedTagEntry(new ByteTag("Unbreakable", $value ? 1 : 0)); $this->getNamedTag()->setByte("Unbreakable", $value ? 1 : 0);
} }
/** /**

View File

@ -39,7 +39,6 @@ use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\ByteTag; use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\NamedTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\Player; use pocketmine\Player;
@ -186,11 +185,11 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasCustomBlockData() : bool{ public function hasCustomBlockData() : bool{
return $this->getNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG) instanceof CompoundTag; return $this->getNamedTag()->hasTag(self::TAG_BLOCK_ENTITY_TAG, CompoundTag::class);
} }
public function clearCustomBlockData(){ public function clearCustomBlockData(){
$this->removeNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG); $this->getNamedTag()->removeTag(self::TAG_BLOCK_ENTITY_TAG);
return $this; return $this;
} }
@ -202,7 +201,7 @@ class Item implements ItemIds, \JsonSerializable{
public function setCustomBlockData(CompoundTag $compound) : Item{ public function setCustomBlockData(CompoundTag $compound) : Item{
$tags = clone $compound; $tags = clone $compound;
$tags->setName(self::TAG_BLOCK_ENTITY_TAG); $tags->setName(self::TAG_BLOCK_ENTITY_TAG);
$this->setNamedTagEntry($tags); $this->getNamedTag()->setTag($tags);
return $this; return $this;
} }
@ -211,15 +210,14 @@ class Item implements ItemIds, \JsonSerializable{
* @return CompoundTag|null * @return CompoundTag|null
*/ */
public function getCustomBlockData() : ?CompoundTag{ public function getCustomBlockData() : ?CompoundTag{
$tag = $this->getNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG); return $this->getNamedTag()->getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
return $tag instanceof CompoundTag ? $tag : null;
} }
/** /**
* @return bool * @return bool
*/ */
public function hasEnchantments() : bool{ public function hasEnchantments() : bool{
return $this->getNamedTagEntry(self::TAG_ENCH) instanceof ListTag; return $this->getNamedTag()->hasTag(self::TAG_ENCH, ListTag::class);
} }
/** /**
@ -229,7 +227,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{ public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
$ench = $this->getNamedTagEntry(self::TAG_ENCH); $ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return false; return false;
} }
@ -251,7 +249,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return EnchantmentInstance|null * @return EnchantmentInstance|null
*/ */
public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{ public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
$ench = $this->getNamedTagEntry(self::TAG_ENCH); $ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return null; return null;
} }
@ -277,7 +275,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return Item * @return Item
*/ */
public function removeEnchantment(Enchantment $enchantment, int $level = -1) : Item{ public function removeEnchantment(Enchantment $enchantment, int $level = -1) : Item{
$ench = $this->getNamedTagEntry(self::TAG_ENCH); $ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return $this; return $this;
} }
@ -291,8 +289,6 @@ class Item implements ItemIds, \JsonSerializable{
} }
} }
$this->setNamedTagEntry($ench);
return $this; return $this;
} }
@ -300,7 +296,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return Item * @return Item
*/ */
public function removeEnchantments() : Item{ public function removeEnchantments() : Item{
$this->removeNamedTagEntry(self::TAG_ENCH); $this->getNamedTag()->removeTag(self::TAG_ENCH);
return $this; return $this;
} }
@ -313,7 +309,7 @@ class Item implements ItemIds, \JsonSerializable{
public function addEnchantment(EnchantmentInstance $enchantment) : Item{ public function addEnchantment(EnchantmentInstance $enchantment) : Item{
$found = false; $found = false;
$ench = $this->getNamedTagEntry(self::TAG_ENCH); $ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
$ench = new ListTag(self::TAG_ENCH, [], NBT::TAG_Compound); $ench = new ListTag(self::TAG_ENCH, [], NBT::TAG_Compound);
}else{ }else{
@ -337,7 +333,7 @@ class Item implements ItemIds, \JsonSerializable{
])); ]));
} }
$this->setNamedTagEntry($ench); $this->getNamedTag()->setTag($ench);
return $this; return $this;
} }
@ -349,7 +345,7 @@ class Item implements ItemIds, \JsonSerializable{
/** @var EnchantmentInstance[] $enchantments */ /** @var EnchantmentInstance[] $enchantments */
$enchantments = []; $enchantments = [];
$ench = $this->getNamedTagEntry(self::TAG_ENCH); $ench = $this->getNamedTag()->getListTag(self::TAG_ENCH);
if($ench instanceof ListTag){ if($ench instanceof ListTag){
/** @var CompoundTag $entry */ /** @var CompoundTag $entry */
foreach($ench as $entry){ foreach($ench as $entry){
@ -390,7 +386,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasCustomName() : bool{ public function hasCustomName() : bool{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
return $display->hasTag(self::TAG_DISPLAY_NAME); return $display->hasTag(self::TAG_DISPLAY_NAME);
} }
@ -402,7 +398,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return string * @return string
*/ */
public function getCustomName() : string{ public function getCustomName() : string{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
return $display->getString(self::TAG_DISPLAY_NAME, ""); return $display->getString(self::TAG_DISPLAY_NAME, "");
} }
@ -421,13 +417,13 @@ class Item implements ItemIds, \JsonSerializable{
} }
/** @var CompoundTag $display */ /** @var CompoundTag $display */
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if(!($display instanceof CompoundTag)){ if(!($display instanceof CompoundTag)){
$display = new CompoundTag(self::TAG_DISPLAY); $display = new CompoundTag(self::TAG_DISPLAY);
} }
$display->setString(self::TAG_DISPLAY_NAME, $name); $display->setString(self::TAG_DISPLAY_NAME, $name);
$this->setNamedTagEntry($display); $this->getNamedTag()->setTag($display);
return $this; return $this;
} }
@ -436,14 +432,14 @@ class Item implements ItemIds, \JsonSerializable{
* @return Item * @return Item
*/ */
public function clearCustomName() : Item{ public function clearCustomName() : Item{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
$display->removeTag(self::TAG_DISPLAY_NAME); $display->removeTag(self::TAG_DISPLAY_NAME);
if($display->getCount() === 0){ if($display->getCount() === 0){
$this->removeNamedTagEntry($display->getName()); $this->getNamedTag()->removeTag(self::TAG_DISPLAY);
}else{ }else{
$this->setNamedTagEntry($display); $this->getNamedTag()->setTag($display);
} }
} }
@ -454,7 +450,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return string[] * @return string[]
*/ */
public function getLore() : array{ public function getLore() : array{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if($display instanceof CompoundTag and ($lore = $display->getListTag(self::TAG_DISPLAY_LORE)) !== null){ if($display instanceof CompoundTag and ($lore = $display->getListTag(self::TAG_DISPLAY_LORE)) !== null){
return $lore->getAllValues(); return $lore->getAllValues();
} }
@ -468,7 +464,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return Item * @return Item
*/ */
public function setLore(array $lines) : Item{ public function setLore(array $lines) : Item{
$display = $this->getNamedTagEntry(self::TAG_DISPLAY); $display = $this->getNamedTag()->getCompoundTag(self::TAG_DISPLAY);
if(!($display instanceof CompoundTag)){ if(!($display instanceof CompoundTag)){
$display = new CompoundTag(self::TAG_DISPLAY, []); $display = new CompoundTag(self::TAG_DISPLAY, []);
} }
@ -477,32 +473,11 @@ class Item implements ItemIds, \JsonSerializable{
return new StringTag("", $str); return new StringTag("", $str);
}, $lines), NBT::TAG_String)); }, $lines), NBT::TAG_String));
$this->setNamedTagEntry($display); $this->getNamedTag()->setTag($display);
return $this; return $this;
} }
/**
* @param string $name
*
* @return NamedTag|null
*/
public function getNamedTagEntry(string $name) : ?NamedTag{
return $this->getNamedTag()->getTag($name);
}
public function setNamedTagEntry(NamedTag $new) : void{
$tag = $this->getNamedTag();
$tag->setTag($new);
$this->setNamedTag($tag);
}
public function removeNamedTagEntry(string $name) : void{
$tag = $this->getNamedTag();
$tag->removeTag($name);
$this->setNamedTag($tag);
}
/** /**
* Returns whether this Item has a non-empty NBT. * Returns whether this Item has a non-empty NBT.
* @return bool * @return bool

View File

@ -91,7 +91,7 @@ class WritableBook extends Item{
$page = $pagesTag->get($pageId); $page = $pagesTag->get($pageId);
$page->setString(self::TAG_PAGE_TEXT, $pageText); $page->setString(self::TAG_PAGE_TEXT, $pageText);
$this->setNamedTagEntry($pagesTag); $this->getNamedTag()->setTag($pagesTag);
return $created; return $created;
} }
@ -116,7 +116,7 @@ class WritableBook extends Item{
])); ]));
} }
$this->setNamedTagEntry($pagesTag); $this->getNamedTag()->setTag($pagesTag);
} }
/** /**
@ -129,7 +129,6 @@ class WritableBook extends Item{
public function deletePage(int $pageId) : bool{ public function deletePage(int $pageId) : bool{
$pagesTag = $this->getPagesTag(); $pagesTag = $this->getPagesTag();
$pagesTag->remove($pageId); $pagesTag->remove($pageId);
$this->setNamedTagEntry($pagesTag);
return true; return true;
} }
@ -150,7 +149,7 @@ class WritableBook extends Item{
new StringTag(self::TAG_PAGE_PHOTONAME, "") new StringTag(self::TAG_PAGE_PHOTONAME, "")
])); ]));
$this->setNamedTagEntry($pagesTag); $this->getNamedTag()->setTag($pagesTag);
return true; return true;
} }

View File

@ -1715,7 +1715,7 @@ class Level implements ChunkManager, Metadatable{
} }
if($player->isAdventure(true) and !$ev->isCancelled()){ if($player->isAdventure(true) and !$ev->isCancelled()){
$tag = $item->getNamedTagEntry("CanDestroy"); $tag = $item->getNamedTag()->getListTag("CanDestroy");
$canBreak = false; $canBreak = false;
if($tag instanceof ListTag){ if($tag instanceof ListTag){
foreach($tag as $v){ foreach($tag as $v){
@ -1865,7 +1865,7 @@ class Level implements ChunkManager, Metadatable{
$ev = new BlockPlaceEvent($player, $hand, $blockReplace, $blockClicked, $item); $ev = new BlockPlaceEvent($player, $hand, $blockReplace, $blockClicked, $item);
if($player->isAdventure(true) and !$ev->isCancelled()){ if($player->isAdventure(true) and !$ev->isCancelled()){
$canPlace = false; $canPlace = false;
$tag = $item->getNamedTagEntry("CanPlaceOn"); $tag = $item->getNamedTag()->getListTag("CanPlaceOn");
if($tag instanceof ListTag){ if($tag instanceof ListTag){
foreach($tag as $v){ foreach($tag as $v){
if($v instanceof StringTag){ if($v instanceof StringTag){