From baf2f60850932e1f628e842af68bbd08bfdfc7b1 Mon Sep 17 00:00:00 2001 From: blablubbabc Date: Wed, 13 Jan 2021 10:17:16 +1100 Subject: [PATCH] #3018: Serialize text component properties in the same order as Minecraft --- .../bungee/chat/BaseComponentSerializer.java | 51 ++++++++++--------- .../bungee/chat/TextComponentSerializer.java | 8 +-- .../md_5/bungee/api/chat/ComponentsTest.java | 2 +- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java index ae69b231..fc9739a8 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java @@ -22,14 +22,6 @@ public class BaseComponentSerializer protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context) { - if ( object.has( "color" ) ) - { - component.setColor( ChatColor.of( object.get( "color" ).getAsString() ) ); - } - if ( object.has( "font" ) ) - { - component.setFont( object.get( "font" ).getAsString() ); - } if ( object.has( "bold" ) ) { component.setBold( object.get( "bold" ).getAsBoolean() ); @@ -50,14 +42,14 @@ public class BaseComponentSerializer { component.setObfuscated( object.get( "obfuscated" ).getAsBoolean() ); } + if ( object.has( "color" ) ) + { + component.setColor( ChatColor.of( object.get( "color" ).getAsString() ) ); + } if ( object.has( "insertion" ) ) { component.setInsertion( object.get( "insertion" ).getAsString() ); } - if ( object.has( "extra" ) ) - { - component.setExtra( Arrays.asList( context.deserialize( object.get( "extra" ), BaseComponent[].class ) ) ); - } //Events if ( object.has( "clickEvent" ) ) @@ -121,6 +113,15 @@ public class BaseComponentSerializer component.setHoverEvent( hoverEvent ); } } + + if ( object.has( "font" ) ) + { + component.setFont( object.get( "font" ).getAsString() ); + } + if ( object.has( "extra" ) ) + { + component.setExtra( Arrays.asList( context.deserialize( object.get( "extra" ), BaseComponent[].class ) ) ); + } } protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context) @@ -135,14 +136,6 @@ public class BaseComponentSerializer { Preconditions.checkArgument( !ComponentSerializer.serializedComponents.get().contains( component ), "Component loop" ); ComponentSerializer.serializedComponents.get().add( component ); - if ( component.getColorRaw() != null ) - { - object.addProperty( "color", component.getColorRaw().getName() ); - } - if ( component.getFontRaw() != null ) - { - object.addProperty( "font", component.getFontRaw() ); - } if ( component.isBoldRaw() != null ) { object.addProperty( "bold", component.isBoldRaw() ); @@ -163,16 +156,15 @@ public class BaseComponentSerializer { object.addProperty( "obfuscated", component.isObfuscatedRaw() ); } + if ( component.getColorRaw() != null ) + { + object.addProperty( "color", component.getColorRaw().getName() ); + } if ( component.getInsertion() != null ) { object.addProperty( "insertion", component.getInsertion() ); } - if ( component.getExtra() != null ) - { - object.add( "extra", context.serialize( component.getExtra() ) ); - } - //Events if ( component.getClickEvent() != null ) { @@ -195,6 +187,15 @@ public class BaseComponentSerializer } object.add( "hoverEvent", hoverEvent ); } + + if ( component.getFontRaw() != null ) + { + object.addProperty( "font", component.getFontRaw() ); + } + if ( component.getExtra() != null ) + { + object.add( "extra", context.serialize( component.getExtra() ) ); + } } finally { ComponentSerializer.serializedComponents.get().remove( component ); diff --git a/chat/src/main/java/net/md_5/bungee/chat/TextComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/TextComponentSerializer.java index a9347fe0..8a7e9819 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/TextComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/TextComponentSerializer.java @@ -8,8 +8,6 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.lang.reflect.Type; -import java.util.List; -import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer, JsonDeserializer @@ -32,13 +30,9 @@ public class TextComponentSerializer extends BaseComponentSerializer implements @Override public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context) { - List extra = src.getExtra(); JsonObject object = new JsonObject(); + serialize( object, src, context ); object.addProperty( "text", src.getText() ); - if ( src.hasFormatting() || ( extra != null && !extra.isEmpty() ) ) - { - serialize( object, src, context ); - } return object; } } diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java index 23fa3024..20c62f1d 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java @@ -41,7 +41,7 @@ public class ComponentsTest { textComponent } ); - json = "{\"text\":\"Test\",\"hoverEvent\":{\"action\":\"show_item\",\"value\":[{\"text\":\"{id:\\\"minecraft:netherrack\\\",Count:47b}\"}]}}"; + json = "{\"hoverEvent\":{\"action\":\"show_item\",\"value\":[{\"text\":\"{id:\\\"minecraft:netherrack\\\",Count:47b}\"}]},\"text\":\"Test\"}"; testDissembleReassemble( json ); ////////// String hoverVal = "{\"text\":\"{id:\\\"minecraft:dirt\\\",Count:1b}\"}";