mirror of
https://github.com/SpigotMC/BungeeCord.git
synced 2024-11-13 22:05:27 +01:00
#3584: Handle conversion of mixed NBT lists to json
This commit is contained in:
parent
b711e4033f
commit
f5af11193c
@ -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 ) );
|
||||
}
|
||||
|
||||
|
@ -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\"}]}" );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user