99import net .kyori .adventure .text .format .TextDecoration ;
1010
1111import java .io .IOException ;
12- import java .util .Arrays ;
13- import java .util .HashMap ;
14- import java .util .function .Function ;
15- import java .util .stream .Collectors ;
1612
1713import static com .fasterxml .jackson .core .JsonToken .*;
1814import static net .kyori .adventure .text .Component .*;
@@ -34,75 +30,68 @@ public TextComponent deserialize(JsonParser p, DeserializationContext ctx) throw
3430
3531 private TextComponent readComponent (JsonParser p ) throws IOException {
3632 var text = text ();
37- while (p .nextToken () != END_OBJECT )
38- switch (p .currentName ()) {
39- case "text" :
40- text .content (p .nextTextValue ());
41- break ;
42- case "color" :
43- text .color (fromHexString (p .nextTextValue ()));
44- break ;
45- case "clickEvent" :
46- if (p .nextToken () != START_OBJECT )
47- throw new JsonParseException (p , "Expected clickEvent Object" );
48- String action0 = null , value0 = null ;
49- while (p .nextToken () != END_OBJECT )
50- switch (p .currentName ()) {
51- case "action" :
52- action0 = p .nextTextValue ();
53- break ;
54- case "value" :
55- value0 = p .nextTextValue ();
56- break ;
57- default :
58- throw new IllegalStateException ("Unexpected value: " + p .currentName ());
59- }
60- assert action0 != null && value0 != null ;
61- text .clickEvent (switch (ClickEvent .Action .valueOf (action0 .toUpperCase ())) {
62- case OPEN_URL -> openUrl (value0 );
63- case OPEN_FILE -> openFile (value0 );
64- case RUN_COMMAND -> runCommand (value0 );
65- case SUGGEST_COMMAND -> suggestCommand (value0 );
66- case CHANGE_PAGE -> changePage (value0 );
67- case COPY_TO_CLIPBOARD -> copyToClipboard (value0 );
68- });
69- break ;
70- case "hoverEvent" :
71- if (p .nextToken () != START_OBJECT )
72- throw new JsonParseException (p , "Expected hoverEvent Object" );
73- String action1 = null , valueS = null ;
74- TextComponent valueC = null ;
75- while (p .nextToken () != END_OBJECT )
76- switch (p .currentName ()) {
77- case "action" :
78- action1 = p .nextTextValue ();
79- break ;
80- case "value" :
81- if (p .nextToken () == VALUE_STRING )
82- valueS = p .currentValue ().toString ();
83- else if (p .currentToken () == START_OBJECT )
84- valueC = readComponent (p );
85- else throw new JsonParseException (p , "Expected hoverEvent.value data" );
86- break ;
87- default :
88- throw new IllegalStateException ("Unexpected value: " + p .currentName ());
89- }
90- assert action1 != null ;
91- //noinspection SwitchStatementWithTooFewBranches
92- var hoverEvent = switch (action1 ) {
93- case "show_text" -> {
94- assert valueC != null ;
95- yield showText (valueC );
96- }
97- default -> throw new IllegalStateException ("Unexpected value: " + action1 );
98- };
99- text .hoverEvent (hoverEvent );
100- break ;
101- default :
102- for (var decor : TextDecoration .values ())
103- if (decor .toString ().equals (p .currentName ()) && p .nextBooleanValue ())
104- text .decorate (decor );
105- }
33+ while (p .nextToken () != END_OBJECT ) switch (p .currentName ()) {
34+ case "text" :
35+ text .content (p .nextTextValue ());
36+ break ;
37+ case "color" :
38+ text .color (fromHexString (p .nextTextValue ()));
39+ break ;
40+ case "clickEvent" :
41+ if (p .nextToken () != START_OBJECT ) throw new JsonParseException (p , "Expected clickEvent Object" );
42+ String action0 = null , value0 = null ;
43+ while (p .nextToken () != END_OBJECT ) switch (p .currentName ()) {
44+ case "action" :
45+ action0 = p .nextTextValue ();
46+ break ;
47+ case "value" :
48+ value0 = p .nextTextValue ();
49+ break ;
50+ default :
51+ throw new IllegalStateException ("Unexpected value: " + p .currentName ());
52+ }
53+ assert action0 != null && value0 != null ;
54+ text .clickEvent (switch (ClickEvent .Action .valueOf (action0 .toUpperCase ())) {
55+ case OPEN_URL -> openUrl (value0 );
56+ case OPEN_FILE -> openFile (value0 );
57+ case RUN_COMMAND -> runCommand (value0 );
58+ case SUGGEST_COMMAND -> suggestCommand (value0 );
59+ case CHANGE_PAGE -> changePage (value0 );
60+ case COPY_TO_CLIPBOARD -> copyToClipboard (value0 );
61+ default -> throw new UnsupportedOperationException ("TODO" ); // todo
62+ });
63+ break ;
64+ case "hoverEvent" :
65+ if (p .nextToken () != START_OBJECT ) throw new JsonParseException (p , "Expected hoverEvent Object" );
66+ String action1 = null , valueS = null ;
67+ TextComponent valueC = null ;
68+ while (p .nextToken () != END_OBJECT ) switch (p .currentName ()) {
69+ case "action" :
70+ action1 = p .nextTextValue ();
71+ break ;
72+ case "value" :
73+ if (p .nextToken () == VALUE_STRING ) valueS = p .currentValue ().toString ();
74+ else if (p .currentToken () == START_OBJECT ) valueC = readComponent (p );
75+ else throw new JsonParseException (p , "Expected hoverEvent.value data" );
76+ break ;
77+ default :
78+ throw new IllegalStateException ("Unexpected value: " + p .currentName ());
79+ }
80+ assert action1 != null ;
81+ //noinspection SwitchStatementWithTooFewBranches
82+ var hoverEvent = switch (action1 ) {
83+ case "show_text" -> {
84+ assert valueC != null ;
85+ yield showText (valueC );
86+ }
87+ default -> throw new IllegalStateException ("Unexpected value: " + action1 );
88+ };
89+ text .hoverEvent (hoverEvent );
90+ break ;
91+ default :
92+ for (var decor : TextDecoration .values ())
93+ if (decor .toString ().equals (p .currentName ()) && p .nextBooleanValue ()) text .decorate (decor );
94+ }
10695 return text .build ();
10796 }
10897}
0 commit comments