Skip to content

Commit 28303f3

Browse files
committed
Fix deserialization for ClickEvent and HoverEvent
1 parent e2cce54 commit 28303f3

4 files changed

Lines changed: 69 additions & 15 deletions

File tree

src/main/java/org/machinemc/scriptive/events/ClickEvent.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.machinemc.scriptive.Contents;
44

5+
import java.util.Locale;
56
import java.util.Map;
67

78
public record ClickEvent(Action action, String value) implements Contents {
@@ -22,6 +23,19 @@ public Map<String, Object> asMap() {
2223
);
2324
}
2425

26+
public static ClickEvent deserialize(Map<String, String> map) {
27+
Action action;
28+
try {
29+
action = map.containsKey("action") ? Action.valueOf(map.get("action").toUpperCase(Locale.ENGLISH)) : null;
30+
} catch (IllegalArgumentException ignored) {
31+
return null;
32+
}
33+
String value = map.get("action");
34+
if (value == null)
35+
return null;
36+
return new ClickEvent(action, value);
37+
}
38+
2539
public enum Action {
2640
OPEN_URL,
2741
RUN_COMMAND,

src/main/java/org/machinemc/scriptive/events/HoverEvent.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.jetbrains.annotations.Nullable;
44
import org.machinemc.scriptive.Contents;
55
import org.machinemc.scriptive.components.Component;
6+
import org.machinemc.scriptive.components.TextComponent;
7+
import org.machinemc.scriptive.serialization.ComponentSerializer;
68

79
import java.util.HashMap;
810
import java.util.Map;
@@ -30,6 +32,13 @@ public Map<String, Object> asMap() {
3032
);
3133
}
3234

35+
@SuppressWarnings("unchecked")
36+
public static <V extends Value> HoverEvent<?> deserialize(ComponentSerializer serializer, Map<String, Object> map) {
37+
HoverEvent.Action<V> action = (Action<V>) Action.byName(map.get("action") + "");
38+
if (action == null) return null;
39+
return new HoverEvent<>(action, action.parseValue(serializer, map.get("contents")));
40+
}
41+
3342
public static final class Action<V extends Value> {
3443

3544
public static final Action<Text> SHOW_TEXT = new Action<>("show_text", Text.class);
@@ -52,6 +61,41 @@ public Class<V> type() {
5261
return type;
5362
}
5463

64+
@SuppressWarnings("unchecked")
65+
public V parseValue(ComponentSerializer serializer, Object value) {
66+
if (value == null || (this != SHOW_TEXT && !(value instanceof Map<?, ?>))) return null;
67+
if (this == SHOW_TEXT) {
68+
if (value instanceof String string)
69+
return (V) new Text(TextComponent.of(string));
70+
if (!(value instanceof Map<?, ?> map)) return null;
71+
return serializer.deserialize((Map<String, Object>) map);
72+
} else if (this == SHOW_ITEM) {
73+
Map<String, Object> map = (Map<String, Object>) value;
74+
if (!map.containsKey("id") || !map.containsKey("count")) return null;
75+
String id = map.get("id") + "";
76+
int count = Integer.parseInt(map.get("count") + "");
77+
String tag = map.containsKey("tag") ? map.get("tag") + "" : null;
78+
return (V) new Item(id, count, tag);
79+
} else if (this == SHOW_ENTITY) {
80+
Map<String, Object> map = (Map<String, Object>) value;
81+
UUID id = map.containsKey("id") ? UUID.fromString(map.get("id") + "") : null;
82+
if (id == null) return null;
83+
String type = map.containsKey("type") ? map.get("type") + "" : null;
84+
String name = map.containsKey("name") ? map.get("name") + "" : null;
85+
return (V) new Entity(id, type, name);
86+
}
87+
throw new UnsupportedOperationException();
88+
}
89+
90+
public static Action<?> byName(String name) {
91+
return switch (name) {
92+
case "show_text" -> SHOW_TEXT;
93+
case "show_item" -> SHOW_ITEM;
94+
case "show_entity" -> SHOW_ENTITY;
95+
default -> null;
96+
};
97+
}
98+
5599
@SuppressWarnings("unchecked")
56100
public static <V extends Value> Action<V> ofValue(V value) {
57101
return (Action<V>) switch (value) {
@@ -61,6 +105,10 @@ public static <V extends Value> Action<V> ofValue(V value) {
61105
};
62106
}
63107

108+
public static Action<?>[] values() {
109+
return new Action[]{SHOW_TEXT, SHOW_ITEM, SHOW_ENTITY};
110+
}
111+
64112
}
65113

66114
public interface ValueHolder<V extends Value> {

src/main/java/org/machinemc/scriptive/serialization/ComponentSerializerImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.machinemc.scriptive.style.HexColor;
1111

1212
import java.util.*;
13+
import java.util.function.Function;
1314

1415
@SuppressWarnings("unchecked")
1516
public class ComponentSerializerImpl implements ComponentSerializer {
@@ -78,14 +79,15 @@ public <C extends Component> C deserialize(Map<String, Object> map) {
7879
.map(String::valueOf)
7980
.ifPresent(component::setInsertion);
8081

81-
// TODO fix deserialization of these two
8282
Optional.ofNullable(map.get("clickEvent"))
83-
.filter(o -> o instanceof ClickEvent)
84-
.map(o -> (ClickEvent) o)
83+
.filter(o -> o instanceof Map<?, ?>)
84+
.map(o -> (Map<String, String>) o)
85+
.map(ClickEvent::deserialize)
8586
.ifPresent(component::setClickEvent);
8687
Optional.ofNullable(map.get("hoverEvent"))
87-
.filter(o -> o instanceof HoverEvent<?>)
88-
.map(o -> (HoverEvent<?>) o)
88+
.filter(o -> o instanceof Map<?, ?>)
89+
.map(o -> (Map<String, Object>) o)
90+
.map(hoverEventMap -> HoverEvent.deserialize(this, hoverEventMap))
8991
.ifPresent(component::setHoverEvent);
9092

9193
if (map.containsKey("extra")) {

src/main/java/org/machinemc/scriptive/util/StringReader.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ public char read() {
5151
return input.charAt(cursor++);
5252
}
5353

54-
public char next() {
55-
skipWhitespace();
56-
return read();
57-
}
58-
59-
public void skipWhitespace() {
60-
while (canRead() && Character.isWhitespace(peek()))
61-
read();
62-
}
63-
6454
public void reset() {
6555
cursor = 0;
6656
}

0 commit comments

Comments
 (0)