From e8453b78721b00430dcf289719d5464e834bc3d8 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 8 Oct 2017 12:41:57 +0100 Subject: [PATCH] Item->getNamedTag() now always returns a CompoundTag object, removed lots of boilerplate code This change resulted from many complaints and ugly boilerplate code because getNamedTag() is only ever used when you want to read from the tag or modify it. If you have code that depends on this returning null, you should use hasCompoundTag() instead. --- src/pocketmine/item/Item.php | 69 ++++++---------------------- src/pocketmine/item/WritableBook.php | 13 ++---- src/pocketmine/item/WrittenBook.php | 6 +-- 3 files changed, 19 insertions(+), 69 deletions(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index cd685228a..bdc87bf17 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -246,19 +246,12 @@ class Item implements ItemIds, \JsonSerializable{ * @return bool */ public function hasCustomBlockData() : bool{ - if(!$this->hasCompoundTag()){ - return false; - } - $tag = $this->getNamedTag(); return isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag; } public function clearCustomBlockData(){ - if(!$this->hasCompoundTag()){ - return $this; - } $tag = $this->getNamedTag(); if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){ @@ -278,12 +271,7 @@ class Item implements ItemIds, \JsonSerializable{ $tags = clone $compound; $tags->setName("BlockEntityTag"); - if(!$this->hasCompoundTag()){ - $tag = new CompoundTag("", []); - }else{ - $tag = $this->getNamedTag(); - } - + $tag = $this->getNamedTag(); $tag->BlockEntityTag = $tags; $this->setNamedTag($tag); @@ -294,10 +282,6 @@ class Item implements ItemIds, \JsonSerializable{ * @return CompoundTag|null */ public function getCustomBlockData(){ - if(!$this->hasCompoundTag()){ - return null; - } - $tag = $this->getNamedTag(); if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){ return $tag->BlockEntityTag; @@ -310,10 +294,6 @@ class Item implements ItemIds, \JsonSerializable{ * @return bool */ public function hasEnchantments() : bool{ - if(!$this->hasCompoundTag()){ - return false; - } - $tag = $this->getNamedTag(); return isset($tag->ench) and $tag->ench instanceof ListTag; @@ -397,11 +377,7 @@ class Item implements ItemIds, \JsonSerializable{ * @param Enchantment $ench */ public function addEnchantment(Enchantment $ench){ - if(!$this->hasCompoundTag()){ - $tag = new CompoundTag("", []); - }else{ - $tag = $this->getNamedTag(); - } + $tag = $this->getNamedTag(); $found = false; @@ -454,10 +430,6 @@ class Item implements ItemIds, \JsonSerializable{ * @return bool */ public function hasCustomName() : bool{ - if(!$this->hasCompoundTag()){ - return false; - } - $tag = $this->getNamedTag(); if(isset($tag->display)){ $tag = $tag->display; @@ -473,10 +445,6 @@ class Item implements ItemIds, \JsonSerializable{ * @return string */ public function getCustomName() : string{ - if(!$this->hasCompoundTag()){ - return ""; - } - $tag = $this->getNamedTag(); if(isset($tag->display)){ $tag = $tag->display; @@ -498,12 +466,7 @@ class Item implements ItemIds, \JsonSerializable{ $this->clearCustomName(); } - if(!$this->hasCompoundTag()){ - $tag = new CompoundTag("", []); - }else{ - $tag = $this->getNamedTag(); - } - + $tag = $this->getNamedTag(); if(isset($tag->display) and $tag->display instanceof CompoundTag){ $tag->display->Name = new StringTag("Name", $name); }else{ @@ -521,9 +484,6 @@ class Item implements ItemIds, \JsonSerializable{ * @return $this */ public function clearCustomName(){ - if(!$this->hasCompoundTag()){ - return $this; - } $tag = $this->getNamedTag(); if(isset($tag->display) and $tag->display instanceof CompoundTag){ @@ -559,7 +519,7 @@ class Item implements ItemIds, \JsonSerializable{ * @return $this */ public function setLore(array $lines){ - $tag = $this->getNamedTag() ?? new CompoundTag("", []); + $tag = $this->getNamedTag(); if(!isset($tag->display)){ $tag->display = new CompoundTag("display", []); } @@ -580,24 +540,21 @@ class Item implements ItemIds, \JsonSerializable{ * @return Tag|null */ public function getNamedTagEntry($name){ - $tag = $this->getNamedTag(); - if($tag !== null){ - return $tag->{$name} ?? null; - } - - return null; + return $this->getNamedTag()->{$name} ?? null; } /** - * Returns a tree of Tag objects representing the Item's NBT - * @return null|CompoundTag + * Returns a tree of Tag objects representing the Item's NBT. If the item does not have any NBT, an empty CompoundTag + * object is returned to allow the caller to manipulate and apply back to the item. + * + * @return CompoundTag */ - public function getNamedTag(){ - if(!$this->hasCompoundTag()){ - return null; + public function getNamedTag() : CompoundTag{ + if(!$this->hasCompoundTag() and $this->cachedNBT === null){ + $this->cachedNBT = new CompoundTag(); } - return $this->cachedNBT ?? ($this->cachedNBT = $this->cachedNBT = self::parseCompoundTag($this->tags)); + return $this->cachedNBT ?? ($this->cachedNBT = self::parseCompoundTag($this->tags)); } /** diff --git a/src/pocketmine/item/WritableBook.php b/src/pocketmine/item/WritableBook.php index 552d3d12f..e94876d94 100644 --- a/src/pocketmine/item/WritableBook.php +++ b/src/pocketmine/item/WritableBook.php @@ -90,7 +90,7 @@ class WritableBook extends Item{ if($pageId < 0){ throw new \InvalidArgumentException("Page number \"$pageId\" is out of range"); } - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){ $namedTag->pages = new ListTag("pages", []); @@ -137,7 +137,7 @@ class WritableBook extends Item{ * @return bool indicating success */ public function insertPage(int $pageId, string $pageText = "") : bool{ - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){ $namedTag->pages = new ListTag("pages", []); } @@ -169,13 +169,6 @@ class WritableBook extends Item{ return true; } - /** - * @return CompoundTag - */ - protected function getCorrectedNamedTag() : CompoundTag{ - return $this->getNamedTag() ?? new CompoundTag(); - } - public function getMaxStackSize() : int{ return 1; } @@ -216,7 +209,7 @@ class WritableBook extends Item{ * @return CompoundTag[] */ public function getPages() : array{ - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(!isset($namedTag->pages)){ return []; } diff --git a/src/pocketmine/item/WrittenBook.php b/src/pocketmine/item/WrittenBook.php index c17003ad3..29303fac5 100644 --- a/src/pocketmine/item/WrittenBook.php +++ b/src/pocketmine/item/WrittenBook.php @@ -63,7 +63,7 @@ class WrittenBook extends WritableBook{ if($generation < 0 or $generation > 3){ throw new \InvalidArgumentException("Generation \"$generation\" is out of range"); } - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(isset($namedTag->generation)){ $namedTag->generation->setValue($generation); @@ -93,7 +93,7 @@ class WrittenBook extends WritableBook{ * @param string $authorName */ public function setAuthor(string $authorName) : void{ - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(isset($namedTag->author)){ $namedTag->author->setValue($authorName); }else{ @@ -120,7 +120,7 @@ class WrittenBook extends WritableBook{ * @param string $title */ public function setTitle(string $title) : void{ - $namedTag = $this->getCorrectedNamedTag(); + $namedTag = $this->getNamedTag(); if(isset($namedTag->title)){ $namedTag->title->setValue($title); }else{