Skip to content

Commit 5d8c8ac

Browse files
committed
serializer-gson: Align order of Style with Vanilla output
1 parent ff68d6b commit 5d8c8ac

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/StyleSerializer.java

+33-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.io.IOException;
3939
import java.io.StringReader;
4040
import java.lang.reflect.Type;
41+
import java.util.EnumSet;
42+
import java.util.Set;
4143
import net.kyori.adventure.key.Key;
4244
import net.kyori.adventure.text.Component;
4345
import net.kyori.adventure.text.event.ClickEvent;
@@ -49,7 +51,27 @@
4951
import org.checkerframework.checker.nullness.qual.Nullable;
5052

5153
final class StyleSerializer implements JsonDeserializer<Style>, JsonSerializer<Style> {
52-
private static final TextDecoration[] DECORATIONS = TextDecoration.values();
54+
@SuppressWarnings("checkstyle:NoWhitespaceAfter")
55+
private static final TextDecoration[] DECORATIONS = {
56+
// The order here is important -- Minecraft does string comparisons of some
57+
// serialized components so we have to make sure our order matches Vanilla
58+
TextDecoration.BOLD,
59+
TextDecoration.ITALIC,
60+
TextDecoration.UNDERLINED,
61+
TextDecoration.STRIKETHROUGH,
62+
TextDecoration.OBFUSCATED
63+
};
64+
65+
static {
66+
// Ensure coverage of decorations
67+
final Set<TextDecoration> knownDecorations = EnumSet.allOf(TextDecoration.class);
68+
for(final TextDecoration decoration : DECORATIONS) {
69+
knownDecorations.remove(decoration);
70+
}
71+
if(!knownDecorations.isEmpty()) {
72+
throw new IllegalStateException("GSON serializer is missing some text decorations: " + knownDecorations);
73+
}
74+
}
5375

5476
static final String FONT = "font";
5577
static final String COLOR = "color";
@@ -183,16 +205,6 @@ private Codec.Decoder<Component, String, JsonParseException> decoder(final JsonD
183205
public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSerializationContext context) {
184206
final JsonObject json = new JsonObject();
185207

186-
final @Nullable Key font = src.font();
187-
if(font != null) {
188-
json.add(FONT, context.serialize(font));
189-
}
190-
191-
final @Nullable TextColor color = src.color();
192-
if(color != null) {
193-
json.add(COLOR, context.serialize(color));
194-
}
195-
196208
for(int i = 0, length = DECORATIONS.length; i < length; i++) {
197209
final TextDecoration decoration = DECORATIONS[i];
198210
final TextDecoration.State state = src.decoration(decoration);
@@ -203,6 +215,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
203215
}
204216
}
205217

218+
final @Nullable TextColor color = src.color();
219+
if(color != null) {
220+
json.add(COLOR, context.serialize(color));
221+
}
222+
206223
final @Nullable String insertion = src.insertion();
207224
if(insertion != null) {
208225
json.addProperty(INSERTION, insertion);
@@ -228,6 +245,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
228245
json.add(HOVER_EVENT, eventJson);
229246
}
230247

248+
final @Nullable Key font = src.font();
249+
if(font != null) {
250+
json.add(FONT, context.serialize(font));
251+
}
252+
231253
return json;
232254
}
233255

0 commit comments

Comments
 (0)