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.

By: Wesley Wolfe <weswolf@aol.com>
This commit is contained in:
CraftBukkit/Spigot 2013-03-28 20:01:01 -05:00
parent 3c6a12547b
commit f84dc2d381
3 changed files with 12 additions and 5 deletions

View file

@ -106,8 +106,8 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage
public CraftMetaEnchantedBook clone() { public CraftMetaEnchantedBook clone() {
CraftMetaEnchantedBook meta = (CraftMetaEnchantedBook) super.clone(); CraftMetaEnchantedBook meta = (CraftMetaEnchantedBook) super.clone();
if (hasStoredEnchants()) { if (this.enchantments != null) {
meta.enchantments = new HashMap<Enchantment, Integer>(enchantments); meta.enchantments = new HashMap<Enchantment, Integer>(this.enchantments);
} }
return meta; return meta;

View file

@ -490,7 +490,14 @@ class CraftMetaItem implements ItemMeta, Repairable {
@Override @Override
public CraftMetaItem clone() { public CraftMetaItem clone() {
try { try {
return (CraftMetaItem) super.clone(); CraftMetaItem clone = (CraftMetaItem) super.clone();
if (this.lore != null) {
clone.lore = new ArrayList<String>(this.lore);
}
if (this.enchantments != null) {
clone.enchantments = new HashMap<Enchantment, Integer>(this.enchantments);
}
return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new Error(e); throw new Error(e);
} }

View file

@ -117,8 +117,8 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
@Override @Override
public CraftMetaPotion clone() { public CraftMetaPotion clone() {
CraftMetaPotion clone = (CraftMetaPotion) super.clone(); CraftMetaPotion clone = (CraftMetaPotion) super.clone();
if (hasCustomEffects()) { if (this.customEffects != null) {
clone.customEffects = new ArrayList<PotionEffect>(customEffects); clone.customEffects = new ArrayList<PotionEffect>(this.customEffects);
} }
return clone; return clone;
} }