From acd637d48b7550254956b8aa8a09f69980050b39 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Thu, 28 Mar 2013 20:01:01 -0500 Subject: [PATCH] Properly copy collection references in ItemMeta.clone(). Fixes BUKKIT-3913 When cloning an item, all references are copied to the new item. For collections, this makes internal changes become visible in both the old and new items. In CraftMetaItem, clone was not making copies of the appropriate collections and has been fixed for non-null values. In CraftMetaEnchantedBook and CraftMetaPotion, clone was using possible empty collection references and has been changed to explicitly null-check instead. --- .../craftbukkit/inventory/CraftMetaEnchantedBook.java | 4 ++-- .../org/bukkit/craftbukkit/inventory/CraftMetaItem.java | 9 ++++++++- .../bukkit/craftbukkit/inventory/CraftMetaPotion.java | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java index f975e867e9..8d44e55e66 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java @@ -106,8 +106,8 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage public CraftMetaEnchantedBook clone() { CraftMetaEnchantedBook meta = (CraftMetaEnchantedBook) super.clone(); - if (hasStoredEnchants()) { - meta.enchantments = new HashMap(enchantments); + if (this.enchantments != null) { + meta.enchantments = new HashMap(this.enchantments); } return meta; diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 90e7ffc352..0e0bb32e1e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -490,7 +490,14 @@ class CraftMetaItem implements ItemMeta, Repairable { @Override public CraftMetaItem clone() { try { - return (CraftMetaItem) super.clone(); + CraftMetaItem clone = (CraftMetaItem) super.clone(); + if (this.lore != null) { + clone.lore = new ArrayList(this.lore); + } + if (this.enchantments != null) { + clone.enchantments = new HashMap(this.enchantments); + } + return clone; } catch (CloneNotSupportedException e) { throw new Error(e); } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java index ca024d0bfc..e7d27d20ee 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java @@ -117,8 +117,8 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { @Override public CraftMetaPotion clone() { CraftMetaPotion clone = (CraftMetaPotion) super.clone(); - if (hasCustomEffects()) { - clone.customEffects = new ArrayList(customEffects); + if (this.customEffects != null) { + clone.customEffects = new ArrayList(this.customEffects); } return clone; }