From cecab6a169c0d1c94c93cda9a0f3d7a538efb4d0 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 9 Oct 2012 17:41:37 +0200 Subject: [PATCH] Unwrap collections in the packet constructor. --- .../protocol/injector/PacketConstructor.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java b/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java index 135e7eb2..73d6c160 100644 --- a/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java +++ b/ProtocolLib/src/com/comphenix/protocol/injector/PacketConstructor.java @@ -3,6 +3,7 @@ package com.comphenix.protocol.injector; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -11,6 +12,7 @@ import net.minecraft.server.Packet; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.reflect.FieldAccessException; +import com.comphenix.protocol.reflect.instances.DefaultInstances; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -178,9 +180,15 @@ public class PacketConstructor { private static Map, Method> cache = new ConcurrentHashMap, Method>(); + @SuppressWarnings("unchecked") @Override public Object unwrapItem(Object wrappedObject) { + // Special case + if (wrappedObject instanceof Collection) { + return handleCollection((Collection) wrappedObject); + } + Class currentClass = wrappedObject.getClass(); Method cachedMethod = initializeCache(currentClass); @@ -202,6 +210,24 @@ public class PacketConstructor { } } + private Object handleCollection(Collection wrappedObject) { + + @SuppressWarnings("unchecked") + Collection copy = DefaultInstances.DEFAULT.getDefault(wrappedObject.getClass()); + + if (copy != null) { + // Unwrap every element + for (Object element : wrappedObject) { + copy.add(unwrapItem(element)); + } + return copy; + + } else { + // Impossible + return null; + } + } + private Method initializeCache(Class type) { // See if we're already determined this