11package gregtech .api .mui .serialize ;
22
3+ import com .cleanroommc .modularui .drawable .text .FormattingState ;
4+ import it .unimi .dsi .fastutil .objects .Object2ObjectOpenHashMap ;
5+
36import net .minecraft .util .text .TextFormatting ;
47
58import com .cleanroommc .modularui .api .drawable .IKey ;
1417import com .google .gson .JsonSerializationContext ;
1518import org .apache .commons .lang3 .ArrayUtils ;
1619
20+ import java .util .Map ;
21+
1722public class KeySerializer implements JsonHandler <IKey > {
1823
19- // todo fix formatting
24+ private static final Map <String , TextFormatting > FORMATTING_MAP = new Object2ObjectOpenHashMap <>();
25+
26+ static {
27+ for (var tf : TextFormatting .values ()) {
28+ FORMATTING_MAP .put (tf .toString (), tf );
29+ }
30+ }
2031
2132 @ Override
2233 public IKey deserialize (JsonElement json , JsonDeserializationContext context )
@@ -26,11 +37,10 @@ public IKey deserialize(JsonElement json, JsonDeserializationContext context)
2637 return IKey .str (object .get ("string" ).getAsString ());
2738 } else if (object .has ("lang" )) {
2839 String lang = context .deserialize (object .get ("lang" ), String .class );
29- // FormattingState formatting = new FormattingState();
30- // formatting.parseFrom(object.get("format").getAsString());
40+ TextFormatting [] formatting = deserializeArray (object .getAsJsonArray ("format" ), context , TextFormatting []::new );
3141 Object [] args = deserializeArray (
3242 object .getAsJsonArray ("args" ), context , Object []::new );
33- return IKey .lang (lang , args );
43+ return IKey .lang (lang , args ). style ( formatting ) ;
3444 } else if (object .has ("keys" )) {
3545 IKey [] keys = deserializeArray (
3646 object .getAsJsonArray ("keys" ), context , IKey []::new );
@@ -48,16 +58,25 @@ public JsonElement serialize(IKey src, JsonSerializationContext context) {
4858 obj .add ("string" , context .serialize (src .getFormatted ()));
4959 } else if (src instanceof LangKey langKey ) {
5060 obj .add ("lang" , context .serialize (langKey .getKeySupplier ().get ()));
51- // var formatting = langKey.getFormatting();
52- // if (formatting != null)
53- // obj.addProperty("format", formatting.toString());
61+ TextFormatting [] formattings = convert (langKey .getFormatting ());
62+ obj .add ("format" , serializeArray (formattings , context ));
5463 Object [] args = langKey .getArgsSupplier ().get ();
5564 if (!ArrayUtils .isEmpty (args ))
5665 obj .add ("args" , serializeArray (args , context ));
5766 } else if (src instanceof CompoundKey compoundKey ) {
5867 obj .add ("keys" , serializeArray (compoundKey .getKeys (), context ));
59- // obj.add("format", context.serialize( compoundKey.getFormatting()));
68+ obj .add ("format" , serializeArray ( convert ( compoundKey .getFormatting ()), context ));
6069 }
6170 return obj ;
6271 }
72+
73+ public static TextFormatting [] convert (FormattingState state ) {
74+ if (state == null ) return new TextFormatting [0 ];
75+ String s = state .getFormatting ();
76+ TextFormatting [] formattings = new TextFormatting [s .length () / 2 ];
77+ for (int i = 0 ; i < s .length (); i += 2 ) {
78+ formattings [i / 2 ] = FORMATTING_MAP .get (s .substring (i , i + 2 ));
79+ }
80+ return formattings ;
81+ }
6382}
0 commit comments