Added some constants for frequently-accessed item NBT tags

This commit is contained in:
Dylan K. Taylor
2017-10-16 10:24:32 +01:00
parent 8c6ab3e634
commit 72531209bf

View File

@ -46,6 +46,14 @@ use pocketmine\utils\Binary;
use pocketmine\utils\Config; use pocketmine\utils\Config;
class Item implements ItemIds, \JsonSerializable{ class Item implements ItemIds, \JsonSerializable{
const TAG_ENCH = "ench";
const TAG_DISPLAY = "display";
const TAG_BLOCK_ENTITY_TAG = "BlockEntityTag";
const TAG_DISPLAY_NAME = "Name";
const TAG_DISPLAY_LORE = "Lore";
/** @var NBT */ /** @var NBT */
private static $cachedParser = null; private static $cachedParser = null;
@ -241,11 +249,11 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasCustomBlockData() : bool{ public function hasCustomBlockData() : bool{
return $this->getNamedTagEntry("BlockEntityTag") instanceof CompoundTag; return $this->getNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG) instanceof CompoundTag;
} }
public function clearCustomBlockData(){ public function clearCustomBlockData(){
$this->removeNamedTagEntry("BlockEntityTag"); $this->removeNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG);
return $this; return $this;
} }
@ -256,7 +264,7 @@ class Item implements ItemIds, \JsonSerializable{
*/ */
public function setCustomBlockData(CompoundTag $compound){ public function setCustomBlockData(CompoundTag $compound){
$tags = clone $compound; $tags = clone $compound;
$tags->setName("BlockEntityTag"); $tags->setName(self::TAG_BLOCK_ENTITY_TAG);
$this->setNamedTagEntry($tags); $this->setNamedTagEntry($tags);
return $this; return $this;
@ -266,7 +274,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return CompoundTag|null * @return CompoundTag|null
*/ */
public function getCustomBlockData(){ public function getCustomBlockData(){
$tag = $this->getNamedTagEntry("BlockEntityTag"); $tag = $this->getNamedTagEntry(self::TAG_BLOCK_ENTITY_TAG);
return $tag instanceof CompoundTag ? $tag : null; return $tag instanceof CompoundTag ? $tag : null;
} }
@ -274,7 +282,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasEnchantments() : bool{ public function hasEnchantments() : bool{
return $this->getNamedTagEntry("ench") instanceof ListTag; return $this->getNamedTagEntry(self::TAG_ENCH) instanceof ListTag;
} }
/** /**
@ -284,7 +292,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasEnchantment(int $id, int $level = -1) : bool{ public function hasEnchantment(int $id, int $level = -1) : bool{
$ench = $this->getNamedTagEntry("ench"); $ench = $this->getNamedTagEntry(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return false; return false;
} }
@ -305,7 +313,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return Enchantment|null * @return Enchantment|null
*/ */
public function getEnchantment(int $id){ public function getEnchantment(int $id){
$ench = $this->getNamedTagEntry("ench"); $ench = $this->getNamedTagEntry(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return null; return null;
} }
@ -329,7 +337,7 @@ class Item implements ItemIds, \JsonSerializable{
* @param int $level * @param int $level
*/ */
public function removeEnchantment(int $id, int $level = -1){ public function removeEnchantment(int $id, int $level = -1){
$ench = $this->getNamedTagEntry("ench"); $ench = $this->getNamedTagEntry(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
return; return;
} }
@ -346,7 +354,7 @@ class Item implements ItemIds, \JsonSerializable{
} }
public function removeEnchantments(){ public function removeEnchantments(){
$this->removeNamedTagEntry("ench"); $this->removeNamedTagEntry(self::TAG_ENCH);
} }
/** /**
@ -355,9 +363,9 @@ class Item implements ItemIds, \JsonSerializable{
public function addEnchantment(Enchantment $enchantment){ public function addEnchantment(Enchantment $enchantment){
$found = false; $found = false;
$ench = $this->getNamedTagEntry("ench"); $ench = $this->getNamedTagEntry(self::TAG_ENCH);
if(!($ench instanceof ListTag)){ if(!($ench instanceof ListTag)){
$ench = new ListTag("ench", [], NBT::TAG_Compound); $ench = new ListTag(self::TAG_ENCH, [], NBT::TAG_Compound);
}else{ }else{
/** @var CompoundTag $entry */ /** @var CompoundTag $entry */
foreach($ench as $k => $entry){ foreach($ench as $k => $entry){
@ -389,7 +397,7 @@ class Item implements ItemIds, \JsonSerializable{
/** @var Enchantment[] $enchantments */ /** @var Enchantment[] $enchantments */
$enchantments = []; $enchantments = [];
$ench = $this->getNamedTagEntry("ench"); $ench = $this->getNamedTagEntry(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){
@ -408,9 +416,9 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
public function hasCustomName() : bool{ public function hasCustomName() : bool{
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
return $display->hasTag("Name"); return $display->hasTag(self::TAG_DISPLAY_NAME);
} }
return false; return false;
@ -420,9 +428,9 @@ class Item implements ItemIds, \JsonSerializable{
* @return string * @return string
*/ */
public function getCustomName() : string{ public function getCustomName() : string{
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
return $display->getString("Name") ?? ""; return $display->getString(self::TAG_DISPLAY_NAME) ?? "";
} }
return ""; return "";
@ -439,12 +447,12 @@ class Item implements ItemIds, \JsonSerializable{
} }
/** @var CompoundTag $display */ /** @var CompoundTag $display */
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if(!($display instanceof CompoundTag)){ if(!($display instanceof CompoundTag)){
$display = new CompoundTag("display"); $display = new CompoundTag(self::TAG_DISPLAY);
} }
$display->setString("Name", $name); $display->setString(self::TAG_DISPLAY_NAME, $name);
$this->setNamedTagEntry($display); $this->setNamedTagEntry($display);
return $this; return $this;
@ -454,9 +462,9 @@ class Item implements ItemIds, \JsonSerializable{
* @return $this * @return $this
*/ */
public function clearCustomName(){ public function clearCustomName(){
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if($display instanceof CompoundTag){ if($display instanceof CompoundTag){
$display->removeTag("Name"); $display->removeTag(self::TAG_DISPLAY_NAME);
if($display->getCount() === 0){ if($display->getCount() === 0){
$this->removeNamedTagEntry($display->getName()); $this->removeNamedTagEntry($display->getName());
@ -472,8 +480,8 @@ class Item implements ItemIds, \JsonSerializable{
* @return string[] * @return string[]
*/ */
public function getLore() : array{ public function getLore() : array{
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if($display instanceof CompoundTag and ($lore = $display->getListTag("Lore")) !== null){ if($display instanceof CompoundTag and ($lore = $display->getListTag(self::TAG_DISPLAY_LORE)) !== null){
return array_map(function(StringTag $tag) : string{ return array_map(function(StringTag $tag) : string{
return $tag->getValue(); return $tag->getValue();
}, $lore->getValue()); }, $lore->getValue());
@ -488,12 +496,12 @@ class Item implements ItemIds, \JsonSerializable{
* @return $this * @return $this
*/ */
public function setLore(array $lines){ public function setLore(array $lines){
$display = $this->getNamedTagEntry("display"); $display = $this->getNamedTagEntry(self::TAG_DISPLAY);
if(!($display instanceof CompoundTag)){ if(!($display instanceof CompoundTag)){
$display = new CompoundTag("display", []); $display = new CompoundTag(self::TAG_DISPLAY, []);
} }
$display->setTag(new ListTag("Lore", array_map(function(string $str) : StringTag{ $display->setTag(new ListTag(self::TAG_DISPLAY_LORE, array_map(function(string $str) : StringTag{
return new StringTag("", $str); return new StringTag("", $str);
}, $lines), NBT::TAG_String)); }, $lines), NBT::TAG_String));