Skip to content

Commit f669b60

Browse files
committed
fixed usage of getValueOr, improved MapComponentSerializer
1 parent 196a853 commit f669b60

File tree

8 files changed

+56
-24
lines changed

8 files changed

+56
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public ComponentProperties getProperties() {
260260
@SuppressWarnings("unchecked")
261261
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
262262
textFormat = new TextFormat(properties);
263-
setInsertion(properties.getValueOr("insertion", null));
263+
setInsertion(properties.getValue("insertion", String.class).orElse(null));
264264
setClickEvent(properties.getValue("clickEvent", ComponentProperties.class)
265265
.flatMap(ClickEvent::fromProperties)
266266
.orElse(null));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public Class<TranslationComponent> getType() {
324324
public void loadProperties(ComponentProperties properties, ComponentSerializer<?> serializer) {
325325
super.loadProperties(properties, serializer);
326326
translation = properties.getValue("translate", String.class).orElseThrow();
327-
fallback = properties.getValueOr("translate", null);
327+
fallback = properties.getValue("translate", String.class).orElse(null);
328328
arguments = properties.get("with", ComponentProperty.Array.class)
329329
.map(ComponentProperty.Array::value)
330330
.map(array -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static Optional<ClickEvent> fromProperties(ComponentProperties properties
6868
return Optional.empty();
6969
}
7070

71-
String value = properties.getValueOr("value", null);
71+
String value = properties.getValue("value", String.class).orElse(null);
7272
if (value == null) return Optional.empty();
7373
return Optional.of(new ClickEvent(action, value));
7474
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,25 @@ public HoverEvent(Action<V> action, ValueHolder<V> valueHolder) {
5454
public static <V extends HoverEvent.Value> Optional<HoverEvent<V>> fromProperties(ComponentProperties properties,
5555
ComponentSerializer<?> serializer) {
5656
if (!properties.contains("action")) return Optional.empty();
57-
Action<V> action = (Action<V>) Action.byName(properties.getValueOr("action", null));
57+
String actionName = properties.getValue("action", String.class).orElse(null);
58+
if (actionName == null) return Optional.empty();
59+
Action<V> action = (Action<V>) Action.byName(actionName);
5860
if (action == null) return Optional.empty();
5961

6062
if (action == SHOW_TEXT) {
61-
ComponentProperty<?> property = properties.getOr("contents", null);
63+
ComponentProperty<?> property = properties.get("contents", ComponentProperty.class).orElse(null);
6264
if (property == null) return Optional.empty();
6365
return Optional.of(new HoverEvent<>(action, (V) new Text(ComponentProperty.convertToProperties(property).value(), serializer)));
6466
}
6567

6668
if (action == SHOW_ITEM) {
67-
ComponentProperties contents = properties.getValueOr("contents", null);
69+
ComponentProperties contents = properties.getValue("contents", ComponentProperties.class).orElse(null);
6870
if (contents == null) return Optional.empty();
6971
return Optional.of(new HoverEvent<>(action, (V) new Item(contents)));
7072
}
7173

7274
if (action == SHOW_ENTITY) {
73-
ComponentProperties contents = properties.getValueOr("contents", null);
75+
ComponentProperties contents = properties.getValue("contents", ComponentProperties.class).orElse(null);
7476
if (contents == null) return Optional.empty();
7577
return Optional.of(new HoverEvent<>(action, (V) new Entity(contents)));
7678
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ public <T> T getValueOr(String key, T or) {
100100
return (T) value;
101101
}
102102

103+
/**
104+
* Returns unwrapped component property of this map if the wrapped property
105+
* matches the provided class, or empty in case there is no property with
106+
* given key, or it is different property type.
107+
*
108+
* @param key key of the property
109+
* @param propertyClass type of the property
110+
* @return unwrapped property value
111+
* @param <T> value type
112+
* @param <P> property type
113+
*/
114+
public <T, P extends ComponentProperty<T>> Optional<T> getAndUnwrap(String key, Class<P> propertyClass) {
115+
return get(key, propertyClass).map(ComponentProperty::value);
116+
}
117+
103118
/**
104119
* Changes value of this property map.
105120
*

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,51 @@
88
public class MapComponentSerializer extends ComponentSerializer<Map<String, ?>> {
99

1010
@Override
11+
@SuppressWarnings("unchecked")
1112
public Map<String, ?> serializeFromProperties(ComponentProperties properties) {
1213
Objects.requireNonNull(properties, "Component properties can not be null");
13-
Map<String, Object> map = new HashMap<>();
14-
Set<String> keys = properties.getKeys();
15-
for (String key : keys) map.put(key, unwrap(properties.get(key, ComponentProperty.class).orElseThrow()));
16-
return map;
14+
return (Map<String, ?>) unwrap(ComponentProperty.properties(properties));
1715
}
1816

1917
@Override
2018
public ComponentProperties deserializeAsProperties(Map<String, ?> value) {
2119
Objects.requireNonNull(value, "Map can not be null");
22-
ComponentProperties properties = new ComponentProperties();
23-
value.forEach((k, v) -> properties.set(k, wrap(v)));
24-
return properties;
20+
return ComponentProperty.convertToProperties(wrap(value)).value();
2521
}
2622

2723
private Object unwrap(ComponentProperty<?> property) {
2824
return switch (property) {
29-
case ComponentProperty.Properties properties -> serializeFromProperties(properties.value());
30-
case ComponentProperty.Array array -> Arrays.stream(array.value()).map(this::serializeFromProperties).toList();
25+
case ComponentProperty.Properties properties -> {
26+
Map<String, Object> map = new HashMap<>();
27+
properties.value().forEach((k, p) -> map.put(k, unwrap(p)));
28+
yield map;
29+
}
30+
case ComponentProperty.Array array -> {
31+
List<Object> list = new ArrayList<>();
32+
Arrays.stream(array.value())
33+
.map(ComponentProperty::properties)
34+
.map(this::unwrap)
35+
.forEach(list::add);
36+
yield list;
37+
}
3138
default -> property.value();
3239
};
3340
}
3441

35-
@SuppressWarnings("unchecked")
3642
private ComponentProperty<?> wrap(Object o) {
3743
return switch (o) {
38-
case Map<?, ?> map -> ComponentProperty.properties(deserializeAsProperties((Map<String, ?>) map));
44+
case Map<?, ?> map -> {
45+
ComponentProperties properties = new ComponentProperties();
46+
map.forEach((k, e) -> properties.set((String) k, wrap(e)));
47+
yield ComponentProperty.properties(properties);
48+
}
3949
case List<?> list -> {
40-
ComponentProperties[] properties = new ComponentProperties[list.size()];
41-
for (int i = 0; i < properties.length; i++) {
42-
ComponentProperty<?> next = wrap(list.get(i));
43-
properties[i] = ComponentProperty.convertToProperties(next).value();
44-
}
45-
yield ComponentProperty.array(properties);
50+
ComponentProperties[] array = list.stream()
51+
.map(this::wrap)
52+
.map(ComponentProperty::convertToProperties)
53+
.map(ComponentProperty::value)
54+
.toArray(ComponentProperties[]::new);
55+
yield ComponentProperty.array(array);
4656
}
4757
default -> ComponentProperty.of(o);
4858
};

scriptive-gson/src/main/java/org/machinemc/scriptive/serialization/JSONComponentSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public JSONComponentSerializer(Gson gson) {
2222

2323
@Override
2424
public String serializeFromProperties(ComponentProperties properties) {
25+
Objects.requireNonNull(properties, "Component properties can not be null");
2526
JsonElement element = unwrap(ComponentProperty.properties(properties));
2627
return gson.toJson(element);
2728
}
2829

2930
@Override
3031
public ComponentProperties deserializeAsProperties(String value) {
32+
Objects.requireNonNull(value, "JSON can not be null");
3133
JsonElement element = JsonParser.parseString(value);
3234
return ComponentProperty.convertToProperties(wrap(element)).value();
3335
}

scriptive-nbt/src/main/java/org/machinemc/scriptive/serialization/NBTComponentSerializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.machinemc.scriptive.components.Component;
55

66
import java.util.Arrays;
7+
import java.util.Objects;
78

89
/**
910
* Serializer for NBT format.
@@ -17,11 +18,13 @@ public NBTCompound serialize(Component component) {
1718

1819
@Override
1920
public NBTCompound serializeFromProperties(ComponentProperties properties) {
21+
Objects.requireNonNull(properties, "Component properties can not be null");
2022
return (NBTCompound) unwrap(ComponentProperty.properties(properties));
2123
}
2224

2325
@Override
2426
public ComponentProperties deserializeAsProperties(NBT<?> value) {
27+
Objects.requireNonNull(value, "NBT can not be null");
2528
return ComponentProperty.convertToProperties(wrap(value)).value();
2629
}
2730

0 commit comments

Comments
 (0)