✨ Add command argument preprocessors
This commit is contained in:
parent
fcd269b6e7
commit
1f3c3f2bd9
9 changed files with 405 additions and 17 deletions
|
|
@ -28,10 +28,13 @@ import cloud.commandframework.CommandManager;
|
|||
import cloud.commandframework.Description;
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.flags.CommandFlag;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||
import cloud.commandframework.arguments.parser.ParserParameter;
|
||||
import cloud.commandframework.arguments.parser.ParserParameters;
|
||||
import cloud.commandframework.arguments.parser.StandardParameters;
|
||||
import cloud.commandframework.arguments.preprocessor.RegexPreprocessor;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import cloud.commandframework.execution.CommandExecutionHandler;
|
||||
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
|
||||
import cloud.commandframework.meta.CommandMeta;
|
||||
|
|
@ -49,6 +52,8 @@ import java.util.Collection;
|
|||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
|
|
@ -63,6 +68,8 @@ public final class AnnotationParser<C> {
|
|||
|
||||
private final CommandManager<C> manager;
|
||||
private final Map<Class<? extends Annotation>, Function<? extends Annotation, ParserParameters>> annotationMappers;
|
||||
private final Map<Class<? extends Annotation>, Function<? extends Annotation, BiFunction<@NonNull CommandContext<C>,
|
||||
@NonNull Queue<@NonNull String>, @NonNull ArgumentParseResult<Boolean>>>> preprocessorMappers;
|
||||
private final Class<C> commandSenderClass;
|
||||
private final MetaFactory metaFactory;
|
||||
private final FlagExtractor flagExtractor;
|
||||
|
|
@ -86,9 +93,11 @@ public final class AnnotationParser<C> {
|
|||
this.manager = manager;
|
||||
this.metaFactory = new MetaFactory(this, metaMapper);
|
||||
this.annotationMappers = new HashMap<>();
|
||||
this.preprocessorMappers = new HashMap<>();
|
||||
this.flagExtractor = new FlagExtractor(manager);
|
||||
this.registerAnnotationMapper(CommandDescription.class, d ->
|
||||
ParserParameters.single(StandardParameters.DESCRIPTION, d.value()));
|
||||
this.registerPreprocessorMapper(Regex.class, annotation -> RegexPreprocessor.of(annotation.value()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -106,6 +115,21 @@ public final class AnnotationParser<C> {
|
|||
this.annotationMappers.put(annotation, mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a preprocessor mapper
|
||||
*
|
||||
* @param annotation Annotation class
|
||||
* @param preprocessorMapper Preprocessor mapper
|
||||
* @param <A> Annotation type
|
||||
*/
|
||||
public <A extends Annotation> void registerPreprocessorMapper(
|
||||
final @NonNull Class<A> annotation,
|
||||
final @NonNull Function<A, BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||
@NonNull ArgumentParseResult<Boolean>>> preprocessorMapper
|
||||
) {
|
||||
this.preprocessorMappers.put(annotation, preprocessorMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan a class instance of {@link CommandMethod} annotations and attempt to
|
||||
* compile them into {@link Command} instances
|
||||
|
|
@ -315,7 +339,22 @@ public final class AnnotationParser<C> {
|
|||
} else {
|
||||
argumentBuilder.asRequired();
|
||||
}
|
||||
return argumentBuilder.manager(this.manager).withParser(parser).build();
|
||||
|
||||
final CommandArgument<C, ?> builtArgument = argumentBuilder.manager(this.manager).withParser(parser).build();
|
||||
|
||||
/* Add preprocessors */
|
||||
for (final Annotation annotation : annotations) {
|
||||
@SuppressWarnings("ALL") final Function preprocessorMapper =
|
||||
this.preprocessorMappers.get(annotation.annotationType());
|
||||
if (preprocessorMapper != null) {
|
||||
final BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||
@NonNull ArgumentParseResult<Boolean>> preprocessor = (BiFunction<CommandContext<C>,
|
||||
Queue<String>, ArgumentParseResult<Boolean>>) preprocessorMapper.apply(annotation);
|
||||
builtArgument.addPreprocessor(preprocessor);
|
||||
}
|
||||
}
|
||||
|
||||
return builtArgument;
|
||||
}
|
||||
|
||||
@NonNull Map<@NonNull Class<@NonNull ? extends Annotation>,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
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 version of adding {@link cloud.commandframework.arguments.preprocessor.RegexPreprocessor}
|
||||
* as a preprocessor using {@link cloud.commandframework.arguments.CommandArgument#addPreprocessor(BiFunction)}
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Regex {
|
||||
|
||||
/**
|
||||
* Regular expression pattern
|
||||
*
|
||||
* @return Pattern
|
||||
*/
|
||||
@NonNull String value();
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue