Skip to content

Commit 3b25230

Browse files
committed
Finish reimplementing everything in adventure-nbt
1 parent 628c3f2 commit 3b25230

19 files changed

+292
-509
lines changed

worldedit-core/src/main/java/com/sk89q/jnbt/AdventureNBTConverter.java

Lines changed: 63 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.google.common.collect.BiMap;
23+
import com.google.common.collect.ImmutableBiMap;
24+
import com.google.common.collect.ImmutableMap;
2225
import com.sk89q.worldedit.util.nbt.BinaryTag;
23-
import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag;
24-
import com.sk89q.worldedit.util.nbt.ByteBinaryTag;
25-
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
26-
import com.sk89q.worldedit.util.nbt.DoubleBinaryTag;
27-
import com.sk89q.worldedit.util.nbt.EndBinaryTag;
28-
import com.sk89q.worldedit.util.nbt.FloatBinaryTag;
29-
import com.sk89q.worldedit.util.nbt.IntArrayBinaryTag;
30-
import com.sk89q.worldedit.util.nbt.IntBinaryTag;
31-
import com.sk89q.worldedit.util.nbt.ListBinaryTag;
32-
import com.sk89q.worldedit.util.nbt.LongArrayBinaryTag;
33-
import com.sk89q.worldedit.util.nbt.LongBinaryTag;
34-
import com.sk89q.worldedit.util.nbt.ShortBinaryTag;
35-
import com.sk89q.worldedit.util.nbt.StringBinaryTag;
26+
import com.sk89q.worldedit.util.nbt.BinaryTagType;
27+
import com.sk89q.worldedit.util.nbt.BinaryTagTypes;
28+
29+
import java.lang.reflect.Constructor;
30+
import java.lang.reflect.InvocationTargetException;
31+
import java.util.Map;
32+
import java.util.Objects;
33+
import java.util.function.Function;
3634

3735
/**
3836
* Converts between JNBT and Adventure-NBT classes.
@@ -41,92 +39,67 @@
4139
*/
4240
@Deprecated
4341
public class AdventureNBTConverter {
44-
45-
private AdventureNBTConverter() {
46-
47-
}
48-
49-
public static Tag fromAdventure(BinaryTag other) {
50-
if (other instanceof IntArrayBinaryTag) {
51-
return fromAdventure((IntArrayBinaryTag) other);
52-
} else if (other instanceof ListBinaryTag) {
53-
return fromAdventure((ListBinaryTag) other);
54-
} else if (other instanceof EndBinaryTag) {
55-
return fromAdventure();
56-
} else if (other instanceof LongBinaryTag) {
57-
return fromAdventure((LongBinaryTag) other);
58-
} else if (other instanceof LongArrayBinaryTag) {
59-
return fromAdventure((LongArrayBinaryTag) other);
60-
} else if (other instanceof StringBinaryTag) {
61-
return fromAdventure((StringBinaryTag) other);
62-
} else if (other instanceof IntBinaryTag) {
63-
return fromAdventure((IntBinaryTag) other);
64-
} else if (other instanceof ByteBinaryTag) {
65-
return fromAdventure((ByteBinaryTag) other);
66-
} else if (other instanceof ByteArrayBinaryTag) {
67-
return fromAdventure((ByteArrayBinaryTag) other);
68-
} else if (other instanceof CompoundBinaryTag) {
69-
return fromAdventure((CompoundBinaryTag) other);
70-
} else if (other instanceof FloatBinaryTag) {
71-
return fromAdventure((FloatBinaryTag) other);
72-
} else if (other instanceof ShortBinaryTag) {
73-
return fromAdventure((ShortBinaryTag) other);
74-
} else if (other instanceof DoubleBinaryTag) {
75-
return fromAdventure((DoubleBinaryTag) other);
76-
} else {
77-
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
42+
private static final BiMap<Class<? extends Tag>, BinaryTagType<?>> TAG_TYPES =
43+
new ImmutableBiMap.Builder<Class<? extends Tag>, BinaryTagType<?>>()
44+
.put(ByteArrayTag.class, BinaryTagTypes.BYTE_ARRAY)
45+
.put(ByteTag.class, BinaryTagTypes.BYTE)
46+
.put(CompoundTag.class, BinaryTagTypes.COMPOUND)
47+
.put(DoubleTag.class, BinaryTagTypes.DOUBLE)
48+
.put(EndTag.class, BinaryTagTypes.END)
49+
.put(FloatTag.class, BinaryTagTypes.FLOAT)
50+
.put(IntArrayTag.class, BinaryTagTypes.INT_ARRAY)
51+
.put(IntTag.class, BinaryTagTypes.INT)
52+
.put(ListTag.class, BinaryTagTypes.LIST)
53+
.put(LongArrayTag.class, BinaryTagTypes.LONG_ARRAY)
54+
.put(LongTag.class, BinaryTagTypes.LONG)
55+
.put(ShortTag.class, BinaryTagTypes.SHORT)
56+
.put(StringTag.class, BinaryTagTypes.STRING)
57+
.build();
58+
59+
private static final Map<BinaryTagType<?>, Function<BinaryTag, Tag>> CONVERSION;
60+
61+
static {
62+
ImmutableMap.Builder<BinaryTagType<?>, Function<BinaryTag, Tag>> conversion =
63+
ImmutableMap.builder();
64+
65+
for (Map.Entry<Class<? extends Tag>, BinaryTagType<?>> tag : TAG_TYPES.entrySet()) {
66+
Constructor<?>[] constructors = tag.getKey().getConstructors();
67+
for (Constructor<?> c : constructors) {
68+
if (c.getParameterCount() == 1 && BinaryTag.class.isAssignableFrom(c.getParameterTypes()[0])) {
69+
conversion.put(tag.getValue(), binaryTag -> {
70+
try {
71+
return (Tag) c.newInstance(binaryTag);
72+
} catch (InstantiationException | IllegalAccessException e) {
73+
throw new IllegalStateException(e);
74+
} catch (InvocationTargetException e) {
75+
// I assume this is always a RuntimeException since we control the ctor
76+
throw (RuntimeException) e.getCause();
77+
}
78+
});
79+
break;
80+
}
81+
}
7882
}
79-
}
80-
81-
public static DoubleTag fromAdventure(DoubleBinaryTag other) {
82-
return new DoubleTag(other);
83-
}
84-
85-
public static ShortTag fromAdventure(ShortBinaryTag other) {
86-
return new ShortTag(other);
87-
}
88-
89-
public static FloatTag fromAdventure(FloatBinaryTag other) {
90-
return new FloatTag(other);
91-
}
9283

93-
public static CompoundTag fromAdventure(CompoundBinaryTag other) {
94-
return new CompoundTag(other);
84+
CONVERSION = conversion.build();
9585
}
9686

97-
public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) {
98-
return new ByteArrayTag(other);
87+
public static BinaryTagType<?> getAdventureType(Class<? extends Tag> type) {
88+
return Objects.requireNonNull(TAG_TYPES.get(type), () -> "Missing entry for " + type);
9989
}
10090

101-
public static ByteTag fromAdventure(ByteBinaryTag other) {
102-
return new ByteTag(other);
91+
public static Class<? extends Tag> getJNBTType(BinaryTagType<?> type) {
92+
return Objects.requireNonNull(TAG_TYPES.inverse().get(type), () -> "Missing entry for " + type);
10393
}
10494

105-
public static IntTag fromAdventure(IntBinaryTag other) {
106-
return new IntTag(other);
107-
}
108-
109-
public static StringTag fromAdventure(StringBinaryTag other) {
110-
return new StringTag(other);
111-
}
112-
113-
public static LongArrayTag fromAdventure(LongArrayBinaryTag other) {
114-
return new LongArrayTag(other);
115-
}
116-
117-
public static LongTag fromAdventure(LongBinaryTag other) {
118-
return new LongTag(other);
119-
}
120-
121-
public static EndTag fromAdventure() {
122-
return new EndTag();
123-
}
124-
125-
public static ListTag fromAdventure(ListBinaryTag other) {
126-
return new ListTag(other);
95+
private AdventureNBTConverter() {
12796
}
12897

129-
public static IntArrayTag fromAdventure(IntArrayBinaryTag other) {
130-
return new IntArrayTag(other);
98+
public static Tag fromAdventure(BinaryTag other) {
99+
Function<BinaryTag, Tag> conversion = CONVERSION.get(other.type());
100+
if (conversion == null) {
101+
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
102+
}
103+
return conversion.apply(other);
131104
}
132105
}

worldedit-core/src/main/java/com/sk89q/jnbt/ByteArrayTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ByteArrayTag(byte[] value) {
4343
this.innerTag = ByteArrayBinaryTag.of(value);
4444
}
4545

46-
ByteArrayTag(ByteArrayBinaryTag adventureTag) {
46+
public ByteArrayTag(ByteArrayBinaryTag adventureTag) {
4747
super();
4848
this.innerTag = adventureTag;
4949
}

worldedit-core/src/main/java/com/sk89q/jnbt/ByteTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ByteTag(byte value) {
4141
this.innerTag = ByteBinaryTag.of(value);
4242
}
4343

44-
ByteTag(ByteBinaryTag adventureTag) {
44+
public ByteTag(ByteBinaryTag adventureTag) {
4545
super();
4646
this.innerTag = adventureTag;
4747
}

0 commit comments

Comments
 (0)