38
38
import java .io .IOException ;
39
39
import java .io .StringReader ;
40
40
import java .lang .reflect .Type ;
41
+ import java .util .EnumSet ;
42
+ import java .util .Set ;
41
43
import net .kyori .adventure .key .Key ;
42
44
import net .kyori .adventure .text .Component ;
43
45
import net .kyori .adventure .text .event .ClickEvent ;
49
51
import org .checkerframework .checker .nullness .qual .Nullable ;
50
52
51
53
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
+ }
53
75
54
76
static final String FONT = "font" ;
55
77
static final String COLOR = "color" ;
@@ -183,16 +205,6 @@ private Codec.Decoder<Component, String, JsonParseException> decoder(final JsonD
183
205
public JsonElement serialize (final Style src , final Type typeOfSrc , final JsonSerializationContext context ) {
184
206
final JsonObject json = new JsonObject ();
185
207
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
-
196
208
for (int i = 0 , length = DECORATIONS .length ; i < length ; i ++) {
197
209
final TextDecoration decoration = DECORATIONS [i ];
198
210
final TextDecoration .State state = src .decoration (decoration );
@@ -203,6 +215,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
203
215
}
204
216
}
205
217
218
+ final @ Nullable TextColor color = src .color ();
219
+ if (color != null ) {
220
+ json .add (COLOR , context .serialize (color ));
221
+ }
222
+
206
223
final @ Nullable String insertion = src .insertion ();
207
224
if (insertion != null ) {
208
225
json .addProperty (INSERTION , insertion );
@@ -228,6 +245,11 @@ public JsonElement serialize(final Style src, final Type typeOfSrc, final JsonSe
228
245
json .add (HOVER_EVENT , eventJson );
229
246
}
230
247
248
+ final @ Nullable Key font = src .font ();
249
+ if (font != null ) {
250
+ json .add (FONT , context .serialize (font ));
251
+ }
252
+
231
253
return json ;
232
254
}
233
255
0 commit comments