From 068618eb5ba66dc575ce4dc9598555e29eb8a120 Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 3 Jul 2020 09:14:34 +1000 Subject: [PATCH] SPIGOT-5927: Some items NBT data disappears --- .../inventory/CraftItemFactory.java | 4 ++ .../craftbukkit/inventory/CraftItemStack.java | 4 ++ .../inventory/CraftMetaArmorStand.java | 7 +++ .../inventory/CraftMetaEntityTag.java | 11 +++++ .../CraftMetaTropicalFishBucket.java | 43 +++++++++++++++++-- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java index 17bbeeefa3..89a3617068 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java @@ -259,7 +259,11 @@ public final class CraftItemFactory implements ItemFactory { return meta instanceof CraftMetaCrossbow ? meta : new CraftMetaCrossbow(meta); case SUSPICIOUS_STEW: return meta instanceof CraftMetaSuspiciousStew ? meta : new CraftMetaSuspiciousStew(meta); + case COD_BUCKET: + case PUFFERFISH_BUCKET: + case SALMON_BUCKET: case ITEM_FRAME: + case PAINTING: return meta instanceof CraftMetaEntityTag ? meta : new CraftMetaEntityTag(meta); case COMPASS: return meta instanceof CraftMetaCompass ? meta : new CraftMetaCompass(meta); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index dd83cc2cc5..62cdb32a63 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -528,7 +528,11 @@ public final class CraftItemStack extends ItemStack { return new CraftMetaCrossbow(item.getTag()); case SUSPICIOUS_STEW: return new CraftMetaSuspiciousStew(item.getTag()); + case COD_BUCKET: + case PUFFERFISH_BUCKET: + case SALMON_BUCKET: case ITEM_FRAME: + case PAINTING: return new CraftMetaEntityTag(item.getTag()); case COMPASS: return new CraftMetaCompass(item.getTag()); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java index f700522840..1a3549480b 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmorStand.java @@ -15,6 +15,13 @@ public class CraftMetaArmorStand extends CraftMetaItem { CraftMetaArmorStand(CraftMetaItem meta) { super(meta); + + if (!(meta instanceof CraftMetaArmorStand)) { + return; + } + + CraftMetaArmorStand armorStand = (CraftMetaArmorStand) meta; + this.entityTag = armorStand.entityTag; } CraftMetaArmorStand(NBTTagCompound tag) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java index 9af9dc3470..325fec72ab 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java @@ -15,6 +15,13 @@ public class CraftMetaEntityTag extends CraftMetaItem { CraftMetaEntityTag(CraftMetaItem meta) { super(meta); + + if (!(meta instanceof CraftMetaEntityTag)) { + return; + } + + CraftMetaEntityTag entity = (CraftMetaEntityTag) meta; + this.entityTag = entity.entityTag; } CraftMetaEntityTag(NBTTagCompound tag) { @@ -57,7 +64,11 @@ public class CraftMetaEntityTag extends CraftMetaItem { @Override boolean applicableTo(Material type) { switch (type) { + case COD_BUCKET: + case PUFFERFISH_BUCKET: + case SALMON_BUCKET: case ITEM_FRAME: + case PAINTING: return true; default: return false; diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java index eee51f07a2..134f40f301 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java @@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.inventory; import com.google.common.collect.ImmutableMap; import java.util.Map; +import net.minecraft.server.NBTBase; import net.minecraft.server.NBTTagCompound; import org.bukkit.DyeColor; import org.bukkit.Material; @@ -15,8 +16,10 @@ import org.bukkit.inventory.meta.TropicalFishBucketMeta; @DelegateDeserialization(SerializableMeta.class) class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishBucketMeta { static final ItemMetaKey VARIANT = new ItemMetaKey("BucketVariantTag", "fish-variant"); + static final ItemMetaKey ENTITY_TAG = new ItemMetaKey("EntityTag", "entity-tag"); private Integer variant; + private NBTTagCompound entityTag; CraftMetaTropicalFishBucket(CraftMetaItem meta) { super(meta); @@ -35,6 +38,10 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB if (tag.hasKeyOfType(VARIANT.NBT, CraftMagicNumbers.NBT.TAG_INT)) { this.variant = tag.getInt(VARIANT.NBT); } + + if (tag.hasKey(ENTITY_TAG.NBT)) { + entityTag = tag.getCompound(ENTITY_TAG.NBT); + } } CraftMetaTropicalFishBucket(Map map) { @@ -46,6 +53,22 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB } } + @Override + void deserializeInternal(NBTTagCompound tag, Object context) { + super.deserializeInternal(tag, context); + + if (tag.hasKey(ENTITY_TAG.NBT)) { + entityTag = tag.getCompound(ENTITY_TAG.NBT); + } + } + + @Override + void serializeInternal(Map internalTags) { + if (entityTag != null && !entityTag.isEmpty()) { + internalTags.put(ENTITY_TAG.NBT, entityTag); + } + } + @Override void applyToItem(NBTTagCompound tag) { super.applyToItem(tag); @@ -53,6 +76,10 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB if (hasVariant()) { tag.setInt(VARIANT.NBT, variant); } + + if (entityTag != null) { + tag.set(ENTITY_TAG.NBT, entityTag); + } } @Override @@ -71,7 +98,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB } boolean isBucketEmpty() { - return !(hasVariant()); + return !(hasVariant() || entityTag != null); } @Override @@ -126,7 +153,8 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB if (meta instanceof CraftMetaTropicalFishBucket) { CraftMetaTropicalFishBucket that = (CraftMetaTropicalFishBucket) meta; - return (hasVariant() ? that.hasVariant() && this.variant.equals(that.variant) : !that.hasVariant()); + return (hasVariant() ? that.hasVariant() && this.variant.equals(that.variant) : !that.hasVariant()) + && entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : entityTag == null; } return true; } @@ -144,6 +172,9 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB if (hasVariant()) { hash = 61 * hash + variant; } + if (entityTag != null) { + hash = 61 * hash + entityTag.hashCode(); + } return original != hash ? CraftMetaTropicalFishBucket.class.hashCode() ^ hash : hash; } @@ -151,7 +182,13 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB @Override public CraftMetaTropicalFishBucket clone() { - return (CraftMetaTropicalFishBucket) super.clone(); + CraftMetaTropicalFishBucket clone = (CraftMetaTropicalFishBucket) super.clone(); + + if (entityTag != null) { + clone.entityTag = entityTag.clone(); + } + + return clone; } @Override