#3578: bungeecord-chat does not support array format UUIDs

This commit is contained in:
md_5 2023-12-19 19:53:04 +11:00
parent 3deaaadc3a
commit b711e4033f
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 55 additions and 38 deletions

View File

@ -8,6 +8,7 @@ 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.UUID;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
public class EntitySerializer implements JsonSerializer<Entity>, JsonDeserializer<Entity> public class EntitySerializer implements JsonSerializer<Entity>, JsonDeserializer<Entity>
@ -18,9 +19,19 @@ public class EntitySerializer implements JsonSerializer<Entity>, JsonDeserialize
{ {
JsonObject value = element.getAsJsonObject(); 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( return new Entity(
( value.has( "type" ) ) ? value.get( "type" ).getAsString() : null, ( value.has( "type" ) ) ? value.get( "type" ).getAsString() : null,
value.get( "id" ).getAsString(), idString,
( value.has( "name" ) ) ? context.deserialize( value.get( "name" ), BaseComponent.class ) : null ( value.has( "name" ) ) ? context.deserialize( value.get( "name" ), BaseComponent.class ) : null
); );
} }
@ -37,4 +48,9 @@ public class EntitySerializer implements JsonSerializer<Entity>, JsonDeserialize
} }
return object; 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 );
}
} }

View File

@ -4,7 +4,6 @@ import com.google.common.base.Preconditions;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import java.util.ArrayList; import java.util.ArrayList;
@ -90,49 +89,42 @@ public class BaseComponentSerializer
HoverEvent hoverEvent = null; HoverEvent hoverEvent = null;
HoverEvent.Action action = HoverEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase( Locale.ROOT ) ); 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" );
{
continue;
}
JsonElement contents = event.get( type );
try
{
// Plugins previously had support to pass BaseComponent[] into any action. // 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. // If the GSON is possible to be parsed as BaseComponent, attempt to parse as so.
BaseComponent[] components; BaseComponent[] components;
if ( contents.isJsonArray() ) if ( contents.isJsonArray() )
{
components = context.deserialize( contents, BaseComponent[].class );
} else
{
components = new BaseComponent[]
{
context.deserialize( contents, BaseComponent.class )
};
}
hoverEvent = new HoverEvent( action, components );
} catch ( JsonParseException ex )
{ {
Content[] list; components = context.deserialize( contents, BaseComponent[].class );
if ( contents.isJsonArray() ) } else
{
components = new BaseComponent[]
{ {
list = context.deserialize( contents, HoverEvent.getClass( action, true ) ); context.deserialize( contents, BaseComponent.class )
} else };
{
list = new Content[]
{
context.deserialize( contents, HoverEvent.getClass( action, false ) )
};
}
hoverEvent = new HoverEvent( action, new ArrayList<>( Arrays.asList( list ) ) );
} }
hoverEvent = new HoverEvent( action, components );
} else if ( event.has( "contents" ) )
{
JsonElement contents = event.get( "contents" );
// stop the loop as soon as either one is found Content[] list;
break; if ( contents.isJsonArray() )
{
list = context.deserialize( contents, HoverEvent.getClass( action, true ) );
} else
{
list = new Content[]
{
context.deserialize( contents, HoverEvent.getClass( action, false ) )
};
}
hoverEvent = new HoverEvent( action, new ArrayList<>( Arrays.asList( list ) ) );
} }
if ( hoverEvent != null ) if ( hoverEvent != null )
{ {
component.setHoverEvent( hoverEvent ); component.setHoverEvent( hoverEvent );

View File

@ -8,6 +8,7 @@ import java.util.function.Function;
import java.util.function.ObjIntConsumer; import java.util.function.ObjIntConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import net.md_5.bungee.api.ChatColor; 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.api.chat.hover.content.Text;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -88,6 +89,14 @@ public class ComponentsTest
*/ */
} }
@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 @Test
public void testEmptyComponentBuilderCreate() public void testEmptyComponentBuilderCreate()
{ {