10
10
import java .util .Collections ;
11
11
import java .util .IdentityHashMap ;
12
12
import java .util .Locale ;
13
+ import lombok .RequiredArgsConstructor ;
13
14
import net .md_5 .bungee .api .chat .BaseComponent ;
14
15
import net .md_5 .bungee .api .chat .ClickEvent ;
15
16
import net .md_5 .bungee .api .chat .ComponentStyle ;
16
17
import net .md_5 .bungee .api .chat .HoverEvent ;
17
18
import net .md_5 .bungee .api .chat .hover .content .Content ;
18
19
20
+ @ RequiredArgsConstructor
19
21
public class BaseComponentSerializer
20
22
{
21
23
24
+ protected final VersionedComponentSerializer serializer ;
25
+
22
26
protected void deserialize (JsonObject object , BaseComponent component , JsonDeserializationContext context )
23
27
{
24
28
component .applyStyle ( context .deserialize ( object , ComponentStyle .class ) );
@@ -59,7 +63,6 @@ protected void deserialize(JsonObject object, BaseComponent component, JsonDeser
59
63
component .setClickEvent ( new ClickEvent ( action , ( clickEvent .has ( "value" ) ) ? clickEvent .get ( "value" ).getAsString () : "" ) );
60
64
break ;
61
65
}
62
- component .getClickEvent ().setV1_21_5 ( true );
63
66
} else
64
67
{
65
68
component .setClickEvent ( new ClickEvent ( action , ( clickEvent .has ( "value" ) ) ? clickEvent .get ( "value" ).getAsString () : "" ) );
@@ -101,7 +104,6 @@ protected void deserialize(JsonObject object, BaseComponent component, JsonDeser
101
104
};
102
105
}
103
106
hoverEvent = new HoverEvent ( action , new ArrayList <>( Arrays .asList ( list ) ) );
104
- hoverEvent .setV1_21_5 ( newHoverEvent );
105
107
}
106
108
} else
107
109
{
@@ -141,15 +143,15 @@ protected void deserialize(JsonObject object, BaseComponent component, JsonDeser
141
143
protected void serialize (JsonObject object , BaseComponent component , JsonSerializationContext context )
142
144
{
143
145
boolean first = false ;
144
- if ( ComponentSerializer .serializedComponents .get () == null )
146
+ if ( VersionedComponentSerializer .serializedComponents .get () == null )
145
147
{
146
148
first = true ;
147
- ComponentSerializer .serializedComponents .set ( Collections .newSetFromMap ( new IdentityHashMap <BaseComponent , Boolean >() ) );
149
+ VersionedComponentSerializer .serializedComponents .set ( Collections .newSetFromMap ( new IdentityHashMap <BaseComponent , Boolean >() ) );
148
150
}
149
151
try
150
152
{
151
- Preconditions .checkArgument ( !ComponentSerializer .serializedComponents .get ().contains ( component ), "Component loop" );
152
- ComponentSerializer .serializedComponents .get ().add ( component );
153
+ Preconditions .checkArgument ( !VersionedComponentSerializer .serializedComponents .get ().contains ( component ), "Component loop" );
154
+ VersionedComponentSerializer .serializedComponents .get ().add ( component );
153
155
154
156
ComponentStyleSerializer .serializeTo ( component .getStyle (), object );
155
157
@@ -164,63 +166,79 @@ protected void serialize(JsonObject object, BaseComponent component, JsonSeriali
164
166
JsonObject clickEvent = new JsonObject ();
165
167
String actionName = component .getClickEvent ().getAction ().toString ().toLowerCase ( Locale .ROOT );
166
168
clickEvent .addProperty ( "action" , actionName .toLowerCase ( Locale .ROOT ) );
167
- if ( component . getClickEvent (). isV1_21_5 () )
169
+ switch ( serializer . getVersion () )
168
170
{
169
- ClickEvent .Action action = ClickEvent .Action .valueOf ( actionName .toUpperCase ( Locale .ROOT ) );
170
- switch ( action )
171
- {
172
- case OPEN_URL :
173
- clickEvent .addProperty ( "url" , component .getClickEvent ().getValue () );
174
- break ;
175
- case RUN_COMMAND :
176
- case SUGGEST_COMMAND :
177
- clickEvent .addProperty ( "command" , component .getClickEvent ().getValue () );
178
- break ;
179
- case CHANGE_PAGE :
180
- clickEvent .addProperty ( "page" , Integer .parseInt ( component .getClickEvent ().getValue () ) );
181
- break ;
182
- default :
183
- clickEvent .addProperty ( "value" , component .getClickEvent ().getValue () );
184
- break ;
185
- }
186
- object .add ( "click_event" , clickEvent );
187
- } else
188
- {
189
- clickEvent .addProperty ( "value" , component .getClickEvent ().getValue () );
190
- object .add ( "clickEvent" , clickEvent );
171
+ case V1_21_5 :
172
+ ClickEvent .Action action = ClickEvent .Action .valueOf ( actionName .toUpperCase ( Locale .ROOT ) );
173
+ switch ( action )
174
+ {
175
+ case OPEN_URL :
176
+ clickEvent .addProperty ( "url" , component .getClickEvent ().getValue () );
177
+ break ;
178
+ case RUN_COMMAND :
179
+ case SUGGEST_COMMAND :
180
+ clickEvent .addProperty ( "command" , component .getClickEvent ().getValue () );
181
+ break ;
182
+ case CHANGE_PAGE :
183
+ clickEvent .addProperty ( "page" , Integer .parseInt ( component .getClickEvent ().getValue () ) );
184
+ break ;
185
+ default :
186
+ clickEvent .addProperty ( "value" , component .getClickEvent ().getValue () );
187
+ break ;
188
+ }
189
+ object .add ( "click_event" , clickEvent );
190
+ break ;
191
+ case V1_16 :
192
+ clickEvent .addProperty ( "value" , component .getClickEvent ().getValue () );
193
+ object .add ( "clickEvent" , clickEvent );
194
+ break ;
195
+ default :
196
+ throw new IllegalArgumentException ( "Unknown version " + serializer .getVersion () );
191
197
}
192
198
193
199
}
194
200
if ( component .getHoverEvent () != null )
195
201
{
196
202
JsonObject hoverEvent = new JsonObject ();
197
203
hoverEvent .addProperty ( "action" , component .getHoverEvent ().getAction ().toString ().toLowerCase ( Locale .ROOT ) );
198
- boolean newFormat = component .getHoverEvent ().isV1_21_5 ();
199
204
if ( component .getHoverEvent ().isLegacy () )
200
205
{
201
206
hoverEvent .add ( "value" , context .serialize ( component .getHoverEvent ().getContents ().get ( 0 ) ) );
202
207
} else
203
208
{
204
- if ( newFormat )
209
+ switch ( serializer . getVersion () )
205
210
{
206
- if ( component .getHoverEvent ().getAction () == HoverEvent .Action .SHOW_ITEM || component .getHoverEvent ().getAction () == HoverEvent .Action .SHOW_ENTITY )
207
- {
208
- JsonObject inlined = context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
209
- ? component .getHoverEvent ().getContents ().get ( 0 ) : component .getHoverEvent ().getContents () ).getAsJsonObject ();
210
- inlined .entrySet ().forEach ( entry -> hoverEvent .add ( entry .getKey (), entry .getValue () ) );
211
- } else
212
- {
213
- hoverEvent .add ( "value" , context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
211
+ case V1_21_5 :
212
+ if ( component .getHoverEvent ().getAction () == HoverEvent .Action .SHOW_ITEM || component .getHoverEvent ().getAction () == HoverEvent .Action .SHOW_ENTITY )
213
+ {
214
+ JsonObject inlined = context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
215
+ ? component .getHoverEvent ().getContents ().get ( 0 ) : component .getHoverEvent ().getContents () ).getAsJsonObject ();
216
+ inlined .entrySet ().forEach ( entry -> hoverEvent .add ( entry .getKey (), entry .getValue () ) );
217
+ } else
218
+ {
219
+ hoverEvent .add ( "value" , context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
220
+ ? component .getHoverEvent ().getContents ().get ( 0 ) : component .getHoverEvent ().getContents () ) );
221
+ }
222
+ break ;
223
+ case V1_16 :
224
+ hoverEvent .add ( "contents" , context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
214
225
? component .getHoverEvent ().getContents ().get ( 0 ) : component .getHoverEvent ().getContents () ) );
215
- }
216
- } else
217
- {
218
- hoverEvent .add ( "contents" , context .serialize ( ( component .getHoverEvent ().getContents ().size () == 1 )
219
- ? component .getHoverEvent ().getContents ().get ( 0 ) : component .getHoverEvent ().getContents () ) );
226
+ break ;
227
+ default :
228
+ throw new IllegalArgumentException ( "Unknown version " + serializer .getVersion () );
220
229
}
221
-
222
230
}
223
- object .add ( newFormat ? "hover_event" : "hoverEvent" , hoverEvent );
231
+ switch ( serializer .getVersion () )
232
+ {
233
+ case V1_21_5 :
234
+ object .add ( "hover_event" , hoverEvent );
235
+ break ;
236
+ case V1_16 :
237
+ object .add ( "hoverEvent" , hoverEvent );
238
+ break ;
239
+ default :
240
+ throw new IllegalArgumentException ( "Unknown version " + serializer .getVersion () );
241
+ }
224
242
}
225
243
226
244
if ( component .getExtra () != null )
@@ -229,10 +247,10 @@ protected void serialize(JsonObject object, BaseComponent component, JsonSeriali
229
247
}
230
248
} finally
231
249
{
232
- ComponentSerializer .serializedComponents .get ().remove ( component );
250
+ VersionedComponentSerializer .serializedComponents .get ().remove ( component );
233
251
if ( first )
234
252
{
235
- ComponentSerializer .serializedComponents .set ( null );
253
+ VersionedComponentSerializer .serializedComponents .set ( null );
236
254
}
237
255
}
238
256
}
0 commit comments