#3835: Check for dialog loops
This commit is contained in:
parent
363003d8c7
commit
d8f9d81b30
@ -13,6 +13,9 @@ import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Set;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.md_5.bungee.api.dialog.ConfirmationDialog;
|
||||
import net.md_5.bungee.api.dialog.Dialog;
|
||||
@ -28,7 +31,7 @@ import net.md_5.bungee.chat.VersionedComponentSerializer;
|
||||
@RequiredArgsConstructor
|
||||
public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerializer<Dialog>
|
||||
{
|
||||
|
||||
private static final ThreadLocal<Set<Dialog>> serializedDialogs = new ThreadLocal<Set<Dialog>>();
|
||||
private static final BiMap<String, Class<? extends Dialog>> TYPES;
|
||||
private final VersionedComponentSerializer serializer;
|
||||
|
||||
@ -102,6 +105,17 @@ public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerialize
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
||||
boolean first = serializedDialogs.get() == null;
|
||||
if ( first )
|
||||
{
|
||||
serializedDialogs.set( Collections.newSetFromMap( new IdentityHashMap<Dialog, Boolean>() ) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Preconditions.checkArgument( !serializedDialogs.get().contains( src ), "Dialog loop" );
|
||||
serializedDialogs.get().add( src );
|
||||
|
||||
Class<? extends Dialog> realType = src.getClass();
|
||||
String type = TYPES.inverse().get( realType );
|
||||
Preconditions.checkArgument( type != null, "Unknown type %s", typeOfSrc );
|
||||
@ -113,5 +127,13 @@ public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerialize
|
||||
object.asMap().putAll( base.asMap() );
|
||||
|
||||
return object;
|
||||
} finally
|
||||
{
|
||||
serializedDialogs.get().remove( src );
|
||||
if ( first )
|
||||
{
|
||||
serializedDialogs.set( null );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user