From 77376a2fa38ee1190c091f72b5172a8a63f4b316 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 18 Sep 2012 18:37:11 +0200 Subject: [PATCH] Make the Bukkit unwrapper use the actual return type. --- .../protocol/injector/PacketConstructor.java | 48 +++++++++---------- .../injector/PacketFilterManager.java | 9 +--- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java b/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java index fe3eeecd..0708fd5f 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java @@ -42,7 +42,8 @@ public class PacketConstructor { this.unwrappers = Lists.newArrayList((Unwrapper) new BukkitUnwrapper()); } - private PacketConstructor(Constructor constructorMethod, List unwrappers) { + private PacketConstructor(int packetID, Constructor constructorMethod, List unwrappers) { + this.packetID = packetID; this.constructorMethod = constructorMethod; this.unwrappers = unwrappers; } @@ -65,7 +66,7 @@ public class PacketConstructor { * @return A constructor with a different set of unwrappers. */ public PacketConstructor withUnwrappers(List unwrappers) { - return new PacketConstructor(constructorMethod, unwrappers); + return new PacketConstructor(packetID, constructorMethod, unwrappers); } /** @@ -75,17 +76,28 @@ public class PacketConstructor { * @return A packet constructor with these types. * @throws IllegalArgumentException If no packet constructor could be created with these types. */ - public PacketConstructor withPacket(int id, Class[] types) { + public PacketConstructor withPacket(int id, Object[] values) { + + Class[] types = new Class[values.length]; for (int i = 0; i < types.length; i++) { - for (Unwrapper unwrapper : unwrappers) { - Class result = unwrapper.unwrapType(types[i]); + // Default type + if (values[i] != null) { + types[i] = values[i].getClass(); - // Update type we're searching for - if (result != null) { - types[i] = result; - break; + for (Unwrapper unwrapper : unwrappers) { + Object result = unwrapper.unwrapItem(values[i]); + + // Update type we're searching for + if (result != null) { + types[i] = result.getClass(); + break; + } } + + } else { + // Try it + types[i] = Object.class; } } @@ -100,7 +112,7 @@ public class PacketConstructor { if (isCompatible(types, params)) { // Right, we've found our type - return new PacketConstructor(constructor, unwrappers); + return new PacketConstructor(id, constructor, unwrappers); } } @@ -189,21 +201,6 @@ public class PacketConstructor { throw new RuntimeException("Minecraft error.", e); } } - - @Override - public Class unwrapType(Class type) { - - if (type == null) - throw new IllegalArgumentException("type cannot be null."); - - Method unwrapped = initializeCache(type); - - // Determine the unwrapped type - if (unwrapped != null) - return unwrapped.getReturnType(); - else - return null; - } private Method initializeCache(Class type) { @@ -230,6 +227,5 @@ public class PacketConstructor { public static interface Unwrapper { public Object unwrapItem(Object wrappedObject); - public Class unwrapType(Class type); } } diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java index 72e8c601..8b331ddb 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/PacketFilterManager.java @@ -328,14 +328,7 @@ public final class PacketFilterManager implements ProtocolManager { @Override public PacketConstructor createPacketConstructor(int id, Object... arguments) { - Class[] types = new Class[arguments.length]; - - // Initialize types - for (int i = 0; i < arguments.length; i++) { - types[i] = arguments[i] != null ? arguments[i].getClass() : Object.class; - } - - return PacketConstructor.DEFAULT.withPacket(id, types); + return PacketConstructor.DEFAULT.withPacket(id, arguments); } @Override