Finish reimplementing everything in adventure-nbt

This commit is contained in:
Octavia Togami 2021-02-14 21:31:04 -08:00
parent 628c3f2ca1
commit 3b25230f19
No known key found for this signature in database
GPG Key ID: CC364524D1983C99
19 changed files with 295 additions and 512 deletions

View File

@ -19,20 +19,18 @@
package com.sk89q.jnbt;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.util.nbt.BinaryTag;
import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag;
import com.sk89q.worldedit.util.nbt.ByteBinaryTag;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import com.sk89q.worldedit.util.nbt.DoubleBinaryTag;
import com.sk89q.worldedit.util.nbt.EndBinaryTag;
import com.sk89q.worldedit.util.nbt.FloatBinaryTag;
import com.sk89q.worldedit.util.nbt.IntArrayBinaryTag;
import com.sk89q.worldedit.util.nbt.IntBinaryTag;
import com.sk89q.worldedit.util.nbt.ListBinaryTag;
import com.sk89q.worldedit.util.nbt.LongArrayBinaryTag;
import com.sk89q.worldedit.util.nbt.LongBinaryTag;
import com.sk89q.worldedit.util.nbt.ShortBinaryTag;
import com.sk89q.worldedit.util.nbt.StringBinaryTag;
import com.sk89q.worldedit.util.nbt.BinaryTagType;
import com.sk89q.worldedit.util.nbt.BinaryTagTypes;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
/**
* Converts between JNBT and Adventure-NBT classes.
@ -41,92 +39,67 @@ import com.sk89q.worldedit.util.nbt.StringBinaryTag;
*/
@Deprecated
public class AdventureNBTConverter {
private static final BiMap<Class<? extends Tag>, BinaryTagType<?>> TAG_TYPES =
new ImmutableBiMap.Builder<Class<? extends Tag>, BinaryTagType<?>>()
.put(ByteArrayTag.class, BinaryTagTypes.BYTE_ARRAY)
.put(ByteTag.class, BinaryTagTypes.BYTE)
.put(CompoundTag.class, BinaryTagTypes.COMPOUND)
.put(DoubleTag.class, BinaryTagTypes.DOUBLE)
.put(EndTag.class, BinaryTagTypes.END)
.put(FloatTag.class, BinaryTagTypes.FLOAT)
.put(IntArrayTag.class, BinaryTagTypes.INT_ARRAY)
.put(IntTag.class, BinaryTagTypes.INT)
.put(ListTag.class, BinaryTagTypes.LIST)
.put(LongArrayTag.class, BinaryTagTypes.LONG_ARRAY)
.put(LongTag.class, BinaryTagTypes.LONG)
.put(ShortTag.class, BinaryTagTypes.SHORT)
.put(StringTag.class, BinaryTagTypes.STRING)
.build();
private static final Map<BinaryTagType<?>, Function<BinaryTag, Tag>> CONVERSION;
static {
ImmutableMap.Builder<BinaryTagType<?>, Function<BinaryTag, Tag>> conversion =
ImmutableMap.builder();
for (Map.Entry<Class<? extends Tag>, BinaryTagType<?>> tag : TAG_TYPES.entrySet()) {
Constructor<?>[] constructors = tag.getKey().getConstructors();
for (Constructor<?> c : constructors) {
if (c.getParameterCount() == 1 && BinaryTag.class.isAssignableFrom(c.getParameterTypes()[0])) {
conversion.put(tag.getValue(), binaryTag -> {
try {
return (Tag) c.newInstance(binaryTag);
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
// I assume this is always a RuntimeException since we control the ctor
throw (RuntimeException) e.getCause();
}
});
break;
}
}
}
CONVERSION = conversion.build();
}
public static BinaryTagType<?> getAdventureType(Class<? extends Tag> type) {
return Objects.requireNonNull(TAG_TYPES.get(type), () -> "Missing entry for " + type);
}
public static Class<? extends Tag> getJNBTType(BinaryTagType<?> type) {
return Objects.requireNonNull(TAG_TYPES.inverse().get(type), () -> "Missing entry for " + type);
}
private AdventureNBTConverter() {
}
public static Tag fromAdventure(BinaryTag other) {
if (other instanceof IntArrayBinaryTag) {
return fromAdventure((IntArrayBinaryTag) other);
} else if (other instanceof ListBinaryTag) {
return fromAdventure((ListBinaryTag) other);
} else if (other instanceof EndBinaryTag) {
return fromAdventure();
} else if (other instanceof LongBinaryTag) {
return fromAdventure((LongBinaryTag) other);
} else if (other instanceof LongArrayBinaryTag) {
return fromAdventure((LongArrayBinaryTag) other);
} else if (other instanceof StringBinaryTag) {
return fromAdventure((StringBinaryTag) other);
} else if (other instanceof IntBinaryTag) {
return fromAdventure((IntBinaryTag) other);
} else if (other instanceof ByteBinaryTag) {
return fromAdventure((ByteBinaryTag) other);
} else if (other instanceof ByteArrayBinaryTag) {
return fromAdventure((ByteArrayBinaryTag) other);
} else if (other instanceof CompoundBinaryTag) {
return fromAdventure((CompoundBinaryTag) other);
} else if (other instanceof FloatBinaryTag) {
return fromAdventure((FloatBinaryTag) other);
} else if (other instanceof ShortBinaryTag) {
return fromAdventure((ShortBinaryTag) other);
} else if (other instanceof DoubleBinaryTag) {
return fromAdventure((DoubleBinaryTag) other);
} else {
Function<BinaryTag, Tag> conversion = CONVERSION.get(other.type());
if (conversion == null) {
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
}
}
public static DoubleTag fromAdventure(DoubleBinaryTag other) {
return new DoubleTag(other);
}
public static ShortTag fromAdventure(ShortBinaryTag other) {
return new ShortTag(other);
}
public static FloatTag fromAdventure(FloatBinaryTag other) {
return new FloatTag(other);
}
public static CompoundTag fromAdventure(CompoundBinaryTag other) {
return new CompoundTag(other);
}
public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) {
return new ByteArrayTag(other);
}
public static ByteTag fromAdventure(ByteBinaryTag other) {
return new ByteTag(other);
}
public static IntTag fromAdventure(IntBinaryTag other) {
return new IntTag(other);
}
public static StringTag fromAdventure(StringBinaryTag other) {
return new StringTag(other);
}
public static LongArrayTag fromAdventure(LongArrayBinaryTag other) {
return new LongArrayTag(other);
}
public static LongTag fromAdventure(LongBinaryTag other) {
return new LongTag(other);
}
public static EndTag fromAdventure() {
return new EndTag();
}
public static ListTag fromAdventure(ListBinaryTag other) {
return new ListTag(other);
}
public static IntArrayTag fromAdventure(IntArrayBinaryTag other) {
return new IntArrayTag(other);
return conversion.apply(other);
}
}

View File

@ -43,7 +43,7 @@ public final class ByteArrayTag extends Tag {
this.innerTag = ByteArrayBinaryTag.of(value);
}
ByteArrayTag(ByteArrayBinaryTag adventureTag) {
public ByteArrayTag(ByteArrayBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -41,7 +41,7 @@ public final class ByteTag extends Tag {
this.innerTag = ByteBinaryTag.of(value);
}
ByteTag(ByteBinaryTag adventureTag) {
public ByteTag(ByteBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -19,13 +19,16 @@
package com.sk89q.jnbt;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.util.nbt.BinaryTag;
import com.sk89q.worldedit.util.nbt.BinaryTagLike;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import com.sk89q.worldedit.util.nbt.NumberBinaryTag;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* The {@code TAG_Compound} tag.
@ -35,7 +38,7 @@ import java.util.Set;
@Deprecated
public final class CompoundTag extends Tag {
private final Map<String, Tag> value;
private final CompoundBinaryTag innerTag;
/**
* Creates the tag with an empty name.
@ -43,17 +46,13 @@ public final class CompoundTag extends Tag {
* @param value the value of the tag
*/
public CompoundTag(Map<String, Tag> value) {
super();
this.value = Collections.unmodifiableMap(value);
this(CompoundBinaryTag.builder()
.put(Maps.transformValues(value, BinaryTagLike::asBinaryTag))
.build());
}
CompoundTag(CompoundBinaryTag adventureTag) {
Set<String> tags = adventureTag.keySet();
Map<String, Tag> map = new HashMap<>();
for (String tagName : tags) {
map.put(tagName, AdventureNBTConverter.fromAdventure(adventureTag.get(tagName)));
}
this.value = Collections.unmodifiableMap(map);
public CompoundTag(CompoundBinaryTag adventureTag) {
this.innerTag = adventureTag;
}
/**
@ -63,12 +62,16 @@ public final class CompoundTag extends Tag {
* @return true if the tag contains the given key
*/
public boolean containsKey(String key) {
return value.containsKey(key);
return innerTag.keySet().contains(key);
}
@Override
public Map<String, Tag> getValue() {
return value;
ImmutableMap.Builder<String, Tag> map = ImmutableMap.builder();
for (String key : innerTag.keySet()) {
map.put(key, AdventureNBTConverter.fromAdventure(innerTag.get(key)));
}
return map.build();
}
/**
@ -87,7 +90,7 @@ public final class CompoundTag extends Tag {
* @return the builder
*/
public CompoundTagBuilder createBuilder() {
return new CompoundTagBuilder(new HashMap<>(value));
return new CompoundTagBuilder(innerTag);
}
/**
@ -100,12 +103,7 @@ public final class CompoundTag extends Tag {
* @return a byte array
*/
public byte[] getByteArray(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteArrayTag) {
return ((ByteArrayTag) tag).getValue();
} else {
return new byte[0];
}
return this.innerTag.getByteArray(key);
}
/**
@ -118,12 +116,7 @@ public final class CompoundTag extends Tag {
* @return a byte
*/
public byte getByte(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else {
return (byte) 0;
}
return this.innerTag.getByte(key);
}
/**
@ -136,12 +129,7 @@ public final class CompoundTag extends Tag {
* @return a double
*/
public double getDouble(String key) {
Tag tag = value.get(key);
if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
} else {
return 0;
}
return this.innerTag.getDouble(key);
}
/**
@ -155,28 +143,11 @@ public final class CompoundTag extends Tag {
* @return a double
*/
public double asDouble(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
} else {
return 0;
BinaryTag tag = this.innerTag.get(key);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).doubleValue();
}
return 0;
}
/**
@ -189,12 +160,7 @@ public final class CompoundTag extends Tag {
* @return a float
*/
public float getFloat(String key) {
Tag tag = value.get(key);
if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
} else {
return 0;
}
return this.innerTag.getFloat(key);
}
/**
@ -207,12 +173,7 @@ public final class CompoundTag extends Tag {
* @return an int array
*/
public int[] getIntArray(String key) {
Tag tag = value.get(key);
if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue();
} else {
return new int[0];
}
return this.innerTag.getIntArray(key);
}
/**
@ -225,12 +186,7 @@ public final class CompoundTag extends Tag {
* @return an int
*/
public int getInt(String key) {
Tag tag = value.get(key);
if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else {
return 0;
}
return this.innerTag.getInt(key);
}
/**
@ -244,28 +200,11 @@ public final class CompoundTag extends Tag {
* @return an int
*/
public int asInt(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue().intValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().intValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().intValue();
} else {
return 0;
BinaryTag tag = this.innerTag.get(key);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).intValue();
}
return 0;
}
/**
@ -278,12 +217,7 @@ public final class CompoundTag extends Tag {
* @return a list of tags
*/
public List<Tag> getList(String key) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
return ((ListTag) tag).getValue();
} else {
return Collections.emptyList();
}
return getListTag(key).getValue();
}
/**
@ -296,12 +230,7 @@ public final class CompoundTag extends Tag {
* @return a tag list instance
*/
public ListTag getListTag(String key) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
return (ListTag) tag;
} else {
return new ListTag(StringTag.class, Collections.<Tag>emptyList());
}
return new ListTag(this.innerTag.getList(key));
}
/**
@ -319,14 +248,9 @@ public final class CompoundTag extends Tag {
*/
@SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(String key, Class<T> listType) {
Tag tag = value.get(key);
if (tag instanceof ListTag) {
ListTag listTag = (ListTag) tag;
if (listTag.getType().equals(listType)) {
return (List<T>) listTag.getValue();
} else {
return Collections.emptyList();
}
ListTag listTag = getListTag(key);
if (listTag.getType().equals(listType)) {
return (List<T>) listTag.getValue();
} else {
return Collections.emptyList();
}
@ -342,12 +266,7 @@ public final class CompoundTag extends Tag {
* @return an int array
*/
public long[] getLongArray(String key) {
Tag tag = value.get(key);
if (tag instanceof LongArrayTag) {
return ((LongArrayTag) tag).getValue();
} else {
return new long[0];
}
return this.innerTag.getLongArray(key);
}
/**
@ -360,12 +279,7 @@ public final class CompoundTag extends Tag {
* @return a long
*/
public long getLong(String key) {
Tag tag = value.get(key);
if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else {
return 0L;
}
return this.innerTag.getLong(key);
}
/**
@ -379,28 +293,11 @@ public final class CompoundTag extends Tag {
* @return a long
*/
public long asLong(String key) {
Tag tag = value.get(key);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().longValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().longValue();
} else {
return 0L;
BinaryTag tag = this.innerTag.get(key);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).longValue();
}
return 0;
}
/**
@ -413,12 +310,7 @@ public final class CompoundTag extends Tag {
* @return a short
*/
public short getShort(String key) {
Tag tag = value.get(key);
if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else {
return 0;
}
return this.innerTag.getShort(key);
}
/**
@ -431,20 +323,11 @@ public final class CompoundTag extends Tag {
* @return a string
*/
public String getString(String key) {
Tag tag = value.get(key);
if (tag instanceof StringTag) {
return ((StringTag) tag).getValue();
} else {
return "";
}
return this.innerTag.getString(key);
}
@Override
public CompoundBinaryTag asBinaryTag() {
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
for (Map.Entry<String, Tag> child : getValue().entrySet()) {
builder.put(child.getKey(), child.getValue().asBinaryTag());
}
return builder.build();
return this.innerTag;
}
}

View File

@ -19,8 +19,10 @@
package com.sk89q.jnbt;
import java.util.HashMap;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import java.util.Map;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
@ -32,23 +34,24 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Deprecated
public class CompoundTagBuilder {
private final Map<String, Tag> entries;
private final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
/**
* Create a new instance.
*/
CompoundTagBuilder() {
this.entries = new HashMap<>();
}
/**
* Create a new instance and use the given map (which will be modified).
*
* @param value the value
* @param source the value
*/
CompoundTagBuilder(Map<String, Tag> value) {
checkNotNull(value);
this.entries = value;
CompoundTagBuilder(CompoundBinaryTag source) {
checkNotNull(source);
for (String key : source.keySet()) {
this.builder.put(key, Objects.requireNonNull(source.get(key)));
}
}
/**
@ -61,7 +64,7 @@ public class CompoundTagBuilder {
public CompoundTagBuilder put(String key, Tag value) {
checkNotNull(key);
checkNotNull(value);
entries.put(key, value);
this.builder.put(key, value.asBinaryTag());
return this;
}
@ -74,7 +77,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putByteArray(String key, byte[] value) {
return put(key, new ByteArrayTag(value));
this.builder.putByteArray(key, value);
return this;
}
/**
@ -86,7 +90,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putByte(String key, byte value) {
return put(key, new ByteTag(value));
this.builder.putByte(key, value);
return this;
}
/**
@ -98,7 +103,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putDouble(String key, double value) {
return put(key, new DoubleTag(value));
this.builder.putDouble(key, value);
return this;
}
/**
@ -110,7 +116,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putFloat(String key, float value) {
return put(key, new FloatTag(value));
this.builder.putFloat(key, value);
return this;
}
/**
@ -122,7 +129,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putIntArray(String key, int[] value) {
return put(key, new IntArrayTag(value));
this.builder.putIntArray(key, value);
return this;
}
/**
@ -133,7 +141,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putInt(String key, int value) {
return put(key, new IntTag(value));
this.builder.putInt(key, value);
return this;
}
/**
@ -145,7 +154,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putLongArray(String key, long[] value) {
return put(key, new LongArrayTag(value));
this.builder.putLongArray(key, value);
return this;
}
/**
@ -157,7 +167,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putLong(String key, long value) {
return put(key, new LongTag(value));
this.builder.putLong(key, value);
return this;
}
/**
@ -169,7 +180,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putShort(String key, short value) {
return put(key, new ShortTag(value));
this.builder.putShort(key, value);
return this;
}
/**
@ -181,7 +193,8 @@ public class CompoundTagBuilder {
* @return this object
*/
public CompoundTagBuilder putString(String key, String value) {
return put(key, new StringTag(value));
this.builder.putString(key, value);
return this;
}
/**
@ -192,7 +205,7 @@ public class CompoundTagBuilder {
*/
public CompoundTagBuilder remove(String key) {
checkNotNull(key);
entries.remove(key);
this.builder.remove(key);
return this;
}
@ -216,7 +229,7 @@ public class CompoundTagBuilder {
* @return the new compound tag
*/
public CompoundTag build() {
return new CompoundTag(new HashMap<>(entries));
return new CompoundTag(this.builder.build());
}
/**

View File

@ -41,7 +41,7 @@ public final class DoubleTag extends Tag {
this.innerTag = DoubleBinaryTag.of(value);
}
DoubleTag(DoubleBinaryTag adventureTag) {
public DoubleTag(DoubleBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -41,7 +41,7 @@ public final class FloatTag extends Tag {
this.innerTag = FloatBinaryTag.of(value);
}
FloatTag(FloatBinaryTag adventureTag) {
public FloatTag(FloatBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -44,7 +44,7 @@ public final class IntArrayTag extends Tag {
this.innerTag = IntArrayBinaryTag.of(value);
}
IntArrayTag(IntArrayBinaryTag adventureTag) {
public IntArrayTag(IntArrayBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -41,7 +41,7 @@ public final class IntTag extends Tag {
this.innerTag = IntBinaryTag.of(value);
}
IntTag(IntBinaryTag adventureTag) {
public IntTag(IntBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -20,15 +20,17 @@
package com.sk89q.jnbt;
import com.sk89q.worldedit.util.nbt.BinaryTag;
import com.sk89q.worldedit.util.nbt.BinaryTagLike;
import com.sk89q.worldedit.util.nbt.ListBinaryTag;
import com.sk89q.worldedit.util.nbt.NumberBinaryTag;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The {@code TAG_List} tag.
*
@ -37,8 +39,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Deprecated
public final class ListTag extends Tag {
private final Class<? extends Tag> type;
private final List<Tag> value;
private final ListBinaryTag innerTag;
/**
* Creates the tag with an empty name.
@ -47,37 +48,19 @@ public final class ListTag extends Tag {
* @param value the value of the tag
*/
public ListTag(Class<? extends Tag> type, List<? extends Tag> value) {
super();
checkNotNull(value);
this.type = type;
this.value = Collections.unmodifiableList(value);
this(ListBinaryTag.of(
AdventureNBTConverter.getAdventureType(type),
value.stream().map(BinaryTagLike::asBinaryTag).collect(Collectors.toList())
));
}
ListTag(ListBinaryTag adventureTag) {
super();
List<Tag> list = new ArrayList<>();
Class<? extends Tag> listClass = StringTag.class;
int tags = adventureTag.size();
for (int i = 0; i < tags; i++) {
Tag child = AdventureNBTConverter.fromAdventure(adventureTag.get(0));
list.add(child);
listClass = child.getClass();
}
this.type = listClass;
this.value = Collections.unmodifiableList(list);
public ListTag(ListBinaryTag adventureTag) {
this.innerTag = adventureTag;
}
@Override
public ListBinaryTag asBinaryTag() {
ListBinaryTag.Builder<BinaryTag> builder = ListBinaryTag.builder();
for (Tag child : getValue()) {
if (child instanceof EndTag) {
continue;
}
builder.add(child.asBinaryTag());
}
return builder.build();
return this.innerTag;
}
/**
@ -86,12 +69,14 @@ public final class ListTag extends Tag {
* @return The type of item in this list.
*/
public Class<? extends Tag> getType() {
return type;
return AdventureNBTConverter.getJNBTType(this.innerTag.elementType());
}
@Override
public List<Tag> getValue() {
return value;
return this.innerTag.stream()
.map(AdventureNBTConverter::fromAdventure)
.collect(Collectors.toList());
}
/**
@ -104,6 +89,13 @@ public final class ListTag extends Tag {
return new ListTag(getType(), list);
}
private <T> T accessIfExists(int index, Supplier<T> defaultValue, IntFunction<T> accessor) {
if (index >= this.innerTag.size()) {
return defaultValue.get();
}
return accessor.apply(index);
}
/**
* Get the tag if it exists at the given index.
*
@ -112,10 +104,11 @@ public final class ListTag extends Tag {
*/
@Nullable
public Tag getIfExists(int index) {
if (index >= value.size()) {
return null;
}
return value.get(index);
return accessIfExists(
index,
() -> null,
i -> AdventureNBTConverter.fromAdventure(this.innerTag.get(i))
);
}
/**
@ -128,12 +121,11 @@ public final class ListTag extends Tag {
* @return a byte array
*/
public byte[] getByteArray(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ByteArrayTag) {
return ((ByteArrayTag) tag).getValue();
} else {
return new byte[0];
}
return accessIfExists(
index,
() -> new byte[0],
this.innerTag::getByteArray
);
}
/**
@ -146,12 +138,11 @@ public final class ListTag extends Tag {
* @return a byte
*/
public byte getByte(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else {
return (byte) 0;
}
return accessIfExists(
index,
() -> (byte) 0,
this.innerTag::getByte
);
}
/**
@ -164,12 +155,11 @@ public final class ListTag extends Tag {
* @return a double
*/
public double getDouble(int index) {
Tag tag = getIfExists(index);
if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0.0,
this.innerTag::getDouble
);
}
/**
@ -183,28 +173,17 @@ public final class ListTag extends Tag {
* @return a double
*/
public double asDouble(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0.0,
i -> {
BinaryTag tag = this.innerTag.get(i);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).doubleValue();
}
return 0.0;
}
);
}
/**
@ -217,12 +196,11 @@ public final class ListTag extends Tag {
* @return a float
*/
public float getFloat(int index) {
Tag tag = getIfExists(index);
if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0.0f,
this.innerTag::getFloat
);
}
/**
@ -235,12 +213,11 @@ public final class ListTag extends Tag {
* @return an int array
*/
public int[] getIntArray(int index) {
Tag tag = getIfExists(index);
if (tag instanceof IntArrayTag) {
return ((IntArrayTag) tag).getValue();
} else {
return new int[0];
}
return accessIfExists(
index,
() -> new int[0],
this.innerTag::getIntArray
);
}
/**
@ -253,12 +230,11 @@ public final class ListTag extends Tag {
* @return an int
*/
public int getInt(int index) {
Tag tag = getIfExists(index);
if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0,
this.innerTag::getInt
);
}
/**
@ -272,28 +248,17 @@ public final class ListTag extends Tag {
* @return an int
*/
public int asInt(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue().intValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().intValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().intValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0,
i -> {
BinaryTag tag = this.innerTag.get(i);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).intValue();
}
return 0;
}
);
}
/**
@ -306,12 +271,7 @@ public final class ListTag extends Tag {
* @return a list of tags
*/
public List<Tag> getList(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ListTag) {
return ((ListTag) tag).getValue();
} else {
return Collections.emptyList();
}
return getListTag(index).getValue();
}
/**
@ -324,12 +284,11 @@ public final class ListTag extends Tag {
* @return a tag list instance
*/
public ListTag getListTag(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ListTag) {
return (ListTag) tag;
} else {
return new ListTag(StringTag.class, Collections.<Tag>emptyList());
}
return new ListTag(accessIfExists(
index,
ListBinaryTag::empty,
this.innerTag::getList
));
}
/**
@ -347,14 +306,9 @@ public final class ListTag extends Tag {
*/
@SuppressWarnings("unchecked")
public <T extends Tag> List<T> getList(int index, Class<T> listType) {
Tag tag = getIfExists(index);
if (tag instanceof ListTag) {
ListTag listTag = (ListTag) tag;
if (listTag.getType().equals(listType)) {
return (List<T>) listTag.getValue();
} else {
return Collections.emptyList();
}
ListTag listTag = getListTag(index);
if (listTag.getType().equals(listType)) {
return (List<T>) listTag.getValue();
} else {
return Collections.emptyList();
}
@ -370,12 +324,11 @@ public final class ListTag extends Tag {
* @return a long
*/
public long getLong(int index) {
Tag tag = getIfExists(index);
if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else {
return 0L;
}
return accessIfExists(
index,
() -> 0L,
this.innerTag::getLong
);
}
/**
@ -389,28 +342,17 @@ public final class ListTag extends Tag {
* @return a long
*/
public long asLong(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue().longValue();
} else if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue().longValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> 0L,
i -> {
BinaryTag tag = this.innerTag.get(i);
if (tag instanceof NumberBinaryTag) {
return ((NumberBinaryTag) tag).longValue();
}
return 0L;
}
);
}
/**
@ -423,12 +365,11 @@ public final class ListTag extends Tag {
* @return a short
*/
public short getShort(int index) {
Tag tag = getIfExists(index);
if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else {
return 0;
}
return accessIfExists(
index,
() -> (short) 0,
this.innerTag::getShort
);
}
/**
@ -441,12 +382,11 @@ public final class ListTag extends Tag {
* @return a string
*/
public String getString(int index) {
Tag tag = getIfExists(index);
if (tag instanceof StringTag) {
return ((StringTag) tag).getValue();
} else {
return "";
}
return accessIfExists(
index,
() -> "",
this.innerTag::getString
);
}
}

View File

@ -19,10 +19,12 @@
package com.sk89q.jnbt;
import java.util.ArrayList;
import com.sk89q.worldedit.util.nbt.BinaryTag;
import com.sk89q.worldedit.util.nbt.BinaryTagType;
import com.sk89q.worldedit.util.nbt.ListBinaryTag;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
@ -34,18 +36,19 @@ import static com.google.common.base.Preconditions.checkNotNull;
@Deprecated
public class ListTagBuilder {
private final Class<? extends Tag> type;
private final List<Tag> entries;
private final ListBinaryTag.Builder<BinaryTag> builder;
/**
* Create a new instance.
*
* @param type of tag contained in this list
*/
@SuppressWarnings("unchecked")
ListTagBuilder(Class<? extends Tag> type) {
checkNotNull(type);
this.type = type;
this.entries = new ArrayList<>();
this.builder = type != EndTag.class
? ListBinaryTag.builder((BinaryTagType<BinaryTag>) AdventureNBTConverter.getAdventureType(type))
: ListBinaryTag.builder();
}
/**
@ -56,10 +59,7 @@ public class ListTagBuilder {
*/
public ListTagBuilder add(Tag value) {
checkNotNull(value);
if (!type.isInstance(value)) {
throw new IllegalArgumentException(value.getClass().getCanonicalName() + " is not of expected type " + type.getCanonicalName());
}
entries.add(value);
builder.add(value.asBinaryTag());
return this;
}
@ -83,7 +83,7 @@ public class ListTagBuilder {
* @return the new list tag
*/
public ListTag build() {
return new ListTag(type, new ArrayList<>(entries));
return new ListTag(this.builder.build());
}
/**

View File

@ -44,7 +44,7 @@ public class LongArrayTag extends Tag {
this.innerTag = LongArrayBinaryTag.of(value);
}
LongArrayTag(LongArrayBinaryTag adventureTag) {
public LongArrayTag(LongArrayBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -41,7 +41,7 @@ public final class LongTag extends Tag {
this.innerTag = LongBinaryTag.of(value);
}
LongTag(LongBinaryTag adventureTag) {
public LongTag(LongBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -19,9 +19,6 @@
package com.sk89q.jnbt;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A class which holds constant values.
*
@ -30,8 +27,6 @@ import java.nio.charset.StandardCharsets;
@Deprecated
public final class NBTConstants {
public static final Charset CHARSET = StandardCharsets.UTF_8;
public static final int TYPE_END = 0;
public static final int TYPE_BYTE = 1;
public static final int TYPE_SHORT = 2;

View File

@ -87,36 +87,7 @@ public final class NBTUtils {
* @throws IllegalArgumentException if the tag class is invalid.
*/
public static int getTypeCode(Class<? extends Tag> clazz) {
if (clazz.equals(ByteArrayTag.class)) {
return NBTConstants.TYPE_BYTE_ARRAY;
} else if (clazz.equals(ByteTag.class)) {
return NBTConstants.TYPE_BYTE;
} else if (clazz.equals(CompoundTag.class)) {
return NBTConstants.TYPE_COMPOUND;
} else if (clazz.equals(DoubleTag.class)) {
return NBTConstants.TYPE_DOUBLE;
} else if (clazz.equals(EndTag.class)) {
return NBTConstants.TYPE_END;
} else if (clazz.equals(FloatTag.class)) {
return NBTConstants.TYPE_FLOAT;
} else if (clazz.equals(IntTag.class)) {
return NBTConstants.TYPE_INT;
} else if (clazz.equals(ListTag.class)) {
return NBTConstants.TYPE_LIST;
} else if (clazz.equals(LongTag.class)) {
return NBTConstants.TYPE_LONG;
} else if (clazz.equals(ShortTag.class)) {
return NBTConstants.TYPE_SHORT;
} else if (clazz.equals(StringTag.class)) {
return NBTConstants.TYPE_STRING;
} else if (clazz.equals(IntArrayTag.class)) {
return NBTConstants.TYPE_INT_ARRAY;
} else if (clazz.equals(LongArrayTag.class)) {
return NBTConstants.TYPE_LONG_ARRAY;
} else {
throw new IllegalArgumentException("Invalid tag classs ("
+ clazz.getName() + ").");
}
return AdventureNBTConverter.getAdventureType(clazz).id();
}
/**

View File

@ -41,7 +41,7 @@ public final class ShortTag extends Tag {
this.innerTag = ShortBinaryTag.of(value);
}
ShortTag(ShortBinaryTag adventureTag) {
public ShortTag(ShortBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -44,7 +44,7 @@ public final class StringTag extends Tag {
this.innerTag = StringBinaryTag.of(value);
}
StringTag(StringBinaryTag adventureTag) {
public StringTag(StringBinaryTag adventureTag) {
super();
this.innerTag = adventureTag;
}

View File

@ -63,7 +63,7 @@ public interface NbtValued {
@Nullable
default CompoundTag getNbtData() {
CompoundBinaryTag tag = getNbt();
return tag == null ? null : AdventureNBTConverter.fromAdventure(tag);
return tag == null ? null : new CompoundTag(tag);
}
/**
@ -78,10 +78,12 @@ public interface NbtValued {
}
/**
* Returns whether the block contains NBT data. {@link #getNbt()} ()}
* Returns whether the block contains NBT data. {@link #getNbt()}
* must not return null if this method returns true.
*
* @return true if there is NBT data
* @apiNote This must be overridden by new subclasses. See {@link NonAbstractForCompatibility}
* for details
*/
@NonAbstractForCompatibility(
delegateName = "hasNbtData",
@ -104,6 +106,8 @@ public interface NbtValued {
* not return null.</p>
*
* @return compound tag, or null
* @apiNote This must be overridden by new subclasses. See {@link NonAbstractForCompatibility}
* for details
*/
@NonAbstractForCompatibility(
delegateName = "getNbtData",
@ -121,6 +125,8 @@ public interface NbtValued {
* Set the object's NBT data (tile entity data).
*
* @param nbtData NBT data, or null if no data
* @apiNote This must be overridden by new subclasses. See {@link NonAbstractForCompatibility}
* for details
*/
@NonAbstractForCompatibility(
delegateName = "setNbtData",
@ -129,7 +135,7 @@ public interface NbtValued {
default void setNbt(@Nullable CompoundBinaryTag nbtData) {
DeprecationUtil.checkDelegatingOverride(getClass());
setNbtData(nbtData == null ? null : AdventureNBTConverter.fromAdventure(nbtData));
setNbtData(nbtData == null ? null : new CompoundTag(nbtData));
}
}

View File

@ -104,6 +104,8 @@ public interface BlockStateHolder<B extends BlockStateHolder<B>> extends Pattern
*
* @param compoundTag The NBT Data to apply
* @return The BaseBlock
* @apiNote This must be overridden by new subclasses. See {@link NonAbstractForCompatibility}
* for details
*/
@NonAbstractForCompatibility(
delegateName = "toBaseBlock",
@ -112,7 +114,7 @@ public interface BlockStateHolder<B extends BlockStateHolder<B>> extends Pattern
default BaseBlock toBaseBlock(CompoundBinaryTag compoundTag) {
DeprecationUtil.checkDelegatingOverride(getClass());
return toBaseBlock(compoundTag == null ? null : AdventureNBTConverter.fromAdventure(compoundTag));
return toBaseBlock(compoundTag == null ? null : new CompoundTag(compoundTag));
}
@Override