Skip to content

Commit c1861d1

Browse files
committed
feat: update spectator's configurate serializers
1 parent c47acef commit c1861d1

File tree

6 files changed

+83
-15
lines changed

6 files changed

+83
-15
lines changed

nbt/src/test/java/org/screamingsandals/lib/test/nbt/SNBTSerializerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,17 @@ public void testNEscapeSequence() {
352352

353353
assertEquals(expected, actual);
354354
}
355+
356+
@Test
357+
public void testNEscapeSequence2() {
358+
var serializer = SNBTSerializer.builder().build();
359+
360+
var string = "\"\\N{Snowman}OtherString\"";
361+
362+
var actual = serializer.deserialize(string);
363+
364+
var expected = new StringTag("\u2603OtherString");
365+
366+
assertEquals(expected, actual);
367+
}
355368
}

spectator/common/src/main/java/org/screamingsandals/lib/spectator/configurate/ClickEventSerializer.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,24 @@ public class ClickEventSerializer implements TypeSerializer<ClickEvent> {
3232
private static final @NotNull String ACTION_KEY = "action";
3333
private static final @NotNull String VALUE_KEY = "value";
3434

35+
private static final @NotNull String URL_KEY = "url";
36+
private static final @NotNull String COMMAND_KEY = "command";
37+
private static final @NotNull String PAGE_KEY = "page";
38+
3539
@Override
3640
public @NotNull ClickEvent deserialize(@NotNull Type type, @NotNull ConfigurationNode node) throws SerializationException {
3741
try {
3842
var action = ClickEvent.Action.valueOf(node.node(ACTION_KEY).getString("open_url").toUpperCase(Locale.ROOT));
39-
var value = node.node(VALUE_KEY).getString("");
43+
String value;
44+
if (action == ClickEvent.Action.OPEN_URL && node.hasChild(URL_KEY)) {
45+
value = node.node(URL_KEY).getString("");
46+
} else if ((action == ClickEvent.Action.RUN_COMMAND || action == ClickEvent.Action.SUGGEST_COMMAND) && node.hasChild(COMMAND_KEY)) {
47+
value = node.node(COMMAND_KEY).getString("");
48+
} else if (action == ClickEvent.Action.CHANGE_PAGE && node.hasChild(PAGE_KEY)) {
49+
value = node.node(PAGE_KEY).getString("");
50+
} else {
51+
value = node.node(VALUE_KEY).getString("");
52+
}
4053
return ClickEvent.builder()
4154
.action(action)
4255
.value(value)
@@ -54,6 +67,19 @@ public void serialize(@NotNull Type type, @Nullable ClickEvent obj, @NotNull Con
5467
}
5568

5669
node.node(ACTION_KEY).set(obj.action().name().toLowerCase(Locale.ROOT));
57-
node.node(VALUE_KEY).set(obj.value());
70+
switch (obj.action()) {
71+
case OPEN_URL:
72+
node.node(URL_KEY).set(obj.value());
73+
break;
74+
case RUN_COMMAND:
75+
case SUGGEST_COMMAND:
76+
node.node(COMMAND_KEY).set(obj.value());
77+
break;
78+
case CHANGE_PAGE:
79+
node.node(PAGE_KEY).set(obj.value());
80+
break;
81+
default:
82+
node.node(VALUE_KEY).set(obj.value());
83+
}
5884
}
5985
}

spectator/common/src/main/java/org/screamingsandals/lib/spectator/configurate/ComponentSerializer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ public class ComponentSerializer implements TypeSerializer<Component> {
8181
// Shared content & Formatting
8282
private static final @NotNull String EXTRA_KEY = "extra";
8383
private static final @NotNull String COLOR_KEY = "color";
84-
private static final @NotNull String SHADOW_COLOR_KEY = "shadowColor";
84+
private static final @NotNull String SHADOW_COLOR_KEY = "shadow_color";
8585
private static final @NotNull String FONT_KEY = "font";
8686
private static final @NotNull String BOLD_KEY = "bold";
8787
private static final @NotNull String ITALIC_KEY = "italic";
8888
private static final @NotNull String UNDERLINED_KEY = "underlined";
8989
private static final @NotNull String STRIKETHROUGH_KEY = "strikethrough";
9090
private static final @NotNull String OBFUSCATED_KEY = "obfuscated";
9191
private static final @NotNull String INSERTION_KEY = "insertion";
92-
private static final @NotNull String CLICK_EVENT_KEY = "clickEvent";
93-
private static final @NotNull String HOVER_EVENT_KEY = "hoverEvent";
92+
private static final @NotNull String CLICK_EVENT_KEY = "click_event";
93+
private static final @NotNull String CLICK_EVENT_KEY_OLD = "clickEvent";
94+
private static final @NotNull String HOVER_EVENT_KEY = "hover_event";
95+
private static final @NotNull String HOVER_EVENT_KEY_OLD = "hoverEvent";
9496

9597
// Types
9698
private static final @NotNull String TYPE_KEY = "type";
@@ -257,10 +259,14 @@ public class ComponentSerializer implements TypeSerializer<Component> {
257259

258260
if (node.hasChild(CLICK_EVENT_KEY)) {
259261
builder.clickEvent(node.node(CLICK_EVENT_KEY).get(ClickEvent.class));
262+
} else if (node.hasChild(CLICK_EVENT_KEY_OLD)) {
263+
builder.clickEvent(node.node(CLICK_EVENT_KEY_OLD).get(ClickEvent.class));
260264
}
261265

262266
if (node.hasChild(HOVER_EVENT_KEY)) {
263267
builder.hoverEvent(node.node(HOVER_EVENT_KEY).get(HoverEvent.class));
268+
} else if (node.hasChild(HOVER_EVENT_KEY_OLD)) {
269+
builder.hoverEvent(node.node(HOVER_EVENT_KEY_OLD).get(HoverEvent.class));
264270
}
265271

266272
return builder.build();

spectator/common/src/main/java/org/screamingsandals/lib/spectator/configurate/EntityContentSerializer.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,29 @@ public class EntityContentSerializer implements TypeSerializer<EntityContent> {
3232
public static final @NotNull EntityContentSerializer INSTANCE = new EntityContentSerializer();
3333

3434
private static final @NotNull String TYPE_KEY = "type";
35-
private static final @NotNull String ID_KEY = "id";
35+
private static final @NotNull String ID_KEY = "id"; // repurposed since 1.21.5
3636
private static final @NotNull String NAME_KEY = "name";
3737

38+
// New
39+
private static final @NotNull String UUID_KEY = "uuid";
40+
3841
@Override
3942
public @NotNull EntityContent deserialize(@NotNull Type type, @NotNull ConfigurationNode node) throws SerializationException {
4043
try {
41-
var entityType = ResourceLocation.of(node.node(TYPE_KEY).getString("minecraft:pig"));
42-
var idNode = node.node(ID_KEY);
44+
String typeKey;
45+
String idKey;
46+
if (node.hasChild(UUID_KEY)) {
47+
// 1.21.5+
48+
idKey = UUID_KEY;
49+
typeKey = ID_KEY;
50+
} else {
51+
typeKey = TYPE_KEY;
52+
idKey = ID_KEY;
53+
}
54+
55+
var entityType = ResourceLocation.of(node.node(typeKey).getString("minecraft:pig"));
56+
57+
var idNode = node.node(idKey);
4358
var id = idNode.isList() ? deserializeUuidFromListOrRandom(idNode) : idNode.get(UUID.class, UUID.randomUUID());
4459
@Nullable var name = node.node(NAME_KEY).get(Component.class);
4560

@@ -68,8 +83,8 @@ public void serialize(@NotNull Type type, @Nullable EntityContent obj, @NotNull
6883
return;
6984
}
7085

71-
node.node(TYPE_KEY).set(obj.type().asString());
72-
node.node(ID_KEY).set(UUID.class, obj.id());
86+
node.node(ID_KEY).set(obj.type().asString());
87+
node.node(UUID_KEY).set(UUID.class, obj.id());
7388
node.node(NAME_KEY).set(Component.class, obj.name());
7489
}
7590
}

spectator/common/src/main/java/org/screamingsandals/lib/spectator/configurate/HoverEventSerializer.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class HoverEventSerializer implements TypeSerializer<HoverEvent> {
5353
default:
5454
content = value.get(Component.class);
5555
}
56-
} else {
56+
} else if (node.hasChild(CONTENTS_KEY)) {
5757
var contents = node.node(CONTENTS_KEY);
5858
switch (action) {
5959
case SHOW_ITEM:
@@ -65,6 +65,12 @@ public class HoverEventSerializer implements TypeSerializer<HoverEvent> {
6565
default:
6666
content = contents.get(Component.class);
6767
}
68+
} else if (action == HoverEvent.Action.SHOW_ITEM) {
69+
content = node.get(ItemContent.class);
70+
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
71+
content = node.get(EntityContent.class);
72+
} else {
73+
content = null;
6874
}
6975
Preconditions.checkNotNull(content);
7076
return HoverEvent.builder()
@@ -83,16 +89,16 @@ public void serialize(@NotNull Type type, @Nullable HoverEvent obj, @NotNull Con
8389
return;
8490
}
8591

86-
node.node(ACTION_KEY).set(obj.action().name().toLowerCase(Locale.ROOT)); // lower case to match vanilla keys
8792
switch (obj.action()) {
8893
case SHOW_ITEM:
89-
node.node(CONTENTS_KEY).set(ItemContent.class, (ItemContent) obj.content());
94+
node.set(ItemContent.class, (ItemContent) obj.content());
9095
break;
9196
case SHOW_ENTITY:
92-
node.node(CONTENTS_KEY).set(EntityContent.class, (EntityContent) obj.content());
97+
node.set(EntityContent.class, (EntityContent) obj.content());
9398
break;
9499
default:
95-
node.node(CONTENTS_KEY).set(Component.class, (Component) obj.content());
100+
node.node(VALUE_KEY).set(Component.class, (Component) obj.content());
96101
}
102+
node.node(ACTION_KEY).set(obj.action().name().toLowerCase(Locale.ROOT)); // lower case to match vanilla keys
97103
}
98104
}

spectator/common/src/main/java/org/screamingsandals/lib/spectator/configurate/ItemContentSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class ItemContentSerializer implements TypeSerializer<ItemContent> {
4545
var count = node.node(COUNT_KEY).getInt(1);
4646
var tag = node.node(TAG_KEY);
4747
CompoundTag compoundTag;
48+
// TODO: load components
4849
if (!tag.empty()) {
4950
if (tag.isMap()) {
5051
compoundTag = (CompoundTag) TagSerializer.INSTANCE.deserialize(Tag.class, tag);
@@ -73,6 +74,7 @@ public void serialize(@NotNull Type type, @Nullable ItemContent obj, @NotNull Co
7374

7475
node.node(ID_KEY).set(obj.id().asString());
7576
node.node(COUNT_KEY).set(obj.count());
77+
// TODO: save components
7678
node.node(TAG_KEY).set(obj.tag());
7779
}
7880
}

0 commit comments

Comments
 (0)