Correct component loop detector
This commit is contained in:
parent
941450b4e4
commit
38f12840ca
@ -10,6 +10,7 @@ import net.md_5.bungee.api.chat.ClickEvent;
|
|||||||
import net.md_5.bungee.api.chat.HoverEvent;
|
import net.md_5.bungee.api.chat.HoverEvent;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class BaseComponentSerializer
|
public class BaseComponentSerializer
|
||||||
{
|
{
|
||||||
@ -72,6 +73,13 @@ public class BaseComponentSerializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
|
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
|
||||||
|
{
|
||||||
|
boolean first = false;
|
||||||
|
if ( ComponentSerializer.serializedComponents.get() == null ) {
|
||||||
|
first = true;
|
||||||
|
ComponentSerializer.serializedComponents.set( new HashSet<BaseComponent>( ) );
|
||||||
|
}
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Preconditions.checkArgument( !ComponentSerializer.serializedComponents.get().contains( component ), "Component loop" );
|
Preconditions.checkArgument( !ComponentSerializer.serializedComponents.get().contains( component ), "Component loop" );
|
||||||
ComponentSerializer.serializedComponents.get().add( component );
|
ComponentSerializer.serializedComponents.get().add( component );
|
||||||
@ -120,5 +128,12 @@ public class BaseComponentSerializer
|
|||||||
hoverEvent.add( "value", context.serialize( component.getHoverEvent().getValue() ) );
|
hoverEvent.add( "value", context.serialize( component.getHoverEvent().getValue() ) );
|
||||||
object.add( "hoverEvent", hoverEvent );
|
object.add( "hoverEvent", hoverEvent );
|
||||||
}
|
}
|
||||||
|
} finally
|
||||||
|
{
|
||||||
|
ComponentSerializer.serializedComponents.get().remove( component );
|
||||||
|
if (first) {
|
||||||
|
ComponentSerializer.serializedComponents.set( null );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ import net.md_5.bungee.api.chat.TranslatableComponent;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonDeserializer<BaseComponent>
|
public class ComponentSerializer implements JsonDeserializer<BaseComponent>
|
||||||
{
|
{
|
||||||
|
|
||||||
private final static Gson gson = new GsonBuilder().
|
private final static Gson gson = new GsonBuilder().
|
||||||
@ -25,14 +25,7 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
|||||||
registerTypeAdapter( TranslatableComponent.class, new TranslatableComponentSerializer() ).
|
registerTypeAdapter( TranslatableComponent.class, new TranslatableComponentSerializer() ).
|
||||||
create();
|
create();
|
||||||
|
|
||||||
public final static ThreadLocal<HashSet<BaseComponent>> serializedComponents = new ThreadLocal<HashSet<BaseComponent>>()
|
public final static ThreadLocal<HashSet<BaseComponent>> serializedComponents = new ThreadLocal<>();
|
||||||
{
|
|
||||||
@Override
|
|
||||||
protected HashSet<BaseComponent> initialValue()
|
|
||||||
{
|
|
||||||
return new HashSet<>();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static BaseComponent[] parse(String json)
|
public static BaseComponent[] parse(String json)
|
||||||
{
|
{
|
||||||
@ -70,15 +63,4 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
|||||||
}
|
}
|
||||||
return context.deserialize( json, TextComponent.class );
|
return context.deserialize( json, TextComponent.class );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public JsonElement serialize(BaseComponent src, Type typeOfSrc, JsonSerializationContext context)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return context.serialize( src, src.getClass() );
|
|
||||||
} finally
|
|
||||||
{
|
|
||||||
serializedComponents.get().clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -81,21 +81,50 @@ public class ComponentsTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testLoopSimple() {
|
public void testLoopSimple()
|
||||||
|
{
|
||||||
TextComponent component = new TextComponent( "Testing" );
|
TextComponent component = new TextComponent( "Testing" );
|
||||||
component.addExtra( component );
|
component.addExtra( component );
|
||||||
ComponentSerializer.toString( component );
|
ComponentSerializer.toString( component );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testLoopComplex() {
|
public void testLoopComplex()
|
||||||
|
{
|
||||||
TextComponent a = new TextComponent( "A" );
|
TextComponent a = new TextComponent( "A" );
|
||||||
TextComponent b = new TextComponent( "B" );
|
TextComponent b = new TextComponent( "B" );
|
||||||
|
b.setColor( ChatColor.AQUA );
|
||||||
TextComponent c = new TextComponent( "C" );
|
TextComponent c = new TextComponent( "C" );
|
||||||
|
c.setColor( ChatColor.RED );
|
||||||
a.addExtra( b );
|
a.addExtra( b );
|
||||||
b.addExtra( c );
|
b.addExtra( c );
|
||||||
c.addExtra( a );
|
c.addExtra( a );
|
||||||
ComponentSerializer.toString( a );
|
ComponentSerializer.toString( a );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRepeated()
|
||||||
|
{
|
||||||
|
TextComponent a = new TextComponent( "A" );
|
||||||
|
TextComponent b = new TextComponent( "B" );
|
||||||
|
b.setColor( ChatColor.AQUA );
|
||||||
|
a.addExtra( b );
|
||||||
|
a.addExtra( b );
|
||||||
|
ComponentSerializer.toString( a );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testRepeatedError()
|
||||||
|
{
|
||||||
|
TextComponent a = new TextComponent( "A" );
|
||||||
|
TextComponent b = new TextComponent( "B" );
|
||||||
|
b.setColor( ChatColor.AQUA );
|
||||||
|
TextComponent c = new TextComponent( "C" );
|
||||||
|
c.setColor( ChatColor.RED );
|
||||||
|
a.addExtra( b );
|
||||||
|
a.addExtra( c );
|
||||||
|
c.addExtra( a );
|
||||||
|
a.addExtra( b );
|
||||||
|
ComponentSerializer.toString( a );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user