#2909: Don't serialise as array for single element contents

This commit is contained in:
Mystiflow 2020-07-16 18:53:23 +10:00 committed by md_5
parent 0af4bfdbdf
commit c1522ab94c
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 26 additions and 22 deletions

View File

@ -7,15 +7,11 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.Singular;
import lombok.ToString;
/**
@ -25,13 +21,14 @@ import lombok.ToString;
@ToString(of = "nbt")
@EqualsAndHashCode(of = "nbt")
@Setter
@AllArgsConstructor
public final class ItemTag
{
@Getter
private final String nbt;
/*
TODO
private BaseComponent name;
@Singular("ench")
private List<Enchantment> enchantments;
@ -39,6 +36,15 @@ public final class ItemTag
private List<BaseComponent[]> lore;
private Boolean unbreakable;
@RequiredArgsConstructor
public static class Enchantment
{
private final int level;
private final int id;
}
*/
private ItemTag(String nbt)
{
this.nbt = nbt;
@ -49,28 +55,13 @@ public final class ItemTag
return new ItemTag( nbt );
}
@RequiredArgsConstructor
public static class Enchantment
{
private final int level;
private final int id;
}
public static class Serializer implements JsonSerializer<ItemTag>, JsonDeserializer<ItemTag>
{
@Override
public ItemTag deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException
{
// Remove the enclosing string quotes.
String eString = element.toString();
if ( eString.length() >= 2 && eString.charAt( 0 ) == '\"' && eString.charAt( eString.length() - 1 ) == '\"' )
{
eString = eString.substring( 1, eString.length() - 1 );
}
return ItemTag.ofNbt( eString );
return ItemTag.ofNbt( element.getAsJsonPrimitive().getAsString() );
}
@Override

View File

@ -190,7 +190,8 @@ public class BaseComponentSerializer
hoverEvent.add( "value", context.serialize( component.getHoverEvent().getContents().get( 0 ) ) );
} else
{
hoverEvent.add( "contents", context.serialize( component.getHoverEvent().getContents() ) );
hoverEvent.add( "contents", context.serialize( ( component.getHoverEvent().getContents().size() == 1 )
? component.getHoverEvent().getContents().get( 0 ) : component.getHoverEvent().getContents() ) );
}
object.add( "hoverEvent", hoverEvent );
}

View File

@ -237,6 +237,18 @@ public class ComponentsTest
serialized = ComponentSerializer.toString( component );
deserialized = ComponentSerializer.parse( serialized );
Assert.assertEquals( component.getHoverEvent(), deserialized[0].getHoverEvent() );
// Test single content:
String json = "{\"italic\":true,\"color\":\"gray\",\"translate\":\"chat.type.admin\",\"with\":[{\"text\":\"@\"}"
+ ",{\"translate\":\"commands.give.success.single\",\"with\":[\"1\",{\"color\":\"white\""
+ ",\"hoverEvent\":{\"action\":\"show_item\",\"contents\":{\"id\":\"minecraft:diamond_sword\",\"tag\":\""
+ "{Damage:0,display:{Lore:['\\\"test lore'!\\\"'],Name:'\\\"test\\\"'}}\"}},"
+ "\"extra\":[{\"italic\":true,\"extra\":[{\"text\":\"test\"}],\"text\":\"\"},{\"text\":\"]\"}],"
+ "\"text\":\"[\"},{\"insertion\":\"Name\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":"
+ "\"/tell Name \"},\"hoverEvent\":{\"action\":\"show_entity\",\"contents\":"
+ "{\"type\":\"minecraft:player\",\"id\":\"00000000-0000-0000-0000-00000000000000\",\"name\":"
+ "{\"text\":\"Name\"}}},\"text\":\"Name\"}]}]}";
testDissembleReassemble( ComponentSerializer.parse( json ) );
}
@Test