From 342630680507f924bd45397c34cba7fe83bcaa9c Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 8 Jan 2013 22:24:07 +0100 Subject: [PATCH] Finalize the operation to make the NBT wrappers into interfaces. --- .../protocol/events/PacketContainer.java | 4 +- .../protocol/wrappers/BukkitConverters.java | 18 +-- .../wrappers/nbt/AbstractConverted.java | 8 ++ .../wrappers/nbt/ConvertedCollection.java | 8 ++ .../protocol/wrappers/nbt/ConvertedList.java | 8 ++ .../protocol/wrappers/nbt/ConvertedMap.java | 8 ++ .../protocol/wrappers/nbt/ConvertedSet.java | 8 ++ .../protocol/wrappers/nbt/NbtBase.java | 12 +- .../protocol/wrappers/nbt/NbtCompound.java | 3 + .../protocol/wrappers/nbt/NbtFactory.java | 106 ++++++++++-------- .../protocol/wrappers/nbt/NbtList.java | 3 + .../protocol/wrappers/nbt/NbtWrapper.java | 2 + .../wrappers/nbt/WrappedCompound.java | 8 +- .../protocol/wrappers/nbt/WrappedElement.java | 22 ++-- .../protocol/wrappers/nbt/WrappedList.java | 8 +- .../protocol/events/PacketContainerTest.java | 3 +- 16 files changed, 150 insertions(+), 79 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java index 1751b370..4c679cb3 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -56,7 +56,7 @@ import com.comphenix.protocol.wrappers.BukkitConverters; import com.comphenix.protocol.wrappers.ChunkPosition; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; -import com.comphenix.protocol.wrappers.nbt.NbtWrapper; +import com.comphenix.protocol.wrappers.nbt.NbtBase; import com.google.common.base.Function; import com.google.common.collect.Maps; @@ -369,7 +369,7 @@ public class PacketContainer implements Serializable { * Retrieves a read/write structure for NBT classes. * @return A modifier for NBT classes. */ - public StructureModifier> getNbtModifier() { + public StructureModifier> getNbtModifier() { // Allow access to the NBT class in packet 130 return structureModifier.withType( MinecraftReflection.getNBTBaseClass(), diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index 7b78bb53..95606336 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -34,8 +34,8 @@ import com.comphenix.protocol.reflect.EquivalentConverter; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.instances.DefaultInstances; import com.comphenix.protocol.utility.MinecraftReflection; +import com.comphenix.protocol.wrappers.nbt.NbtBase; import com.comphenix.protocol.wrappers.nbt.NbtFactory; -import com.comphenix.protocol.wrappers.nbt.NbtWrapper; /** * Contains several useful equivalent converters for normal Bukkit types. @@ -214,24 +214,24 @@ public class BukkitConverters { * Retrieve an equivalent converter for net.minecraft.server NBT classes and their wrappers. * @return An equivalent converter for NBT. */ - public static EquivalentConverter> getNbtConverter() { - return getIgnoreNull(new EquivalentConverter>() { + public static EquivalentConverter> getNbtConverter() { + return getIgnoreNull(new EquivalentConverter>() { @Override - public Object getGeneric(Class genericType, NbtWrapper specific) { - return specific.getHandle(); + public Object getGeneric(Class genericType, NbtBase specific) { + return NbtFactory.fromBase(specific).getHandle(); } @Override - public NbtWrapper getSpecific(Object generic) { + public NbtBase getSpecific(Object generic) { return NbtFactory.fromNMS(generic); } @Override @SuppressWarnings("unchecked") - public Class> getSpecificType() { + public Class> getSpecificType() { // Damn you Java AGAIN - Class dummy = NbtWrapper.class; - return (Class>) dummy; + Class dummy = NbtBase.class; + return (Class>) dummy; } }); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java index e3670a3d..554d4c6b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/AbstractConverted.java @@ -21,6 +21,14 @@ import javax.annotation.Nullable; import com.google.common.base.Function; +/** + * Represents an object that transform elements of type VInner to type VOuter and back again. + * + * @author Kristian + * + * @param - the first type. + * @param - the second type. + */ abstract class AbstractConverted { /** * Convert a value from the inner map to the outer visible map. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java index 9af8189a..dea203e6 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedCollection.java @@ -25,6 +25,14 @@ import java.util.List; import com.google.common.collect.Iterators; import com.google.common.collect.Lists; +/** + * Represents a collection that wraps another collection by transforming the elements going in and out. + * + * @author Kristian + * + * @param - type of the element in the inner invisible collection. + * @param - type of the elements publically accessible in the outer collection. + */ abstract class ConvertedCollection extends AbstractConverted implements Collection { // Inner collection private Collection inner; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java index 15e21092..3562a7c5 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedList.java @@ -21,6 +21,14 @@ import java.util.Collection; import java.util.List; import java.util.ListIterator; +/** + * Represents a list that wraps another list by transforming the items going in and out. + * + * @author Kristian + * + * @param - type of the items in the inner invisible list. + * @param - type of the items publically accessible in the outer list. + */ abstract class ConvertedList extends ConvertedCollection implements List { private List inner; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java index 7f58f6bd..b662d0da 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedMap.java @@ -21,6 +21,14 @@ import java.util.Collection; import java.util.Map; import java.util.Set; +/** + * Represents a map that wraps another map by transforming the entries going in and out. + * + * @author Kristian + * + * @param - type of the value in the entries in the inner invisible map. + * @param - type of the value in the entries publically accessible in the outer map. + */ abstract class ConvertedMap extends AbstractConverted implements Map { // Inner map private Map inner; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java index 15c072eb..5aa0d203 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/ConvertedSet.java @@ -20,6 +20,14 @@ package com.comphenix.protocol.wrappers.nbt; import java.util.Collection; import java.util.Set; +/** + * Represents a set that wraps another set by transforming the items going in and out. + * + * @author Kristian + * + * @param - type of the element in the inner invisible set. + * @param - type of the elements publically accessible in the outer set. + */ abstract class ConvertedSet extends ConvertedCollection implements Set { public ConvertedSet(Collection inner) { super(inner); 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 eb1b86a7..985f6e22 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 @@ -22,8 +22,10 @@ import com.comphenix.protocol.wrappers.nbt.NbtType; /** * Represents a generic container for an NBT element. - * @author Kristian + *

+ * Use {@link NbtFactory} to load or create an instance. * + * @author Kristian * @param - type of the value that is stored. */ public interface NbtBase { @@ -52,7 +54,13 @@ public interface NbtBase { /** * Retrieve the value of this NBT tag. *

- * Is either a primitive wrapper, a list or a map. + * Is either a primitive {@link java.lang.Number wrapper}, {@link java.lang.String String}, + * {@link java.util.List List} or a {@link java.util.Map Map}. + *

+ * All operations that modify collections directly, such as {@link java.util.List#add(Object) List.add(Object)} or + * {@link java.util.Map#clear() Map.clear()}, are considered optional. This also include members in {@link java.util.Iterator Iterator} and + * {@link java.util.ListIterator ListIterator}. Operations that are not implemented throw a + * {@link java.lang.UnsupportedOperationException UnsupportedOperationException}. * @return Value of this tag. */ public abstract TType getValue(); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java index 35cc0075..ee066ffe 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtCompound.java @@ -9,6 +9,9 @@ import java.util.Set; * Represents a mapping of arbitrary NBT elements and their unique names. *

* Use {@link NbtFactory} to load or create an instance. + *

+ * The {@link NbtBase#getValue()} method returns a {@link java.util.Map} that will correctly return the content + * of this NBT compound, but may throw an {@link UnsupportedOperationException} for any of the write operations. * * @author Kristian */ 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 53c9d7c6..95885165 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 @@ -49,47 +49,47 @@ public class NbtFactory { private static StructureModifier itemStackModifier; /** - * Attempt to cast this wrapper as a compund. + * Attempt to cast this NBT tag as a compund. + * @param tag - the NBT tag to cast. * @return This instance as a compound. * @throws UnsupportedOperationException If this is not a compound. */ - public static NbtCompound asCompound(NbtWrapper wrapper) { - if (wrapper instanceof NbtCompound) - return (NbtCompound) wrapper; - else if (wrapper != null) + public static NbtCompound asCompound(NbtBase tag) { + if (tag instanceof NbtCompound) + return (NbtCompound) tag; + else if (tag != null) throw new UnsupportedOperationException( - "Cannot cast a " + wrapper.getClass() + "( " + wrapper.getType() + ") to TAG_COMPUND."); + "Cannot cast a " + tag.getClass() + "( " + tag.getType() + ") to TAG_COMPUND."); else - throw new IllegalArgumentException("Wrapper cannot be NULL."); + throw new IllegalArgumentException("Tag cannot be NULL."); } /** - * Attempt to cast this wrapper as a list. + * Attempt to cast this NBT tag as a list. + * @param tag - the NBT tag to cast. * @return This instance as a list. * @throws UnsupportedOperationException If this is not a list. */ - public static NbtList asList(NbtWrapper wrapper) { - if (wrapper instanceof NbtList) - return (NbtList) wrapper; - else if (wrapper != null) + public static NbtList asList(NbtBase tag) { + if (tag instanceof NbtList) + return (NbtList) tag; + else if (tag != null) throw new UnsupportedOperationException( - "Cannot cast a " + wrapper.getClass() + "( " + wrapper.getType() + ") to TAG_LIST."); + "Cannot cast a " + tag.getClass() + "( " + tag.getType() + ") to TAG_LIST."); else - throw new IllegalArgumentException("Wrapper cannot be NULL."); + throw new IllegalArgumentException("Tag cannot be NULL."); } /** * Get a NBT wrapper from a NBT base. + *

+ * This may clone the content if the NbtBase is not a NbtWrapper. * @param base - the base class. * @return A NBT wrapper. */ @SuppressWarnings("unchecked") public static NbtWrapper fromBase(NbtBase base) { - if (base instanceof WrappedElement) { - return (WrappedElement) base; - } else if (base instanceof WrappedCompound) { - return (NbtWrapper) base; - } else if (base instanceof WrappedList) { + if (base instanceof NbtWrapper) { return (NbtWrapper) base; } else { if (base.getType() == NbtType.TAG_COMPOUND) { @@ -109,7 +109,7 @@ public class NbtFactory { } else { // Copy directly - NbtWrapper copy = ofType(base.getType(), base.getName()); + NbtWrapper copy = ofWrapper(base.getType(), base.getName()); copy.setValue(base.getValue()); return copy; @@ -136,17 +136,17 @@ public class NbtFactory { } // Use the first and best NBT tag - StructureModifier> modifier = itemStackModifier. + StructureModifier> modifier = itemStackModifier. withTarget(nmsStack). withType(MinecraftReflection.getNBTBaseClass(), BukkitConverters.getNbtConverter()); - NbtWrapper result = modifier.read(0); + NbtBase result = modifier.read(0); // Create the tag if it doesn't exist if (result == null) { result = NbtFactory.ofCompound("tag"); modifier.write(0, result); } - return result; + return fromBase(result); } /** @@ -215,8 +215,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, String value) { - return ofType(NbtType.TAG_STRING, name, value); + public static NbtBase of(String name, String value) { + return ofWrapper(NbtType.TAG_STRING, name, value); } /** @@ -225,8 +225,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, byte value) { - return ofType(NbtType.TAG_BYTE, name, value); + public static NbtBase of(String name, byte value) { + return ofWrapper(NbtType.TAG_BYTE, name, value); } /** @@ -235,8 +235,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, short value) { - return ofType(NbtType.TAG_SHORT, name, value); + public static NbtBase of(String name, short value) { + return ofWrapper(NbtType.TAG_SHORT, name, value); } /** @@ -245,8 +245,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, int value) { - return ofType(NbtType.TAG_INT, name, value); + public static NbtBase of(String name, int value) { + return ofWrapper(NbtType.TAG_INT, name, value); } /** @@ -255,8 +255,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, long value) { - return ofType(NbtType.TAG_LONG, name, value); + public static NbtBase of(String name, long value) { + return ofWrapper(NbtType.TAG_LONG, name, value); } /** @@ -265,8 +265,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, float value) { - return ofType(NbtType.TAG_FLOAT, name, value); + public static NbtBase of(String name, float value) { + return ofWrapper(NbtType.TAG_FLOAT, name, value); } /** @@ -275,8 +275,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, double value) { - return ofType(NbtType.TAG_DOUBlE, name, value); + public static NbtBase of(String name, double value) { + return ofWrapper(NbtType.TAG_DOUBlE, name, value); } /** @@ -285,8 +285,8 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, byte[] value) { - return ofType(NbtType.TAG_BYTE_ARRAY, name, value); + public static NbtBase of(String name, byte[] value) { + return ofWrapper(NbtType.TAG_BYTE_ARRAY, name, value); } /** @@ -295,12 +295,12 @@ public class NbtFactory { * @param value - value of the tag. * @return The constructed NBT tag. */ - public static NbtWrapper of(String name, int[] value) { - return ofType(NbtType.TAG_INT_ARRAY, name, value); + public static NbtBase of(String name, int[] value) { + return ofWrapper(NbtType.TAG_INT_ARRAY, name, value); } /** - * Construct a new NBT compound wrapper initialized with a given list of NBT values. + * Construct a new NBT compound initialized with a given list of NBT values. * @param name - the name of the compound wrapper. * @param list - the list of elements to add. * @return The new wrapped NBT compound. @@ -314,7 +314,7 @@ public class NbtFactory { * @param name - the name of the compound wrapper. * @return The new wrapped NBT compound. */ - public static WrappedCompound ofCompound(String name) { + public static NbtCompound ofCompound(String name) { return WrappedCompound.fromName(name); } @@ -328,6 +328,16 @@ public class NbtFactory { return WrappedList.fromArray(name, elements); } + /** + * Construct a NBT list of out a list of values. + * @param name - name of this list. + * @param elements - elements to add. + * @return The new filled NBT list. + */ + public static NbtList ofList(String name, Collection elements) { + return WrappedList.fromList(name, elements); + } + /** * Create a new NBT wrapper from a given type. * @param type - the NBT type. @@ -336,7 +346,7 @@ public class NbtFactory { * @throws FieldAccessException If we're unable to create the underlying tag. */ @SuppressWarnings({"unchecked", "rawtypes"}) - public static NbtWrapper ofType(NbtType type, String name) { + public static NbtWrapper ofWrapper(NbtType type, String name) { if (type == null) throw new IllegalArgumentException("type cannot be NULL."); if (type == NbtType.TAG_END) @@ -376,8 +386,8 @@ public class NbtFactory { * @return The new wrapped NBT tag. * @throws FieldAccessException If we're unable to create the underlying tag. */ - public static NbtWrapper ofType(NbtType type, String name, T value) { - NbtWrapper created = ofType(type, name); + public static NbtWrapper ofWrapper(NbtType type, String name, T value) { + NbtWrapper created = ofWrapper(type, name); // Update the value created.setValue(value); @@ -393,7 +403,7 @@ public class NbtFactory { * @throws FieldAccessException If we're unable to create the underlying tag. * @throws IllegalArgumentException If the given class type is not valid NBT. */ - public static NbtWrapper ofType(Class type, String name, T value) { - return ofType(NbtType.getTypeFromClass(type), name, value); + public static NbtWrapper ofWrapper(Class type, String name, T value) { + return ofWrapper(NbtType.getTypeFromClass(type), name, value); } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java index f4db93e2..1aa823a2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtList.java @@ -8,6 +8,9 @@ import java.util.List; * Represents a list of NBT tags of the same type without names. *

* Use {@link NbtFactory} to load or create an instance. + *

+ * The {@link NbtBase#getValue()} method returns a {@link java.util.List} that will correctly return the content + * of this NBT list, but may throw an {@link UnsupportedOperationException} for any of the write operations. * * @author Kristian * diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtWrapper.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtWrapper.java index 7317d474..da1916f0 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtWrapper.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtWrapper.java @@ -21,6 +21,8 @@ import java.io.DataOutput; /** * Indicates that this NBT wraps an underlying net.minecraft.server instance. + *

+ * Use {@link NbtFactory} to load or create instances. * * @author Kristian * diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java index 2a5315dd..899fae1e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedCompound.java @@ -28,7 +28,7 @@ import java.util.Set; * * @author Kristian */ -public class WrappedCompound implements NbtWrapper>>, Iterable>, NbtCompound { +class WrappedCompound implements NbtWrapper>>, Iterable>, NbtCompound { // A list container private WrappedElement> container; @@ -42,7 +42,7 @@ public class WrappedCompound implements NbtWrapper>>, Ite */ public static WrappedCompound fromName(String name) { // Simplify things for the caller - return (WrappedCompound) NbtFactory.>>ofType(NbtType.TAG_COMPOUND, name); + return (WrappedCompound) NbtFactory.>>ofWrapper(NbtType.TAG_COMPOUND, name); } /** @@ -63,7 +63,7 @@ public class WrappedCompound implements NbtWrapper>>, Ite * Construct a wrapped compound from a given NMS handle. * @param handle - the NMS handle. */ - WrappedCompound(Object handle) { + public WrappedCompound(Object handle) { this.container = new WrappedElement>(handle); } @@ -164,7 +164,7 @@ public class WrappedCompound implements NbtWrapper>>, Ite // Create or get a compound if (nbt == null) - put(nbt = NbtFactory.ofType(type, key)); + put(nbt = NbtFactory.ofWrapper(type, key)); else if (nbt.getType() != type) throw new IllegalArgumentException("Cannot get tag " + nbt + ": Not a " + type); diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedElement.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedElement.java index 963d7bdf..ff855cfc 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedElement.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedElement.java @@ -27,14 +27,12 @@ import com.comphenix.protocol.utility.MinecraftReflection; import com.google.common.base.Objects; /** - * Represents an arbitrary NBT tag element, composite or not. - *

- * Use {@link NbtFactory} to load or create an instance. - * @author Kristian + * Represents a wrapped NBT tag element, composite or not. * + * @author Kristian * @param - type of the value field. */ -public class WrappedElement implements NbtWrapper { +class WrappedElement implements NbtWrapper { // Structure modifier for the base class private static volatile StructureModifier baseModifier; @@ -57,7 +55,7 @@ public class WrappedElement implements NbtWrapper { * Initialize a NBT wrapper for a generic element. * @param handle - the NBT element to wrap. */ - WrappedElement(Object handle) { + public WrappedElement(Object handle) { this.handle = handle; } @@ -135,12 +133,20 @@ public class WrappedElement implements NbtWrapper { return type; } - NbtType getSubType() { + /** + * Retrieve the sub element type of the underlying NMS NBT list. + * @return The NBT sub type. + */ + public NbtType getSubType() { int subID = getCurrentBaseModifier().withType(byte.class).withTarget(handle).read(0); return NbtType.getTypeFromID(subID); } - void setSubType(NbtType type) { + /** + * Set the sub element type of the underlying NMS NBT list. + * @param type - the new sub element type. + */ + public void setSubType(NbtType type) { byte subID = (byte) type.getRawID(); getCurrentBaseModifier().withType(byte.class).withTarget(handle).write(0, subID); } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java index 4f2df203..24fa7a05 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/WrappedList.java @@ -34,7 +34,7 @@ import com.google.common.collect.Iterables; * * @param - the type of the value in each NBT sub element. */ -public class WrappedList implements NbtWrapper>>, Iterable, NbtList { +class WrappedList implements NbtWrapper>>, Iterable, NbtList { // A list container private WrappedElement> container; @@ -47,7 +47,7 @@ public class WrappedList implements NbtWrapper>>, Ite * @return The new empty NBT list. */ public static WrappedList fromName(String name) { - return (WrappedList) NbtFactory.>>ofType(NbtType.TAG_LIST, name); + return (WrappedList) NbtFactory.>>ofWrapper(NbtType.TAG_LIST, name); } /** @@ -62,7 +62,7 @@ public class WrappedList implements NbtWrapper>>, Ite for (T element : elements) { if (element == null) throw new IllegalArgumentException("An NBT list cannot contain a null element!"); - result.add(NbtFactory.ofType(element.getClass(), EMPTY_NAME, element)); + result.add(NbtFactory.ofWrapper(element.getClass(), EMPTY_NAME, element)); } return result; } @@ -79,7 +79,7 @@ public class WrappedList implements NbtWrapper>>, Ite for (T element : elements) { if (element == null) throw new IllegalArgumentException("An NBT list cannot contain a null element!"); - result.add(NbtFactory.ofType(element.getClass(), EMPTY_NAME, element)); + result.add(NbtFactory.ofWrapper(element.getClass(), EMPTY_NAME, element)); } return result; } diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java index 6df0bf45..8359c753 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/events/PacketContainerTest.java @@ -50,7 +50,6 @@ import com.comphenix.protocol.wrappers.ChunkPosition; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.nbt.NbtCompound; -import com.comphenix.protocol.wrappers.nbt.WrappedCompound; import com.comphenix.protocol.wrappers.nbt.NbtFactory; import com.google.common.collect.Iterables; @@ -257,7 +256,7 @@ public class PacketContainerTest { public void testGetNbtModifier() { PacketContainer updateTileEntity = new PacketContainer(132); - WrappedCompound compound = NbtFactory.ofCompound("test"); + NbtCompound compound = NbtFactory.ofCompound("test"); compound.put("test", "name"); compound.put(NbtFactory.ofList("ages", 1, 2, 3));