Skip to content

Commit 1b58d7b

Browse files
committed
Migrate to adventure-nbt
1 parent 5aa81ff commit 1b58d7b

33 files changed

Lines changed: 658 additions & 85 deletions

buildSrc/src/main/kotlin/LibsConfig.kt

Lines changed: 2 additions & 1 deletion
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") {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* WorldEdit, a Minecraft world manipulation toolkit
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldEdit team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.jnbt;
21+
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+
* Converts between JNBT and Adventure-NBT classes.
39+
*
40+
* @deprecated JNBT is being removed in WE8.
41+
*/
42+
@Deprecated
43+
public class AdventureNBTConverter {
44+
45+
private AdventureNBTConverter() {
46+
47+
}
48+
49+
public static BinaryTag toAdventure(Tag tag) {
50+
if (tag instanceof IntArrayTag) {
51+
return toAdventure((IntArrayTag) tag);
52+
} else if (tag instanceof ListTag) {
53+
return toAdventure((ListTag) tag);
54+
} else if (tag instanceof LongTag) {
55+
return toAdventure((LongTag) tag);
56+
} else if (tag instanceof LongArrayTag) {
57+
return toAdventure((LongArrayTag) tag);
58+
} else if (tag instanceof StringTag) {
59+
return toAdventure((StringTag) tag);
60+
} else if (tag instanceof IntTag) {
61+
return toAdventure((IntTag) tag);
62+
} else if (tag instanceof ByteTag) {
63+
return toAdventure((ByteTag) tag);
64+
} else if (tag instanceof ByteArrayTag) {
65+
return toAdventure((ByteArrayTag) tag);
66+
} else if (tag instanceof CompoundTag) {
67+
return toAdventure((CompoundTag) tag);
68+
} else if (tag instanceof FloatTag) {
69+
return toAdventure((FloatTag) tag);
70+
} else if (tag instanceof ShortTag) {
71+
return toAdventure((ShortTag) tag);
72+
} else if (tag instanceof DoubleTag) {
73+
return toAdventure((DoubleTag) tag);
74+
} else {
75+
throw new IllegalArgumentException("Can't convert tag of type " + tag.getClass().getCanonicalName());
76+
}
77+
}
78+
79+
private static DoubleBinaryTag toAdventure(DoubleTag tag) {
80+
return tag.toAdventure();
81+
}
82+
83+
private static ShortBinaryTag toAdventure(ShortTag tag) {
84+
return tag.toAdventure();
85+
}
86+
87+
private static FloatBinaryTag toAdventure(FloatTag tag) {
88+
return tag.toAdventure();
89+
}
90+
91+
private static ByteArrayBinaryTag toAdventure(ByteArrayTag tag) {
92+
return tag.toAdventure();
93+
}
94+
95+
private static ByteBinaryTag toAdventure(ByteTag tag) {
96+
return tag.toAdventure();
97+
}
98+
99+
private static IntBinaryTag toAdventure(IntTag tag) {
100+
return tag.toAdventure();
101+
}
102+
103+
private static StringBinaryTag toAdventure(StringTag tag) {
104+
return tag.toAdventure();
105+
}
106+
107+
private static LongArrayBinaryTag toAdventure(LongArrayTag tag) {
108+
return tag.toAdventure();
109+
}
110+
111+
private static LongBinaryTag toAdventure(LongTag tag) {
112+
return tag.toAdventure();
113+
}
114+
115+
private static IntArrayBinaryTag toAdventure(IntArrayTag tag) {
116+
return tag.toAdventure();
117+
}
118+
119+
public static ListBinaryTag toAdventure(ListTag tag) {
120+
return tag.toAdventure();
121+
}
122+
123+
public static CompoundBinaryTag toAdventure(CompoundTag tag) {
124+
return tag.toAdventure();
125+
}
126+
127+
public static Tag fromAdventure(BinaryTag other) {
128+
if (other instanceof IntArrayBinaryTag) {
129+
return fromAdventure((IntArrayBinaryTag) other);
130+
} else if (other instanceof ListBinaryTag) {
131+
return fromAdventure((ListBinaryTag) other);
132+
} else if (other instanceof EndBinaryTag) {
133+
return fromAdventure();
134+
} else if (other instanceof LongBinaryTag) {
135+
return fromAdventure((LongBinaryTag) other);
136+
} else if (other instanceof LongArrayBinaryTag) {
137+
return fromAdventure((LongArrayBinaryTag) other);
138+
} else if (other instanceof StringBinaryTag) {
139+
return fromAdventure((StringBinaryTag) other);
140+
} else if (other instanceof IntBinaryTag) {
141+
return fromAdventure((IntBinaryTag) other);
142+
} else if (other instanceof ByteBinaryTag) {
143+
return fromAdventure((ByteBinaryTag) other);
144+
} else if (other instanceof ByteArrayBinaryTag) {
145+
return fromAdventure((ByteArrayBinaryTag) other);
146+
} else if (other instanceof CompoundBinaryTag) {
147+
return fromAdventure((CompoundBinaryTag) other);
148+
} else if (other instanceof FloatBinaryTag) {
149+
return fromAdventure((FloatBinaryTag) other);
150+
} else if (other instanceof ShortBinaryTag) {
151+
return fromAdventure((ShortBinaryTag) other);
152+
} else if (other instanceof DoubleBinaryTag) {
153+
return fromAdventure((DoubleBinaryTag) other);
154+
} else {
155+
throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
156+
}
157+
}
158+
159+
public static DoubleTag fromAdventure(DoubleBinaryTag other) {
160+
return new DoubleTag(other);
161+
}
162+
163+
public static ShortTag fromAdventure(ShortBinaryTag other) {
164+
return new ShortTag(other);
165+
}
166+
167+
public static FloatTag fromAdventure(FloatBinaryTag other) {
168+
return new FloatTag(other);
169+
}
170+
171+
public static CompoundTag fromAdventure(CompoundBinaryTag other) {
172+
return new CompoundTag(other);
173+
}
174+
175+
public static ByteArrayTag fromAdventure(ByteArrayBinaryTag other) {
176+
return new ByteArrayTag(other);
177+
}
178+
179+
public static ByteTag fromAdventure(ByteBinaryTag other) {
180+
return new ByteTag(other);
181+
}
182+
183+
public static IntTag fromAdventure(IntBinaryTag other) {
184+
return new IntTag(other);
185+
}
186+
187+
public static StringTag fromAdventure(StringBinaryTag other) {
188+
return new StringTag(other);
189+
}
190+
191+
public static LongArrayTag fromAdventure(LongArrayBinaryTag other) {
192+
return new LongArrayTag(other);
193+
}
194+
195+
public static LongTag fromAdventure(LongBinaryTag other) {
196+
return new LongTag(other);
197+
}
198+
199+
public static EndTag fromAdventure() {
200+
return new EndTag();
201+
}
202+
203+
public static ListTag fromAdventure(ListBinaryTag other) {
204+
return new ListTag(other);
205+
}
206+
207+
public static IntArrayTag fromAdventure(IntArrayBinaryTag other) {
208+
return new IntArrayTag(other);
209+
}
210+
}

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

Lines changed: 18 additions & 4 deletions
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

Lines changed: 18 additions & 4 deletions
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
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919

2020
package com.sk89q.jnbt;
2121

22+
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
23+
2224
import java.util.Collections;
2325
import java.util.HashMap;
2426
import java.util.List;
2527
import java.util.Map;
28+
import java.util.Set;
2629

2730
/**
2831
* The {@code TAG_Compound} tag.
32+
*
33+
* @deprecated Use {@link com.sk89q.worldedit.util.nbt.CompoundBinaryTag}.
2934
*/
35+
@Deprecated
3036
public final class CompoundTag extends Tag {
3137

3238
private final Map<String, Tag> value;
@@ -41,6 +47,23 @@ public CompoundTag(Map<String, Tag> value) {
4147
this.value = Collections.unmodifiableMap(value);
4248
}
4349

50+
CompoundTag(CompoundBinaryTag adventureTag) {
51+
Set<String> tags = adventureTag.keySet();
52+
Map<String, Tag> map = new HashMap<>();
53+
for (String tagName : tags) {
54+
map.put(tagName, AdventureNBTConverter.fromAdventure(adventureTag.get(tagName)));
55+
}
56+
this.value = Collections.unmodifiableMap(map);
57+
}
58+
59+
CompoundBinaryTag toAdventure() {
60+
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
61+
for (Map.Entry<String, Tag> child : getValue().entrySet()) {
62+
builder.put(child.getKey(), AdventureNBTConverter.toAdventure(child.getValue()));
63+
}
64+
return builder.build();
65+
}
66+
4467
/**
4568
* Returns whether this compound tag contains the given key.
4669
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
/**
2828
* Helps create compound tags.
29+
*
30+
* @deprecated Use {@link com.sk89q.worldedit.util.nbt.CompoundBinaryTag.Builder}.
2931
*/
32+
@Deprecated
3033
public class CompoundTagBuilder {
3134

3235
private final Map<String, Tag> entries;

0 commit comments

Comments
 (0)