#2884: Mojangson in hover events cannot be parsed
This commit is contained in:
parent
2e4b08e5ab
commit
637e7e93e0
@ -6,6 +6,7 @@ import com.google.gson.JsonDeserializer;
|
||||
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 com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
@ -297,9 +298,19 @@ public final class HoverEvent
|
||||
{
|
||||
JsonObject value = element.getAsJsonObject();
|
||||
|
||||
int count = -1;
|
||||
if ( value.has( "Count" ) )
|
||||
{
|
||||
JsonPrimitive countObj = value.get( "Count" ).getAsJsonPrimitive();
|
||||
count = ( countObj.isString() )
|
||||
// NBT can serialize as {#int"b"} so remove the b
|
||||
? Integer.parseInt( countObj.getAsString().substring( countObj.getAsString().length() - 1 ) )
|
||||
: countObj.getAsInt();
|
||||
}
|
||||
|
||||
return new ContentItem(
|
||||
( value.has( "id" ) ) ? value.get( "id" ).getAsString() : null,
|
||||
( value.has( "Count" ) ) ? value.get( "Count" ).getAsInt() : -1,
|
||||
count,
|
||||
( value.has( "tag" ) ) ? context.deserialize( value.get( "tag" ), ItemTag.class ) : null
|
||||
);
|
||||
}
|
||||
|
@ -71,17 +71,14 @@ public class BaseComponentSerializer
|
||||
HoverEvent hoverEvent = null;
|
||||
HoverEvent.Action action = HoverEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase( Locale.ROOT ) );
|
||||
|
||||
if ( event.has( "value" ) )
|
||||
for ( String type : Arrays.asList( "value", "contents" ) )
|
||||
{
|
||||
HoverEvent.Content[] ret = new HoverEvent.Content[]
|
||||
{
|
||||
context.deserialize( event.get( "value" ), HoverEvent.getClass( action, false ) )
|
||||
};
|
||||
hoverEvent = new HoverEvent( action, ret );
|
||||
} else if ( event.has( "contents" ) )
|
||||
if ( !event.has( type ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
HoverEvent.Content[] list;
|
||||
JsonElement contents = event.get( "contents" );
|
||||
JsonElement contents = event.get( type );
|
||||
if ( contents.isJsonArray() )
|
||||
{
|
||||
list = context.deserialize( contents, HoverEvent.getClass( action, true ) );
|
||||
@ -94,10 +91,16 @@ public class BaseComponentSerializer
|
||||
}
|
||||
|
||||
hoverEvent = new HoverEvent( action, new ArrayList<>( Arrays.asList( list ) ) );
|
||||
|
||||
// stop the loop as soon as either one is found
|
||||
break;
|
||||
}
|
||||
if ( hoverEvent != null )
|
||||
{
|
||||
component.setHoverEvent( hoverEvent );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
|
||||
{
|
||||
|
@ -12,6 +12,16 @@ import org.junit.Test;
|
||||
public class ComponentsTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void testItemParse()
|
||||
{
|
||||
String json = "{\"extra\":[{\"text\":\"[\"},{\"extra\":[{\"translate\":\"block.minecraft.dirt\"}],\"text\":\"\"},{\"text\":\"]\"}],\"hoverEvent\":{\"action\":\"show_item\",\"value\":[{\"text\":\"{id:\\\"minecraft:dirt\\\",Count:1b}\"}]},\"text\":\"\"}";
|
||||
BaseComponent[] component = ComponentSerializer.parse( json );
|
||||
String serialised = ComponentSerializer.toString( component );
|
||||
BaseComponent[] deserialised = ComponentSerializer.parse( serialised );
|
||||
Assert.assertEquals( TextComponent.toLegacyText( deserialised ), TextComponent.toLegacyText( component ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyComponentBuilder()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user