From 2b90acf53e8eeb49903e32e4d18e68096b4a0bf6 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 9 Oct 2012 16:48:55 +0200 Subject: [PATCH] Ensure that non-CraftBukkit item stacks are correctly converted. --- .../protocol/events/PacketContainer.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ProtocolLib/src/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/com/comphenix/protocol/events/PacketContainer.java index 34bd0a49..ce949ecf 100644 --- a/ProtocolLib/src/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/com/comphenix/protocol/events/PacketContainer.java @@ -145,7 +145,7 @@ public class PacketContainer implements Serializable { // Convert from and to the Bukkit wrapper return structureModifier.withType(net.minecraft.server.ItemStack.class, new EquivalentConverter() { public Object getGeneric(ItemStack specific) { - return ((CraftItemStack) specific).getHandle(); + return toStackNMS(specific); } @Override @@ -175,7 +175,7 @@ public class PacketContainer implements Serializable { // Unwrap every item for (int i = 0; i < result.length; i++) { - result[i] = ((CraftItemStack) specific[i]).getHandle(); + result[i] = toStackNMS(specific[i]); } return result; } @@ -199,6 +199,20 @@ public class PacketContainer implements Serializable { }); } + /** + * Convert an item stack to the NMS equivalent. + * @param stack - Bukkit stack to convert. + * @return A bukkit stack. + */ + private net.minecraft.server.ItemStack toStackNMS(ItemStack stack) { + // We must be prepared for an object that simply implements ItemStcak + if (stack instanceof CraftItemStack) { + return ((CraftItemStack) stack).getHandle(); + } else { + return (new CraftItemStack(stack)).getHandle(); + } + } + /** * Retrieves a read/write structure for the world type enum. *