Detect component loops
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
@@ -72,6 +73,8 @@ public class BaseComponentSerializer
|
||||
|
||||
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
|
||||
{
|
||||
Preconditions.checkArgument( !ComponentSerializer.serializedComponents.get().contains( component ), "Component loop" );
|
||||
ComponentSerializer.serializedComponents.get().add( component );
|
||||
if ( component.getColorRaw() != null )
|
||||
{
|
||||
object.addProperty( "color", component.getColorRaw().getName() );
|
||||
|
@@ -14,6 +14,7 @@ import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonDeserializer<BaseComponent>
|
||||
{
|
||||
@@ -24,6 +25,15 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
||||
registerTypeAdapter( TranslatableComponent.class, new TranslatableComponentSerializer() ).
|
||||
create();
|
||||
|
||||
public final static ThreadLocal<HashSet<BaseComponent>> serializedComponents = new ThreadLocal<HashSet<BaseComponent>>()
|
||||
{
|
||||
@Override
|
||||
protected HashSet<BaseComponent> initialValue()
|
||||
{
|
||||
return new HashSet<>();
|
||||
}
|
||||
};
|
||||
|
||||
public static BaseComponent[] parse(String json)
|
||||
{
|
||||
if ( json.startsWith( "[" ) )
|
||||
@@ -64,6 +74,11 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
||||
@Override
|
||||
public JsonElement serialize(BaseComponent src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return context.serialize( src, src.getClass() );
|
||||
try {
|
||||
return context.serialize( src, src.getClass() );
|
||||
} finally
|
||||
{
|
||||
serializedComponents.get().clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user