Skip to content

Commit b47d54e

Browse files
authored
Merge pull request #3 from MachineMC/properties-serializer
Properties serializer
2 parents 073d9d6 + 6867cce commit b47d54e

File tree

14 files changed

+118
-82
lines changed

14 files changed

+118
-82
lines changed

scriptive-core/src/main/java/org/machinemc/scriptive/components/BaseComponent.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ public ComponentProperties getProperties() {
257257

258258
@Override
259259
@MustBeInvokedByOverriders
260-
@SuppressWarnings("unchecked")
261-
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
260+
public void loadProperties(ComponentProperties properties, ComponentSerializer serializer) {
262261
textFormat = new TextFormat(properties);
263262
setInsertion(properties.getValue("insertion", String.class).orElse(null));
264263
setClickEvent(properties.getValue("clickEvent", ComponentProperties.class)
@@ -270,9 +269,7 @@ public void loadProperties(ComponentProperties properties, ComponentSerializer<?
270269
clearSiblings();
271270
properties.getValue("extra", ComponentProperties[].class).ifPresent(extra -> {
272271
Component[] siblings = Arrays.stream(extra)
273-
.map(serializer::serializeFromProperties)
274-
.map(Object.class::cast)
275-
.map(o -> ((ComponentSerializer<Object>) serializer).deserialize(o))
272+
.map(serializer::deserialize)
276273
.toArray(Component[]::new);
277274
for (Component sibling : siblings) append(sibling);
278275
});

scriptive-core/src/main/java/org/machinemc/scriptive/components/Component.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ default ComponentModifier modify() {
272272
* @param properties properties to load to this component
273273
* @param serializer serializer
274274
*/
275-
void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer);
275+
void loadProperties(ComponentProperties properties, ComponentSerializer serializer);
276276

277277
@Override
278278
default HoverEvent.Text asHoverEventValue() {

scriptive-core/src/main/java/org/machinemc/scriptive/components/KeybindComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Class<KeybindComponent> getType() {
9898
}
9999

100100
@Override
101-
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
101+
public void loadProperties(ComponentProperties properties, ComponentSerializer serializer) {
102102
super.loadProperties(properties, serializer);
103103
keybind = properties.getValue("keybind", String.class).orElseThrow();
104104
}

scriptive-core/src/main/java/org/machinemc/scriptive/components/TextComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public Class<TextComponent> getType() {
120120
}
121121

122122
@Override
123-
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
123+
public void loadProperties(ComponentProperties properties, ComponentSerializer serializer) {
124124
super.loadProperties(properties, serializer);
125125
text = properties.getValue("text", String.class).orElseThrow();
126126
}

scriptive-core/src/main/java/org/machinemc/scriptive/components/TranslationComponent.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,16 @@ public Class<TranslationComponent> getType() {
320320
}
321321

322322
@Override
323-
@SuppressWarnings("unchecked")
324-
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
323+
public void loadProperties(ComponentProperties properties, ComponentSerializer serializer) {
325324
super.loadProperties(properties, serializer);
326325
translation = properties.getValue("translate", String.class).orElseThrow();
327326
fallback = properties.getValue("translate", String.class).orElse(null);
328327
arguments = properties.get("with", ComponentProperty.Array.class)
329328
.map(ComponentProperty.Array::value)
330329
.map(array -> {
331330
Component[] components = new Component[array.length];
332-
for (int i = 0; i < array.length; i++) {
333-
Object next = serializer.serializeFromProperties(array[i]);
334-
components[i] = ((ComponentSerializer<Object>) serializer).deserialize(next);
335-
}
331+
for (int i = 0; i < array.length; i++)
332+
components[i] = serializer.deserialize(array[i]);
336333
return components;
337334
}).orElse(null);
338335
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ public HoverEvent(Action<V> action, ValueHolder<V> valueHolder) {
4646
* Creates hover event from component properties.
4747
*
4848
* @param properties component properties
49-
* @param serializer serializer to use for deserialization of text hover events
5049
* @return hover event
5150
* @param <V> hover event value type
5251
*/
5352
@SuppressWarnings("unchecked")
5453
public static <V extends HoverEvent.Value> Optional<HoverEvent<V>> fromProperties(ComponentProperties properties,
55-
ComponentSerializer<?> serializer) {
54+
ComponentSerializer serializer) {
5655
if (!properties.contains("action")) return Optional.empty();
5756
String actionName = properties.getValue("action", String.class).orElse(null);
5857
if (actionName == null) return Optional.empty();
@@ -74,7 +73,7 @@ public static <V extends HoverEvent.Value> Optional<HoverEvent<V>> fromPropertie
7473
if (action == SHOW_ENTITY) {
7574
ComponentProperties contents = properties.getValue("contents", ComponentProperties.class).orElse(null);
7675
if (contents == null) return Optional.empty();
77-
return Optional.of(new HoverEvent<>(action, (V) new Entity(contents)));
76+
return Optional.of(new HoverEvent<>(action, (V) new Entity(contents, serializer)));
7877
}
7978

8079
return Optional.empty();
@@ -228,9 +227,8 @@ public record Text(Component component) implements Value {
228227
Objects.requireNonNull(component, "Component can not be null");
229228
}
230229

231-
@SuppressWarnings("unchecked")
232-
public Text(ComponentProperties properties, ComponentSerializer<?> serializer) {
233-
this(((ComponentSerializer<Object>) serializer).deserialize(serializer.serializeFromProperties(properties)));
230+
public Text(ComponentProperties componentProperties, ComponentSerializer serializer) {
231+
this(serializer.deserialize(componentProperties));
234232
}
235233

236234
@Override
@@ -280,17 +278,17 @@ public Item(ComponentProperties properties) {
280278
* if this value is not specified
281279
* @param name custom name of the entity
282280
*/
283-
public record Entity(UUID id, @Nullable String type, @Nullable ComponentProperties name) implements Value {
281+
public record Entity(UUID id, @Nullable String type, @Nullable Component name) implements Value {
284282

285283
public Entity {
286284
Objects.requireNonNull(id, "Entity UUID can not be null");
287285
}
288286

289-
public Entity(ComponentProperties properties) {
287+
public Entity(ComponentProperties properties, ComponentSerializer serializer) {
290288
this(
291289
UUID.fromString(properties.getValue("id", String.class).orElseThrow()),
292290
properties.getValue("type", String.class).orElse(null),
293-
properties.getValue("name", ComponentProperties.class).orElse(null)
291+
properties.getValue("name", ComponentProperties.class).map(serializer::deserialize).orElse(null)
294292
);
295293
}
296294

@@ -299,7 +297,8 @@ public Entity(ComponentProperties properties) {
299297
ComponentProperties properties = new ComponentProperties();
300298
properties.set("id", id.toString());
301299
properties.set("type", type);
302-
properties.set("name", name);
300+
if (name != null)
301+
properties.set("name", name.getProperties());
303302
return properties.unmodifiableView();
304303
}
305304

scriptive-core/src/main/java/org/machinemc/scriptive/serialization/ComponentSerializer.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
/**
1717
* A {@link Component} serializer and deserializer.
18-
*
19-
* @param <T> serialized type
2018
*/
21-
public abstract class ComponentSerializer<T> {
19+
public class ComponentSerializer {
2220

2321
private final Set<Class<? extends Component>> registered = Collections.newSetFromMap(new ConcurrentHashMap<>());
2422

@@ -27,22 +25,6 @@ public abstract class ComponentSerializer<T> {
2725

2826
private final Map<Class<? extends Component>, Function<ComponentProperties, ? extends Component>> componentCreators = new ConcurrentHashMap<>();
2927

30-
/**
31-
* Serializes component properties.
32-
*
33-
* @param properties component properties
34-
* @return value
35-
*/
36-
public abstract T serializeFromProperties(ComponentProperties properties);
37-
38-
/**
39-
* Deserializes to component properties.
40-
*
41-
* @param value value
42-
* @return component properties
43-
*/
44-
public abstract ComponentProperties deserializeAsProperties(T value);
45-
4628
/**
4729
* Creates new component serializer and automatically registers the 3
4830
* default components for vanilla Minecraft client.
@@ -82,10 +64,20 @@ public <C extends Component> void register(Class<C> type, Supplier<C> emptySuppl
8264
* @param component component
8365
* @return output
8466
*/
85-
public T serialize(Component component) {
67+
public <T> T serialize(Component component, PropertiesSerializer<T> propertiesSerializer) {
68+
return propertiesSerializer.serialize(serialize(component));
69+
}
70+
71+
/**
72+
* Serializes the given component.
73+
*
74+
* @param component component
75+
* @return output
76+
*/
77+
public ComponentProperties serialize(Component component) {
8678
if (!registered.contains(component.getType()))
8779
throw new UnsupportedOperationException("Serializer does not support components of type " + component.getType().getName());
88-
return serializeFromProperties(component.getProperties());
80+
return component.getProperties();
8981
}
9082

9183
/**
@@ -94,8 +86,17 @@ public T serialize(Component component) {
9486
* @param value input
9587
* @return component
9688
*/
97-
public Component deserialize(T value) {
98-
ComponentProperties properties = deserializeAsProperties(value);
89+
public <T> Component deserialize(T value, PropertiesSerializer<T> propertiesSerializer) {
90+
return deserialize(propertiesSerializer.deserialize(value));
91+
}
92+
93+
/**
94+
* Deserializes the given component.
95+
*
96+
* @param properties input
97+
* @return component
98+
*/
99+
public Component deserialize(ComponentProperties properties) {
99100
Class<? extends Component> type = getComponentTypeFromProperties(properties);
100101
if (type == null) throw new IllegalArgumentException("Unknown serialized component type");
101102
return newComponent(type, properties);
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
/**
66
* Serializer for Java {@link Map}.
77
*/
8-
public class MapComponentSerializer extends ComponentSerializer<Map<String, ?>> {
8+
public class MapPropertiesSerializer implements PropertiesSerializer<Map<String, ?>> {
9+
10+
private static final MapPropertiesSerializer INSTANCE = new MapPropertiesSerializer();
11+
12+
private MapPropertiesSerializer() {}
913

1014
@Override
1115
@SuppressWarnings("unchecked")
12-
public Map<String, ?> serializeFromProperties(ComponentProperties properties) {
16+
public Map<String, ?> serialize(ComponentProperties properties) {
1317
Objects.requireNonNull(properties, "Component properties can not be null");
1418
return (Map<String, ?>) unwrap(ComponentProperty.properties(properties));
1519
}
1620

1721
@Override
18-
public ComponentProperties deserializeAsProperties(Map<String, ?> value) {
22+
public ComponentProperties deserialize(Map<String, ?> value) {
1923
Objects.requireNonNull(value, "Map can not be null");
2024
return ComponentProperty.convertToProperties(wrap(value)).value();
2125
}
@@ -58,4 +62,8 @@ private ComponentProperty<?> wrap(Object o) {
5862
};
5963
}
6064

65+
public static MapPropertiesSerializer get() {
66+
return INSTANCE;
67+
}
68+
6169
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.machinemc.scriptive.serialization;
2+
3+
public interface PropertiesSerializer<T> {
4+
5+
/**
6+
* Serializes component properties.
7+
*
8+
* @param properties component properties
9+
* @return value
10+
*/
11+
T serialize(ComponentProperties properties);
12+
13+
/**
14+
* Deserializes to component properties.
15+
*
16+
* @param value value
17+
* @return component properties
18+
*/
19+
ComponentProperties deserialize(T value);
20+
21+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.Map;
99

10-
public class MapComponentSerializerTest {
10+
public class MapPropertiesSerializerTest {
1111

1212
@Test
1313
public void simpleTest() {
@@ -19,10 +19,11 @@ public void simpleTest() {
1919
TextComponent sibling = TextComponent.of("I am a child component");
2020
component.append(sibling);
2121

22-
MapComponentSerializer serializer = new MapComponentSerializer();
23-
Map<String, ?> map = serializer.serialize(component);
22+
ComponentSerializer componentSerializer = new ComponentSerializer();
23+
MapPropertiesSerializer propertiesSerializer = MapPropertiesSerializer.get();
24+
Map<String, ?> map = componentSerializer.serialize(component, propertiesSerializer);
2425

25-
Component deserialized = serializer.deserialize(map);
26+
Component deserialized = componentSerializer.deserialize(map, propertiesSerializer);
2627

2728
assert component.equals(deserialized);
2829
}

0 commit comments

Comments
 (0)