From fb4564cc37c37a19a8920025de6bb19dbf852338 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 16 Dec 2017 10:18:34 +1100 Subject: SPIGOT-2892: Fix some clone implementations and add unit test --- .../craftbukkit/inventory/CraftMetaBanner.java | 7 +++++++ .../craftbukkit/inventory/CraftMetaBlockState.java | 9 +++++++++ .../craftbukkit/inventory/CraftMetaSpawnEgg.java | 3 +++ .../craftbukkit/inventory/ItemMetaCloneTest.java | 22 ++++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java index aa1a210a..7b356789 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java @@ -200,4 +200,11 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta { boolean applicableTo(Material type) { return type == Material.BANNER; } + + @Override + public CraftMetaBanner clone() { + CraftMetaBanner meta = (CraftMetaBanner) super.clone(); + meta.patterns = new ArrayList<>(patterns); + return meta; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java index fe874e0d..3bd9ee47 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java @@ -213,6 +213,15 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta return false; } + @Override + public CraftMetaBlockState clone() { + CraftMetaBlockState meta = (CraftMetaBlockState) super.clone(); + if (blockEntityTag != null) { + meta.blockEntityTag = blockEntityTag.g(); + } + return meta; + } + @Override public boolean hasBlockState() { return blockEntityTag != null; diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java index 2f438eb4..70616a2f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java @@ -176,6 +176,9 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta { CraftMetaSpawnEgg clone = (CraftMetaSpawnEgg) super.clone(); clone.spawnedType = spawnedType; + if (entityTag != null) { + clone.entityTag = entityTag.g(); + } return clone; } diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java new file mode 100644 index 00000000..834fb297 --- /dev/null +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaCloneTest.java @@ -0,0 +1,22 @@ +package org.bukkit.craftbukkit.inventory; + +import java.lang.reflect.Method; +import org.bukkit.Material; +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +public class ItemMetaCloneTest { + + @Test + public void testClone() throws Throwable { + for (Material material : ItemStackTest.COMPOUND_MATERIALS) { + Class clazz = CraftItemFactory.instance().getItemMeta(material).getClass(); + + Method clone = clazz.getDeclaredMethod("clone"); + assertNotNull("Class " + clazz + " does not override clone()", clone); + assertThat("Class " + clazz + " clone return type does not match", clone.getReturnType(), is(equalTo(clazz))); + } + } +} -- cgit v1.2.3