From 9fb5f7f2dd7a78bd155f898b1849cc9ddce406a0 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 8 Jan 2013 02:00:42 +0100 Subject: [PATCH] Fine tuning of the NBT system. Added more test cases. --- .../protocol/reflect/PrettyPrinter.java | 20 +++++ .../protocol/wrappers/nbt/NbtBase.java | 2 + .../protocol/wrappers/nbt/NbtFactory.java | 54 +++++++++++++ .../wrappers/nbt/NbtCompoundTest.java | 78 +++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrettyPrinter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrettyPrinter.java index ad285df8..f33fb1b2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrettyPrinter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/PrettyPrinter.java @@ -37,6 +37,20 @@ public class PrettyPrinter { */ public final static int RECURSE_DEPTH = 3; + /** + * Print the content of an object. + * @param object - the object to serialize. + * @param stop - superclass that will stop the process. + * @return String representation of the class. + * @throws IllegalAccessException + */ + public static String printObject(Object object) throws IllegalAccessException { + if (object == null) + throw new IllegalArgumentException("object cannot be NULL."); + + return printObject(object, object.getClass(), Object.class); + } + /** * Print the content of an object. * @param object - the object to serialize. @@ -45,6 +59,9 @@ public class PrettyPrinter { * @throws IllegalAccessException */ public static String printObject(Object object, Class start, Class stop) throws IllegalAccessException { + if (object == null) + throw new IllegalArgumentException("object cannot be NULL."); + return printObject(object, start, stop, RECURSE_DEPTH); } @@ -56,6 +73,9 @@ public class PrettyPrinter { * @throws IllegalAccessException */ public static String printObject(Object object, Class start, Class stop, int hierachyDepth) throws IllegalAccessException { + if (object == null) + throw new IllegalArgumentException("object cannot be NULL."); + StringBuilder output = new StringBuilder(); Set previous = new HashSet(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtBase.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtBase.java index 1b9e521e..0ef91f75 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtBase.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtBase.java @@ -34,6 +34,8 @@ public interface NbtBase { /** * Retrieve the value of this NBT tag. + *

+ * Is either a primitive wrapper, a list or a map. * @return Value of this tag. */ public abstract TType getValue(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java index 0223c011..55aeb313 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java @@ -192,38 +192,92 @@ public class NbtFactory { } } + /** + * Constructs a NBT tag of type string. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, String value) { return ofType(NbtType.TAG_STRING, name, value); } + /** + * Constructs a NBT tag of type byte. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, byte value) { return ofType(NbtType.TAG_BYTE, name, value); } + /** + * Constructs a NBT tag of type short. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, short value) { return ofType(NbtType.TAG_SHORT, name, value); } + /** + * Constructs a NBT tag of type int. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, int value) { return ofType(NbtType.TAG_INT, name, value); } + /** + * Constructs a NBT tag of type long. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, long value) { return ofType(NbtType.TAG_LONG, name, value); } + /** + * Constructs a NBT tag of type float. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, float value) { return ofType(NbtType.TAG_FLOAT, name, value); } + /** + * Constructs a NBT tag of type double. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, double value) { return ofType(NbtType.TAG_DOUBlE, name, value); } + /** + * Constructs a NBT tag of type byte array. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, byte[] value) { return ofType(NbtType.TAG_BYTE_ARRAY, name, value); } + /** + * Constructs a NBT tag of type int array. + * @param name - name of the tag. + * @param value - value of the tag. + * @return The constructed NBT tag. + */ public static NbtBase of(String name, int[] value) { return ofType(NbtType.TAG_INT_ARRAY, name, value); } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java new file mode 100644 index 00000000..b217aec4 --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/wrappers/nbt/NbtCompoundTest.java @@ -0,0 +1,78 @@ +package com.comphenix.protocol.wrappers.nbt; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.comphenix.protocol.utility.MinecraftReflection; + +public class NbtCompoundTest { + @BeforeClass + public static void setupBukkit() { + MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_4_6", "org.bukkit.craftbukkit.v1_4_6"); + } + + @Test + public void testCustomTags() { + NbtCustomTag test = new NbtCustomTag("hello", 12); + + NbtCompound map = NbtCompound.fromName("test"); + map.put(test); + + // Note that the custom tag will be cloned + assertEquals(12, map.getInteger("hello")); + } + + /** + * Represents a custom NBT tag. + * + * @author Kristian + * + * @param - the value of the tag. + */ + public static class NbtCustomTag implements NbtBase { + private String name; + private TValue value; + private NbtType type; + + public NbtCustomTag(String name, TValue value) { + if (value == null) + throw new IllegalArgumentException("Cannot create a custom tag from NULL."); + this.value = value; + this.name = name; + this.type = NbtType.getTypeFromClass(value.getClass()); + + } + + @Override + public NbtType getType() { + return type; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public TValue getValue() { + return value; + } + + @Override + public void setValue(TValue newValue) { + this.value = newValue; + } + + @Override + public NbtBase deepClone() { + return new NbtCustomTag(name, value); + } + } +}