Skip to content

Commit 11cc8dd

Browse files
committed
chore(serializer-configurate4): Resolve compile errors in the serializer
There is still more work needed to fully handle some changes to click and hover events
1 parent 1cd9e60 commit 11cc8dd

File tree

13 files changed

+103
-92
lines changed

13 files changed

+103
-92
lines changed

api/src/main/java/module-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
@NullMarked
1010
module net.kyori.adventure.api {
1111
requires transitive net.kyori.adventure.key;
12-
requires transitive org.jspecify;
13-
requires transitive org.jetbrains.annotations;
12+
requires static transitive org.jspecify;
13+
requires static transitive org.jetbrains.annotations;
1414

1515
exports net.kyori.adventure;
1616
exports net.kyori.adventure.audience;

serializer-configurate4/src/main/java/module-info.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
module net.kyori.adventure.serializer.configurate4 {
88
requires transitive net.kyori.adventure.api;
99
requires net.kyori.adventure.text.serializer.commons;
10-
requires io.leangen.geantyref;
11-
requires org.spongepowered.configurate;
12-
requires static org.checkerframework.checker.qual;
10+
requires transitive io.leangen.geantyref;
11+
requires transitive org.spongepowered.configurate;
12+
requires static transitive org.checkerframework.checker.qual;
1313

1414
exports net.kyori.adventure.serializer.configurate4;
1515
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.kyori.adventure.serializer.configurate4;
2+
3+
import io.leangen.geantyref.GenericTypeReflector;
4+
import java.lang.reflect.Type;
5+
import net.kyori.adventure.text.event.ClickEvent;
6+
import org.checkerframework.checker.nullness.qual.Nullable;
7+
import org.spongepowered.configurate.ConfigurationNode;
8+
import org.spongepowered.configurate.serialize.SerializationException;
9+
import org.spongepowered.configurate.serialize.TypeSerializer;
10+
11+
public class ClickEventPayloadSerializer implements TypeSerializer<ClickEvent.Payload> {
12+
@Override
13+
public ClickEvent.Payload deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
14+
final Class<? extends ClickEvent.Payload> raw = GenericTypeReflector.erase(type).asSubclass(ClickEvent.Payload.class);
15+
if (ClickEvent.Payload.Custom.class.equals(raw) || ClickEvent.Payload.Dialog.class.equals(raw)) {
16+
throw new SerializationException("Do not know how to deserialize a " + raw.getSimpleName() + " payload");
17+
} else if (ClickEvent.Payload.Int.class.equals(raw)) {
18+
return ClickEvent.Payload.integer(node.getInt());
19+
} else if (ClickEvent.Payload.Text.class.equals(raw)) {
20+
return ClickEvent.Payload.string(node.getString());
21+
} else {
22+
throw new SerializationException("Do not know how to deserialize a " + raw.getSimpleName() + " payload!");
23+
}
24+
}
25+
26+
@Override
27+
public void serialize(final Type type, final ClickEvent.@Nullable Payload obj, final ConfigurationNode node) throws SerializationException {
28+
switch (obj) {
29+
case null -> node.set(null);
30+
case ClickEvent.Payload.Custom c -> throw new SerializationException("Do not know how to serialize a custom payload");
31+
case ClickEvent.Payload.Dialog d -> throw new SerializationException("Do not know how to serialize a dialog");
32+
case ClickEvent.Payload.Int i -> node.set(i.integer());
33+
case ClickEvent.Payload.Text s -> node.set(s.value());
34+
}
35+
36+
}
37+
}

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/ComponentTypeSerializer.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,21 @@ public Component deserialize(final Type type, final ConfigurationNode value) thr
9797
return this.deserialize0(value);
9898
}
9999

100-
private BuildableComponent<?, ?> deserialize0(final ConfigurationNode value) throws SerializationException {
100+
private Component deserialize0(final ConfigurationNode value) throws SerializationException {
101101
// Try to read as a string
102102
if (!value.isList() && !value.isMap()) {
103103
final String str = value.getString();
104104
if (str != null) {
105105
if (this.stringSerial != null) {
106-
final Component ret = this.stringSerial.deserialize(str);
107-
if (!(ret instanceof BuildableComponent<?, ?>)) {
108-
throw new SerializationException("Result " + ret + " is not builable");
109-
}
110-
return (BuildableComponent<?, ?>) ret;
106+
return this.stringSerial.deserialize(str);
111107
} else {
112108
return Component.text(str);
113109
}
114110
}
115111
} else if (value.isList()) {
116112
ComponentBuilder<?, ?> parent = null;
117113
for (final ConfigurationNode childElement : value.childrenList()) {
118-
final BuildableComponent<?, ?> child = this.deserialize0(childElement);
114+
final Component child = this.deserialize0(childElement);
119115
if (parent == null) {
120116
parent = child.toBuilder();
121117
} else {
@@ -162,12 +158,12 @@ public Component deserialize(final Type type, final ConfigurationNode value) thr
162158
.name(name.getString())
163159
.objective(objective.getString());
164160
// score components can have a value sometimes, let's grab it
165-
final ConfigurationNode scoreValue = score.node(SCORE_VALUE);
161+
/*final ConfigurationNode scoreValue = score.node(SCORE_VALUE);
166162
if (!scoreValue.virtual()) {
167163
component = builder.value(scoreValue.getString());
168-
} else {
164+
} else {*/
169165
component = builder;
170-
}
166+
//}
171167
} else if (children.containsKey(SELECTOR)) {
172168
component = Component.selector().pattern(children.get(SELECTOR).getString());
173169
} else if (children.containsKey(KEYBIND)) {
@@ -278,13 +274,13 @@ public void serialize(final Type type, final @Nullable Component src, final Conf
278274
score.node(SCORE_OBJECTIVE).set(sc.objective());
279275
// score component value is optional
280276
@SuppressWarnings("deprecation")
281-
final @Nullable String scoreValue = sc.value();
277+
final String scoreValue = sc.value();
282278
if (scoreValue != null) score.node(SCORE_VALUE).set(scoreValue);
283279
} else if (src instanceof SelectorComponent) {
284280
value.node(SELECTOR).set(((SelectorComponent) src).pattern());
285281
} else if (src instanceof KeybindComponent) {
286282
value.node(KEYBIND).set(((KeybindComponent) src).keybind());
287-
} else if (src instanceof final NBTComponent<?, ?> nc) {
283+
} else if (src instanceof final NBTComponent<?> nc) {
288284
value.node(NBT).set(nc.nbtPath());
289285
value.node(NBT_INTERPRET).set(nc.interpret());
290286
switch (src) {
@@ -293,17 +289,14 @@ public void serialize(final Type type, final @Nullable Component src, final Conf
293289
case StorageNBTComponent storageNBTComponent -> value.node(NBT_STORAGE).set(KeySerializer.INSTANCE.type(), storageNBTComponent.storage());
294290
default -> throw notSureHowToSerialize(src);
295291
}
296-
} else if (src instanceof ObjectComponent) {
297-
final ObjectComponent objectComponent = (ObjectComponent) src;
292+
} else if (src instanceof final ObjectComponent objectComponent) {
298293
final ObjectContents contents = objectComponent.contents();
299-
if (contents instanceof SpriteObjectContents) {
300-
final SpriteObjectContents spriteContents = (SpriteObjectContents) contents;
294+
if (contents instanceof final SpriteObjectContents spriteContents) {
301295
if (!spriteContents.atlas().equals(SpriteObjectContents.DEFAULT_ATLAS)) {
302296
value.node(OBJECT_ATLAS).set(KeySerializer.INSTANCE.type(), spriteContents.atlas());
303297
}
304298
value.node(OBJECT_SPRITE).set(KeySerializer.INSTANCE.type(), spriteContents.sprite());
305-
} else if (contents instanceof PlayerHeadObjectContents) {
306-
final PlayerHeadObjectContents playerHeadContents = (PlayerHeadObjectContents) contents;
299+
} else if (contents instanceof final PlayerHeadObjectContents playerHeadContents) {
307300
value.node(OBJECT_HAT).set(playerHeadContents.hat());
308301
final String playerName = playerHeadContents.name();
309302
final UUID playerId = playerHeadContents.id();
@@ -348,7 +341,7 @@ public void serialize(final Type type, final @Nullable Component src, final Conf
348341
}
349342
}
350343

351-
private static <C extends NBTComponent<C, B>, B extends NBTComponentBuilder<C, B>> B nbt(final B builder, final String nbt, final boolean interpret) {
344+
private static <C extends NBTComponent<C>, B extends NBTComponentBuilder<C, B>> B nbt(final B builder, final String nbt, final boolean interpret) {
352345
return builder
353346
.nbtPath(nbt)
354347
.interpret(interpret);

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/ConfigurateComponentSerializerImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import net.kyori.adventure.sound.SoundStop;
3030
import net.kyori.adventure.text.Component;
3131
import net.kyori.adventure.text.TranslationArgument;
32-
import net.kyori.adventure.text.event.ClickEventImpl;
33-
import net.kyori.adventure.text.event.HoverEventImpl;
32+
import net.kyori.adventure.text.event.ClickEvent;
33+
import net.kyori.adventure.text.event.HoverEvent;
3434
import net.kyori.adventure.text.format.ShadowColor;
3535
import net.kyori.adventure.text.format.Style;
3636
import net.kyori.adventure.text.format.TextDecoration;
@@ -66,7 +66,7 @@ private ConfigurateComponentSerializerImpl(final Builder builder) {
6666
@Override
6767
public Component deserialize(final ConfigurationNode input) {
6868
try {
69-
final @Nullable Component deserialized = input.get(Component.class);
69+
final Component deserialized = input.get(Component.class);
7070
if (deserialized != null) {
7171
return deserialized;
7272
}
@@ -102,12 +102,12 @@ private TypeSerializerCollection makeSerializers(final TypeSerializerCollection.
102102
.register(TextColorSerializer.INSTANCE)
103103
.register(BlockNBTPosSerializer.INSTANCE)
104104
.register(TranslationArgument.class, TranslationArgumentTypeSerializer.INSTANCE)
105-
.registerExact(new IndexSerializer<>(TypeToken.get(ClickEventImpl.Action.class), ClickEventImpl.Action.NAMES))
106-
.registerExact(new IndexSerializer<>(new TypeToken<HoverEventImpl.Action<?>>() {}, HoverEventImpl.Action.NAMES))
105+
.registerExact(new IndexSerializer<>(new TypeToken<ClickEvent.Action<?>>() {}, ClickEvent.Action.NAMES))
106+
.registerExact(new IndexSerializer<>(new TypeToken<HoverEvent.Action<?>>() {}, HoverEvent.Action.NAMES))
107107
.registerExact(new IndexSerializer<>(TypeToken.get(Sound.Source.class), Sound.Source.NAMES))
108108
.registerExact(new IndexSerializer<>(TypeToken.get(TextDecoration.class), TextDecoration.NAMES))
109-
.registerExact(HoverEventImpl.ShowEntity.class, HoverEventShowEntitySerializer.INSTANCE)
110-
.registerExact(HoverEventImpl.ShowItem.class, HoverEventShowItemSerializer.INSTANCE)
109+
.registerExact(HoverEvent.ShowEntity.class, HoverEventShowEntitySerializer.INSTANCE)
110+
.registerExact(HoverEvent.ShowItem.class, HoverEventShowItemSerializer.INSTANCE)
111111
.register(ConfigurateDataComponentValue.class, ConfigurateDataComponentValueTypeSerializer.INSTANCE)
112112
.register(ShadowColor.class, ShadowColorSerializer.INSTACE)
113113
.register(PlayerHeadObjectContents.ProfileProperty.class, ProfilePropertySerializer.INSTANCE)

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/ConfigurateDataComponentValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @since 4.17.0
3333
*/
34-
public interface ConfigurateDataComponentValue extends DataComponentValue {
34+
public sealed interface ConfigurateDataComponentValue extends DataComponentValue permits SnapshottingConfigurateDataComponentValue {
3535
/**
3636
* Create a data component value capturing the value of an existing node.
3737
*

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/HoverEventShowEntitySerializer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@
2727
import java.util.UUID;
2828
import net.kyori.adventure.key.Key;
2929
import net.kyori.adventure.text.Component;
30-
import net.kyori.adventure.text.event.HoverEventImpl;
30+
import net.kyori.adventure.text.event.HoverEvent;
3131
import net.kyori.adventure.text.serializer.commons.ComponentTreeConstants;
3232
import org.jspecify.annotations.Nullable;
3333
import org.spongepowered.configurate.ConfigurationNode;
3434
import org.spongepowered.configurate.serialize.SerializationException;
3535
import org.spongepowered.configurate.serialize.TypeSerializer;
3636

37-
final class HoverEventShowEntitySerializer implements TypeSerializer<HoverEventImpl.ShowEntity> {
37+
final class HoverEventShowEntitySerializer implements TypeSerializer<HoverEvent.ShowEntity> {
3838
static final HoverEventShowEntitySerializer INSTANCE = new HoverEventShowEntitySerializer();
3939

4040
private HoverEventShowEntitySerializer() {
4141
}
4242

4343
@Override
44-
public HoverEventImpl.ShowEntity deserialize(final Type type, final ConfigurationNode value) throws SerializationException {
44+
public HoverEvent.ShowEntity deserialize(final Type type, final ConfigurationNode value) throws SerializationException {
4545
final Key typeId = value.node(ComponentTreeConstants.SHOW_ENTITY_TYPE).get(Key.class);
4646
final UUID id = value.node(ComponentTreeConstants.SHOW_ENTITY_ID).get(UUID.class);
4747
if (typeId == null || id == null) {
4848
throw new SerializationException("A show entity hover event needs type and id fields to be deserialized");
4949
}
50-
final @Nullable Component name = value.node(ComponentTreeConstants.SHOW_ENTITY_NAME).get(Component.class);
50+
final Component name = value.node(ComponentTreeConstants.SHOW_ENTITY_NAME).get(Component.class);
5151

52-
return HoverEventImpl.ShowEntity.showEntity(typeId, id, name);
52+
return HoverEvent.ShowEntity.showEntity(typeId, id, name);
5353
}
5454

5555
@Override
56-
public void serialize(final Type type, final HoverEventImpl.@Nullable ShowEntity obj, final ConfigurationNode value) throws SerializationException {
56+
public void serialize(final Type type, final HoverEvent.@Nullable ShowEntity obj, final ConfigurationNode value) throws SerializationException {
5757
if (obj == null) {
5858
value.set(null);
5959
return;

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/HoverEventShowItemSerializer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
import java.util.Map;
3030
import net.kyori.adventure.key.Key;
3131
import net.kyori.adventure.nbt.api.BinaryTagHolder;
32-
import net.kyori.adventure.text.event.HoverEventImpl;
32+
import net.kyori.adventure.text.event.HoverEvent;
3333
import net.kyori.adventure.text.serializer.commons.ComponentTreeConstants;
3434
import org.jspecify.annotations.Nullable;
3535
import org.spongepowered.configurate.ConfigurationNode;
3636
import org.spongepowered.configurate.serialize.SerializationException;
3737
import org.spongepowered.configurate.serialize.TypeSerializer;
3838

39-
final class HoverEventShowItemSerializer implements TypeSerializer<HoverEventImpl.ShowItem> {
39+
final class HoverEventShowItemSerializer implements TypeSerializer<HoverEvent.ShowItem> {
4040
static final HoverEventShowItemSerializer INSTANCE = new HoverEventShowItemSerializer();
4141

4242
private static final TypeToken<Map<Key, ConfigurateDataComponentValue>> COMPONENT_MAP_TYPE = new TypeToken<Map<Key, ConfigurateDataComponentValue>>() {
@@ -46,7 +46,7 @@ private HoverEventShowItemSerializer() {
4646
}
4747

4848
@Override
49-
public HoverEventImpl.ShowItem deserialize(final Type type, final ConfigurationNode value) throws SerializationException {
49+
public HoverEvent.ShowItem deserialize(final Type type, final ConfigurationNode value) throws SerializationException {
5050
final Key id = value.node(ComponentTreeConstants.SHOW_ITEM_ID).get(Key.class);
5151
if (id == null) {
5252
throw new SerializationException("An id is required to deserialize the show_item hover event");
@@ -56,17 +56,17 @@ public HoverEventImpl.ShowItem deserialize(final Type type, final ConfigurationN
5656
if (!components.virtual()) {
5757
final Map<Key, ConfigurateDataComponentValue> componentsMap = components.require(COMPONENT_MAP_TYPE);
5858

59-
return HoverEventImpl.ShowItem.showItem(id, count, new HashMap<>(componentsMap));
59+
return HoverEvent.ShowItem.showItem(id, count, new HashMap<>(componentsMap));
6060
} else {
6161
// legacy (pre-1.20.5)
6262
@SuppressWarnings("deprecation")
6363
final String tag = value.node(ComponentTreeConstants.SHOW_ITEM_TAG).getString();
64-
return HoverEventImpl.ShowItem.showItem(id, count, tag == null ? null : BinaryTagHolder.binaryTagHolder(tag));
64+
return HoverEvent.ShowItem.showItem(id, count, tag == null ? null : BinaryTagHolder.binaryTagHolder(tag));
6565
}
6666
}
6767

6868
@Override
69-
public void serialize(final Type type, final HoverEventImpl.@Nullable ShowItem obj, final ConfigurationNode value) throws SerializationException {
69+
public void serialize(final Type type, final HoverEvent.@Nullable ShowItem obj, final ConfigurationNode value) throws SerializationException {
7070
if (obj == null) {
7171
value.set(null);
7272
return;

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/ShadowColorSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
final class ShadowColorSerializer implements TypeSerializer<ShadowColor> {
3636
static final TypeSerializer<ShadowColor> INSTACE = new ShadowColorSerializer(false);
3737

38-
private boolean emitFloats;
38+
private final boolean emitFloats;
3939

4040
private ShadowColorSerializer(final boolean emitFloats) {
4141
this.emitFloats = emitFloats;

serializer-configurate4/src/main/java/net/kyori/adventure/serializer/configurate4/SnapshottingConfigurateDataComponentValue.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@
2323
*/
2424
package net.kyori.adventure.serializer.configurate4;
2525

26-
import java.util.stream.Stream;
27-
import net.kyori.examination.ExaminableProperty;
2826
import org.spongepowered.configurate.AttributedConfigurationNode;
2927
import org.spongepowered.configurate.BasicConfigurationNode;
3028
import org.spongepowered.configurate.CommentedConfigurationNode;
3129
import org.spongepowered.configurate.ConfigurationNode;
3230

33-
final class SnapshottingConfigurateDataComponentValue implements ConfigurateDataComponentValue {
34-
private final ConfigurationNode ownedNode;
35-
31+
record SnapshottingConfigurateDataComponentValue(ConfigurationNode ownedNode) implements ConfigurateDataComponentValue {
3632
// capture the value of an existing node without exposing any mutable state
3733
static SnapshottingConfigurateDataComponentValue create(final ConfigurationNode existing) {
3834
final ConfigurationNode owned;
@@ -49,19 +45,8 @@ static SnapshottingConfigurateDataComponentValue create(final ConfigurationNode
4945
return new SnapshottingConfigurateDataComponentValue(owned);
5046
}
5147

52-
private SnapshottingConfigurateDataComponentValue(final ConfigurationNode owned) {
53-
this.ownedNode = owned;
54-
}
55-
5648
@Override
5749
public void applyTo(final ConfigurationNode node) {
5850
node.from(this.ownedNode);
5951
}
60-
61-
@Override
62-
public Stream<? extends ExaminableProperty> examinableProperties() {
63-
return Stream.of(
64-
ExaminableProperty.of("ownedNode", this.ownedNode)
65-
);
66-
}
6752
}

0 commit comments

Comments
 (0)