Properly clone custom nbt tags inside ItemMeta (#7849)

This commit is contained in:
SoSeDiK 2023-08-22 07:59:31 +03:00 committed by GitHub
parent fed9042390
commit 27d39cac4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,61 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: SoSeDiK <mrsosedik@gmail.com>
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<String, Tag> unhandledTags = new TreeMap<String, Tag>(); // Visible for testing only // Paper
+ Map<String, Tag> unhandledTags = new TreeMap<String, Tag>(); // 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<String, Tag> getTagsCloned() {
+ final Map<String, Tag> tags = new HashMap<>();
+ this.customDataTags.forEach((key, tag) -> tags.put(key, tag.copy()));
+ return tags;
+ }
// Paper end
}