diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/MemoryElement.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/MemoryElement.java new file mode 100644 index 00000000..e8287954 --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/MemoryElement.java @@ -0,0 +1,65 @@ +package com.comphenix.protocol.wrappers.nbt; + +class MemoryElement implements NbtBase { + private String name; + private TType value; + private NbtType type; + + public MemoryElement(String name, TType value) { + if (name == null) + throw new IllegalArgumentException("Name cannot be NULL."); + if (value == null) + throw new IllegalArgumentException("Element cannot be NULL."); + + this.name = name; + this.value = value; + this.type = NbtType.getTypeFromClass(value.getClass()); + } + + public MemoryElement(String name, TType value, NbtType type) { + if (name == null) + throw new IllegalArgumentException("Name cannot be NULL."); + if (type == null) + throw new IllegalArgumentException("Type cannot be NULL."); + + this.name = name; + this.value = value; + this.type = type; + } + + @Override + public boolean accept(NbtVisitor visitor) { + return visitor.visit(this); + } + + @Override + public NbtType getType() { + return type; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public TType getValue() { + return value; + } + + @Override + public void setValue(TType newValue) { + this.value = newValue; + } + + @Override + public NbtBase deepClone() { + // This assumes value is an immutable object + return new MemoryElement(name, value, type); + } +} 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 cc4deeb3..245a1c42 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 @@ -5,6 +5,8 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +import javax.annotation.Nonnull; + /** * Represents a mapping of arbitrary NBT elements and their unique names. *

@@ -52,8 +54,9 @@ public interface NbtCompound extends NbtBase>>, Iterable< * Set a entry based on its name. * @param entry - entry with a name and value. * @return This compound, for chaining. + * @throws IllegalArgumentException If entry is NULL. */ - public abstract NbtCompound put(NbtBase entry); + public abstract NbtCompound put(@Nonnull NbtBase entry); /** * Retrieve the string value of an entry identified by a given key. @@ -255,7 +258,18 @@ public interface NbtCompound extends NbtBase>>, Iterable< * @return This current compound, for chaining. */ public abstract NbtCompound put(String key, int[] value); - + + /** + * Associates a given Java primitive value, list, map or NbtBase with a certain key. + *

+ * If the value is NULL, the corresponding key is removed. + * + * @param key - the name of the new entry, + * @param value - the value of the new entry, or NULL to remove the current value. + * @return This current compound, for chaining. + */ + public abstract NbtCompound putObject(String key, Object value); + /** * Retrieve the compound (map) value of an entry identified by a given key. * @param key - the key of the entry. 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 217c555d..222d8340 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 @@ -152,7 +152,13 @@ class WrappedCompound implements NbtWrapper>>, Iterable> newValue) { // Write all the entries for (Map.Entry> entry : newValue.entrySet()) { - put(entry.getValue()); + Object value = entry.getValue(); + + // We don't really know + if (value instanceof NbtBase) + put(entry.getValue()); + else + putObject(entry.getKey(), entry.getValue()); } } @@ -255,6 +261,19 @@ class WrappedCompound implements NbtWrapper>>, Iterable) value); + } else { + NbtBase base = new MemoryElement(key, value); + put(base); + } + return this; + } + /** * Retrieve the byte value of an entry identified by a given key. * @param key - the key of the entry.