From 555b5d0795b8b541d50614dc31275b8c44aa654b Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Fri, 21 Dec 2012 18:33:09 +0100 Subject: [PATCH] Create a typed ItemStack array when writing it to a field. FIXES 24. After the package versioning commit, ProtocolLib switched to performing everything exclusively with reflection. Unfortunately, this may have introduced a couple of new bugs. This problem is caused by the fact that ProtocolLib converts a Bukkit ItemStack array to an Object array instead of an array of NMS ItemStacks. This object array can't be assigned to a ItemStack array, so the write operation fails. The fix is to create a typed array with reflection. --- .../com/comphenix/protocol/events/PacketContainer.java | 7 ++++--- .../comphenix/protocol/utility/MinecraftReflection.java | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java index a7b4a265..373c7d98 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; @@ -241,12 +242,12 @@ public class PacketContainer implements Serializable { BukkitConverters.getIgnoreNull(new EquivalentConverter() { public Object getGeneric(ClassgenericType, ItemStack[] specific) { - Object[] result = new Object[specific.length]; + Class nmsStack = MinecraftReflection.getItemStackClass(); + Object[] result = (Object[]) Array.newInstance(nmsStack, specific.length); // Unwrap every item for (int i = 0; i < result.length; i++) { - result[i] = stackConverter.getGeneric( - MinecraftReflection.getItemStackClass(), specific[i]); + result[i] = stackConverter.getGeneric(nmsStack, specific[i]); } return result; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java index 15ba850f..bf6ffc20 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java @@ -314,12 +314,12 @@ public class MinecraftReflection { * @return The NetHandler class. */ public static Class getNetHandlerClass() { - return getMinecraftClass("NetHandler"); + return getMinecraftClass("NetHandler", "Connection"); } /** - * Retrieve the NetLoginHandler class. - * @return The NetLoginHandler class. + * Retrieve the NMS ItemStack class. + * @return The ItemStack class. */ @SuppressWarnings("rawtypes") public static Class getItemStackClass() {