Make CommandMethod applicable to class
This commit is contained in:
parent
6cd1105642
commit
730e78e212
3 changed files with 21 additions and 4 deletions
|
|
@ -406,13 +406,16 @@ public final class AnnotationParser<C> {
|
||||||
final @NonNull Object instance,
|
final @NonNull Object instance,
|
||||||
final @NonNull Collection<@NonNull CommandMethodPair> methodPairs
|
final @NonNull Collection<@NonNull CommandMethodPair> methodPairs
|
||||||
) {
|
) {
|
||||||
|
final CommandMethod classCommandMethod = instance.getClass().getAnnotation(CommandMethod.class);
|
||||||
|
final String syntaxPrefix = classCommandMethod == null ? "" : (classCommandMethod.value() + " ");
|
||||||
final Collection<Command<C>> commands = new ArrayList<>();
|
final Collection<Command<C>> commands = new ArrayList<>();
|
||||||
for (final CommandMethodPair commandMethodPair : methodPairs) {
|
for (final CommandMethodPair commandMethodPair : methodPairs) {
|
||||||
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
|
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
|
||||||
final Method method = commandMethodPair.getMethod();
|
final Method method = commandMethodPair.getMethod();
|
||||||
final List<SyntaxFragment> tokens = this.syntaxParser.apply(commandMethod.value());
|
final String syntax = syntaxPrefix + commandMethod.value();
|
||||||
|
final List<SyntaxFragment> tokens = this.syntaxParser.apply(syntax);
|
||||||
/* Determine command name */
|
/* Determine command name */
|
||||||
final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0];
|
final String commandToken = syntax.split(" ")[0].split("\\|")[0];
|
||||||
@SuppressWarnings("rawtypes") final CommandManager manager = this.manager;
|
@SuppressWarnings("rawtypes") final CommandManager manager = this.manager;
|
||||||
final SimpleCommandMeta.Builder metaBuilder = SimpleCommandMeta.builder()
|
final SimpleCommandMeta.Builder metaBuilder = SimpleCommandMeta.builder()
|
||||||
.with(this.metaFactory.apply(method));
|
.with(this.metaFactory.apply(method));
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import java.lang.annotation.Target;
|
||||||
* Used to declare a class method as a command method
|
* Used to declare a class method as a command method
|
||||||
*/
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.METHOD)
|
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||||
public @interface CommandMethod {
|
public @interface CommandMethod {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ import cloud.commandframework.context.CommandContext;
|
||||||
import cloud.commandframework.meta.SimpleCommandMeta;
|
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||||
import io.leangen.geantyref.TypeToken;
|
import io.leangen.geantyref.TypeToken;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
@ -91,7 +92,9 @@ class AnnotationParserTest {
|
||||||
(injector, builder) -> builder.argument(IntegerArgument.of(injector.value()))
|
(injector, builder) -> builder.argument(IntegerArgument.of(injector.value()))
|
||||||
);
|
);
|
||||||
/* Parse the class. Required for both testMethodConstruction() and testNamedSuggestionProvider() */
|
/* Parse the class. Required for both testMethodConstruction() and testNamedSuggestionProvider() */
|
||||||
commands = annotationParser.parse(this);
|
commands = new ArrayList<>();
|
||||||
|
commands.addAll(annotationParser.parse(this));
|
||||||
|
commands.addAll(annotationParser.parse(new ClassCommandMethod()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -104,6 +107,7 @@ class AnnotationParserTest {
|
||||||
manager.executeCommand(new TestCommandSender(), "test 101").join());
|
manager.executeCommand(new TestCommandSender(), "test 101").join());
|
||||||
manager.executeCommand(new TestCommandSender(), "flagcommand -p").join();
|
manager.executeCommand(new TestCommandSender(), "flagcommand -p").join();
|
||||||
manager.executeCommand(new TestCommandSender(), "flagcommand --print --word peanut").join();
|
manager.executeCommand(new TestCommandSender(), "flagcommand --print --word peanut").join();
|
||||||
|
manager.executeCommand(new TestCommandSender(), "class method").join();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -253,6 +257,16 @@ class AnnotationParserTest {
|
||||||
System.out.printf("Injected value: %s\n", injectableValue.toString());
|
System.out.printf("Injected value: %s\n", injectableValue.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CommandMethod("class")
|
||||||
|
private static class ClassCommandMethod {
|
||||||
|
|
||||||
|
@CommandMethod("method")
|
||||||
|
public void annotatedMethod() {
|
||||||
|
System.out.println("kekw");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@CommandPermission("some.permission")
|
@CommandPermission("some.permission")
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue