Skip to content

Commit b94d3c3

Browse files
committed
Make all but compound/list JNBT tags just a reference to an internal Adventure one.
1 parent d82c612 commit b94d3c3

30 files changed

+431
-177
lines changed

buildSrc/src/main/kotlin/LibsConfig.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ fun Project.applyLibrariesConfiguration() {
2828

2929
val relocations = mapOf(
3030
"net.kyori.text" to "com.sk89q.worldedit.util.formatting.text",
31-
"net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori"
31+
"net.kyori.minecraft" to "com.sk89q.worldedit.util.kyori",
32+
"net.kyori.adventure.nbt" to "com.sk89q.worldedit.util.nbt"
3233
)
3334

3435
tasks.register<ShadowJar>("jar") {

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

+80-110
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,25 @@
1919

2020
package com.sk89q.jnbt;
2121

22-
import net.kyori.adventure.nbt.BinaryTag;
23-
import net.kyori.adventure.nbt.ByteArrayBinaryTag;
24-
import net.kyori.adventure.nbt.ByteBinaryTag;
25-
import net.kyori.adventure.nbt.CompoundBinaryTag;
26-
import net.kyori.adventure.nbt.DoubleBinaryTag;
27-
import net.kyori.adventure.nbt.EndBinaryTag;
28-
import net.kyori.adventure.nbt.FloatBinaryTag;
29-
import net.kyori.adventure.nbt.IntArrayBinaryTag;
30-
import net.kyori.adventure.nbt.IntBinaryTag;
31-
import net.kyori.adventure.nbt.ListBinaryTag;
32-
import net.kyori.adventure.nbt.LongArrayBinaryTag;
33-
import net.kyori.adventure.nbt.LongBinaryTag;
34-
import net.kyori.adventure.nbt.ShortBinaryTag;
35-
import net.kyori.adventure.nbt.StringBinaryTag;
36-
37-
import java.util.ArrayList;
38-
import java.util.Arrays;
39-
import java.util.HashMap;
40-
import java.util.List;
41-
import java.util.Map;
42-
import java.util.Set;
43-
22+
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;
36+
37+
/**
38+
* @deprecated JNBT is being removed in WE8.
39+
*/
40+
@Deprecated
4441
public class AdventureNBTConverter {
4542

4643
private AdventureNBTConverter() {
@@ -77,162 +74,135 @@ public static BinaryTag toAdventure(Tag tag) {
7774
}
7875
}
7976

80-
public static IntArrayBinaryTag toAdventure(IntArrayTag tag) {
81-
int[] value = tag.getValue();
82-
return IntArrayBinaryTag.of(Arrays.copyOf(value, value.length));
77+
private static DoubleBinaryTag toAdventure(DoubleTag tag) {
78+
return tag.toAdventure();
8379
}
8480

85-
public static ListBinaryTag toAdventure(ListTag tag) {
86-
ListBinaryTag.Builder<BinaryTag> builder = ListBinaryTag.builder();
87-
for (Tag child : tag.getValue()) {
88-
if (child instanceof EndTag) {
89-
continue;
90-
}
91-
builder.add(toAdventure(child));
92-
}
93-
return builder.build();
81+
private static ShortBinaryTag toAdventure(ShortTag tag) {
82+
return tag.toAdventure();
9483
}
9584

96-
public static LongBinaryTag toAdventure(LongTag tag) {
97-
return LongBinaryTag.of(tag.getValue());
85+
private static FloatBinaryTag toAdventure(FloatTag tag) {
86+
return tag.toAdventure();
9887
}
9988

100-
public static LongArrayBinaryTag toAdventure(LongArrayTag tag) {
101-
return LongArrayBinaryTag.of(tag.getValue().clone());
89+
private static ByteArrayBinaryTag toAdventure(ByteArrayTag tag) {
90+
return tag.toAdventure();
10291
}
10392

104-
public static StringBinaryTag toAdventure(StringTag tag) {
105-
return StringBinaryTag.of(tag.getValue());
93+
private static ByteBinaryTag toAdventure(ByteTag tag) {
94+
return tag.toAdventure();
10695
}
10796

108-
public static IntBinaryTag toAdventure(IntTag tag) {
109-
return IntBinaryTag.of(tag.getValue());
97+
private static IntBinaryTag toAdventure(IntTag tag) {
98+
return tag.toAdventure();
11099
}
111100

112-
public static ByteBinaryTag toAdventure(ByteTag tag) {
113-
return ByteBinaryTag.of(tag.getValue());
101+
private static StringBinaryTag toAdventure(StringTag tag) {
102+
return tag.toAdventure();
114103
}
115104

116-
public static ByteArrayBinaryTag toAdventure(ByteArrayTag tag) {
117-
return ByteArrayBinaryTag.of(tag.getValue().clone());
105+
private static LongArrayBinaryTag toAdventure(LongArrayTag tag) {
106+
return tag.toAdventure();
118107
}
119108

120-
public static CompoundBinaryTag toAdventure(CompoundTag tag) {
121-
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
122-
for (Map.Entry<String, Tag> child : tag.getValue().entrySet()) {
123-
builder.put(child.getKey(), toAdventure(child.getValue()));
124-
}
125-
return builder.build();
109+
private static LongBinaryTag toAdventure(LongTag tag) {
110+
return tag.toAdventure();
126111
}
127112

128-
public static FloatBinaryTag toAdventure(FloatTag tag) {
129-
return FloatBinaryTag.of(tag.getValue());
113+
private static IntArrayBinaryTag toAdventure(IntArrayTag tag) {
114+
return tag.toAdventure();
130115
}
131116

132-
public static ShortBinaryTag toAdventure(ShortTag tag) {
133-
return ShortBinaryTag.of(tag.getValue());
117+
public static ListBinaryTag toAdventure(ListTag tag) {
118+
return tag.toAdventure();
134119
}
135120

136-
public static DoubleBinaryTag toAdventure(DoubleTag tag) {
137-
return DoubleBinaryTag.of(tag.getValue());
121+
public static CompoundBinaryTag toAdventure(CompoundTag tag) {
122+
return tag.toAdventure();
138123
}
139124

140125
public static Tag fromAdventure(BinaryTag other) {
141126
if (other instanceof IntArrayBinaryTag) {
142-
return AdventureNBTConverter.fromAdventure((IntArrayBinaryTag) other);
127+
return fromAdventure((IntArrayBinaryTag) other);
143128
} else if (other instanceof ListBinaryTag) {
144-
return AdventureNBTConverter.fromAdventure((ListBinaryTag) other);
129+
return fromAdventure((ListBinaryTag) other);
145130
} else if (other instanceof EndBinaryTag) {
146-
return AdventureNBTConverter.fromAdventure((EndBinaryTag) other);
131+
return fromAdventure();
147132
} else if (other instanceof LongBinaryTag) {
148-
return AdventureNBTConverter.fromAdventure((LongBinaryTag) other);
133+
return fromAdventure((LongBinaryTag) other);
149134
} else if (other instanceof LongArrayBinaryTag) {
150-
return AdventureNBTConverter.fromAdventure((LongArrayBinaryTag) other);
135+
return fromAdventure((LongArrayBinaryTag) other);
151136
} else if (other instanceof StringBinaryTag) {
152-
return AdventureNBTConverter.fromAdventure((StringBinaryTag) other);
137+
return fromAdventure((StringBinaryTag) other);
153138
} else if (other instanceof IntBinaryTag) {
154-
return AdventureNBTConverter.fromAdventure((IntBinaryTag) other);
139+
return fromAdventure((IntBinaryTag) other);
155140
} else if (other instanceof ByteBinaryTag) {
156-
return AdventureNBTConverter.fromAdventure((ByteBinaryTag) other);
141+
return fromAdventure((ByteBinaryTag) other);
157142
} else if (other instanceof ByteArrayBinaryTag) {
158-
return AdventureNBTConverter.fromAdventure((ByteArrayBinaryTag) other);
143+
return fromAdventure((ByteArrayBinaryTag) other);
159144
} else if (other instanceof CompoundBinaryTag) {
160-
return AdventureNBTConverter.fromAdventure((CompoundBinaryTag) other);
145+
return fromAdventure((CompoundBinaryTag) other);
161146
} else if (other instanceof FloatBinaryTag) {
162-
return AdventureNBTConverter.fromAdventure((FloatBinaryTag) other);
147+
return fromAdventure((FloatBinaryTag) other);
163148
} else if (other instanceof ShortBinaryTag) {
164-
return AdventureNBTConverter.fromAdventure((ShortBinaryTag) other);
149+
return fromAdventure((ShortBinaryTag) other);
165150
} else if (other instanceof DoubleBinaryTag) {
166151
return fromAdventure((DoubleBinaryTag) other);
167152
} else {
168153
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
169154
}
170155
}
171156

172-
public static IntArrayTag fromAdventure(IntArrayBinaryTag other) {
173-
int[] value = other.value();
174-
return new IntArrayTag(Arrays.copyOf(value, value.length));
157+
public static DoubleTag fromAdventure(DoubleBinaryTag other) {
158+
return new DoubleTag(other);
175159
}
176160

177-
public static ListTag fromAdventure(ListBinaryTag other) {
178-
List<Tag> list = new ArrayList<>();
179-
Class<? extends Tag> listClass = StringTag.class;
180-
int tags = other.size();
181-
for (int i = 0; i < tags; i++) {
182-
Tag child = fromAdventure(other.get(0));
183-
list.add(child);
184-
listClass = child.getClass();
185-
}
186-
return new ListTag(listClass, list);
161+
public static ShortTag fromAdventure(ShortBinaryTag other) {
162+
return new ShortTag(other);
187163
}
188164

189-
public static EndTag fromAdventure(EndBinaryTag other) {
190-
return new EndTag();
165+
public static FloatTag fromAdventure(FloatBinaryTag other) {
166+
return new FloatTag(other);
191167
}
192168

193-
public static LongTag fromAdventure(LongBinaryTag other) {
194-
return new LongTag(other.value());
169+
public static CompoundTag fromAdventure(CompoundBinaryTag other) {
170+
return new CompoundTag(other);
195171
}
196172

197-
public static LongArrayTag fromAdventure(LongArrayBinaryTag other) {
198-
return new LongArrayTag(other.value().clone());
173+
public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) {
174+
return new ByteArrayTag(other);
199175
}
200176

201-
public static StringTag fromAdventure(StringBinaryTag other) {
202-
return new StringTag(other.value());
177+
public static ByteTag fromAdventure(ByteBinaryTag other) {
178+
return new ByteTag(other);
203179
}
204180

205181
public static IntTag fromAdventure(IntBinaryTag other) {
206-
return new IntTag(other.value());
182+
return new IntTag(other);
207183
}
208184

209-
public static ByteTag fromAdventure(ByteBinaryTag other) {
210-
return new ByteTag(other.value());
185+
public static StringTag fromAdventure(StringBinaryTag other) {
186+
return new StringTag(other);
211187
}
212188

213-
public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) {
214-
return new ByteArrayTag(other.value().clone());
189+
public static LongArrayTag fromAdventure(LongArrayBinaryTag other) {
190+
return new LongArrayTag(other);
215191
}
216192

217-
public static CompoundTag fromAdventure(CompoundBinaryTag other) {
218-
Set<String> tags = other.keySet();
219-
Map<String, Tag> map = new HashMap<>();
220-
for (String tagName : tags) {
221-
map.put(tagName, fromAdventure(other.get(tagName)));
222-
}
223-
return new CompoundTag(map);
193+
public static LongTag fromAdventure(LongBinaryTag other) {
194+
return new LongTag(other);
224195
}
225196

226-
public static FloatTag fromAdventure(FloatBinaryTag other) {
227-
return new FloatTag(other.value());
197+
public static EndTag fromAdventure() {
198+
return new EndTag();
228199
}
229200

230-
public static ShortTag fromAdventure(ShortBinaryTag other) {
231-
return new ShortTag(other.value());
201+
public static ListTag fromAdventure(ListBinaryTag other) {
202+
return new ListTag(other);
232203
}
233204

234-
public static DoubleTag fromAdventure(DoubleBinaryTag other) {
235-
return new DoubleTag(other.value());
205+
public static IntArrayTag fromAdventure(IntArrayBinaryTag other) {
206+
return new IntArrayTag(other);
236207
}
237-
238208
}

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.sk89q.worldedit.util.nbt.ByteArrayBinaryTag;
23+
2224
import java.util.Locale;
2325

2426
/**
2527
* The {@code TAG_Byte_Array} tag.
28+
*
29+
* @deprecated Use {@link ByteArrayBinaryTag}.
2630
*/
31+
@Deprecated
2732
public final class ByteArrayTag extends Tag {
2833

29-
private final byte[] value;
34+
private final ByteArrayBinaryTag innerTag;
3035

3136
/**
3237
* Creates the tag with an empty name.
@@ -35,18 +40,27 @@ public final class ByteArrayTag extends Tag {
3540
*/
3641
public ByteArrayTag(byte[] value) {
3742
super();
38-
this.value = value;
43+
this.innerTag = ByteArrayBinaryTag.of(value);
44+
}
45+
46+
ByteArrayTag(ByteArrayBinaryTag adventureTag) {
47+
super();
48+
this.innerTag = adventureTag;
49+
}
50+
51+
ByteArrayBinaryTag toAdventure() {
52+
return this.innerTag;
3953
}
4054

4155
@Override
4256
public byte[] getValue() {
43-
return value;
57+
return innerTag.value();
4458
}
4559

4660
@Override
4761
public String toString() {
4862
StringBuilder hex = new StringBuilder();
49-
for (byte b : value) {
63+
for (byte b : innerTag.value()) {
5064
String hexDigits = Integer.toHexString(b).toUpperCase(Locale.ROOT);
5165
if (hexDigits.length() == 1) {
5266
hex.append("0");

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.sk89q.worldedit.util.nbt.ByteBinaryTag;
23+
2224
/**
2325
* The {@code TAG_Byte} tag.
26+
*
27+
* @deprecated Use {@link ByteBinaryTag}.
2428
*/
29+
@Deprecated
2530
public final class ByteTag extends Tag {
2631

27-
private final byte value;
32+
private final ByteBinaryTag innerTag;
2833

2934
/**
3035
* Creates the tag with an empty name.
@@ -33,17 +38,26 @@ public final class ByteTag extends Tag {
3338
*/
3439
public ByteTag(byte value) {
3540
super();
36-
this.value = value;
41+
this.innerTag = ByteBinaryTag.of(value);
42+
}
43+
44+
ByteTag(ByteBinaryTag adventureTag) {
45+
super();
46+
this.innerTag = adventureTag;
47+
}
48+
49+
ByteBinaryTag toAdventure() {
50+
return this.innerTag;
3751
}
3852

3953
@Override
4054
public Byte getValue() {
41-
return value;
55+
return innerTag.value();
4256
}
4357

4458
@Override
4559
public String toString() {
46-
return "TAG_Byte(" + value + ")";
60+
return "TAG_Byte(" + innerTag.value() + ")";
4761
}
4862

4963
}

0 commit comments

Comments
 (0)