mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-01 00:11:39 +01:00
Fine tuning of the NBT system. Added more test cases.
This commit is contained in:
parent
70589a6263
commit
9fb5f7f2dd
@ -37,6 +37,20 @@ public class PrettyPrinter {
|
|||||||
*/
|
*/
|
||||||
public final static int RECURSE_DEPTH = 3;
|
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.
|
* Print the content of an object.
|
||||||
* @param object - the object to serialize.
|
* @param object - the object to serialize.
|
||||||
@ -45,6 +59,9 @@ public class PrettyPrinter {
|
|||||||
* @throws IllegalAccessException
|
* @throws IllegalAccessException
|
||||||
*/
|
*/
|
||||||
public static String printObject(Object object, Class<?> start, Class<?> stop) 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);
|
return printObject(object, start, stop, RECURSE_DEPTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +73,9 @@ public class PrettyPrinter {
|
|||||||
* @throws IllegalAccessException
|
* @throws IllegalAccessException
|
||||||
*/
|
*/
|
||||||
public static String printObject(Object object, Class<?> start, Class<?> stop, int hierachyDepth) 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();
|
StringBuilder output = new StringBuilder();
|
||||||
Set<Object> previous = new HashSet<Object>();
|
Set<Object> previous = new HashSet<Object>();
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ public interface NbtBase<TType> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the value of this NBT tag.
|
* Retrieve the value of this NBT tag.
|
||||||
|
* <p>
|
||||||
|
* Is either a primitive wrapper, a list or a map.
|
||||||
* @return Value of this tag.
|
* @return Value of this tag.
|
||||||
*/
|
*/
|
||||||
public abstract TType getValue();
|
public abstract TType getValue();
|
||||||
|
@ -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<String> of(String name, String value) {
|
public static NbtBase<String> of(String name, String value) {
|
||||||
return ofType(NbtType.TAG_STRING, name, 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<Byte> of(String name, byte value) {
|
public static NbtBase<Byte> of(String name, byte value) {
|
||||||
return ofType(NbtType.TAG_BYTE, name, 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<Short> of(String name, short value) {
|
public static NbtBase<Short> of(String name, short value) {
|
||||||
return ofType(NbtType.TAG_SHORT, name, 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<Integer> of(String name, int value) {
|
public static NbtBase<Integer> of(String name, int value) {
|
||||||
return ofType(NbtType.TAG_INT, name, 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<Long> of(String name, long value) {
|
public static NbtBase<Long> of(String name, long value) {
|
||||||
return ofType(NbtType.TAG_LONG, name, 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<Float> of(String name, float value) {
|
public static NbtBase<Float> of(String name, float value) {
|
||||||
return ofType(NbtType.TAG_FLOAT, name, 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<Double> of(String name, double value) {
|
public static NbtBase<Double> of(String name, double value) {
|
||||||
return ofType(NbtType.TAG_DOUBlE, name, 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<byte[]> of(String name, byte[] value) {
|
public static NbtBase<byte[]> of(String name, byte[] value) {
|
||||||
return ofType(NbtType.TAG_BYTE_ARRAY, name, 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<int[]> of(String name, int[] value) {
|
public static NbtBase<int[]> of(String name, int[] value) {
|
||||||
return ofType(NbtType.TAG_INT_ARRAY, name, value);
|
return ofType(NbtType.TAG_INT_ARRAY, name, value);
|
||||||
}
|
}
|
||||||
|
@ -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<Integer> test = new NbtCustomTag<Integer>("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 <TValue> - the value of the tag.
|
||||||
|
*/
|
||||||
|
public static class NbtCustomTag<TValue> implements NbtBase<TValue> {
|
||||||
|
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<TValue> deepClone() {
|
||||||
|
return new NbtCustomTag<TValue>(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user