From 27d39cac4fac65022f0f63a430fff961e71aace5 Mon Sep 17 00:00:00 2001 From: SoSeDiK Date: Tue, 22 Aug 2023 07:59:31 +0300 Subject: [PATCH] Properly clone custom nbt tags inside ItemMeta (#7849) --- .../1019-Deep-clone-unhandled-nbt-tags.patch | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 patches/server/1019-Deep-clone-unhandled-nbt-tags.patch diff --git a/patches/server/1019-Deep-clone-unhandled-nbt-tags.patch b/patches/server/1019-Deep-clone-unhandled-nbt-tags.patch new file mode 100644 index 0000000000..f37bf37494 --- /dev/null +++ b/patches/server/1019-Deep-clone-unhandled-nbt-tags.patch @@ -0,0 +1,61 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: SoSeDiK +Date: Thu, 26 May 2022 03:30:05 +0300 +Subject: [PATCH] Deep clone unhandled nbt tags + + +diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +index 076e06908a0cf97f86a64a15ca0231c5b0f06fec..3df1822b55358a9bdf41bcacd5b7fecfd8f05dfa 100644 +--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java ++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +@@ -303,7 +303,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { + private static final CraftPersistentDataTypeRegistry DATA_TYPE_REGISTRY = new CraftPersistentDataTypeRegistry(); + + private CompoundTag internalTag; +- final Map unhandledTags = new TreeMap(); // Visible for testing only // Paper ++ Map unhandledTags = new TreeMap(); // Visible for testing only // Paper // Paper - remove final + private CraftPersistentDataContainer persistentDataContainer = new CraftPersistentDataContainer(CraftMetaItem.DATA_TYPE_REGISTRY); + + private int version = CraftMagicNumbers.INSTANCE.getDataVersion(); // Internal use only +@@ -344,8 +344,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { + this.destroyableKeys = new java.util.HashSet<>(meta.destroyableKeys); + } + // Paper end +- this.unhandledTags.putAll(meta.unhandledTags); +- this.persistentDataContainer.putAll(meta.persistentDataContainer.getRaw()); ++ // Paper start - Deep clone unhandled nbt tags ++ meta.unhandledTags.forEach((key, tag) -> this.unhandledTags.put(key, tag.copy())); ++ this.persistentDataContainer.putAll(meta.persistentDataContainer.getTagsCloned()); ++ // Paper end + + this.internalTag = meta.internalTag; + if (this.internalTag != null) { +@@ -1393,7 +1395,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { + if (this.hasAttributeModifiers()) { + clone.attributeModifiers = LinkedHashMultimap.create(this.attributeModifiers); + } +- clone.persistentDataContainer = new CraftPersistentDataContainer(this.persistentDataContainer.getRaw(), CraftMetaItem.DATA_TYPE_REGISTRY); ++ // Paper start - Deep clone unhandled nbt tags ++ clone.persistentDataContainer = new CraftPersistentDataContainer(this.persistentDataContainer.getTagsCloned(), CraftMetaItem.DATA_TYPE_REGISTRY); ++ clone.unhandledTags = new TreeMap<>(this.unhandledTags); ++ clone.unhandledTags.replaceAll((key, tag) -> tag.copy()); ++ // Paper end - Deep clone unhandled nbt tags + clone.hideFlag = this.hideFlag; + clone.unbreakable = this.unbreakable; + clone.damage = this.damage; +diff --git a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java +index 65013fd2ca24c4bf1cfd67e314927e72542d3e68..b040bd8be5c1803d0b4045990489ae4182f69568 100644 +--- a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java ++++ b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java +@@ -189,5 +189,11 @@ public class CraftPersistentDataContainer implements PersistentDataContainer { + this.putAll(compound); + } + } ++ ++ public Map getTagsCloned() { ++ final Map tags = new HashMap<>(); ++ this.customDataTags.forEach((key, tag) -> tags.put(key, tag.copy())); ++ return tags; ++ } + // Paper end + }