mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-13 06:55:29 +00:00
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.
This commit is contained in:
parent
00bf190e54
commit
e8453b7872
@ -246,19 +246,12 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasCustomBlockData() : bool{
|
public function hasCustomBlockData() : bool{
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
|
|
||||||
return isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag;
|
return isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clearCustomBlockData(){
|
public function clearCustomBlockData(){
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
|
|
||||||
if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){
|
if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){
|
||||||
@ -278,12 +271,7 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
$tags = clone $compound;
|
$tags = clone $compound;
|
||||||
$tags->setName("BlockEntityTag");
|
$tags->setName("BlockEntityTag");
|
||||||
|
|
||||||
if(!$this->hasCompoundTag()){
|
$tag = $this->getNamedTag();
|
||||||
$tag = new CompoundTag("", []);
|
|
||||||
}else{
|
|
||||||
$tag = $this->getNamedTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag->BlockEntityTag = $tags;
|
$tag->BlockEntityTag = $tags;
|
||||||
$this->setNamedTag($tag);
|
$this->setNamedTag($tag);
|
||||||
|
|
||||||
@ -294,10 +282,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return CompoundTag|null
|
* @return CompoundTag|null
|
||||||
*/
|
*/
|
||||||
public function getCustomBlockData(){
|
public function getCustomBlockData(){
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){
|
if(isset($tag->BlockEntityTag) and $tag->BlockEntityTag instanceof CompoundTag){
|
||||||
return $tag->BlockEntityTag;
|
return $tag->BlockEntityTag;
|
||||||
@ -310,10 +294,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasEnchantments() : bool{
|
public function hasEnchantments() : bool{
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
|
|
||||||
return isset($tag->ench) and $tag->ench instanceof ListTag;
|
return isset($tag->ench) and $tag->ench instanceof ListTag;
|
||||||
@ -397,11 +377,7 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @param Enchantment $ench
|
* @param Enchantment $ench
|
||||||
*/
|
*/
|
||||||
public function addEnchantment(Enchantment $ench){
|
public function addEnchantment(Enchantment $ench){
|
||||||
if(!$this->hasCompoundTag()){
|
$tag = $this->getNamedTag();
|
||||||
$tag = new CompoundTag("", []);
|
|
||||||
}else{
|
|
||||||
$tag = $this->getNamedTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
$found = false;
|
$found = false;
|
||||||
|
|
||||||
@ -454,10 +430,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasCustomName() : bool{
|
public function hasCustomName() : bool{
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
if(isset($tag->display)){
|
if(isset($tag->display)){
|
||||||
$tag = $tag->display;
|
$tag = $tag->display;
|
||||||
@ -473,10 +445,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCustomName() : string{
|
public function getCustomName() : string{
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
if(isset($tag->display)){
|
if(isset($tag->display)){
|
||||||
$tag = $tag->display;
|
$tag = $tag->display;
|
||||||
@ -498,12 +466,7 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
$this->clearCustomName();
|
$this->clearCustomName();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$this->hasCompoundTag()){
|
$tag = $this->getNamedTag();
|
||||||
$tag = new CompoundTag("", []);
|
|
||||||
}else{
|
|
||||||
$tag = $this->getNamedTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isset($tag->display) and $tag->display instanceof CompoundTag){
|
if(isset($tag->display) and $tag->display instanceof CompoundTag){
|
||||||
$tag->display->Name = new StringTag("Name", $name);
|
$tag->display->Name = new StringTag("Name", $name);
|
||||||
}else{
|
}else{
|
||||||
@ -521,9 +484,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function clearCustomName(){
|
public function clearCustomName(){
|
||||||
if(!$this->hasCompoundTag()){
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
|
|
||||||
if(isset($tag->display) and $tag->display instanceof CompoundTag){
|
if(isset($tag->display) and $tag->display instanceof CompoundTag){
|
||||||
@ -559,7 +519,7 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setLore(array $lines){
|
public function setLore(array $lines){
|
||||||
$tag = $this->getNamedTag() ?? new CompoundTag("", []);
|
$tag = $this->getNamedTag();
|
||||||
if(!isset($tag->display)){
|
if(!isset($tag->display)){
|
||||||
$tag->display = new CompoundTag("display", []);
|
$tag->display = new CompoundTag("display", []);
|
||||||
}
|
}
|
||||||
@ -580,24 +540,21 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return Tag|null
|
* @return Tag|null
|
||||||
*/
|
*/
|
||||||
public function getNamedTagEntry($name){
|
public function getNamedTagEntry($name){
|
||||||
$tag = $this->getNamedTag();
|
return $this->getNamedTag()->{$name} ?? null;
|
||||||
if($tag !== null){
|
|
||||||
return $tag->{$name} ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a tree of Tag objects representing the Item's NBT
|
* Returns a tree of Tag objects representing the Item's NBT. If the item does not have any NBT, an empty CompoundTag
|
||||||
* @return null|CompoundTag
|
* object is returned to allow the caller to manipulate and apply back to the item.
|
||||||
|
*
|
||||||
|
* @return CompoundTag
|
||||||
*/
|
*/
|
||||||
public function getNamedTag(){
|
public function getNamedTag() : CompoundTag{
|
||||||
if(!$this->hasCompoundTag()){
|
if(!$this->hasCompoundTag() and $this->cachedNBT === null){
|
||||||
return 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,7 +90,7 @@ class WritableBook extends Item{
|
|||||||
if($pageId < 0){
|
if($pageId < 0){
|
||||||
throw new \InvalidArgumentException("Page number \"$pageId\" is out of range");
|
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)){
|
if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){
|
||||||
$namedTag->pages = new ListTag("pages", []);
|
$namedTag->pages = new ListTag("pages", []);
|
||||||
@ -137,7 +137,7 @@ class WritableBook extends Item{
|
|||||||
* @return bool indicating success
|
* @return bool indicating success
|
||||||
*/
|
*/
|
||||||
public function insertPage(int $pageId, string $pageText = "") : bool{
|
public function insertPage(int $pageId, string $pageText = "") : bool{
|
||||||
$namedTag = $this->getCorrectedNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){
|
if(!isset($namedTag->pages) or !($namedTag->pages instanceof ListTag)){
|
||||||
$namedTag->pages = new ListTag("pages", []);
|
$namedTag->pages = new ListTag("pages", []);
|
||||||
}
|
}
|
||||||
@ -169,13 +169,6 @@ class WritableBook extends Item{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return CompoundTag
|
|
||||||
*/
|
|
||||||
protected function getCorrectedNamedTag() : CompoundTag{
|
|
||||||
return $this->getNamedTag() ?? new CompoundTag();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMaxStackSize() : int{
|
public function getMaxStackSize() : int{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -216,7 +209,7 @@ class WritableBook extends Item{
|
|||||||
* @return CompoundTag[]
|
* @return CompoundTag[]
|
||||||
*/
|
*/
|
||||||
public function getPages() : array{
|
public function getPages() : array{
|
||||||
$namedTag = $this->getCorrectedNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
if(!isset($namedTag->pages)){
|
if(!isset($namedTag->pages)){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ class WrittenBook extends WritableBook{
|
|||||||
if($generation < 0 or $generation > 3){
|
if($generation < 0 or $generation > 3){
|
||||||
throw new \InvalidArgumentException("Generation \"$generation\" is out of range");
|
throw new \InvalidArgumentException("Generation \"$generation\" is out of range");
|
||||||
}
|
}
|
||||||
$namedTag = $this->getCorrectedNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
|
|
||||||
if(isset($namedTag->generation)){
|
if(isset($namedTag->generation)){
|
||||||
$namedTag->generation->setValue($generation);
|
$namedTag->generation->setValue($generation);
|
||||||
@ -93,7 +93,7 @@ class WrittenBook extends WritableBook{
|
|||||||
* @param string $authorName
|
* @param string $authorName
|
||||||
*/
|
*/
|
||||||
public function setAuthor(string $authorName) : void{
|
public function setAuthor(string $authorName) : void{
|
||||||
$namedTag = $this->getCorrectedNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
if(isset($namedTag->author)){
|
if(isset($namedTag->author)){
|
||||||
$namedTag->author->setValue($authorName);
|
$namedTag->author->setValue($authorName);
|
||||||
}else{
|
}else{
|
||||||
@ -120,7 +120,7 @@ class WrittenBook extends WritableBook{
|
|||||||
* @param string $title
|
* @param string $title
|
||||||
*/
|
*/
|
||||||
public function setTitle(string $title) : void{
|
public function setTitle(string $title) : void{
|
||||||
$namedTag = $this->getCorrectedNamedTag();
|
$namedTag = $this->getNamedTag();
|
||||||
if(isset($namedTag->title)){
|
if(isset($namedTag->title)){
|
||||||
$namedTag->title->setValue($title);
|
$namedTag->title->setValue($title);
|
||||||
}else{
|
}else{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user