From b711e4033fff3e98d5e33fdd429b4cd33aa23987 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 19 Dec 2023 19:53:04 +1100 Subject: [PATCH] #3578: bungeecord-chat does not support array format UUIDs --- .../chat/hover/content/EntitySerializer.java | 18 +++++- .../bungee/chat/BaseComponentSerializer.java | 64 ++++++++----------- .../md_5/bungee/api/chat/ComponentsTest.java | 9 +++ 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/hover/content/EntitySerializer.java b/chat/src/main/java/net/md_5/bungee/api/chat/hover/content/EntitySerializer.java index 531433262c..654dc3aa50 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/hover/content/EntitySerializer.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/hover/content/EntitySerializer.java @@ -8,6 +8,7 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.lang.reflect.Type; +import java.util.UUID; import net.md_5.bungee.api.chat.BaseComponent; public class EntitySerializer implements JsonSerializer, JsonDeserializer @@ -18,9 +19,19 @@ public Entity deserialize(JsonElement element, Type type, JsonDeserializationCon { JsonObject value = element.getAsJsonObject(); + String idString; + JsonElement id = value.get( "id" ); + if ( id.isJsonArray() ) + { + idString = parseUUID( context.deserialize( id, int[].class ) ).toString(); + } else + { + idString = id.getAsString(); + } + return new Entity( ( value.has( "type" ) ) ? value.get( "type" ).getAsString() : null, - value.get( "id" ).getAsString(), + idString, ( value.has( "name" ) ) ? context.deserialize( value.get( "name" ), BaseComponent.class ) : null ); } @@ -37,4 +48,9 @@ public JsonElement serialize(Entity content, Type type, JsonSerializationContext } return object; } + + private static UUID parseUUID(int[] array) + { + return new UUID( (long) array[0] << 32 | (long) array[1] & 0XFFFFFFFFL, (long) array[2] << 32 | (long) array[3] & 0XFFFFFFFFL ); + } } 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 b213498e13..b7e85f1f12 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 @@ -4,7 +4,6 @@ import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import java.util.ArrayList; @@ -90,49 +89,42 @@ protected void deserialize(JsonObject object, BaseComponent component, JsonDeser HoverEvent hoverEvent = null; HoverEvent.Action action = HoverEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase( Locale.ROOT ) ); - for ( String type : Arrays.asList( "value", "contents" ) ) + if ( event.has( "value" ) ) { - if ( !event.has( type ) ) + JsonElement contents = event.get( "value" ); + + // Plugins previously had support to pass BaseComponent[] into any action. + // If the GSON is possible to be parsed as BaseComponent, attempt to parse as so. + BaseComponent[] components; + if ( contents.isJsonArray() ) { - continue; - } - JsonElement contents = event.get( type ); - try + components = context.deserialize( contents, BaseComponent[].class ); + } else { - - // Plugins previously had support to pass BaseComponent[] into any action. - // If the GSON is possible to be parsed as BaseComponent, attempt to parse as so. - BaseComponent[] components; - if ( contents.isJsonArray() ) + components = new BaseComponent[] { - components = context.deserialize( contents, BaseComponent[].class ); - } else - { - components = new BaseComponent[] - { - context.deserialize( contents, BaseComponent.class ) - }; - } - hoverEvent = new HoverEvent( action, components ); - } catch ( JsonParseException ex ) + context.deserialize( contents, BaseComponent.class ) + }; + } + hoverEvent = new HoverEvent( action, components ); + } else if ( event.has( "contents" ) ) + { + JsonElement contents = event.get( "contents" ); + + Content[] list; + if ( contents.isJsonArray() ) { - Content[] list; - if ( contents.isJsonArray() ) - { - list = context.deserialize( contents, HoverEvent.getClass( action, true ) ); - } else + list = context.deserialize( contents, HoverEvent.getClass( action, true ) ); + } else + { + list = new Content[] { - list = new Content[] - { - context.deserialize( contents, HoverEvent.getClass( action, false ) ) - }; - } - hoverEvent = new HoverEvent( action, new ArrayList<>( Arrays.asList( list ) ) ); + context.deserialize( contents, HoverEvent.getClass( action, false ) ) + }; } - - // stop the loop as soon as either one is found - break; + hoverEvent = new HoverEvent( action, new ArrayList<>( Arrays.asList( list ) ) ); } + if ( hoverEvent != null ) { component.setHoverEvent( hoverEvent ); 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 5319ecf31c..b4595b327a 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 @@ -8,6 +8,7 @@ import java.util.function.ObjIntConsumer; import java.util.function.Supplier; import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.hover.content.Entity; import net.md_5.bungee.api.chat.hover.content.Text; import net.md_5.bungee.chat.ComponentSerializer; import org.junit.jupiter.api.Test; @@ -88,6 +89,14 @@ public void testItemParse() */ } + @Test + public void testArrayUUIDParse() + { + BaseComponent[] uuidComponent = ComponentSerializer.parse( "{\"translate\":\"multiplayer.player.joined\",\"with\":[{\"text\":\"Rexcantor64\",\"hoverEvent\":{\"contents\":{\"type\":\"minecraft:player\",\"id\":[1328556382,-2138814985,-1895806765,-1039963041],\"name\":\"Rexcantor64\"},\"action\":\"show_entity\"},\"insertion\":\"Rexcantor64\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/tell Rexcantor64 \"}}],\"color\":\"yellow\"}" ); + assertEquals( "4f30295e-8084-45f7-8f00-48d3c2036c5f", ( (Entity) ( (TranslatableComponent) uuidComponent[0] ).getWith().get( 0 ).getHoverEvent().getContents().get( 0 ) ).getId() ); + testDissembleReassemble( uuidComponent ); + } + @Test public void testEmptyComponentBuilderCreate() {