Item: optimise serializeCompoundTag() a little

This commit is contained in:
Dylan K. Taylor 2023-04-18 16:18:34 +01:00
parent a77fc8109f
commit 674b65f789
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -353,30 +353,35 @@ class Item implements \JsonSerializable{
} }
protected function serializeCompoundTag(CompoundTag $tag) : void{ protected function serializeCompoundTag(CompoundTag $tag) : void{
$display = $tag->getCompoundTag(self::TAG_DISPLAY) ?? new CompoundTag(); $display = $tag->getCompoundTag(self::TAG_DISPLAY);
$this->hasCustomName() ? if($this->customName !== ""){
$display->setString(self::TAG_DISPLAY_NAME, $this->getCustomName()) : $display ??= new CompoundTag();
$display->removeTag(self::TAG_DISPLAY_NAME); $display->setString(self::TAG_DISPLAY_NAME, $this->customName);
}else{
$display?->removeTag(self::TAG_DISPLAY_NAME);
}
if(count($this->lore) > 0){ if(count($this->lore) > 0){
$loreTag = new ListTag(); $loreTag = new ListTag();
foreach($this->lore as $line){ foreach($this->lore as $line){
$loreTag->push(new StringTag($line)); $loreTag->push(new StringTag($line));
} }
$display ??= new CompoundTag();
$display->setTag(self::TAG_DISPLAY_LORE, $loreTag); $display->setTag(self::TAG_DISPLAY_LORE, $loreTag);
}else{ }else{
$display->removeTag(self::TAG_DISPLAY_LORE); $display?->removeTag(self::TAG_DISPLAY_LORE);
} }
$display->count() > 0 ? $display !== null && $display->count() > 0 ?
$tag->setTag(self::TAG_DISPLAY, $display) : $tag->setTag(self::TAG_DISPLAY, $display) :
$tag->removeTag(self::TAG_DISPLAY); $tag->removeTag(self::TAG_DISPLAY);
if($this->hasEnchantments()){ if(count($this->enchantments) > 0){
$ench = new ListTag(); $ench = new ListTag();
foreach($this->getEnchantments() as $enchantmentInstance){ $enchantmentIdMap = EnchantmentIdMap::getInstance();
foreach($this->enchantments as $enchantmentInstance){
$ench->push(CompoundTag::create() $ench->push(CompoundTag::create()
->setShort(self::TAG_ENCH_ID, EnchantmentIdMap::getInstance()->toId($enchantmentInstance->getType())) ->setShort(self::TAG_ENCH_ID, $enchantmentIdMap->toId($enchantmentInstance->getType()))
->setShort(self::TAG_ENCH_LVL, $enchantmentInstance->getLevel()) ->setShort(self::TAG_ENCH_LVL, $enchantmentInstance->getLevel())
); );
} }
@ -385,8 +390,8 @@ class Item implements \JsonSerializable{
$tag->removeTag(self::TAG_ENCH); $tag->removeTag(self::TAG_ENCH);
} }
($blockData = $this->getCustomBlockData()) !== null ? $this->blockEntityTag !== null ?
$tag->setTag(self::TAG_BLOCK_ENTITY_TAG, clone $blockData) : $tag->setTag(self::TAG_BLOCK_ENTITY_TAG, clone $this->blockEntityTag) :
$tag->removeTag(self::TAG_BLOCK_ENTITY_TAG); $tag->removeTag(self::TAG_BLOCK_ENTITY_TAG);
if(count($this->canPlaceOn) > 0){ if(count($this->canPlaceOn) > 0){