✨ Add named suggestion providers
This allows for pre-registration of command suggestion providers, that can then be used in annotated command methods.
This commit is contained in:
parent
37e0b4e91b
commit
3c7bd63f07
6 changed files with 112 additions and 8 deletions
|
|
@ -52,7 +52,9 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
|
@ -300,7 +302,7 @@ public final class AnnotationParser<C> {
|
|||
final TypeToken<?> token = TypeToken.get(parameter.getParameterizedType());
|
||||
final ParserParameters parameters = this.manager.getParserRegistry()
|
||||
.parseAnnotations(token, annotations);
|
||||
|
||||
/* Create the argument parser */
|
||||
final ArgumentParser<C, ?> parser;
|
||||
if (argumentPair.getArgument().parserName().isEmpty()) {
|
||||
parser = this.manager.getParserRegistry()
|
||||
|
|
@ -323,17 +325,19 @@ public final class AnnotationParser<C> {
|
|||
token.toString()
|
||||
)));
|
||||
}
|
||||
|
||||
/* Check whether or not the corresponding method parameter actually exists */
|
||||
if (syntaxFragment == null || syntaxFragment.getArgumentMode() == ArgumentMode.LITERAL) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Invalid command argument '%s' in method '%s': "
|
||||
+ "Missing syntax mapping", argumentPair.getArgument().value(), method.getName()));
|
||||
}
|
||||
final Argument argument = argumentPair.getArgument();
|
||||
/* Create the argument builder */
|
||||
@SuppressWarnings("ALL") final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(
|
||||
parameter.getType(),
|
||||
argument.value()
|
||||
);
|
||||
/* Set the argument requirement status */
|
||||
if (syntaxFragment.getArgumentMode() == ArgumentMode.OPTIONAL) {
|
||||
if (argument.defaultValue().isEmpty()) {
|
||||
argumentBuilder.asOptional();
|
||||
|
|
@ -343,9 +347,21 @@ public final class AnnotationParser<C> {
|
|||
} else {
|
||||
argumentBuilder.asRequired();
|
||||
}
|
||||
|
||||
/* Check whether or not a suggestion provider should be set */
|
||||
if (!argument.suggestions().isEmpty()) {
|
||||
final String suggestionProviderName = argument.suggestions();
|
||||
final Optional<BiFunction<CommandContext<C>, String, List<String>>> suggestionsFunction =
|
||||
this.manager.getParserRegistry().getSuggestionProvider(suggestionProviderName);
|
||||
argumentBuilder.withSuggestionsProvider(
|
||||
suggestionsFunction.orElseThrow(() ->
|
||||
new IllegalArgumentException(String.format(
|
||||
"There is no suggestion provider with name '%s'. Did you forget to register it?",
|
||||
suggestionProviderName
|
||||
)))
|
||||
);
|
||||
}
|
||||
/* Build the argument */
|
||||
final CommandArgument<C, ?> builtArgument = argumentBuilder.manager(this.manager).withParser(parser).build();
|
||||
|
||||
/* Add preprocessors */
|
||||
for (final Annotation annotation : annotations) {
|
||||
@SuppressWarnings("ALL") final Function preprocessorMapper =
|
||||
|
|
@ -357,7 +373,7 @@ public final class AnnotationParser<C> {
|
|||
builtArgument.addPreprocessor(preprocessor);
|
||||
}
|
||||
}
|
||||
|
||||
/* Yay, we're done */
|
||||
return builtArgument;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import java.lang.annotation.ElementType;
|
|||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* Annotation used to indicate that a method parameter is a command argument
|
||||
|
|
@ -49,6 +50,22 @@ public @interface Argument {
|
|||
*/
|
||||
String parserName() default "";
|
||||
|
||||
/**
|
||||
* Name of the suggestions provider to use. If the string is left empty, the default
|
||||
* provider for the argument parser will be used. Otherwise,
|
||||
* the {@link cloud.commandframework.arguments.parser.ParserRegistry} instance in the
|
||||
* {@link cloud.commandframework.CommandManager} will be queried for a matching suggestion provider.
|
||||
* <p>
|
||||
* For this to work, the suggestion needs to be registered in the parser registry. To do this, use
|
||||
* {@link cloud.commandframework.arguments.parser.ParserRegistry#registerSuggestionProvider(String, BiFunction)}.
|
||||
* The registry instance can be retrieved using {@link cloud.commandframework.CommandManager#getParserRegistry()}.
|
||||
*
|
||||
* @return The name of the suggestion provider, or {@code ""} if the default suggestion provider for the argument parser
|
||||
* should be used instead
|
||||
* @since 1.1.0
|
||||
*/
|
||||
String suggestions() default "";
|
||||
|
||||
/**
|
||||
* Get the default value
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue