Archiviert
13
0

NbtCompound can now accepts arbitrary primitive, list or map objects.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-02-18 13:27:41 +01:00
Ursprung e919056f9b
Commit 61ae40b936
3 geänderte Dateien mit 101 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,65 @@
package com.comphenix.protocol.wrappers.nbt;
class MemoryElement<TType> implements NbtBase<TType> {
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<TType> deepClone() {
// This assumes value is an immutable object
return new MemoryElement<TType>(name, value, type);
}
}

Datei anzeigen

@ -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.
* <p>
@ -52,8 +54,9 @@ public interface NbtCompound extends NbtBase<Map<String, 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 <T> NbtCompound put(NbtBase<T> entry);
public abstract <T> NbtCompound put(@Nonnull NbtBase<T> entry);
/**
* Retrieve the string value of an entry identified by a given key.
@ -255,7 +258,18 @@ public interface NbtCompound extends NbtBase<Map<String, 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.
* <p>
* 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.

Datei anzeigen

@ -152,7 +152,13 @@ class WrappedCompound implements NbtWrapper<Map<String, NbtBase<?>>>, Iterable<N
public void setValue(Map<String, NbtBase<?>> newValue) {
// Write all the entries
for (Map.Entry<String, NbtBase<?>> 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<Map<String, NbtBase<?>>>, Iterable<N
return this;
}
@Override
public NbtCompound putObject(String key, Object value) {
if (value == null) {
remove(key);
} else if (value instanceof NbtBase) {
put(key, (NbtBase<?>) value);
} else {
NbtBase<?> base = new MemoryElement<Object>(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.