Skip to content

Commit

Permalink
fix EntitySelector method discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Dec 8, 2022
1 parent a1d208f commit c67d194
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private static ArgumentType<Object> createEntityArgument(final boolean single, f
}

private static final class EntityArgumentParseFunction implements WrappedBrigadierParser.ParseFunction<Object> {

static final EntityArgumentParseFunction INSTANCE = new EntityArgumentParseFunction();

@Override
Expand Down Expand Up @@ -132,6 +133,7 @@ public Object apply(
}

private abstract static class SelectorParser<C, T> implements ArgumentParser<C, T>, SelectorMapper<T> {

protected static final Supplier<Object> NO_PLAYERS_EXCEPTION_TYPE =
Suppliers.memoize(() -> findExceptionType("argument.entity.notfound.player"));
protected static final Supplier<Object> NO_ENTITIES_EXCEPTION_TYPE =
Expand All @@ -141,6 +143,7 @@ private abstract static class SelectorParser<C, T> implements ArgumentParser<C,

// Hide brigadier references in inner class
protected static final class Thrower {

private final Object type;

Thrower(final Object simpleCommandExceptionType) {
Expand Down Expand Up @@ -228,12 +231,14 @@ private static Object findExceptionType(final String type) {
}

abstract static class EntitySelectorParser<C, T> extends SelectorParser<C, T> {

protected EntitySelectorParser(final boolean single) {
super(single, false);
}
}

abstract static class PlayerSelectorParser<C, T> extends SelectorParser<C, T> {

protected PlayerSelectorParser(final boolean single) {
super(single, true);
}
Expand All @@ -258,6 +263,7 @@ protected List<String> legacySuggestions(
}

private static class ModernSelectorParser<C, T> implements ArgumentParser<C, T> {

private final ArgumentParser<C, Object> wrappedBrigadierParser;
private final SelectorMapper<T> mapper;

Expand Down Expand Up @@ -326,12 +332,14 @@ public List<String> suggestions(
}

static final class EntitySelectorWrapper {

private static volatile @MonotonicNonNull Methods methods;

private final CommandContext<?> commandContext;
private final Object selector;

private static final class Methods {

private @MonotonicNonNull Method getBukkitEntity;
private @MonotonicNonNull Method entity;
private @MonotonicNonNull Method player;
Expand All @@ -342,7 +350,9 @@ private static final class Methods {
final Object nativeSender = commandContext.get(WrappedBrigadierParser.COMMAND_CONTEXT_BRIGADIER_NATIVE_SENDER);
final Class<?> nativeSenderClass = nativeSender.getClass();
for (final Method method : selector.getClass().getDeclaredMethods()) {
if (method.getParameterCount() != 1 || !method.getParameterTypes()[0].equals(nativeSenderClass)) {
if (method.getParameterCount() != 1
|| !method.getParameterTypes()[0].equals(nativeSenderClass)
|| !Modifier.isPublic(method.getModifiers())) {
continue;
}

Expand Down Expand Up @@ -475,6 +485,7 @@ List<Player> players() {

@FunctionalInterface
interface ReflectiveOperation<T> {

T run() throws ReflectiveOperationException;
}

Expand All @@ -494,6 +505,7 @@ private static <T> T reflectiveOperation(final ReflectiveOperation<T> op) {

@FunctionalInterface
interface SelectorMapper<T> {

T mapResult(String input, EntitySelectorWrapper wrapper) throws Exception; // throws CommandSyntaxException
}

Expand Down

0 comments on commit c67d194

Please sign in to comment.