Skip to content

Commit

Permalink
#3578: bungeecord-chat does not support array format UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
md-5 committed Dec 19, 2023
1 parent 3deaaad commit b711e40
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
Expand Up @@ -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<Entity>, JsonDeserializer<Entity>
Expand All @@ -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
);
}
Expand All @@ -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 );
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit b711e40

Please sign in to comment.