#3018: Serialize text component properties in the same order as Minecraft

This commit is contained in:
blablubbabc 2021-01-13 10:17:16 +11:00 committed by md_5
parent 9ac39005f8
commit baf2f60850
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 28 additions and 33 deletions

View File

@ -22,14 +22,6 @@ public class BaseComponentSerializer
protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context) 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" ) ) if ( object.has( "bold" ) )
{ {
component.setBold( object.get( "bold" ).getAsBoolean() ); component.setBold( object.get( "bold" ).getAsBoolean() );
@ -50,14 +42,14 @@ public class BaseComponentSerializer
{ {
component.setObfuscated( object.get( "obfuscated" ).getAsBoolean() ); component.setObfuscated( object.get( "obfuscated" ).getAsBoolean() );
} }
if ( object.has( "color" ) )
{
component.setColor( ChatColor.of( object.get( "color" ).getAsString() ) );
}
if ( object.has( "insertion" ) ) if ( object.has( "insertion" ) )
{ {
component.setInsertion( object.get( "insertion" ).getAsString() ); component.setInsertion( object.get( "insertion" ).getAsString() );
} }
if ( object.has( "extra" ) )
{
component.setExtra( Arrays.asList( context.<BaseComponent[]>deserialize( object.get( "extra" ), BaseComponent[].class ) ) );
}
//Events //Events
if ( object.has( "clickEvent" ) ) if ( object.has( "clickEvent" ) )
@ -121,6 +113,15 @@ public class BaseComponentSerializer
component.setHoverEvent( hoverEvent ); component.setHoverEvent( hoverEvent );
} }
} }
if ( object.has( "font" ) )
{
component.setFont( object.get( "font" ).getAsString() );
}
if ( object.has( "extra" ) )
{
component.setExtra( Arrays.asList( context.<BaseComponent[]>deserialize( object.get( "extra" ), BaseComponent[].class ) ) );
}
} }
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context) 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" ); Preconditions.checkArgument( !ComponentSerializer.serializedComponents.get().contains( component ), "Component loop" );
ComponentSerializer.serializedComponents.get().add( component ); 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 ) if ( component.isBoldRaw() != null )
{ {
object.addProperty( "bold", component.isBoldRaw() ); object.addProperty( "bold", component.isBoldRaw() );
@ -163,16 +156,15 @@ public class BaseComponentSerializer
{ {
object.addProperty( "obfuscated", component.isObfuscatedRaw() ); object.addProperty( "obfuscated", component.isObfuscatedRaw() );
} }
if ( component.getColorRaw() != null )
{
object.addProperty( "color", component.getColorRaw().getName() );
}
if ( component.getInsertion() != null ) if ( component.getInsertion() != null )
{ {
object.addProperty( "insertion", component.getInsertion() ); object.addProperty( "insertion", component.getInsertion() );
} }
if ( component.getExtra() != null )
{
object.add( "extra", context.serialize( component.getExtra() ) );
}
//Events //Events
if ( component.getClickEvent() != null ) if ( component.getClickEvent() != null )
{ {
@ -195,6 +187,15 @@ public class BaseComponentSerializer
} }
object.add( "hoverEvent", hoverEvent ); 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 } finally
{ {
ComponentSerializer.serializedComponents.get().remove( component ); ComponentSerializer.serializedComponents.get().remove( component );

View File

@ -8,8 +8,6 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import java.lang.reflect.Type; 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; import net.md_5.bungee.api.chat.TextComponent;
public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TextComponent>, JsonDeserializer<TextComponent> public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TextComponent>, JsonDeserializer<TextComponent>
@ -32,13 +30,9 @@ public class TextComponentSerializer extends BaseComponentSerializer implements
@Override @Override
public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context)
{ {
List<BaseComponent> extra = src.getExtra();
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
object.addProperty( "text", src.getText() );
if ( src.hasFormatting() || ( extra != null && !extra.isEmpty() ) )
{
serialize( object, src, context ); serialize( object, src, context );
} object.addProperty( "text", src.getText() );
return object; return object;
} }
} }

View File

@ -41,7 +41,7 @@ public class ComponentsTest
{ {
textComponent 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 ); testDissembleReassemble( json );
////////// //////////
String hoverVal = "{\"text\":\"{id:\\\"minecraft:dirt\\\",Count:1b}\"}"; String hoverVal = "{\"text\":\"{id:\\\"minecraft:dirt\\\",Count:1b}\"}";