Update packet container serialization for Minecraft 1.6.2.

This commit is contained in:
Kristian S. Stangeland 2013-07-10 23:41:49 +02:00
parent c590e4a825
commit 1000378b78
2 changed files with 23 additions and 3 deletions

View File

@ -17,7 +17,9 @@
package com.comphenix.protocol.events;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
@ -49,6 +51,7 @@ import com.comphenix.protocol.reflect.cloning.CollectionCloner;
import com.comphenix.protocol.reflect.cloning.FieldCloner;
import com.comphenix.protocol.reflect.cloning.ImmutableDetector;
import com.comphenix.protocol.reflect.cloning.AggregateCloner.BuilderParameters;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
import com.comphenix.protocol.reflect.instances.DefaultInstances;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.StreamSerializer;
@ -456,7 +459,7 @@ public class PacketContainer implements Serializable {
try {
// Call the write-method
getMethodLazily(writeMethods, handle.getClass(), "write", DataOutputStream.class).
getMethodLazily(writeMethods, handle.getClass(), "write", DataOutput.class).
invoke(handle, new DataOutputStream(output));
} catch (IllegalArgumentException e) {
@ -483,7 +486,7 @@ public class PacketContainer implements Serializable {
// Call the read method
try {
getMethodLazily(readMethods, handle.getClass(), "read", DataInputStream.class).
getMethodLazily(readMethods, handle.getClass(), "read", DataInput.class).
invoke(handle, new DataInputStream(input));
} catch (IllegalArgumentException e) {
@ -513,7 +516,12 @@ public class PacketContainer implements Serializable {
// Atomic operation
if (method == null) {
Method initialized = FuzzyReflection.fromClass(handleClass).getMethodByParameters(methodName, parameterClass);
Method initialized = FuzzyReflection.fromClass(handleClass).getMethod(
FuzzyMethodContract.newBuilder().
parameterCount(1).
parameterDerivedOf(parameterClass).
returnTypeVoid().
build());
method = lookup.putIfAbsent(handleClass, initialized);
// Use our version if we succeeded

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import java.lang.reflect.Array;
import java.util.List;
import org.apache.commons.lang.SerializationUtils;
// Will have to be updated for every version though
import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemFactory;
@ -306,6 +307,17 @@ public class PacketContainerTest {
assertEquals(list, watchableAccessor.read(0));
}
@Test
public void testSerialization() {
PacketContainer chat = new PacketContainer(3);
chat.getStrings().write(0, "Test");
PacketContainer copy = (PacketContainer) SerializationUtils.clone(chat);
assertEquals(3, copy.getID());
assertEquals("Test", copy.getStrings().read(0));
}
@Test
public void testDeepClone() {
// Try constructing all the packets