#3584: Handle conversion of mixed NBT lists to json

This commit is contained in:
md_5 2023-12-22 18:01:30 +11:00
parent b711e4033f
commit f5af11193c
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 47 additions and 2 deletions

View File

@ -124,9 +124,11 @@ public final class TagUtil
for ( JsonElement jsonEl : jsonArray )
{
SpecificTag subTag = fromJson( jsonEl );
if ( subTag.tagType() != listType )
if ( !( subTag instanceof CompoundTag ) )
{
throw new IllegalArgumentException( "Cannot convert mixed JsonArray to Tag" );
CompoundTag wrapper = new CompoundTag();
wrapper.add( "", subTag );
subTag = wrapper;
}
tagItems.add( subTag );
@ -179,6 +181,20 @@ public final class TagUtil
JsonArray jsonList = new JsonArray( items.size() );
for ( SpecificTag subTag : items )
{
if ( subTag instanceof CompoundTag )
{
CompoundTag compound = (CompoundTag) subTag;
if ( compound.size() == 1 )
{
SpecificTag first = (SpecificTag) compound.get( "" );
if ( first != null )
{
jsonList.add( toJson( first ) );
continue;
}
}
}
jsonList.add( toJson( subTag ) );
}

View File

@ -0,0 +1,29 @@
package net.md_5.bungee.protocol;
import static org.junit.jupiter.api.Assertions.*;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.junit.jupiter.api.Test;
import se.llbit.nbt.SpecificTag;
public class TagUtilTest
{
private static final Gson GSON = new Gson();
private static void testDissembleReassemble(String json)
{
JsonElement parsedJson = GSON.fromJson( json, JsonElement.class );
SpecificTag nbt = TagUtil.fromJson( parsedJson );
JsonElement convertedElement = TagUtil.toJson( nbt );
String convertedJson = GSON.toJson( convertedElement );
assertEquals( json, convertedJson );
}
@Test
public void testStringLiteral()
{
testDissembleReassemble( "{\"text\":\"\",\"extra\":[\"hello\",{\"text\":\"there\",\"color\":\"#ff0000\"},{\"text\":\"friend\",\"font\":\"minecraft:default\"}]}" );
}
}