|
19 | 19 |
|
20 | 20 | package com.sk89q.jnbt;
|
21 | 21 |
|
| 22 | +import com.google.common.collect.BiMap; |
| 23 | +import com.google.common.collect.ImmutableBiMap; |
| 24 | +import com.google.common.collect.ImmutableMap; |
22 | 25 | 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; |
36 | 34 |
|
37 | 35 | /**
|
38 | 36 | * Converts between JNBT and Adventure-NBT classes.
|
|
41 | 39 | */
|
42 | 40 | @Deprecated
|
43 | 41 | 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 | + } |
78 | 82 | }
|
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 |
| - } |
92 | 83 |
|
93 |
| - public static CompoundTag fromAdventure(CompoundBinaryTag other) { |
94 |
| - return new CompoundTag(other); |
| 84 | + CONVERSION = conversion.build(); |
95 | 85 | }
|
96 | 86 |
|
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); |
99 | 89 | }
|
100 | 90 |
|
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); |
103 | 93 | }
|
104 | 94 |
|
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() { |
127 | 96 | }
|
128 | 97 |
|
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); |
131 | 104 | }
|
132 | 105 | }
|
0 commit comments