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