-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.patch
432 lines (415 loc) · 21.6 KB
/
patch.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
diff --git a/MCStructs-snbt/src/main/java/net/lenni0451/mcstructs/snbt/impl/v1_12/SNbtDeserializer_v1_12.java b/MCStructs-snbt/src/main/java/net/lenni0451/mcstructs/snbt/impl/v1_12/SNbtDeserializer_v1_12.java
index 60ad052..1b98e7a 100644
--- a/MCStructs-snbt/src/main/java/net/lenni0451/mcstructs/snbt/impl/v1_12/SNbtDeserializer_v1_12.java
+++ b/MCStructs-snbt/src/main/java/net/lenni0451/mcstructs/snbt/impl/v1_12/SNbtDeserializer_v1_12.java
@@ -95,10 +95,28 @@ public class SNbtDeserializer_v1_12 implements ISNbtDeserializer<CompoundTag> {
reader.read();
reader.skipWhitespaces();
if (!reader.canRead()) throw this.makeException(reader, "Expected value");
- else if (c == 'B') return new ByteArrayTag(this.readPrimitiveList(reader, ByteTag.class, ByteArrayTag.class));
- else if (c == 'L') return new LongArrayTag(this.readPrimitiveList(reader, LongTag.class, LongArrayTag.class));
- else if (c == 'I') return new IntArrayTag(this.readPrimitiveList(reader, IntTag.class, IntArrayTag.class));
- else throw new SNbtDeserializeException("Invalid array type '" + c + "' found");
+ else if (c == 'B') {
+ final ListTag<ByteTag> tags = this.readPrimitiveList(reader, ByteTag.class, ByteArrayTag.class);
+ final byte[] array = new byte[tags.size()];
+ for (int i = 0; i < tags.size(); i++) {
+ array[i] = tags.get(i).asByte();
+ }
+ return new ByteArrayTag(array);
+ } else if (c == 'L') {
+ final ListTag<LongTag> tags = this.readPrimitiveList(reader, LongTag.class, LongArrayTag.class);
+ final long[] array = new long[tags.size()];
+ for (int i = 0; i < tags.size(); i++) {
+ array[i] = tags.get(i).asLong();
+ }
+ return new LongArrayTag(array);
+ } else if (c == 'I') {
+ final ListTag<IntTag> tags = this.readPrimitiveList(reader, IntTag.class, IntArrayTag.class);
+ final int[] array = new int[tags.size()];
+ for (int i = 0; i < tags.size(); i++) {
+ array[i] = tags.get(i).asInt();
+ }
+ return new IntArrayTag(array);
+ } else throw new SNbtDeserializeException("Invalid array type '" + c + "' found");
}
protected Tag readValue(final StringReader_v1_12 reader) throws SNbtDeserializeException {
diff --git a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/ATextComponent.java b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/ATextComponent.java
index a60fc64..e839e9b 100644
--- a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/ATextComponent.java
+++ b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/ATextComponent.java
@@ -32,6 +32,13 @@ public abstract class ATextComponent implements ICopyable<ATextComponent> {
private final List<ATextComponent> siblings = new ArrayList<>();
private Style style = new Style();
+ private int level = 1;
+ private void setLevel(int level) {
+ this.level = Math.max(this.level, level);
+ if (this.level > 512) {
+ throw new IllegalArgumentException("Too deep");
+ }
+ }
/**
* Append a string to this component.
@@ -52,6 +59,7 @@ public abstract class ATextComponent implements ICopyable<ATextComponent> {
*/
public ATextComponent append(final ATextComponent component) {
this.siblings.add(component);
+ component.setLevel(this.level + 1);
return this;
}
@@ -63,6 +71,7 @@ public abstract class ATextComponent implements ICopyable<ATextComponent> {
*/
public ATextComponent append(final ATextComponent... components) {
this.siblings.addAll(Arrays.asList(components));
+ for (ATextComponent component : components) component.setLevel(this.level + 1);
return this;
}
@@ -177,24 +186,28 @@ public abstract class ATextComponent implements ICopyable<ATextComponent> {
return out.toString();
}
- /**
- * @return A legacy formatted string representation of this component
- */
- public String asLegacyFormatString() {
- StringBuilder out = new StringBuilder();
- if (this.style.getColor() != null && this.style.getColor().isFormattingColor()) out.append(COLOR_CHAR).append(this.style.getColor().getCode());
- else out.append("§r");
- if (this.style.isObfuscated()) out.append(COLOR_CHAR).append(TextFormatting.OBFUSCATED.getCode());
- if (this.style.isBold()) out.append(COLOR_CHAR).append(TextFormatting.BOLD.getCode());
- if (this.style.isStrikethrough()) out.append(COLOR_CHAR).append(TextFormatting.STRIKETHROUGH.getCode());
- if (this.style.isUnderlined()) out.append(COLOR_CHAR).append(TextFormatting.UNDERLINE.getCode());
- if (this.style.isItalic()) out.append(COLOR_CHAR).append(TextFormatting.ITALIC.getCode());
- out.append(this.asSingleString());
+ public void visit(final Consumer<String> consumer) {
+ if (this.style.getColor() != null && this.style.getColor().isFormattingColor()) consumer.accept(Character.toString(COLOR_CHAR) + this.style.getColor().getCode());
+ else consumer.accept("§r");
+ if (this.style.isObfuscated()) consumer.accept(Character.toString(COLOR_CHAR) + TextFormatting.OBFUSCATED.getCode());
+ if (this.style.isBold()) consumer.accept(Character.toString(COLOR_CHAR) + TextFormatting.BOLD.getCode());
+ if (this.style.isStrikethrough()) consumer.accept(Character.toString(COLOR_CHAR) + TextFormatting.STRIKETHROUGH.getCode());
+ if (this.style.isUnderlined()) consumer.accept(Character.toString(COLOR_CHAR) + TextFormatting.UNDERLINE.getCode());
+ if (this.style.isItalic()) consumer.accept(Character.toString(COLOR_CHAR) + TextFormatting.ITALIC.getCode());
+ consumer.accept(this.asSingleString());
for (ATextComponent sibling : this.siblings) {
ATextComponent copy = sibling.copy();
copy.getStyle().setParent(this.style);
- out.append(copy.asLegacyFormatString());
+ copy.visit(consumer);
}
+ }
+ /**
+ * @return A legacy formatted string representation of this component
+ */
+ public final String asLegacyFormatString() {
+ // Improve traversal performance
+ StringBuilder out = new StringBuilder();
+ this.visit(out::append);
return out.toString();
}
diff --git a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/components/TranslationComponent.java b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/components/TranslationComponent.java
index 91b176d..8158521 100644
--- a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/components/TranslationComponent.java
+++ b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/components/TranslationComponent.java
@@ -142,8 +142,29 @@ public class TranslationComponent extends ATextComponent {
}
@Override
- public String asLegacyFormatString() {
- return this.resolveIntoComponents().asLegacyFormatString();
+ public void visit(final java.util.function.Consumer<String> consumer) {
+ if (consumer instanceof TranslatableContentConsumer) {
+ this.resolveIntoComponents().visit(consumer);
+ } else {
+ this.resolveIntoComponents().visit(new TranslatableContentConsumer(consumer));
+ }
+ }
+ private static final class TranslatableContentConsumer implements java.util.function.Consumer<String> {
+ private static final IllegalArgumentException EX = new IllegalArgumentException("Too long");
+ private final java.util.function.Consumer<String> runnable;
+ private int visited;
+
+ private TranslatableContentConsumer(final java.util.function.Consumer<String> runnable) {
+ this.runnable = runnable;
+ }
+
+ @Override
+ public void accept(final String s) {
+ if (visited++ > 32) {
+ throw EX;
+ }
+ this.runnable.accept(s);
+ }
}
@Override
diff --git a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/EntityHoverEvent.java b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/EntityHoverEvent.java
index ed2644d..55c6e02 100644
--- a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/EntityHoverEvent.java
+++ b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/EntityHoverEvent.java
@@ -117,10 +117,10 @@ public class EntityHoverEvent extends AHoverEvent {
@Override
public String toString() {
return ToString.of(this)
- .put("action", this.action)
- .put("entityType", this.entityType)
- .put("uuid", this.uuid)
- .put("name", this.name, Objects::nonNull)
+ .add("action", this.action)
+ .add("entityType", this.entityType)
+ .add("uuid", this.uuid)
+ .add("name", this.name, Objects::nonNull)
.toString();
}
diff --git a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/ItemHoverEvent.java b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/ItemHoverEvent.java
index 2812bd2..aea17e6 100644
--- a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/ItemHoverEvent.java
+++ b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/events/hover/impl/ItemHoverEvent.java
@@ -119,10 +119,10 @@ public class ItemHoverEvent extends AHoverEvent {
@Override
public String toString() {
return ToString.of(this)
- .put("action", this.action)
- .put("item", this.item)
- .put("count", this.count, count -> count != 1)
- .put("nbt", this.nbt, Objects::nonNull)
+ .add("action", this.action)
+ .add("item", this.item)
+ .add("count", this.count, count -> count != 1)
+ .add("nbt", this.nbt, Objects::nonNull)
.toString();
}
diff --git a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/utils/JsonNbtConverter.java b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/utils/JsonNbtConverter.java
index 8bd6503..e512af7 100644
--- a/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/utils/JsonNbtConverter.java
+++ b/MCStructs-text/src/main/java/net/lenni0451/mcstructs/text/utils/JsonNbtConverter.java
@@ -25,23 +25,15 @@ public class JsonNbtConverter {
@Nullable
public static JsonElement toJson(@Nullable final Tag tag) {
if (tag == null) return null;
- switch (tag) {
- case END:
- return null;
- case BYTE:
- case SHORT:
- case INT:
- case LONG:
- case FLOAT:
- case DOUBLE:
+ if (tag instanceof NumberTag) {
return new JsonPrimitive(((NumberTag) tag).getValue());
- case BYTE_ARRAY:
+ } else if (tag instanceof ByteArrayTag) {
JsonArray byteArray = new JsonArray();
for (byte b : ((ByteArrayTag) tag).getValue()) byteArray.add(b);
return byteArray;
- case STRING:
+ } else if (tag instanceof StringTag) {
return new JsonPrimitive(((StringTag) tag).getValue());
- case LIST:
+ } else if (tag instanceof ListTag<?>) {
JsonArray list = new JsonArray();
ListTag<Tag> listTag = ((ListTag) tag);
for (Tag tagInList : listTag.getValue()) {
@@ -55,19 +47,19 @@ public class JsonNbtConverter {
list.add(toJson(tagInList));
}
return list;
- case COMPOUND:
+ } else if (tag instanceof CompoundTag) {
JsonObject compound = new JsonObject();
- for (Map.Entry<String, Tag> entry : ((CompoundTag) tag).getValue().entrySet()) compound.put(entry.getKey(), toJson(entry.getValue()));
+ for (Map.Entry<String, Tag> entry : ((CompoundTag) tag).getValue().entrySet()) compound.add(entry.getKey(), toJson(entry.getValue()));
return compound;
- case INT_ARRAY:
+ } else if (tag instanceof IntArrayTag) {
JsonArray intArray = new JsonArray();
for (int i : ((IntArrayTag) tag).getValue()) intArray.add(i);
return intArray;
- case LONG_ARRAY:
+ } else if (tag instanceof LongArrayTag) {
JsonArray longArray = new JsonArray();
for (long l : ((LongArrayTag) tag).getValue()) longArray.add(l);
return longArray;
- default:
+ } else {
throw new IllegalArgumentException("Unknown Nbt type: " + tag);
}
}
@@ -90,31 +82,37 @@ public class JsonNbtConverter {
JsonArray array = element.getAsJsonArray();
List<Tag> nbtTags = new ArrayList<>();
Tag listType = null;
+ boolean mixedList = false;
for (JsonElement arrayElement : array) {
Tag tag = toNbt(arrayElement);
nbtTags.add(tag);
listType = getListType(listType, tag);
+ if (listType == null) mixedList = true;
}
if (listType == null) {
return new ListTag<>();
- } else if (listType == Tag.END) { //Mixed list
+ } else if (mixedList) { //Mixed list
ListTag<CompoundTag> list = new ListTag<>();
for (Tag tag : nbtTags) {
if (tag instanceof CompoundTag) list.add(((CompoundTag) tag));
- else list.add(new CompoundTag().put("", tag));
+ else {
+ final CompoundTag entries = new CompoundTag();
+ entries.put("", tag);
+ list.add(entries);
+ }
}
return list;
- } else if (listType == Tag.BYTE) {
+ } else if (listType instanceof ByteTag) {
byte[] bytes = new byte[nbtTags.size()];
- for (int i = 0; i < nbtTags.size(); i++) bytes[i] = nbtTags.get(i).asByteTag().byteValue();
+ for (int i = 0; i < nbtTags.size(); i++) bytes[i] = ((NumberTag) nbtTags.get(i)).asByte();
return new ByteArrayTag(bytes);
- } else if (listType == Tag.INT) {
+ } else if (listType instanceof IntTag) {
int[] ints = new int[nbtTags.size()];
- for (int i = 0; i < nbtTags.size(); i++) ints[i] = nbtTags.get(i).asIntTag().asInt();
+ for (int i = 0; i < nbtTags.size(); i++) ints[i] = ((NumberTag) nbtTags.get(i)).asInt();
return new IntArrayTag(ints);
- } else if (listType == Tag.LONG) {
+ } else if (listType instanceof LongTag) {
long[] longs = new long[nbtTags.size()];
- for (int i = 0; i < nbtTags.size(); i++) longs[i] = nbtTags.get(i).asIntTag().asInt();
+ for (int i = 0; i < nbtTags.size(); i++) longs[i] = ((NumberTag) nbtTags.get(i)).asLong();
return new LongArrayTag(longs);
} else {
return new ListTag<>(nbtTags);
@@ -148,7 +146,7 @@ public class JsonNbtConverter {
private static Tag getListType(final Tag current, final Tag tag) {
if (current == null) return tag;
- if (current != tag) return Tag.END; //Placeholder for mixed lists
+ if (tag == null || current.getClass() != tag.getClass()) return null; //Placeholder for mixed lists
return current;
}
diff --git a/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java b/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
index cb0628a..18f5d2e 100644
--- a/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
+++ b/MCStructs-text/src/test/java/net/lenni0451/mcstructs/text/serializer/TextComponentCodecTest.java
@@ -61,9 +61,9 @@ class TextComponentCodecTest {
@Test
void legacyItemDeserialization() throws SNbtSerializeException {
- CompoundTag legacyNbt = new CompoundTag()
- .put("id", "stone")
- .putByte("Count", (byte) 5);
+ CompoundTag legacyNbt = new CompoundTag();
+ legacyNbt.put("id", new StringTag("stone"));
+ legacyNbt.putByte("Count", (byte) 5);
ATextComponent legacyComponent = new StringComponent("test")
.setStyle(new Style()
.setHoverEvent(new TextHoverEvent(HoverEventAction.SHOW_ITEM, new StringComponent(SNbtSerializer.LATEST.serialize(legacyNbt))))
@@ -80,10 +80,10 @@ class TextComponentCodecTest {
@Test
void legacyEntityDeserialization() throws SNbtSerializeException {
UUID randomUUID = UUID.randomUUID();
- CompoundTag legacyNbt = new CompoundTag()
- .put("name", "{\"text\":\"test\"}")
- .put("type", "cow")
- .put("id", randomUUID.toString());
+ CompoundTag legacyNbt = new CompoundTag();
+ legacyNbt.put("name", new StringTag("{\"text\":\"test\"}"));
+ legacyNbt.put("type", new StringTag("cow"));
+ legacyNbt.put("id", new StringTag(randomUUID.toString()));
ATextComponent legacyComponent = new StringComponent("test")
.setStyle(new Style()
.setHoverEvent(new TextHoverEvent(HoverEventAction.SHOW_ENTITY, new StringComponent(SNbtSerializer.LATEST.serialize(legacyNbt))))
@@ -100,19 +100,31 @@ class TextComponentCodecTest {
@Test
void arrayWithTag() {
- ListTag<Tag> tags = new ListTag<>()
- .add(new CompoundTag()
- .putString("translate", "test")
- .addByteArray("with", (byte) 1, (byte) 2, (byte) 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addIntArray("with", 1, 2, 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addLongArray("with", 1, 2, 3))
- .add(new CompoundTag()
- .putString("translate", "test")
- .addList("with", 1, 2, 3));
+ CompoundTag translateWithByteArray = new CompoundTag();
+ translateWithByteArray.putString("translate", "test");
+ translateWithByteArray.put("with", new ByteArrayTag(new byte[]{1, 2, 3}));
+
+ CompoundTag translateWithIntArray = new CompoundTag();
+ translateWithIntArray.putString("translate", "test");
+ translateWithIntArray.put("with", new IntArrayTag(new int[]{1, 2, 3}));
+
+ CompoundTag translateWithLongArray = new CompoundTag();
+ translateWithLongArray.putString("translate", "test");
+ translateWithLongArray.put("with", new LongArrayTag(new long[]{1, 2, 3}));
+
+ CompoundTag translateWithList = new CompoundTag();
+ ListTag<IntTag> numberList = new ListTag<>(IntTag.class);
+ numberList.add(new IntTag(1));
+ numberList.add(new IntTag(2));
+ numberList.add(new IntTag(3));
+ translateWithList.putString("translate", "test");
+ translateWithList.put("with", numberList);
+
+ ListTag<CompoundTag> tags = new ListTag<>(CompoundTag.class);
+ tags.add(translateWithByteArray);
+ tags.add(translateWithIntArray);
+ tags.add(translateWithLongArray);
+ tags.add(translateWithList);
ATextComponent component = new TranslationComponent("test", (byte) 1, (byte) 2, (byte) 3)
.append(new TranslationComponent("test", 1, 2, 3))
.append(new TranslationComponent("test", 1L, 2L, 3L))
diff --git a/build.gradle b/build.gradle
index 2178d6b..6b8f7e1 100644
--- a/build.gradle
+++ b/build.gradle
@@ -60,17 +60,17 @@ subprojects {
publishing {
repositories {
maven {
- name = "reposilite"
- def releasesUrl = "https://maven.lenni0451.net/releases"
- def snapshotsUrl = "https://maven.lenni0451.net/snapshots"
- url = project.maven_version.endsWith("SNAPSHOT") ? snapshotsUrl : releasesUrl
-
- credentials(PasswordCredentials)
+ name = "Via"
+ url = uri("https://repo.viaversion.com/")
+ credentials {
+ username = System.getenv("via_username")
+ password = System.getenv("via_password")
+ }
authentication {
- basic(BasicAuthentication)
+ create(BasicAuthentication)
}
}
- maven {
+ /*maven {
name = "ossrh"
def releasesUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
@@ -80,7 +80,7 @@ subprojects {
authentication {
basic(BasicAuthentication)
}
- }
+ }*/
}
publications {
maven(MavenPublication) {
diff --git a/settings.gradle b/settings.gradle
index 64fd6ec..46ca6cc 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -11,11 +11,6 @@ plugins {
rootProject.name = "MCStructs"
-include(":MCStructs-all")
-include(":MCStructs-converter")
include(":MCStructs-core")
-include(":MCStructs-data")
-include(":MCStructs-itemcomponents")
-include(":MCStructs-nbt")
include(":MCStructs-snbt")
include(":MCStructs-text")