Improve the annotated command method code and add more supported annotations

This commit is contained in:
Alexander Söderberg 2020-09-18 21:30:00 +02:00
parent 1e58ca3f13
commit 3f852d068e
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
15 changed files with 392 additions and 59 deletions

View file

@ -30,6 +30,7 @@ import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.parser.ArgumentParser;
import com.intellectualsites.commands.arguments.parser.ParserParameters;
import com.intellectualsites.commands.arguments.parser.StandardParameters;
import com.intellectualsites.commands.execution.CommandExecutionHandler;
import com.intellectualsites.commands.meta.CommandMeta;
@ -42,11 +43,11 @@ import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@ -60,18 +61,61 @@ public final class AnnotationParser<C, M extends CommandMeta> {
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
.asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>").asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]").asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>")
.asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]")
.asPredicate();
private final Function<ParserParameters, M> metaMapper;
private final CommandManager<C, M> manager;
private final Map<Class<? extends Annotation>, Function<? extends Annotation, ParserParameters>> annotationMappers;
private final Class<C> commandSenderClass;
/**
* Construct a new annotation parser
*
* @param manager Command manager instance
* @param commandSenderClass Command sender class
* @param metaMapper Function that is used to create {@link CommandMeta} instances from annotations on the
* command methods. These annotations will be mapped to
* {@link com.intellectualsites.commands.arguments.parser.ParserParameter}. Mappers for the
* parser parameters can be registered using {@link #registerAnnotationMapper(Class, Function)}
*/
public AnnotationParser(@Nonnull final CommandManager<C, M> manager) {
public AnnotationParser(@Nonnull final CommandManager<C, M> manager,
@Nonnull final Class<C> commandSenderClass,
@Nonnull final Function<ParserParameters, M> metaMapper) {
this.commandSenderClass = commandSenderClass;
this.manager = manager;
this.metaMapper = metaMapper;
this.annotationMappers = Maps.newHashMap();
this.registerAnnotationMapper(Description.class, d -> ParserParameters.single(StandardParameters.DESCRIPTION, d.value()));
}
/**
* Register an annotation mapper
*
* @param annotation Annotation class
* @param mapper Mapping function
* @param <A> Annotation type
*/
public <A extends Annotation> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
@Nonnull final Function<A, ParserParameters> mapper) {
this.annotationMappers.put(annotation, mapper);
}
@Nonnull
private M createMeta(@Nonnull final Annotation[] annotations) {
final ParserParameters parameters = ParserParameters.empty();
for (final Annotation annotation : annotations) {
@SuppressWarnings("ALL") final Function function = this.annotationMappers.get(annotation.annotationType());
if (function == null) {
continue;
}
//noinspection unchecked
parameters.merge((ParserParameters) function.apply(annotation));
}
return this.metaMapper.apply(parameters);
}
/**
@ -84,13 +128,16 @@ public final class AnnotationParser<C, M extends CommandMeta> {
*/
@Nonnull
public <T> Collection<Command<C, M>> parse(@Nonnull final T instance) {
final Method[] methods = instance.getClass().getMethods();
final Method[] methods = instance.getClass().getDeclaredMethods();
final Collection<CommandMethodPair> commandMethodPairs = new ArrayList<>();
for (final Method method : methods) {
final CommandMethod commandMethod = method.getAnnotation(CommandMethod.class);
if (commandMethod == null) {
continue;
}
if (!method.isAccessible()) {
method.setAccessible(true);
}
if (method.getReturnType() != Void.TYPE) {
throw new IllegalArgumentException(String.format("@CommandMethod annotated method '%s' has non-void return type",
method.getName()));
@ -112,13 +159,13 @@ public final class AnnotationParser<C, M extends CommandMeta> {
for (final CommandMethodPair commandMethodPair : methodPairs) {
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
final Method method = commandMethodPair.getMethod();
final LinkedHashMap<String, ArgumentMode> tokens = this.parseSyntax(commandMethod.value());
final LinkedHashMap<String, SyntaxFragment> tokens = this.parseSyntax(commandMethod.value());
/* Determine command name */
final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0];
@SuppressWarnings("ALL")
Command.Builder builder = this.manager.commandBuilder(commandToken,
Collections.emptyList(),
manager.createDefaultCommandMeta());
tokens.get(commandToken).getMinor(),
this.createMeta(method.getAnnotations()));
final Collection<ArgumentParameterPair> arguments = this.getArguments(method);
final Map<String, CommandArgument<C, ?>> commandArguments = Maps.newHashMap();
/* Go through all annotated parameters and build up the argument tree */
@ -130,13 +177,13 @@ public final class AnnotationParser<C, M extends CommandMeta> {
}
boolean commandNameFound = false;
/* Build the command tree */
for (final Map.Entry<String, ArgumentMode> entry : tokens.entrySet()) {
for (final Map.Entry<String, SyntaxFragment> entry : tokens.entrySet()) {
if (!commandNameFound) {
commandNameFound = true;
continue;
}
if (entry.getValue() == ArgumentMode.LITERAL) {
builder = builder.literal(entry.getKey());
if (entry.getValue().getArgumentMode() == ArgumentMode.LITERAL) {
builder = builder.literal(entry.getKey(), entry.getValue().getMinor().toArray(new String[0]));
} else {
final CommandArgument<C, ?> argument = commandArguments.get(entry.getKey());
if (argument == null) {
@ -148,8 +195,24 @@ public final class AnnotationParser<C, M extends CommandMeta> {
builder = builder.argument(argument);
}
}
/* Try to find the command sender type */
Class<? extends C> senderType = null;
for (final Parameter parameter : method.getParameters()) {
if (parameter.isAnnotationPresent(Argument.class)) {
continue;
}
if (this.commandSenderClass.isAssignableFrom(parameter.getType())) {
senderType = (Class<? extends C>) parameter.getType();
break;
}
}
/* Decorate command data */
builder = builder.withPermission(commandMethod.permission()).withSenderType(commandMethod.requiredSender());
builder = builder.withPermission(commandMethod.permission());
if (commandMethod.requiredSender() != Object.class) {
builder = builder.withSenderType(commandMethod.requiredSender());
} else if (senderType != null) {
builder = builder.withSenderType(senderType);
}
/* Construct the handler */
final CommandExecutionHandler<C> commandExecutionHandler = commandContext -> {
final List<Object> parameters = new ArrayList<>(method.getParameterCount());
@ -189,7 +252,7 @@ public final class AnnotationParser<C, M extends CommandMeta> {
@Nonnull
@SuppressWarnings("unchecked")
private CommandArgument<C, ?> buildArgument(@Nonnull final Method method,
@Nullable final ArgumentMode argumentMode,
@Nullable final SyntaxFragment syntaxFragment,
@Nonnull final ArgumentParameterPair argumentPair) {
final Parameter parameter = argumentPair.getParameter();
final Collection<Annotation> annotations = Arrays.asList(parameter.getAnnotations());
@ -204,16 +267,15 @@ public final class AnnotationParser<C, M extends CommandMeta> {
+ "for that type",
parameter.getName(), method.getName(),
token.toString())));
if (argumentMode == null || argumentMode == ArgumentMode.LITERAL) {
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();
@SuppressWarnings("ALL")
final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(),
@SuppressWarnings("ALL") final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(),
argument.value());
if (argumentMode == ArgumentMode.OPTIONAL) {
if (syntaxFragment.getArgumentMode() == ArgumentMode.OPTIONAL) {
if (argument.defaultValue().isEmpty()) {
argumentBuilder.asOptional();
} else {
@ -226,22 +288,30 @@ public final class AnnotationParser<C, M extends CommandMeta> {
}
@Nonnull
LinkedHashMap<String, ArgumentMode> parseSyntax(@Nonnull final String syntax) {
LinkedHashMap<String, SyntaxFragment> parseSyntax(@Nonnull final String syntax) {
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
final LinkedHashMap<String, ArgumentMode> map = new LinkedHashMap<>();
final LinkedHashMap<String, SyntaxFragment> map = new LinkedHashMap<>();
while (stringTokenizer.hasMoreTokens()) {
final String token = stringTokenizer.nextToken();
String major;
List<String> minor = new ArrayList<>();
ArgumentMode mode;
if (PATTERN_ARGUMENT_REQUIRED.test(token)) {
map.put(token.substring(1, token.length() - 1), ArgumentMode.REQUIRED);
major = token.substring(1, token.length() - 1);
mode = ArgumentMode.REQUIRED;
} else if (PATTERN_ARGUMENT_OPTIONAL.test(token)) {
map.put(token.substring(1, token.length() - 1), ArgumentMode.OPTIONAL);
major = token.substring(1, token.length() - 1);
mode = ArgumentMode.OPTIONAL;
} else if (PATTERN_ARGUMENT_LITERAL.test(token)) {
final String[] literals = token.split("\\|");
/* Actually use the other literals as well */
map.put(literals[0], ArgumentMode.LITERAL);
major = literals[0];
minor.addAll(Arrays.asList(literals).subList(1, literals.length));
mode = ArgumentMode.LITERAL;
} else {
throw new IllegalArgumentException(String.format("Unrecognizable syntax token '%s'", syntax));
}
map.put(major, new SyntaxFragment(major, minor, mode));
}
return map;
}
@ -306,7 +376,41 @@ public final class AnnotationParser<C, M extends CommandMeta> {
enum ArgumentMode {
LITERAL, OPTIONAL, REQUIRED
LITERAL,
OPTIONAL,
REQUIRED
}
private static final class SyntaxFragment {
private final String major;
private final List<String> minor;
private final ArgumentMode argumentMode;
private SyntaxFragment(@Nonnull final String major,
@Nonnull final List<String> minor,
@Nonnull final ArgumentMode argumentMode) {
this.major = major;
this.minor = minor;
this.argumentMode = argumentMode;
}
@Nonnull
private String getMajor() {
return this.major;
}
@Nonnull
private List<String> getMinor() {
return this.minor;
}
@Nonnull
private ArgumentMode getArgumentMode() {
return this.argumentMode;
}
}
}

View file

@ -0,0 +1,45 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Maps to {@link com.intellectualsites.commands.arguments.parser.StandardParameters#DESCRIPTION}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Description {
/**
* Command description
*
* @return Command syntax
*/
String value() default "";
}

View file

@ -23,7 +23,6 @@
//
package com.intellectualsites.commands.annotations;
import com.google.common.collect.Maps;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.annotations.specifier.Range;
@ -34,7 +33,6 @@ import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletionException;
class AnnotationParserTest {
@ -45,18 +43,7 @@ class AnnotationParserTest {
@BeforeAll
static void setup() {
manager = new TestCommandManager();
annotationParser = new AnnotationParser<>(manager);
}
@Test
void testSyntaxParsing() {
final String text = "literal <required> [optional]";
final Map<String, AnnotationParser.ArgumentMode> arguments = annotationParser.parseSyntax(text);
final Map<String, AnnotationParser.ArgumentMode> map = Maps.newLinkedHashMap();
map.put("literal", AnnotationParser.ArgumentMode.LITERAL);
map.put("required", AnnotationParser.ArgumentMode.REQUIRED);
map.put("optional", AnnotationParser.ArgumentMode.OPTIONAL);
Assertions.assertEquals(map, arguments);
annotationParser = new AnnotationParser<>(manager, TestCommandSender.class, p -> SimpleCommandMeta.empty());
}
@Test
@ -64,11 +51,12 @@ class AnnotationParserTest {
final Collection<Command<TestCommandSender, SimpleCommandMeta>> commands = annotationParser.parse(this);
Assertions.assertFalse(commands.isEmpty());
manager.executeCommand(new TestCommandSender(), "test 10").join();
manager.executeCommand(new TestCommandSender(), "t 10").join();
Assertions.assertThrows(CompletionException.class, () ->
manager.executeCommand(new TestCommandSender(), "test 101").join());
}
@CommandMethod("test <int> [string]")
@CommandMethod("test|t <int> [string]")
public void testCommand(@Nonnull final TestCommandSender sender,
@Argument("int") @Range(max = "100") final int argument,
@Nonnull @Argument(value = "string", defaultValue = "potato") final String string) {

View file

@ -285,6 +285,8 @@ public final class CommandTree<C, M extends CommandMeta> {
return child.getValue().getParser().suggestions(commandContext, commandQueue.peek());
} else if (child.isLeaf()) {
return Collections.emptyList();
} else if (commandQueue.peek().isEmpty()) {
return child.getValue().getParser().suggestions(commandContext, commandQueue.remove());
}
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
if (result.getParsedValue().isPresent()) {

View file

@ -0,0 +1,46 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// 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 com.intellectualsites.commands.annotations.specifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Command completions, separated by "," or ", "
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Completions {
/**
* Command completions
*
* @return Command completions
*/
String value() default "";
}

View file

@ -28,6 +28,7 @@ import com.intellectualsites.commands.arguments.parser.ArgumentParser;
import com.intellectualsites.commands.context.CommandContext;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@ -93,16 +94,28 @@ public final class StaticArgument<C> extends CommandArgument<C, String> {
return Collections.unmodifiableSet(((StaticArgumentParser<C>) this.getParser()).getAcceptedStrings());
}
/**
* Get an immutable list of all aliases that are not the main literal
*
* @return Immutable view of the optional argument aliases
*/
@Nonnull
public List<String> getAlternativeAliases() {
return Collections.unmodifiableList(new ArrayList<>(((StaticArgumentParser<C>) this.getParser()).acceptedStrings));
}
private static final class StaticArgumentParser<C> implements ArgumentParser<C, String> {
private final String name;
private final Set<String> acceptedStrings = new HashSet<>();
private final Set<String> alternativeAliases = new HashSet<>();
private StaticArgumentParser(@Nonnull final String name, @Nonnull final String... aliases) {
this.name = name;
this.acceptedStrings.add(this.name);
this.acceptedStrings.addAll(Arrays.asList(aliases));
this.alternativeAliases.addAll(Arrays.asList(aliases));
}
@Nonnull

View file

@ -46,6 +46,21 @@ public final class ParserParameters {
return new ParserParameters();
}
/**
* Create a {@link ParserParameters} instance containing a single key-value par
*
* @param parameter Parameter
* @param value Value
* @param <T> Value type
* @return Constructed instance
*/
@Nonnull
public static <T> ParserParameters single(@Nonnull final ParserParameter<T> parameter, @Nonnull final T value) {
final ParserParameters parameters = new ParserParameters();
parameters.store(parameter, value);
return parameters;
}
/**
* Check if this instance contains a parameter-object pair for a given parameter
*

View file

@ -45,6 +45,16 @@ public final class StandardParameters {
*/
public static final ParserParameter<Number> RANGE_MAX = create("max", TypeToken.of(Number.class));
/**
* Command description
*/
public static final ParserParameter<String> DESCRIPTION = create("description", TypeToken.of(String.class));
/**
* Command completions
*/
public static final ParserParameter<String[]> COMPLETIONS = create("completions", TypeToken.of(String[].class));
private static <T> ParserParameter<T> create(@Nonnull final String key, @Nonnull final TypeToken<T> expectedType) {
return new ParserParameter<>(key, expectedType);
}

View file

@ -25,6 +25,7 @@ package com.intellectualsites.commands.arguments.parser;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import com.intellectualsites.commands.annotations.specifier.Completions;
import com.intellectualsites.commands.annotations.specifier.Range;
import com.intellectualsites.commands.arguments.standard.BooleanArgument;
import com.intellectualsites.commands.arguments.standard.ByteArgument;
@ -38,8 +39,8 @@ import com.intellectualsites.commands.arguments.standard.StringArgument;
import javax.annotation.Nonnull;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@ -75,6 +76,7 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
public StandardParserRegistry() {
/* Register standard mappers */
this.<Range, Number>registerAnnotationMapper(Range.class, new RangeMapper<>());
this.<Completions, String>registerAnnotationMapper(Completions.class, new CompletionsMapper());
/* Register standard types */
this.registerParserSupplier(TypeToken.of(Byte.class), options ->
@ -95,7 +97,8 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
this.registerParserSupplier(TypeToken.of(Character.class), options -> new CharArgument.CharacterParser<C>());
/* Make this one less awful */
this.registerParserSupplier(TypeToken.of(String.class), options -> new StringArgument.StringParser<C>(
StringArgument.StringMode.SINGLE, (context, s) -> Collections.emptyList()));
StringArgument.StringMode.SINGLE, (context, s) ->
Arrays.asList(options.get(StandardParameters.COMPLETIONS, new String[0]))));
/* Add options to this */
this.registerParserSupplier(TypeToken.of(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
}
@ -229,4 +232,18 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
}
private static final class CompletionsMapper implements BiFunction<Completions, TypeToken<?>, ParserParameters> {
@Override
public ParserParameters apply(final Completions completions, final TypeToken<?> token) {
if (token.getRawType().equals(String.class)) {
final String[] splitCompletions = completions.value().replace(" ", "").split(",");
return ParserParameters.single(StandardParameters.COMPLETIONS, splitCompletions);
}
return ParserParameters.empty();
}
}
}

View file

@ -30,11 +30,19 @@ import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.parsing.NumberParseException;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@SuppressWarnings("unused")
public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
private static final int MAX_SUGGESTIONS_INCREMENT = 10;
private static final int NUMBER_SHIFT_MULTIPLIER = 10;
private final int min;
private final int max;
@ -224,6 +232,31 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
public boolean isContextFree() {
return true;
}
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext,
@Nonnull final String input) {
if (input.isEmpty()) {
return IntStream.range(0, MAX_SUGGESTIONS_INCREMENT).mapToObj(Integer::toString).collect(Collectors.toList());
}
try {
final int inputInt = Integer.parseInt(input);
if (inputInt > this.getMax()) {
return Collections.emptyList();
} else {
final List<String> suggestions = new LinkedList<>();
suggestions.add(input); /* It's a valid number, so we suggest it */
for (int i = 0; i < MAX_SUGGESTIONS_INCREMENT
&& (inputInt * NUMBER_SHIFT_MULTIPLIER) + i <= this.getMax(); i++) {
suggestions.add(Integer.toString((inputInt * NUMBER_SHIFT_MULTIPLIER) + i));
}
return suggestions;
}
} catch (final Exception ignored) {
return Collections.emptyList(); /* Invalid input */
}
}
}

View file

@ -62,7 +62,7 @@ public final class InvalidCommandSenderException extends CommandParseException {
@Override
public String getMessage() {
return String.format("%s is not allowed to execute that command. Must be of type %s",
getCommandSender().toString(),
getCommandSender().getClass().getSimpleName(),
requiredSender.getSimpleName());
}
}

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.arguments.standard.EnumArgument;
import com.intellectualsites.commands.arguments.standard.IntegerArgument;
import com.intellectualsites.commands.arguments.standard.StringArgument;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import org.junit.jupiter.api.Assertions;
@ -50,6 +51,14 @@ public class CommandSuggestionsTest {
.build())
.argument(EnumArgument.required(TestEnum.class, "enum"))
.build());
manager.command(manager.commandBuilder("test")
.literal("comb")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
.build())
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withMin(1).withMax(95).asOptional().build())
.build());
}
@Test
@ -59,7 +68,7 @@ public class CommandSuggestionsTest {
Assertions.assertTrue(suggestions.isEmpty());
final String input2 = "test ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("one", "two","var"), suggestions2);
Assertions.assertEquals(Arrays.asList("comb", "one", "two","var"), suggestions2);
}
@Test
@ -85,6 +94,19 @@ public class CommandSuggestionsTest {
Assertions.assertTrue(suggestions.isEmpty());
}
@Test
void testComb() {
final String input = "test comb ";
final List<String> suggestions = manager.suggest(new TestCommandSender(), input);
Assertions.assertEquals(Arrays.asList("one", "two"), suggestions);
final String input2 = "test comb one ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"), suggestions2);
final String input3 = "test comb one 9";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Arrays.asList("9", "90", "91", "92", "93", "94", "95"), suggestions3);
}
public enum TestEnum {
FOO, BAR

View file

@ -71,5 +71,10 @@
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-annotations</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View file

@ -23,7 +23,14 @@
//
package com.intellectualsites.commands;
import com.intellectualsites.commands.annotations.AnnotationParser;
import com.intellectualsites.commands.annotations.Argument;
import com.intellectualsites.commands.annotations.CommandMethod;
import com.intellectualsites.commands.annotations.Description;
import com.intellectualsites.commands.annotations.specifier.Completions;
import com.intellectualsites.commands.annotations.specifier.Range;
import com.intellectualsites.commands.arguments.parser.ArgumentParseResult;
import com.intellectualsites.commands.arguments.parser.StandardParameters;
import com.intellectualsites.commands.arguments.standard.BooleanArgument;
import com.intellectualsites.commands.arguments.standard.DoubleArgument;
import com.intellectualsites.commands.arguments.standard.EnumArgument;
@ -33,17 +40,21 @@ import com.intellectualsites.commands.arguments.standard.StringArgument;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.parsers.WorldArgument;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class BukkitTest extends JavaPlugin {
@ -54,13 +65,18 @@ public final class BukkitTest extends JavaPlugin {
@Override
public void onEnable() {
try {
final PaperCommandManager<BukkitCommandSender> mgr = new PaperCommandManager<>(
final PaperCommandManager<CommandSender> mgr = new PaperCommandManager<>(
this,
CommandExecutionCoordinator
.simpleCoordinator(),
BukkitCommandSender::of,
BukkitCommandSender::getInternalSender
Function.identity(),
Function.identity()
);
final AnnotationParser<CommandSender, BukkitCommandMeta> annotationParser
= new AnnotationParser<>(mgr, CommandSender.class, p ->
BukkitCommandMetaBuilder.builder().withDescription(p.get(StandardParameters.DESCRIPTION,
"No description")).build());
annotationParser.parse(this);
mgr.registerBrigadier();
mgr.command(mgr.commandBuilder("gamemode",
Collections.singleton("gajmöde"),
@ -68,7 +84,7 @@ public final class BukkitTest extends JavaPlugin {
.withDescription("Your ugli")
.build())
.argument(EnumArgument.required(GameMode.class, "gamemode"))
.argument(StringArgument.<BukkitCommandSender>newBuilder("player")
.argument(StringArgument.<CommandSender>newBuilder("player")
.withSuggestionsProvider((v1, v2) -> {
final List<String> suggestions =
new ArrayList<>(
@ -80,18 +96,17 @@ public final class BukkitTest extends JavaPlugin {
suggestions.add("cat");
return suggestions;
}).build())
.handler(c -> c.getSender()
.asPlayer()
.handler(c -> ((Player) c.getSender())
.setGameMode(c.<GameMode>get("gamemode")
.orElse(GameMode.SURVIVAL)))
.build())
.command(mgr.commandBuilder("kenny")
.literal("sux")
.argument(IntegerArgument
.<BukkitCommandSender>newBuilder("perc")
.<CommandSender>newBuilder("perc")
.withMin(PERC_MIN).withMax(PERC_MAX).build())
.handler(context -> {
context.getSender().asPlayer().sendMessage(String.format(
((Player) context.getSender()).sendMessage(String.format(
"Kenny sux %d%%",
context.<Integer>get("perc").orElse(PERC_MIN)
));
@ -122,13 +137,14 @@ public final class BukkitTest extends JavaPlugin {
.sendMessage(String.format("UUID: %s\n", c.<UUID>get("uuid").orElse(null))))
.build())
.command(mgr.commandBuilder("give")
.withSenderType(Player.class)
.argument(EnumArgument.required(Material.class, "material"))
.argument(IntegerArgument.required("amount"))
.handler(c -> {
final Material material = c.getRequired("material");
final int amount = c.getRequired("amount");
final ItemStack itemStack = new ItemStack(material, amount);
c.getSender().asPlayer().getInventory().addItem(itemStack);
((Player) c.getSender()).getInventory().addItem(itemStack);
c.getSender().sendMessage("You've been given stuff, bro.");
})
.build())
@ -138,7 +154,7 @@ public final class BukkitTest extends JavaPlugin {
.argument(WorldArgument.required("world"))
.handler(c -> {
final World world = c.getRequired("world");
c.getSender().asPlayer().teleport(world.getSpawnLocation());
((Player) c.getSender()).teleport(world.getSpawnLocation());
c.getSender().sendMessage("Teleported.");
})
.build())
@ -155,4 +171,12 @@ public final class BukkitTest extends JavaPlugin {
}
}
@Description("Test cloud command using @CommandMethod")
@CommandMethod(value = "annotation|a <input> [number]", permission = "some.permission.node")
private void annotatedCommand(@Nonnull final Player player,
@Argument("input") @Completions("one,two,duck") @Nonnull final String input,
@Argument("number") @Range(max = "100") final int number) {
player.sendMessage(ChatColor.GOLD + "Your input was: " + ChatColor.AQUA + input + ChatColor.GREEN + " (" + number + ")");
}
}

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
@ -32,17 +33,20 @@ import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull;
import java.util.List;
final class BukkitCommand<C>
extends org.bukkit.command.Command implements PluginIdentifiableCommand {
final class BukkitCommand<C> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
private final CommandArgument<C, ?> command;
private final BukkitCommandManager<C> bukkitCommandManager;
private final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand;
@SuppressWarnings("unchecked")
BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand,
@Nonnull final CommandArgument<C, ?> command,
@Nonnull final BukkitCommandManager<C> bukkitCommandManager) {
super(command.getName());
super(command.getName(),
cloudCommand.getCommandMeta().getOrDefault("description", ""),
"",
((StaticArgument<C>) command).getAlternativeAliases());
this.command = command;
this.bukkitCommandManager = bukkitCommandManager;
this.cloudCommand = cloudCommand;
@ -92,4 +96,9 @@ final class BukkitCommand<C>
return this.bukkitCommandManager.getOwningPlugin();
}
@Override
public String getPermission() {
return this.cloudCommand.getCommandPermission();
}
}