mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-01-19 08:55:25 +01:00
Check super types and interfaces on PlotPlayer#from
fixes https://github.com/IntellectualSites/FastAsyncWorldEdit/issues/1140
This commit is contained in:
parent
8e23b10f7c
commit
7898313b0b
@ -76,6 +76,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -83,6 +85,7 @@ import java.util.HashSet;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -126,15 +129,36 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> PlotPlayer<T> from(final @NonNull T object) {
|
public static <T> PlotPlayer<T> from(final @NonNull T object) {
|
||||||
if (!converters.containsKey(object.getClass())) {
|
// fast path
|
||||||
|
if (converters.containsKey(object.getClass())) {
|
||||||
|
return converters.get(object.getClass()).convert(object);
|
||||||
|
}
|
||||||
|
// slow path, meant to only run once per object#getClass instance
|
||||||
|
Queue<Class<?>> toVisit = new ArrayDeque<>();
|
||||||
|
toVisit.add(object.getClass());
|
||||||
|
Class<?> current;
|
||||||
|
while ((current = toVisit.poll()) != null) {
|
||||||
|
PlotPlayerConverter converter = converters.get(current);
|
||||||
|
if (converter != null) {
|
||||||
|
if (current != object.getClass()) {
|
||||||
|
// register shortcut for this sub type to avoid further loops
|
||||||
|
converters.put(object.getClass(), converter);
|
||||||
|
LOGGER.info("Registered {} as with converter for {}", object.getClass(), current);
|
||||||
|
}
|
||||||
|
return converter.convert(object);
|
||||||
|
}
|
||||||
|
// no converter found yet
|
||||||
|
if (current.getSuperclass() != null) {
|
||||||
|
toVisit.add(current.getSuperclass()); // add super class if available
|
||||||
|
}
|
||||||
|
toVisit.addAll(Arrays.asList(current.getInterfaces())); // add interfaces
|
||||||
|
}
|
||||||
throw new IllegalArgumentException(String
|
throw new IllegalArgumentException(String
|
||||||
.format(
|
.format(
|
||||||
"There is no registered PlotPlayer converter for type %s",
|
"There is no registered PlotPlayer converter for type %s",
|
||||||
object.getClass().getSimpleName()
|
object.getClass().getSimpleName()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
return converters.get(object.getClass()).convert(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> void registerConverter(
|
public static <T> void registerConverter(
|
||||||
final @NonNull Class<T> clazz,
|
final @NonNull Class<T> clazz,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user