From 5d7895d741d0cd0e5883429038a71a549c52187c Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 23 Jan 2014 02:55:47 +0100 Subject: [PATCH] Fixed a VERY long standing bug discovered by libraryaddict. Constructing a WrappedWatchableObject with a net.minecraft.server.ItemStack would cause ProtocolLib to throw an IllegalArgumentException, even though WrappedWatchableObject. setValue(Object) accepts ItemStacks without trouble. This is because WrappedWatchableObject.getUnwrappedType() didn't handle ItemStacks at all. --- .../wrappers/WrappedWatchableObject.java | 2 ++ .../wrappers/WrappedWatchableObjectTest.java | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedWatchableObjectTest.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java index 9c849efe..0b0b87a2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java @@ -346,6 +346,8 @@ public class WrappedWatchableObject extends AbstractWrapper { return MinecraftReflection.getChunkPositionClass(); else if (wrapped.equals(WrappedChunkCoordinate.class)) return MinecraftReflection.getChunkCoordinatesClass(); + else if (ItemStack.class.isAssignableFrom(wrapped)) + return MinecraftReflection.getItemStackClass(); else return wrapped; } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedWatchableObjectTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedWatchableObjectTest.java new file mode 100644 index 00000000..ea1059fd --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/WrappedWatchableObjectTest.java @@ -0,0 +1,31 @@ +package com.comphenix.protocol.wrappers; + +import static org.junit.Assert.*; + +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_7_R1.inventory.CraftItemFactory; +import org.bukkit.inventory.ItemStack; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; + +import com.comphenix.protocol.BukkitInitialization; + +@RunWith(org.powermock.modules.junit4.PowerMockRunner.class) +@PrepareForTest(CraftItemFactory.class) +public class WrappedWatchableObjectTest { + @BeforeClass + public static void initializeBukkit() throws IllegalAccessException { + BukkitInitialization.initializeItemMeta(); + } + + @Test + public void testItemStack() { + final ItemStack stack = new ItemStack(Material.GOLD_AXE); + final WrappedWatchableObject test = new WrappedWatchableObject(0, stack); + + ItemStack value = (ItemStack) test.getValue(); + assertEquals(value.getType(), stack.getType()); + } +}