✨ 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.Description;
|
||||||
import cloud.commandframework.arguments.CommandArgument;
|
import cloud.commandframework.arguments.CommandArgument;
|
||||||
import cloud.commandframework.arguments.flags.CommandFlag;
|
import cloud.commandframework.arguments.flags.CommandFlag;
|
||||||
|
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||||
import cloud.commandframework.arguments.parser.ArgumentParser;
|
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||||
import cloud.commandframework.arguments.parser.ParserParameter;
|
import cloud.commandframework.arguments.parser.ParserParameter;
|
||||||
import cloud.commandframework.arguments.parser.ParserParameters;
|
import cloud.commandframework.arguments.parser.ParserParameters;
|
||||||
import cloud.commandframework.arguments.parser.StandardParameters;
|
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.execution.CommandExecutionHandler;
|
||||||
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
|
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
|
||||||
import cloud.commandframework.meta.CommandMeta;
|
import cloud.commandframework.meta.CommandMeta;
|
||||||
|
|
@ -49,6 +52,8 @@ import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -63,6 +68,8 @@ public final class AnnotationParser<C> {
|
||||||
|
|
||||||
private final CommandManager<C> manager;
|
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, 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 Class<C> commandSenderClass;
|
||||||
private final MetaFactory metaFactory;
|
private final MetaFactory metaFactory;
|
||||||
private final FlagExtractor flagExtractor;
|
private final FlagExtractor flagExtractor;
|
||||||
|
|
@ -86,9 +93,11 @@ public final class AnnotationParser<C> {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.metaFactory = new MetaFactory(this, metaMapper);
|
this.metaFactory = new MetaFactory(this, metaMapper);
|
||||||
this.annotationMappers = new HashMap<>();
|
this.annotationMappers = new HashMap<>();
|
||||||
|
this.preprocessorMappers = new HashMap<>();
|
||||||
this.flagExtractor = new FlagExtractor(manager);
|
this.flagExtractor = new FlagExtractor(manager);
|
||||||
this.registerAnnotationMapper(CommandDescription.class, d ->
|
this.registerAnnotationMapper(CommandDescription.class, d ->
|
||||||
ParserParameters.single(StandardParameters.DESCRIPTION, d.value()));
|
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);
|
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
|
* Scan a class instance of {@link CommandMethod} annotations and attempt to
|
||||||
* compile them into {@link Command} instances
|
* compile them into {@link Command} instances
|
||||||
|
|
@ -315,7 +339,22 @@ public final class AnnotationParser<C> {
|
||||||
} else {
|
} else {
|
||||||
argumentBuilder.asRequired();
|
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>,
|
@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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -357,9 +357,20 @@ public final class CommandTree<C> {
|
||||||
final CommandArgument<C, ?> argument = child.getValue();
|
final CommandArgument<C, ?> argument = child.getValue();
|
||||||
final CommandContext.ArgumentTiming argumentTiming = commandContext.createTiming(argument);
|
final CommandContext.ArgumentTiming argumentTiming = commandContext.createTiming(argument);
|
||||||
|
|
||||||
|
// START: Parsing
|
||||||
argumentTiming.setStart(System.nanoTime());
|
argumentTiming.setStart(System.nanoTime());
|
||||||
final ArgumentParseResult<?> result = argument.getParser().parse(commandContext, commandQueue);
|
final ArgumentParseResult<?> result;
|
||||||
|
final ArgumentParseResult<Boolean> preParseResult = child.getValue().preprocess(
|
||||||
|
commandContext,
|
||||||
|
commandQueue
|
||||||
|
);
|
||||||
|
if (!preParseResult.getFailure().isPresent() && preParseResult.getParsedValue().orElse(false)) {
|
||||||
|
result = argument.getParser().parse(commandContext, commandQueue);
|
||||||
|
} else {
|
||||||
|
result = preParseResult;
|
||||||
|
}
|
||||||
argumentTiming.setEnd(System.nanoTime(), result.getFailure().isPresent());
|
argumentTiming.setEnd(System.nanoTime(), result.getFailure().isPresent());
|
||||||
|
// END: Parsing
|
||||||
|
|
||||||
if (result.getParsedValue().isPresent()) {
|
if (result.getParsedValue().isPresent()) {
|
||||||
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
||||||
|
|
@ -476,6 +487,19 @@ public final class CommandTree<C> {
|
||||||
} else if (commandQueue.peek().isEmpty()) {
|
} else if (commandQueue.peek().isEmpty()) {
|
||||||
return child.getValue().getSuggestionsProvider().apply(commandContext, commandQueue.remove());
|
return child.getValue().getSuggestionsProvider().apply(commandContext, commandQueue.remove());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// START: Preprocessing
|
||||||
|
final ArgumentParseResult<Boolean> preParseResult = child.getValue().preprocess(
|
||||||
|
commandContext,
|
||||||
|
commandQueue
|
||||||
|
);
|
||||||
|
if (preParseResult.getFailure().isPresent() || !preParseResult.getParsedValue().orElse(false)) {
|
||||||
|
final String value = commandQueue.peek() == null ? "" : commandQueue.peek();
|
||||||
|
return child.getValue().getSuggestionsProvider().apply(commandContext, value);
|
||||||
|
}
|
||||||
|
// END: Preprocessing
|
||||||
|
|
||||||
|
// START: Parsing
|
||||||
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
||||||
if (result.getParsedValue().isPresent()) {
|
if (result.getParsedValue().isPresent()) {
|
||||||
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
||||||
|
|
@ -484,6 +508,7 @@ public final class CommandTree<C> {
|
||||||
final String value = commandQueue.peek() == null ? "" : commandQueue.peek();
|
final String value = commandQueue.peek() == null ? "" : commandQueue.peek();
|
||||||
return child.getValue().getSuggestionsProvider().apply(commandContext, value);
|
return child.getValue().getSuggestionsProvider().apply(commandContext, value);
|
||||||
}
|
}
|
||||||
|
// END: Parsing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* There are 0 or more static arguments as children. No variable child arguments are present */
|
/* There are 0 or more static arguments as children. No variable child arguments are present */
|
||||||
|
|
|
||||||
|
|
@ -33,8 +33,12 @@ import io.leangen.geantyref.TypeToken;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Queue;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
@ -82,6 +86,12 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
* Suggestion provider
|
* Suggestion provider
|
||||||
*/
|
*/
|
||||||
private final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider;
|
private final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider;
|
||||||
|
/**
|
||||||
|
* Argument preprocessors that allows for extensions to existing argument types
|
||||||
|
* without having to update all parsers
|
||||||
|
*/
|
||||||
|
private final Collection<BiFunction<@NonNull CommandContext<C>,
|
||||||
|
@NonNull Queue<@NonNull String>, @NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors;
|
||||||
/**
|
/**
|
||||||
* Whether or not the argument has been used before
|
* Whether or not the argument has been used before
|
||||||
*/
|
*/
|
||||||
|
|
@ -89,6 +99,41 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
|
|
||||||
private Command<C> owningCommand;
|
private Command<C> owningCommand;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new command argument
|
||||||
|
*
|
||||||
|
* @param required Whether or not the argument is required
|
||||||
|
* @param name The argument name
|
||||||
|
* @param parser The argument parser
|
||||||
|
* @param defaultValue Default value used when no value is provided by the command sender
|
||||||
|
* @param valueType Type produced by the parser
|
||||||
|
* @param suggestionsProvider Suggestions provider
|
||||||
|
* @param argumentPreprocessors Argument preprocessors
|
||||||
|
*/
|
||||||
|
public CommandArgument(
|
||||||
|
final boolean required,
|
||||||
|
final @NonNull String name,
|
||||||
|
final @NonNull ArgumentParser<C, T> parser,
|
||||||
|
final @NonNull String defaultValue,
|
||||||
|
final @NonNull TypeToken<T> valueType,
|
||||||
|
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
|
||||||
|
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||||
|
@NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
|
||||||
|
) {
|
||||||
|
this.required = required;
|
||||||
|
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||||
|
if (!NAME_PATTERN.asPredicate().test(name)) {
|
||||||
|
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||||
|
}
|
||||||
|
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
this.valueType = valueType;
|
||||||
|
this.suggestionsProvider = suggestionsProvider == null
|
||||||
|
? buildDefaultSuggestionsProvider(this)
|
||||||
|
: suggestionsProvider;
|
||||||
|
this.argumentPreprocessors = new LinkedList<>(argumentPreprocessors);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new command argument
|
* Construct a new command argument
|
||||||
*
|
*
|
||||||
|
|
@ -107,17 +152,7 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
final @NonNull TypeToken<T> valueType,
|
final @NonNull TypeToken<T> valueType,
|
||||||
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
|
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
|
||||||
) {
|
) {
|
||||||
this.required = required;
|
this(required, name, parser, defaultValue, valueType, suggestionsProvider, Collections.emptyList());
|
||||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
|
||||||
if (!NAME_PATTERN.asPredicate().test(name)) {
|
|
||||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
|
||||||
}
|
|
||||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
this.valueType = valueType;
|
|
||||||
this.suggestionsProvider = suggestionsProvider == null
|
|
||||||
? buildDefaultSuggestionsProvider(this)
|
|
||||||
: suggestionsProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -229,6 +264,48 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
return String.format("%s{name=%s}", this.getClass().getSimpleName(), this.name);
|
return String.format("%s{name=%s}", this.getClass().getSimpleName(), this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new preprocessor. If all preprocessor has succeeding {@link ArgumentParseResult results}
|
||||||
|
* that all return {@code true}, the argument will be passed onto the parser.
|
||||||
|
* <p>
|
||||||
|
* It is important that the preprocessor doesn't pop any input. Instead, it should only peek.
|
||||||
|
*
|
||||||
|
* @param preprocessor Preprocessor
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
|
public @NonNull CommandArgument<C, T> addPreprocessor(
|
||||||
|
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<String>,
|
||||||
|
@NonNull ArgumentParseResult<Boolean>> preprocessor
|
||||||
|
) {
|
||||||
|
this.argumentPreprocessors.add(preprocessor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preprocess command input. This will immediately forward any failed argument parse results.
|
||||||
|
* If none fails, a {@code true} result will be returned
|
||||||
|
*
|
||||||
|
* @param context Command context
|
||||||
|
* @param input Remaining command input. None will be popped
|
||||||
|
* @return Parsing error, or argument containing {@code true}
|
||||||
|
*/
|
||||||
|
public @NonNull ArgumentParseResult<Boolean> preprocess(
|
||||||
|
final @NonNull CommandContext<C> context,
|
||||||
|
final @NonNull Queue<String> input
|
||||||
|
) {
|
||||||
|
for (final BiFunction<@NonNull CommandContext<C>, @NonNull Queue<String>,
|
||||||
|
@NonNull ArgumentParseResult<Boolean>> preprocessor : this.argumentPreprocessors) {
|
||||||
|
final ArgumentParseResult<Boolean> result = preprocessor.apply(
|
||||||
|
context,
|
||||||
|
input
|
||||||
|
);
|
||||||
|
if (result.getFailure().isPresent()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ArgumentParseResult.success(true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the owning command
|
* Get the owning command
|
||||||
*
|
*
|
||||||
|
|
@ -376,6 +453,9 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
private String defaultValue = "";
|
private String defaultValue = "";
|
||||||
private BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider;
|
private BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider;
|
||||||
|
|
||||||
|
private final Collection<BiFunction<@NonNull CommandContext<C>,
|
||||||
|
@NonNull String, @NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors = new LinkedList<>();
|
||||||
|
|
||||||
protected Builder(
|
protected Builder(
|
||||||
final @NonNull TypeToken<T> valueType,
|
final @NonNull TypeToken<T> valueType,
|
||||||
final @NonNull String name
|
final @NonNull String name
|
||||||
|
|
@ -489,8 +569,13 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
||||||
if (this.suggestionsProvider == null) {
|
if (this.suggestionsProvider == null) {
|
||||||
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
|
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
|
||||||
}
|
}
|
||||||
return new CommandArgument<>(this.required, this.name, this.parser,
|
return new CommandArgument<>(
|
||||||
this.defaultValue, this.valueType, this.suggestionsProvider
|
this.required,
|
||||||
|
this.name,
|
||||||
|
this.parser,
|
||||||
|
this.defaultValue,
|
||||||
|
this.valueType,
|
||||||
|
this.suggestionsProvider
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,127 @@
|
||||||
|
//
|
||||||
|
// 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.arguments.preprocessor;
|
||||||
|
|
||||||
|
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||||
|
import cloud.commandframework.context.CommandContext;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command preprocessor that filters based on regular expressions
|
||||||
|
*
|
||||||
|
* @param <C> Command sender type
|
||||||
|
*/
|
||||||
|
public final class RegexPreprocessor<C> implements BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||||
|
@NonNull ArgumentParseResult<Boolean>> {
|
||||||
|
|
||||||
|
private final String rawPattern;
|
||||||
|
private final Predicate<@NonNull String> predicate;
|
||||||
|
|
||||||
|
private RegexPreprocessor(final @NonNull String pattern) {
|
||||||
|
this.rawPattern = pattern;
|
||||||
|
this.predicate = Pattern.compile(pattern).asPredicate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new preprocessor
|
||||||
|
*
|
||||||
|
* @param pattern Regular expression
|
||||||
|
* @param <C> Command sender type
|
||||||
|
* @return Preprocessor instance
|
||||||
|
*/
|
||||||
|
public static <C> @NonNull RegexPreprocessor<C> of(final @NonNull String pattern) {
|
||||||
|
return new RegexPreprocessor<>(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull ArgumentParseResult<Boolean> apply(
|
||||||
|
@NonNull final CommandContext<C> context, @NonNull final Queue<@NonNull String> strings
|
||||||
|
) {
|
||||||
|
final String head = strings.peek();
|
||||||
|
if (head == null) {
|
||||||
|
throw new NullPointerException("No input");
|
||||||
|
}
|
||||||
|
if (predicate.test(head)) {
|
||||||
|
return ArgumentParseResult.success(true);
|
||||||
|
}
|
||||||
|
return ArgumentParseResult.failure(
|
||||||
|
new RegexValidationException(
|
||||||
|
this.rawPattern,
|
||||||
|
head
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown when input fails regex matching in {@link RegexPreprocessor}
|
||||||
|
*/
|
||||||
|
public static final class RegexValidationException extends IllegalArgumentException {
|
||||||
|
|
||||||
|
private final String pattern;
|
||||||
|
private final String failedString;
|
||||||
|
|
||||||
|
private RegexValidationException(
|
||||||
|
@NonNull final String pattern,
|
||||||
|
@NonNull final String failedString
|
||||||
|
) {
|
||||||
|
this.pattern = pattern;
|
||||||
|
this.failedString = failedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return String.format(
|
||||||
|
"Input '%s' does not match the required pattern '%s'",
|
||||||
|
failedString,
|
||||||
|
pattern
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the string that failed the verification
|
||||||
|
*
|
||||||
|
* @return Failed string
|
||||||
|
*/
|
||||||
|
public @NonNull String getFailedString() {
|
||||||
|
return this.failedString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the pattern that caused the string to fail
|
||||||
|
*
|
||||||
|
* @return Pattern
|
||||||
|
*/
|
||||||
|
public @NonNull String getPattern() {
|
||||||
|
return this.pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pre-made argument preprocessors
|
||||||
|
*/
|
||||||
|
package cloud.commandframework.arguments.preprocessor;
|
||||||
|
|
@ -135,8 +135,12 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public @NonNull BooleanArgument<C> build() {
|
public @NonNull BooleanArgument<C> build() {
|
||||||
return new BooleanArgument<>(this.isRequired(), this.getName(), this.liberal,
|
return new BooleanArgument<>(
|
||||||
this.getDefaultValue(), this.getSuggestionsProvider()
|
this.isRequired(),
|
||||||
|
this.getName(),
|
||||||
|
this.liberal,
|
||||||
|
this.getDefaultValue(),
|
||||||
|
this.getSuggestionsProvider()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ package cloud.commandframework;
|
||||||
|
|
||||||
import cloud.commandframework.arguments.CommandArgument;
|
import cloud.commandframework.arguments.CommandArgument;
|
||||||
import cloud.commandframework.arguments.compound.ArgumentPair;
|
import cloud.commandframework.arguments.compound.ArgumentPair;
|
||||||
|
import cloud.commandframework.arguments.preprocessor.RegexPreprocessor;
|
||||||
import cloud.commandframework.arguments.standard.EnumArgument;
|
import cloud.commandframework.arguments.standard.EnumArgument;
|
||||||
import cloud.commandframework.arguments.standard.FloatArgument;
|
import cloud.commandframework.arguments.standard.FloatArgument;
|
||||||
import cloud.commandframework.arguments.standard.IntegerArgument;
|
import cloud.commandframework.arguments.standard.IntegerArgument;
|
||||||
|
|
@ -134,6 +135,14 @@ class CommandTreeTest {
|
||||||
.handler(c -> {
|
.handler(c -> {
|
||||||
System.out.printf("%f\n", c.<Float>get("num"));
|
System.out.printf("%f\n", c.<Float>get("num"));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
/* Build command for testing preprocessing */
|
||||||
|
manager.command(manager.commandBuilder("preprocess")
|
||||||
|
.argument(
|
||||||
|
StringArgument.<TestCommandSender>of("argument")
|
||||||
|
.addPreprocessor(RegexPreprocessor.of("[A-Za-z]{3,5}"))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -252,6 +261,15 @@ class CommandTreeTest {
|
||||||
manager.executeCommand(new TestCommandSender(), "float 100").join();
|
manager.executeCommand(new TestCommandSender(), "float 100").join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPreprocessors() {
|
||||||
|
manager.executeCommand(new TestCommandSender(), "preprocess abc").join();
|
||||||
|
Assertions.assertThrows(
|
||||||
|
CompletionException.class,
|
||||||
|
() -> manager.executeCommand(new TestCommandSender(), "preprocess ab").join()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final class SpecificCommandSender extends TestCommandSender {
|
public static final class SpecificCommandSender extends TestCommandSender {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import cloud.commandframework.annotations.CommandMethod;
|
||||||
import cloud.commandframework.annotations.CommandPermission;
|
import cloud.commandframework.annotations.CommandPermission;
|
||||||
import cloud.commandframework.annotations.Confirmation;
|
import cloud.commandframework.annotations.Confirmation;
|
||||||
import cloud.commandframework.annotations.Flag;
|
import cloud.commandframework.annotations.Flag;
|
||||||
|
import cloud.commandframework.annotations.Regex;
|
||||||
import cloud.commandframework.annotations.specifier.Greedy;
|
import cloud.commandframework.annotations.specifier.Greedy;
|
||||||
import cloud.commandframework.arguments.CommandArgument;
|
import cloud.commandframework.arguments.CommandArgument;
|
||||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||||
|
|
@ -440,4 +441,16 @@ public final class ExamplePlugin extends JavaPlugin {
|
||||||
player.sendMessage(ChatColor.GREEN + String.format("You have been given %d x %s", number, material));
|
player.sendMessage(ChatColor.GREEN + String.format("You have been given %d x %s", number, material));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CommandMethod("example pay <money>")
|
||||||
|
@CommandDescription("Command to test the preprocessing system")
|
||||||
|
private void commandPay(
|
||||||
|
final @NonNull CommandSender sender,
|
||||||
|
final @Argument("money") @Regex("(?=.*?\\d)^\\$?(([1-9]\\d{0,2}(,\\d{3})*)|\\d+)?(\\.\\d{1,2})?$") String money
|
||||||
|
) {
|
||||||
|
bukkitAudiences.sender(sender).sendMessage(
|
||||||
|
Component.text().append(Component.text("You have been given ", NamedTextColor.AQUA))
|
||||||
|
.append(Component.text(money, NamedTextColor.GOLD))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue