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

@ -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