Archiviert
13
0

Update the stream serializer for Minecraft 1.6.2

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-07-10 23:33:25 +02:00
Ursprung a4eb219a9a
Commit c590e4a825
2 geänderte Dateien mit 53 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -2,7 +2,9 @@ package com.comphenix.protocol.utility;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
@ -13,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
/**
* Utility methods for reading and writing Minecraft objects to streams.
@ -37,11 +40,14 @@ public class StreamSerializer {
public ItemStack deserializeItemStack(@Nonnull DataInputStream input) throws IOException {
if (input == null)
throw new IllegalArgumentException("Input stream cannot be NULL.");
if (readItemMethod == null)
readItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).
getMethodByParameters("readPacket",
MinecraftReflection.getItemStackClass(),
new Class<?>[] {DataInputStream.class});
if (readItemMethod == null) {
readItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).getMethod(
FuzzyMethodContract.newBuilder().
parameterCount(1).
parameterDerivedOf(DataInput.class).
returnDerivedOf(MinecraftReflection.getItemStackClass()).
build());
}
try {
Object nmsItem = readItemMethod.invoke(null, input);
@ -88,10 +94,12 @@ public class StreamSerializer {
Object nmsItem = MinecraftReflection.getMinecraftItemStack(stack);
if (writeItemMethod == null)
writeItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).
getMethodByParameters("writePacket", new Class<?>[] {
MinecraftReflection.getItemStackClass(),
DataOutputStream.class });
writeItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).getMethod(
FuzzyMethodContract.newBuilder().
parameterCount(2).
parameterDerivedOf(MinecraftReflection.getItemStackClass(), 0).
parameterDerivedOf(DataOutput.class, 1).
build());
try {
writeItemMethod.invoke(null, nmsItem, output);
} catch (Exception e) {

Datei anzeigen

@ -0,0 +1,36 @@
package com.comphenix.protocol.utility;
import static org.junit.Assert.*;
import java.io.IOException;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemFactory;
import org.bukkit.inventory.ItemStack;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import com.comphenix.protocol.BukkitInitialization;
@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
@PrepareForTest(CraftItemFactory.class)
public class StreamSerializerTest {
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
BukkitInitialization.initializeItemMeta();
}
@Test
public void testSerializer() throws IOException {
ItemStack before = new ItemStack(Material.GOLD_AXE);
StreamSerializer serializer = new StreamSerializer();
String data = serializer.serializeItemStack(before);
ItemStack after = serializer.deserializeItemStack(data);
assertEquals(before.getType(), after.getType());
assertEquals(before.getAmount(), after.getAmount());
}
}