From ccd0e8ae0e307b59942f72ff6b40bb150f920cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 19 Sep 2020 12:14:09 +0200 Subject: [PATCH] Clean up generics (and get rid of the type parameter for command meta data) --- .mvn/wrapper/MavenWrapperDownloader.java | 11 ++- .../annotations/AnnotationParser.java | 52 +++++------ .../annotations/AnnotationParserTest.java | 4 +- .../annotations/TestCommandManager.java | 2 +- .../intellectualsites/commands/Command.java | 61 ++++++------- .../commands/CommandManager.java | 52 ++++++----- .../commands/CommandTree.java | 91 +++++++++---------- .../commands/annotations/specifier/Range.java | 4 +- .../commands/arguments/CommandArgument.java | 20 ++-- .../commands/arguments/StaticArgument.java | 4 +- .../arguments/parser/ParserParameter.java | 6 +- .../arguments/parser/ParserParameters.java | 2 +- .../arguments/parser/StandardParameters.java | 9 +- .../parser/StandardParserRegistry.java | 6 +- .../arguments/standard/BooleanArgument.java | 20 ++-- .../arguments/standard/ByteArgument.java | 44 +++++---- .../arguments/standard/CharArgument.java | 2 +- .../arguments/standard/DoubleArgument.java | 41 ++++----- .../arguments/standard/EnumArgument.java | 17 ++-- .../arguments/standard/FloatArgument.java | 41 ++++----- .../arguments/standard/IntegerArgument.java | 87 +++++++++--------- .../arguments/standard/LongArgument.java | 41 ++++----- .../arguments/standard/ShortArgument.java | 39 ++++---- .../arguments/standard/StringArgument.java | 2 +- .../CommandExecutionCoordinator.java | 26 +++--- .../commands/execution/CommandResult.java | 3 +- .../preprocessor/CommandPreprocessor.java | 2 +- .../internal/CommandRegistrationHandler.java | 32 +++---- .../commands/meta/CommandMeta.java | 31 ++++++- .../commands/meta/SimpleCommandMeta.java | 30 ++---- .../commands/package-info.java | 1 + .../commands/CommandPreProcessorTest.java | 24 ++--- .../commands/CommandSuggestionsTest.java | 20 ++-- .../commands/CommandTreeTest.java | 28 +++--- .../commands/ParserRegistryTest.java | 10 +- .../commands/TestCommandManager.java | 2 +- .../commands/jline/JLineCommandManager.java | 62 ++++++------- .../brigadier/CloudBrigadierManager.java | 24 ++--- .../commands/BukkitTest.java | 59 ++++++------ .../commands/bukkit/BukkitCommand.java | 13 +-- .../commands/bukkit/BukkitCommandManager.java | 7 +- .../bukkit/BukkitCommandMetaBuilder.java | 9 +- .../BukkitPluginRegistrationHandler.java | 6 +- .../bukkit/parsers/MaterialArgument.java | 2 +- .../bukkit/parsers/WorldArgument.java | 2 +- .../commands/bungee/BungeeCommand.java | 79 ++++++++-------- .../commands/bungee/BungeeCommandManager.java | 8 +- .../BungeePluginRegistrationHandler.java | 10 +- .../paper/PaperBrigadierListener.java | 10 +- .../commands/paper/PaperCommandManager.java | 15 ++- 50 files changed, 577 insertions(+), 596 deletions(-) diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java index b901097f..fa827660 100644 --- a/.mvn/wrapper/MavenWrapperDownloader.java +++ b/.mvn/wrapper/MavenWrapperDownloader.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import java.net.*; import java.io.*; import java.nio.channels.*; @@ -25,7 +26,7 @@ public class MavenWrapperDownloader { * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. */ private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" - + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; /** * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to @@ -54,7 +55,7 @@ public class MavenWrapperDownloader { // wrapperUrl parameter. File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); String url = DEFAULT_DOWNLOAD_URL; - if(mavenWrapperPropertyFile.exists()) { + if (mavenWrapperPropertyFile.exists()) { FileInputStream mavenWrapperPropertyFileInputStream = null; try { mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); @@ -65,7 +66,7 @@ public class MavenWrapperDownloader { System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); } finally { try { - if(mavenWrapperPropertyFileInputStream != null) { + if (mavenWrapperPropertyFileInputStream != null) { mavenWrapperPropertyFileInputStream.close(); } } catch (IOException e) { @@ -76,8 +77,8 @@ public class MavenWrapperDownloader { System.out.println("- Downloading from: " + url); File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); - if(!outputFile.getParentFile().exists()) { - if(!outputFile.getParentFile().mkdirs()) { + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { System.out.println( "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); } diff --git a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java index f8ad99f2..f0909f3f 100644 --- a/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/com/intellectualsites/commands/annotations/AnnotationParser.java @@ -57,7 +57,7 @@ import java.util.regex.Pattern; * @param Command sender type * @param Command meta type */ -public final class AnnotationParser { +public final class AnnotationParser { private static final Predicate PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*") .asPredicate(); @@ -67,8 +67,8 @@ public final class AnnotationParser { .asPredicate(); - private final Function metaMapper; - private final CommandManager manager; + private final Function metaMapper; + private final CommandManager manager; private final Map, Function> annotationMappers; private final Class commandSenderClass; @@ -82,9 +82,9 @@ public final class AnnotationParser { * {@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 manager, + public AnnotationParser(@Nonnull final CommandManager manager, @Nonnull final Class commandSenderClass, - @Nonnull final Function metaMapper) { + @Nonnull final Function metaMapper) { this.commandSenderClass = commandSenderClass; this.manager = manager; this.metaMapper = metaMapper; @@ -105,7 +105,7 @@ public final class AnnotationParser { } @Nonnull - private M createMeta(@Nonnull final Annotation[] annotations) { + private CommandMeta 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()); @@ -127,7 +127,7 @@ public final class AnnotationParser { * @return Collection of parsed annotations */ @Nonnull - public Collection> parse(@Nonnull final T instance) { + public Collection> parse(@Nonnull final T instance) { final Method[] methods = instance.getClass().getDeclaredMethods(); final Collection commandMethodPairs = new ArrayList<>(); for (final Method method : methods) { @@ -144,28 +144,31 @@ public final class AnnotationParser { } commandMethodPairs.add(new CommandMethodPair(method, commandMethod)); } - final Collection> commands = this.construct(instance, commandMethodPairs); - for (final Command command : commands) { - this.manager.command(command); + final Collection> commands = this.construct(instance, commandMethodPairs); + for (final Command command : commands) { + @SuppressWarnings("ALL") final CommandManager commandManager = this.manager; + //noinspection all + commandManager.command(command); } return commands; } @Nonnull @SuppressWarnings("unchecked") - private Collection> construct(@Nonnull final Object instance, - @Nonnull final Collection methodPairs) { - final Collection> commands = new ArrayList<>(); + private Collection> construct(@Nonnull final Object instance, + @Nonnull final Collection methodPairs) { + final Collection> commands = new ArrayList<>(); for (final CommandMethodPair commandMethodPair : methodPairs) { final CommandMethod commandMethod = commandMethodPair.getCommandMethod(); final Method method = commandMethodPair.getMethod(); final LinkedHashMap tokens = this.parseSyntax(commandMethod.value()); /* Determine command name */ final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0]; + @SuppressWarnings("ALL") final CommandManager manager = this.manager; @SuppressWarnings("ALL") - Command.Builder builder = this.manager.commandBuilder(commandToken, - tokens.get(commandToken).getMinor(), - this.createMeta(method.getAnnotations())); + Command.Builder builder = manager.commandBuilder(commandToken, + tokens.get(commandToken).getMinor(), + this.createMeta(method.getAnnotations())); final Collection arguments = this.getArguments(method); final Map> commandArguments = Maps.newHashMap(); /* Go through all annotated parameters and build up the argument tree */ @@ -270,7 +273,7 @@ public final class AnnotationParser { 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())); + + "Missing syntax mapping", argumentPair.getArgument().value(), method.getName())); } final Argument argument = argumentPair.getArgument(); @SuppressWarnings("ALL") final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(), @@ -329,6 +332,12 @@ public final class AnnotationParser { } + enum ArgumentMode { + LITERAL, + OPTIONAL, + REQUIRED + } + private static final class CommandMethodPair { private final Method method; @@ -351,7 +360,6 @@ public final class AnnotationParser { } - private static final class ArgumentParameterPair { private final Parameter parameter; @@ -374,14 +382,6 @@ public final class AnnotationParser { } - - enum ArgumentMode { - LITERAL, - OPTIONAL, - REQUIRED - } - - private static final class SyntaxFragment { private final String major; diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java index 1db1df1f..b5986211 100644 --- a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/AnnotationParserTest.java @@ -37,7 +37,7 @@ import java.util.concurrent.CompletionException; class AnnotationParserTest { - private static CommandManager manager; + private static CommandManager manager; private static AnnotationParser annotationParser; @BeforeAll @@ -48,7 +48,7 @@ class AnnotationParserTest { @Test void testMethodConstruction() { - final Collection> commands = annotationParser.parse(this); + final Collection> commands = annotationParser.parse(this); Assertions.assertFalse(commands.isEmpty()); manager.executeCommand(new TestCommandSender(), "test 10").join(); manager.executeCommand(new TestCommandSender(), "t 10").join(); diff --git a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java index 3750d77c..5c7cf3ff 100644 --- a/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java +++ b/cloud-annotations/src/test/java/com/intellectualsites/commands/annotations/TestCommandManager.java @@ -30,7 +30,7 @@ import com.intellectualsites.commands.meta.SimpleCommandMeta; import javax.annotation.Nonnull; -public class TestCommandManager extends CommandManager { +public class TestCommandManager extends CommandManager { protected TestCommandManager() { super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler()); diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java index 1562db03..41709a5b 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java @@ -41,21 +41,20 @@ import java.util.function.Consumer; * A command consists out of a chain of {@link CommandArgument command arguments}. * * @param Command sender type - * @param Command meta type */ @SuppressWarnings("unused") -public class Command { +public class Command { @Nonnull private final List> arguments; @Nonnull private final CommandExecutionHandler commandExecutionHandler; @Nullable private final Class senderType; @Nonnull private final String commandPermission; - @Nonnull private final M commandMeta; + @Nonnull private final CommandMeta commandMeta; /** * Construct a new command * - * @param commandArguments Command arguments + * @param commandArguments Command arguments * @param commandExecutionHandler Execution handler * @param senderType Required sender type. May be {@code null} * @param commandPermission Command permission @@ -65,7 +64,7 @@ public class Command { @Nonnull final CommandExecutionHandler commandExecutionHandler, @Nullable final Class senderType, @Nonnull final String commandPermission, - @Nonnull final M commandMeta) { + @Nonnull final CommandMeta commandMeta) { this.arguments = Objects.requireNonNull(commandArguments, "Command arguments may not be null"); if (this.arguments.size() == 0) { throw new IllegalArgumentException("At least one command argument is required"); @@ -93,7 +92,7 @@ public class Command { /** * Construct a new command * - * @param commandArguments Command arguments + * @param commandArguments Command arguments * @param commandExecutionHandler Execution handler * @param senderType Required sender type. May be {@code null} * @param commandMeta Command meta instance @@ -101,14 +100,14 @@ public class Command { public Command(@Nonnull final List> commandArguments, @Nonnull final CommandExecutionHandler commandExecutionHandler, @Nullable final Class senderType, - @Nonnull final M commandMeta) { + @Nonnull final CommandMeta commandMeta) { this(commandArguments, commandExecutionHandler, senderType, "", commandMeta); } /** * Construct a new command * - * @param commandArguments Command arguments + * @param commandArguments Command arguments * @param commandExecutionHandler Execution handler * @param commandPermission Command permission * @param commandMeta Command meta instance @@ -116,7 +115,7 @@ public class Command { public Command(@Nonnull final List> commandArguments, @Nonnull final CommandExecutionHandler commandExecutionHandler, @Nonnull final String commandPermission, - @Nonnull final M commandMeta) { + @Nonnull final CommandMeta commandMeta) { this(commandArguments, commandExecutionHandler, null, "", commandMeta); } @@ -127,13 +126,12 @@ public class Command { * @param commandMeta Command meta instance * @param aliases Command aliases * @param Command sender type - * @param Command meta type * @return Command builder */ @Nonnull - public static Builder newBuilder(@Nonnull final String commandName, - @Nonnull final M commandMeta, - @Nonnull final String... aliases) { + public static Builder newBuilder(@Nonnull final String commandName, + @Nonnull final CommandMeta commandMeta, + @Nonnull final String... aliases) { return new Builder<>(null, commandMeta, null, Collections.singletonList(StaticArgument.required(commandName, aliases)), new CommandExecutionHandler.NullCommandExecutionHandler<>(), ""); @@ -185,7 +183,7 @@ public class Command { * @return Command meta */ @Nonnull - public M getCommandMeta() { + public CommandMeta getCommandMeta() { return this.commandMeta; } @@ -196,7 +194,7 @@ public class Command { * @param other Command to compare to * @return List containing the longest shared argument chain */ - public List> getSharedArgumentChain(@Nonnull final Command other) { + public List> getSharedArgumentChain(@Nonnull final Command other) { final List> commandArguments = new LinkedList<>(); for (int i = 0; i < this.arguments.size() && i < other.arguments.size(); i++) { if (this.arguments.get(i).equals(other.arguments.get(i))) { @@ -214,19 +212,18 @@ public class Command { * setter method will return a new builder instance. * * @param Command sender type - * @param Command meta type */ - public static final class Builder { + public static final class Builder { - @Nonnull private final M commandMeta; + @Nonnull private final CommandMeta commandMeta; @Nonnull private final List> commandArguments; @Nonnull private final CommandExecutionHandler commandExecutionHandler; @Nullable private final Class senderType; @Nonnull private final String commandPermission; - @Nullable private final CommandManager commandManager; + @Nullable private final CommandManager commandManager; - private Builder(@Nullable final CommandManager commandManager, - @Nonnull final M commandMeta, + private Builder(@Nullable final CommandManager commandManager, + @Nonnull final CommandMeta commandMeta, @Nullable final Class senderType, @Nonnull final List> commandArguments, @Nonnull final CommandExecutionHandler commandExecutionHandler, @@ -248,7 +245,7 @@ public class Command { * @return New builder instance using the provided command manager */ @Nonnull - public Builder manager(@Nullable final CommandManager commandManager) { + public Builder manager(@Nullable final CommandManager commandManager) { return new Builder<>(commandManager, this.commandMeta, this.senderType, this.commandArguments, this.commandExecutionHandler, this.commandPermission); } @@ -261,7 +258,7 @@ public class Command { * @return New builder instance with the modified command chain */ @Nonnull - public Builder literal(@Nonnull final String main, @Nonnull final String... aliases) { + public Builder literal(@Nonnull final String main, @Nonnull final String... aliases) { return this.argument(StaticArgument.required(main, aliases)); } @@ -269,11 +266,11 @@ public class Command { * Add a new command argument to the command * * @param argument Argument to add - * @param Argument type + * @param Argument type * @return New builder instance with the command argument inserted into the argument list */ @Nonnull - public Builder argument(@Nonnull final CommandArgument argument) { + public Builder argument(@Nonnull final CommandArgument argument) { final List> commandArguments = new LinkedList<>(this.commandArguments); commandArguments.add(argument); return new Builder<>(this.commandManager, this.commandMeta, this.senderType, commandArguments, @@ -290,9 +287,9 @@ public class Command { * @return New builder instance with the command argument inserted into the argument list */ @Nonnull - public Builder argument(@Nonnull final Class clazz, - @Nonnull final String name, - @Nonnull final Consumer> builderConsumer) { + public Builder argument(@Nonnull final Class clazz, + @Nonnull final String name, + @Nonnull final Consumer> builderConsumer) { final CommandArgument.Builder builder = CommandArgument.ofType(clazz, name); if (this.commandManager != null) { builder.manager(this.commandManager); @@ -308,7 +305,7 @@ public class Command { * @return New builder instance using the command execution handler */ @Nonnull - public Builder handler(@Nonnull final CommandExecutionHandler commandExecutionHandler) { + public Builder handler(@Nonnull final CommandExecutionHandler commandExecutionHandler) { return new Builder<>(this.commandManager, this.commandMeta, this.senderType, this.commandArguments, commandExecutionHandler, this.commandPermission); } @@ -320,7 +317,7 @@ public class Command { * @return New builder instance using the command execution handler */ @Nonnull - public Builder withSenderType(@Nonnull final Class senderType) { + public Builder withSenderType(@Nonnull final Class senderType) { return new Builder<>(this.commandManager, this.commandMeta, senderType, this.commandArguments, this.commandExecutionHandler, this.commandPermission); } @@ -332,7 +329,7 @@ public class Command { * @return New builder instance using the command permission */ @Nonnull - public Builder withPermission(@Nonnull final String permission) { + public Builder withPermission(@Nonnull final String permission) { return new Builder<>(this.commandManager, this.commandMeta, this.senderType, this.commandArguments, this.commandExecutionHandler, permission); } @@ -343,7 +340,7 @@ public class Command { * @return Built command */ @Nonnull - public Command build() { + public Command build() { return new Command<>(Collections.unmodifiableList(this.commandArguments), this.commandExecutionHandler, this.senderType, this.commandPermission, this.commandMeta); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java index 9bcd5cce..3ee3a8de 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -57,18 +57,17 @@ import java.util.function.Function; * The manager is responsible for command registration, parsing delegation, etc. * * @param Command sender type - * @param Command meta type */ @SuppressWarnings("unused") -public abstract class CommandManager { +public abstract class CommandManager { private final CommandContextFactory commandContextFactory = new StandardCommandContextFactory<>(); private final ServicePipeline servicePipeline = ServicePipeline.builder().build(); private final ParserRegistry parserRegistry = new StandardParserRegistry<>(); - private final CommandExecutionCoordinator commandExecutionCoordinator; - private final CommandRegistrationHandler commandRegistrationHandler; - private final CommandTree commandTree; + private final CommandExecutionCoordinator commandExecutionCoordinator; + private final CommandRegistrationHandler commandRegistrationHandler; + private final CommandTree commandTree; private CommandSyntaxFormatter commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>(); private CommandSuggestionProcessor commandSuggestionProcessor = new FilteringCommandSuggestionProcessor<>(); @@ -80,8 +79,8 @@ public abstract class CommandManager { * @param commandRegistrationHandler Command registration handler */ public CommandManager( - @Nonnull final Function, CommandExecutionCoordinator> commandExecutionCoordinator, - @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + @Nonnull final Function, CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { this.commandTree = CommandTree.newTree(this, commandRegistrationHandler); this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree); this.commandRegistrationHandler = commandRegistrationHandler; @@ -159,7 +158,7 @@ public abstract class CommandManager { * @param command Command to register * @return The command manager instance */ - public CommandManager command(@Nonnull final Command command) { + public CommandManager command(@Nonnull final Command command) { this.commandTree.insertCommand(command); return this; } @@ -189,7 +188,7 @@ public abstract class CommandManager { * @return Command registration handler */ @Nonnull - protected CommandRegistrationHandler getCommandRegistrationHandler() { + protected CommandRegistrationHandler getCommandRegistrationHandler() { return this.commandRegistrationHandler; } @@ -211,9 +210,9 @@ public abstract class CommandManager { * @return Builder instance */ @Nonnull - public Command.Builder commandBuilder(@Nonnull final String name, - @Nonnull final Collection aliases, - @Nonnull final M meta) { + public Command.Builder commandBuilder(@Nonnull final String name, + @Nonnull final Collection aliases, + @Nonnull final CommandMeta meta) { return Command.newBuilder(name, meta, aliases.toArray(new String[0])); } @@ -222,25 +221,28 @@ public abstract class CommandManager { * * @param name Command name * @param meta Command meta + * @param aliases Command aliases * @return Builder instance */ @Nonnull - public Command.Builder commandBuilder(@Nonnull final String name, - @Nonnull final M meta) { - return Command.newBuilder(name, meta); + public Command.Builder commandBuilder(@Nonnull final String name, + @Nonnull final CommandMeta meta, + @Nonnull final String... aliases) { + return Command.newBuilder(name, meta, aliases); } /** * Create a new command builder using a default command meta instance. * - * @param name Command name + * @param name Command name + * @param aliases Command aliases * @return Builder instance * @throws UnsupportedOperationException If the command manager does not support default command meta creation * @see #createDefaultCommandMeta() Default command meta creation */ @Nonnull - public Command.Builder commandBuilder(@Nonnull final String name) { - return Command.newBuilder(name, this.createDefaultCommandMeta()).manager(this); + public Command.Builder commandBuilder(@Nonnull final String name, @Nonnull final String... aliases) { + return Command.newBuilder(name, this.createDefaultCommandMeta(), aliases).manager(this); } /** @@ -263,7 +265,7 @@ public abstract class CommandManager { * @return Command tree */ @Nonnull - public CommandTree getCommandTree() { + public CommandTree getCommandTree() { return this.commandTree; } @@ -274,7 +276,7 @@ public abstract class CommandManager { * @throws UnsupportedOperationException If the command manager does not support this operation */ @Nonnull - public abstract M createDefaultCommandMeta(); + public abstract CommandMeta createDefaultCommandMeta(); /** * Register a new command preprocessor. The order they are registered in is respected, and they @@ -297,12 +299,12 @@ public abstract class CommandManager { */ public State preprocessContext(@Nonnull final CommandContext context, @Nonnull final LinkedList inputQueue) { this.servicePipeline.pump(new CommandPreprocessingContext<>(context, inputQueue)) - .through(new TypeToken>() { - }) - .getResult(); + .through(new TypeToken>() { + }) + .getResult(); return context.get(AcceptingCommandPreprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty() - ? State.REJECTED - : State.ACCEPTED; + ? State.REJECTED + : State.ACCEPTED; } /** diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java b/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java index 8b73bdb3..e89aba4c 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandTree.java @@ -35,7 +35,6 @@ import com.intellectualsites.commands.exceptions.NoCommandInLeafException; import com.intellectualsites.commands.exceptions.NoPermissionException; import com.intellectualsites.commands.exceptions.NoSuchCommandException; import com.intellectualsites.commands.internal.CommandRegistrationHandler; -import com.intellectualsites.commands.meta.CommandMeta; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -57,18 +56,17 @@ import java.util.stream.Collectors; * Tree containing all commands and command paths * * @param Command sender type - * @param Command meta type */ -public final class CommandTree { +public final class CommandTree { private final Object commandLock = new Object(); private final Node> internalTree = new Node<>(null); - private final CommandManager commandManager; - private final CommandRegistrationHandler commandRegistrationHandler; + private final CommandManager commandManager; + private final CommandRegistrationHandler commandRegistrationHandler; - private CommandTree(@Nonnull final CommandManager commandManager, - @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + private CommandTree(@Nonnull final CommandManager commandManager, + @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { this.commandManager = commandManager; this.commandRegistrationHandler = commandRegistrationHandler; } @@ -79,13 +77,12 @@ public final class CommandTree { * @param commandManager Command manager * @param commandRegistrationHandler Command registration handler * @param Command sender type - * @param Command meta type * @return New command tree */ @Nonnull - public static CommandTree newTree( - @Nonnull final CommandManager commandManager, - @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + public static CommandTree newTree( + @Nonnull final CommandManager commandManager, + @Nonnull final CommandRegistrationHandler commandRegistrationHandler) { return new CommandTree<>(commandManager, commandRegistrationHandler); } @@ -99,13 +96,13 @@ public final class CommandTree { * @throws NoPermissionException If the sender lacks permission to execute the command * @throws InvalidSyntaxException If the command syntax is invalid */ - public Optional> parse(@Nonnull final CommandContext commandContext, - @Nonnull final Queue args) throws + public Optional> parse(@Nonnull final CommandContext commandContext, + @Nonnull final Queue args) throws NoSuchCommandException, NoPermissionException, InvalidSyntaxException { - final Optional> commandOptional = parseCommand(new ArrayList<>(), - commandContext, - args, - this.internalTree); + final Optional> commandOptional = parseCommand(new ArrayList<>(), + commandContext, + args, + this.internalTree); commandOptional.flatMap(Command::getSenderType).ifPresent(requiredType -> { if (!requiredType.isAssignableFrom(commandContext.getSender().getClass())) { throw new InvalidCommandSenderException(commandContext.getSender(), requiredType, Collections.emptyList()); @@ -114,10 +111,10 @@ public final class CommandTree { return commandOptional; } - private Optional> parseCommand(@Nonnull final List> parsedArguments, - @Nonnull final CommandContext commandContext, - @Nonnull final Queue commandQueue, - @Nonnull final Node> root) { + private Optional> parseCommand(@Nonnull final List> parsedArguments, + @Nonnull final CommandContext commandContext, + @Nonnull final Queue commandQueue, + @Nonnull final Node> root) { String permission = this.isPermitted(commandContext.getSender(), root); if (permission != null) { throw new NoPermissionException(permission, commandContext.getSender(), this.getChain(root) @@ -126,10 +123,10 @@ public final class CommandTree { .collect(Collectors.toList())); } - final Optional> parsedChild = this.attemptParseUnambiguousChild(parsedArguments, - commandContext, - root, - commandQueue); + final Optional> parsedChild = this.attemptParseUnambiguousChild(parsedArguments, + commandContext, + root, + commandQueue); // noinspection all if (parsedChild != null) { return parsedChild; @@ -191,10 +188,10 @@ public final class CommandTree { } @Nullable - private Optional> attemptParseUnambiguousChild(@Nonnull final List> parsedArguments, - @Nonnull final CommandContext commandContext, - @Nonnull final Node> root, - @Nonnull final Queue commandQueue) { + private Optional> attemptParseUnambiguousChild(@Nonnull final List> parsedArguments, + @Nonnull final CommandContext commandContext, + @Nonnull final Node> root, + @Nonnull final Queue commandQueue) { String permission; final List>> children = root.getChildren(); if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticArgument)) { @@ -253,7 +250,7 @@ public final class CommandTree { .stream() .map(Node::getValue) .collect( - Collectors.toList())); + Collectors.toList())); } } else { parsedArguments.add(child.getValue()); @@ -262,9 +259,9 @@ public final class CommandTree { } else if (result.getFailure().isPresent()) { throw new ArgumentParseException(result.getFailure().get(), commandContext.getSender(), this.getChain(child) - .stream() - .map(Node::getValue) - .collect(Collectors.toList())); + .stream() + .map(Node::getValue) + .collect(Collectors.toList())); } } } @@ -286,8 +283,8 @@ public final class CommandTree { @Nonnull private List getSuggestions(@Nonnull final CommandContext commandContext, - @Nonnull final Queue commandQueue, - @Nonnull final Node> root) { + @Nonnull final Queue commandQueue, + @Nonnull final Node> root) { /* If the sender isn't allowed to access the root node, no suggestions are needed */ if (this.isPermitted(commandContext.getSender(), root) != null) { @@ -360,7 +357,7 @@ public final class CommandTree { * @param command Command to insert */ @SuppressWarnings("unchecked") - public void insertCommand(@Nonnull final Command command) { + public void insertCommand(@Nonnull final Command command) { synchronized (this.commandLock) { Node> node = this.internalTree; for (final CommandArgument argument : command.getArguments()) { @@ -394,8 +391,9 @@ public final class CommandTree { } if (node.isLeaf()) { return this.commandManager.hasPermission(sender, - Objects.requireNonNull(Objects.requireNonNull(node.value, "node.value").getOwningCommand(), - "owning command").getCommandPermission()) + Objects.requireNonNull( + Objects.requireNonNull(node.value, "node.value").getOwningCommand(), + "owning command").getCommandPermission()) ? null : Objects.requireNonNull(node.value.getOwningCommand(), "owning command").getCommandPermission(); } /* @@ -433,8 +431,7 @@ public final class CommandTree { if (leaf.getOwningCommand() == null) { throw new NoCommandInLeafException(leaf); } else { - // noinspection unchecked - final Command owningCommand = (Command) leaf.getOwningCommand(); + final Command owningCommand = leaf.getOwningCommand(); this.commandRegistrationHandler.registerCommand(owningCommand); } }); @@ -451,9 +448,9 @@ public final class CommandTree { // Go through all nodes from the tail upwards until a collision occurs for (final Node> commandArgumentNode : chain) { if (commandArgumentNode.nodeMeta.containsKey("permission") && !commandArgumentNode.nodeMeta.get("permission") - .equalsIgnoreCase( - node.nodeMeta - .get("permission"))) { + .equalsIgnoreCase( + node.nodeMeta + .get("permission"))) { commandArgumentNode.nodeMeta.put("permission", ""); } else { commandArgumentNode.nodeMeta.put("permission", node.nodeMeta.get("permission")); @@ -513,12 +510,8 @@ public final class CommandTree { } @Nullable - private Command cast(@Nullable final Command command) { - if (command == null) { - return null; - } - @SuppressWarnings("unchecked") final Command casted = (Command) command; - return casted; + private Command cast(@Nullable final Command command) { + return command; } /** diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/annotations/specifier/Range.java b/cloud-core/src/main/java/com/intellectualsites/commands/annotations/specifier/Range.java index 1fb50eba..fccaeb65 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/annotations/specifier/Range.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/annotations/specifier/Range.java @@ -42,13 +42,13 @@ public @interface Range { * * @return String serialized number */ - @Nonnull String min() default ""; + @Nonnull String min() default ""; /** * Maximum value accepted by the parser * * @return String serialized number */ - @Nonnull String max() default ""; + @Nonnull String max() default ""; } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/CommandArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/CommandArgument.java index cfd9e5da..0ef5dd70 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/CommandArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/CommandArgument.java @@ -76,7 +76,7 @@ public class CommandArgument implements Comparable> */ private final Class valueType; - private Command owningCommand; + private Command owningCommand; /** * Construct a new command argument @@ -105,10 +105,10 @@ public class CommandArgument implements Comparable> /** * 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 valueType Type produced by the parser + * @param required Whether or not the argument is required + * @param name The argument name + * @param parser The argument parser + * @param valueType Type produced by the parser */ public CommandArgument(final boolean required, @Nonnull final String name, @@ -174,7 +174,7 @@ public class CommandArgument implements Comparable> * @return Owning command */ @Nullable - public Command getOwningCommand() { + public Command getOwningCommand() { return this.owningCommand; } @@ -183,7 +183,7 @@ public class CommandArgument implements Comparable> * * @param owningCommand Owning command */ - public void setOwningCommand(@Nonnull final Command owningCommand) { + public void setOwningCommand(@Nonnull final Command owningCommand) { if (this.owningCommand != null) { throw new IllegalStateException("Cannot replace owning command"); } @@ -265,7 +265,7 @@ public class CommandArgument implements Comparable> private final Class valueType; private final String name; - private CommandManager manager; + private CommandManager manager; private boolean required = true; private ArgumentParser parser; private String defaultValue = ""; @@ -284,7 +284,7 @@ public class CommandArgument implements Comparable> * @return Builder instance */ @Nonnull - public Builder manager(@Nonnull final CommandManager manager) { + public Builder manager(@Nonnull final CommandManager manager) { this.manager = manager; return this; } @@ -357,7 +357,7 @@ public class CommandArgument implements Comparable> public CommandArgument build() { if (this.parser == null && this.manager != null) { this.parser = this.manager.getParserRegistry().createParser(TypeToken.of(valueType), ParserParameters.empty()) - .orElse(null); + .orElse(null); } if (this.parser == null) { this.parser = (c, i) -> ArgumentParseResult diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/StaticArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/StaticArgument.java index ffbcdeb8..b0aa99c8 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/StaticArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/StaticArgument.java @@ -57,7 +57,7 @@ public final class StaticArgument extends CommandArgument { */ @Nonnull public static StaticArgument required(@Nonnull final String name, - @Nonnull final String... aliases) { + @Nonnull final String... aliases) { return new StaticArgument<>(true, name, aliases); } @@ -71,7 +71,7 @@ public final class StaticArgument extends CommandArgument { */ @Nonnull public static StaticArgument optional(@Nonnull final String name, - @Nonnull final String... aliases) { + @Nonnull final String... aliases) { return new StaticArgument<>(false, name, aliases); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameter.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameter.java index 2ce61fc2..0edd98f4 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameter.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameter.java @@ -54,7 +54,8 @@ public class ParserParameter { * * @return Parameter key */ - @Nonnull public String getKey() { + @Nonnull + public String getKey() { return this.key; } @@ -63,7 +64,8 @@ public class ParserParameter { * * @return Expected type */ - @Nonnull public TypeToken getExpectedType() { + @Nonnull + public TypeToken getExpectedType() { return this.expectedType; } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameters.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameters.java index 2c84c2da..98ee984a 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameters.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/ParserParameters.java @@ -52,7 +52,7 @@ public final class ParserParameters { * @param parameter Parameter * @param value Value * @param Value type - * @return Constructed instance + * @return Constructed instance */ @Nonnull public static ParserParameters single(@Nonnull final ParserParameter parameter, @Nonnull final T value) { diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java index 47db61fc..0491331e 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParameters.java @@ -32,29 +32,26 @@ import javax.annotation.Nonnull; */ public final class StandardParameters { - private StandardParameters() { - } - /** * Minimum value accepted by a numerical parser */ public static final ParserParameter RANGE_MIN = create("min", TypeToken.of(Number.class)); - /** * Maximum value accepted by a numerical parser */ public static final ParserParameter RANGE_MAX = create("max", TypeToken.of(Number.class)); - /** * Command description */ public static final ParserParameter DESCRIPTION = create("description", TypeToken.of(String.class)); - /** * Command completions */ public static final ParserParameter COMPLETIONS = create("completions", TypeToken.of(String[].class)); + private StandardParameters() { + } + private static ParserParameter create(@Nonnull final String key, @Nonnull final TypeToken expectedType) { return new ParserParameter<>(key, expectedType); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java index db75babe..32c908e0 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/parser/StandardParserRegistry.java @@ -148,9 +148,9 @@ public final class StandardParserRegistry implements ParserRegistry { if (producer == null) { /* Give enums special treatment */ if (actualType.isSubtypeOf(Enum.class)) { - @SuppressWarnings("all") - final EnumArgument.EnumParser enumArgument = new EnumArgument.EnumParser((Class) - actualType.getRawType()); + @SuppressWarnings("all") final EnumArgument.EnumParser enumArgument = new EnumArgument.EnumParser((Class) + actualType + .getRawType()); // noinspection all return Optional.of(enumArgument); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/BooleanArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/BooleanArgument.java index cd302044..98e8b3fe 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/BooleanArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/BooleanArgument.java @@ -89,10 +89,18 @@ public final class BooleanArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final String defaultNum) { + final String defaultNum) { return BooleanArgument.newBuilder(name).asOptionalWithDefault(defaultNum).build(); } + /** + * Get the liberal boolean + * + * @return Liberal boolean + */ + public boolean isLiberal() { + return liberal; + } public static final class Builder extends CommandArgument.Builder { @@ -127,16 +135,6 @@ public final class BooleanArgument extends CommandArgument { } - /** - * Get the liberal boolean - * - * @return Liberal boolean - */ - public boolean isLiberal() { - return liberal; - } - - public static final class BooleanParser implements ArgumentParser { private static final List LIBERAL = Arrays.asList("TRUE", "YES", "ON", "FALSE", "NO", "OFF"); diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ByteArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ByteArgument.java index 28e6a2c3..d3c27b07 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ByteArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ByteArgument.java @@ -26,8 +26,8 @@ package com.intellectualsites.commands.arguments.standard; import com.intellectualsites.commands.arguments.CommandArgument; import com.intellectualsites.commands.arguments.parser.ArgumentParseResult; import com.intellectualsites.commands.arguments.parser.ArgumentParser; -import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import javax.annotation.Nonnull; import java.util.List; @@ -90,11 +90,29 @@ public final class ByteArgument extends CommandArgument { * @param Command sender type * @return Created argument */ - @Nonnull public static CommandArgument optional(@Nonnull final String name, - final byte defaultNum) { + @Nonnull + public static CommandArgument optional(@Nonnull final String name, + final byte defaultNum) { return ByteArgument.newBuilder(name).asOptionalWithDefault(Byte.toString(defaultNum)).build(); } + /** + * Get the minimum accepted byteeger that could have been parsed + * + * @return Minimum byteeger + */ + public byte getMin() { + return this.min; + } + + /** + * Get the maximum accepted byteeger that could have been parsed + * + * @return Maximum byteeger + */ + public byte getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -142,26 +160,6 @@ public final class ByteArgument extends CommandArgument { } - - /** - * Get the minimum accepted byteeger that could have been parsed - * - * @return Minimum byteeger - */ - public byte getMin() { - return this.min; - } - - /** - * Get the maximum accepted byteeger that could have been parsed - * - * @return Maximum byteeger - */ - public byte getMax() { - return this.max; - } - - public static final class ByteParser implements ArgumentParser { private final byte min; diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/CharArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/CharArgument.java index b17004bc..4ed53742 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/CharArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/CharArgument.java @@ -85,7 +85,7 @@ public final class CharArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final String defaultNum) { + final String defaultNum) { return CharArgument.newBuilder(name).asOptionalWithDefault(defaultNum).build(); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/DoubleArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/DoubleArgument.java index 3fe37840..b88e130a 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/DoubleArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/DoubleArgument.java @@ -26,8 +26,8 @@ package com.intellectualsites.commands.arguments.standard; import com.intellectualsites.commands.arguments.CommandArgument; import com.intellectualsites.commands.arguments.parser.ArgumentParseResult; import com.intellectualsites.commands.arguments.parser.ArgumentParser; -import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import javax.annotation.Nonnull; import java.util.Queue; @@ -94,10 +94,27 @@ public final class DoubleArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final double defaultNum) { + final double defaultNum) { return DoubleArgument.newBuilder(name).asOptionalWithDefault(Double.toString(defaultNum)).build(); } + /** + * Get the minimum accepted double that could have been parsed + * + * @return Minimum double + */ + public double getMin() { + return this.min; + } + + /** + * Get the maximum accepted double that could have been parsed + * + * @return Maximum double + */ + public double getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -145,26 +162,6 @@ public final class DoubleArgument extends CommandArgument { } - - /** - * Get the minimum accepted double that could have been parsed - * - * @return Minimum double - */ - public double getMin() { - return this.min; - } - - /** - * Get the maximum accepted double that could have been parsed - * - * @return Maximum double - */ - public double getMax() { - return this.max; - } - - public static final class DoubleParser implements ArgumentParser { private final double min; diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/EnumArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/EnumArgument.java index 03e6b8fd..42144989 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/EnumArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/EnumArgument.java @@ -192,6 +192,14 @@ public class EnumArgument> extends CommandArgument { this.enumClass = enumClass; } + @Nonnull + @SuppressWarnings("all") + private static String join(@Nonnull final Class clazz) { + final EnumSet enumSet = EnumSet.allOf(clazz); + return enumSet.stream() + .map(e -> e.toString().toLowerCase()) + .collect(Collectors.joining(", ")); + } /** * Get the input provided by the sender @@ -217,15 +225,6 @@ public class EnumArgument> extends CommandArgument { return String.format("'%s' is not one of the following: %s", this.input, join(enumClass)); } - @Nonnull - @SuppressWarnings("all") - private static String join(@Nonnull final Class clazz) { - final EnumSet enumSet = EnumSet.allOf(clazz); - return enumSet.stream() - .map(e -> e.toString().toLowerCase()) - .collect(Collectors.joining(", ")); - } - } } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/FloatArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/FloatArgument.java index 991f1041..71a55128 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/FloatArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/FloatArgument.java @@ -26,8 +26,8 @@ package com.intellectualsites.commands.arguments.standard; import com.intellectualsites.commands.arguments.CommandArgument; import com.intellectualsites.commands.arguments.parser.ArgumentParseResult; import com.intellectualsites.commands.arguments.parser.ArgumentParser; -import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import javax.annotation.Nonnull; import java.util.Queue; @@ -94,10 +94,27 @@ public final class FloatArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final float defaultNum) { + final float defaultNum) { return FloatArgument.newBuilder(name).asOptionalWithDefault(Float.toString(defaultNum)).build(); } + /** + * Get the minimum accepted float that could have been parsed + * + * @return Minimum float + */ + public float getMin() { + return this.min; + } + + /** + * Get the maximum accepted float that could have been parsed + * + * @return Maximum float + */ + public float getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -145,26 +162,6 @@ public final class FloatArgument extends CommandArgument { } - - /** - * Get the minimum accepted float that could have been parsed - * - * @return Minimum float - */ - public float getMin() { - return this.min; - } - - /** - * Get the maximum accepted float that could have been parsed - * - * @return Maximum float - */ - public float getMax() { - return this.max; - } - - public static final class FloatParser implements ArgumentParser { private final float min; diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/IntegerArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/IntegerArgument.java index 420eeccb..25adae54 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/IntegerArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/IntegerArgument.java @@ -26,8 +26,8 @@ package com.intellectualsites.commands.arguments.standard; import com.intellectualsites.commands.arguments.CommandArgument; import com.intellectualsites.commands.arguments.parser.ArgumentParseResult; import com.intellectualsites.commands.arguments.parser.ArgumentParser; -import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import javax.annotation.Nonnull; import java.util.Collections; @@ -102,10 +102,27 @@ public final class IntegerArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final int defaultNum) { + final int defaultNum) { return IntegerArgument.newBuilder(name).asOptionalWithDefault(Integer.toString(defaultNum)).build(); } + /** + * Get the minimum accepted integer that could have been parsed + * + * @return Minimum integer + */ + public int getMin() { + return this.min; + } + + /** + * Get the maximum accepted integer that could have been parsed + * + * @return Maximum integer + */ + public int getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -153,26 +170,6 @@ public final class IntegerArgument extends CommandArgument { } - - /** - * Get the minimum accepted integer that could have been parsed - * - * @return Minimum integer - */ - public int getMin() { - return this.min; - } - - /** - * Get the maximum accepted integer that could have been parsed - * - * @return Maximum integer - */ - public int getMax() { - return this.max; - } - - public static final class IntegerParser implements ArgumentParser { private final int min; @@ -189,6 +186,29 @@ public final class IntegerArgument extends CommandArgument { this.max = max; } + @Nonnull + static List getSuggestions(final long min, final long max, @Nonnull final String input) { + if (input.isEmpty()) { + return IntStream.range(0, MAX_SUGGESTIONS_INCREMENT).mapToObj(Integer::toString).collect(Collectors.toList()); + } + try { + final long inputNum = Long.parseLong(input); + if (inputNum > max) { + return Collections.emptyList(); + } else { + final List suggestions = new LinkedList<>(); + suggestions.add(input); /* It's a valid number, so we suggest it */ + for (int i = 0; i < MAX_SUGGESTIONS_INCREMENT + && (inputNum * NUMBER_SHIFT_MULTIPLIER) + i <= max; i++) { + suggestions.add(Long.toString((inputNum * NUMBER_SHIFT_MULTIPLIER) + i)); + } + return suggestions; + } + } catch (final Exception ignored) { + return Collections.emptyList(); + } + } + @Nonnull @Override public ArgumentParseResult parse( @@ -240,29 +260,6 @@ public final class IntegerArgument extends CommandArgument { return getSuggestions(this.min, this.max, input); } - @Nonnull - static List getSuggestions(final long min, final long max, @Nonnull final String input) { - if (input.isEmpty()) { - return IntStream.range(0, MAX_SUGGESTIONS_INCREMENT).mapToObj(Integer::toString).collect(Collectors.toList()); - } - try { - final long inputNum = Long.parseLong(input); - if (inputNum > max) { - return Collections.emptyList(); - } else { - final List suggestions = new LinkedList<>(); - suggestions.add(input); /* It's a valid number, so we suggest it */ - for (int i = 0; i < MAX_SUGGESTIONS_INCREMENT - && (inputNum * NUMBER_SHIFT_MULTIPLIER) + i <= max; i++) { - suggestions.add(Long.toString((inputNum * NUMBER_SHIFT_MULTIPLIER) + i)); - } - return suggestions; - } - } catch (final Exception ignored) { - return Collections.emptyList(); - } - } - } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/LongArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/LongArgument.java index dfe4e9b6..65470ee6 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/LongArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/LongArgument.java @@ -26,8 +26,8 @@ package com.intellectualsites.commands.arguments.standard; import com.intellectualsites.commands.arguments.CommandArgument; import com.intellectualsites.commands.arguments.parser.ArgumentParseResult; import com.intellectualsites.commands.arguments.parser.ArgumentParser; -import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.exceptions.parsing.NumberParseException; import javax.annotation.Nonnull; import java.util.List; @@ -95,10 +95,27 @@ public final class LongArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final long defaultNum) { + final long defaultNum) { return LongArgument.newBuilder(name).asOptionalWithDefault(Long.toString(defaultNum)).build(); } + /** + * Get the minimum accepted long that could have been parsed + * + * @return Minimum long + */ + public long getMin() { + return this.min; + } + + /** + * Get the maximum accepted long that could have been parsed + * + * @return Maximum long + */ + public long getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -146,26 +163,6 @@ public final class LongArgument extends CommandArgument { } - - /** - * Get the minimum accepted long that could have been parsed - * - * @return Minimum long - */ - public long getMin() { - return this.min; - } - - /** - * Get the maximum accepted long that could have been parsed - * - * @return Maximum long - */ - public long getMax() { - return this.max; - } - - private static final class LongParser implements ArgumentParser { private final long min; diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ShortArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ShortArgument.java index 5bd07ed8..434a7a18 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ShortArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/ShortArgument.java @@ -95,10 +95,27 @@ public final class ShortArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final short defaultNum) { + final short defaultNum) { return ShortArgument.newBuilder(name).asOptionalWithDefault(Short.toString(defaultNum)).build(); } + /** + * Get the minimum accepted short that could have been parsed + * + * @return Minimum short + */ + public short getMin() { + return this.min; + } + + /** + * Get the maximum accepted short that could have been parsed + * + * @return Maximum short + */ + public short getMax() { + return this.max; + } public static final class Builder extends CommandArgument.Builder { @@ -146,26 +163,6 @@ public final class ShortArgument extends CommandArgument { } - - /** - * Get the minimum accepted short that could have been parsed - * - * @return Minimum short - */ - public short getMin() { - return this.min; - } - - /** - * Get the maximum accepted short that could have been parsed - * - * @return Maximum short - */ - public short getMax() { - return this.max; - } - - public static final class ShortParser implements ArgumentParser { private final short min; diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/StringArgument.java b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/StringArgument.java index 94fa6f3e..0abaee95 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/StringArgument.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/arguments/standard/StringArgument.java @@ -95,7 +95,7 @@ public final class StringArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - final String defaultNum) { + final String defaultNum) { return StringArgument.newBuilder(name).asOptionalWithDefault(defaultNum).build(); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java b/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java index bd8a1851..024955cb 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandExecutionCoordinator.java @@ -25,7 +25,6 @@ package com.intellectualsites.commands.execution; import com.intellectualsites.commands.CommandTree; import com.intellectualsites.commands.context.CommandContext; -import com.intellectualsites.commands.meta.CommandMeta; import javax.annotation.Nonnull; import java.util.Queue; @@ -39,18 +38,17 @@ import java.util.function.Function; * not command may be executed in parallel, etc. * * @param Command sender type - * @param Command meta type */ -public abstract class CommandExecutionCoordinator { +public abstract class CommandExecutionCoordinator { - private final CommandTree commandTree; + private final CommandTree commandTree; /** * Construct a new command execution coordinator * * @param commandTree Command tree */ - public CommandExecutionCoordinator(@Nonnull final CommandTree commandTree) { + public CommandExecutionCoordinator(@Nonnull final CommandTree commandTree) { this.commandTree = commandTree; } @@ -58,11 +56,10 @@ public abstract class CommandExecutionCoordinator { * Returns a simple command execution coordinator that executes all commands immediately, on the calling thread * * @param Command sender type - * @param Command meta type * @return New coordinator instance */ - public static Function, - CommandExecutionCoordinator> simpleCoordinator() { + public static Function, + CommandExecutionCoordinator> simpleCoordinator() { return SimpleCoordinator::new; } @@ -74,7 +71,7 @@ public abstract class CommandExecutionCoordinator { * @return Future that completes with the result */ public abstract CompletableFuture> coordinateExecution(@Nonnull CommandContext commandContext, - @Nonnull Queue input); + @Nonnull Queue input); /** * Get the command tree @@ -82,7 +79,7 @@ public abstract class CommandExecutionCoordinator { * @return Command tree */ @Nonnull - protected CommandTree getCommandTree() { + protected CommandTree getCommandTree() { return this.commandTree; } @@ -91,18 +88,17 @@ public abstract class CommandExecutionCoordinator { * A simple command execution coordinator that executes all commands immediately, on the calling thread * * @param Command sender type - * @param Command meta type */ - public static final class SimpleCoordinator extends - CommandExecutionCoordinator { + public static final class SimpleCoordinator extends + CommandExecutionCoordinator { - private SimpleCoordinator(@Nonnull final CommandTree commandTree) { + private SimpleCoordinator(@Nonnull final CommandTree commandTree) { super(commandTree); } @Override public CompletableFuture> coordinateExecution(@Nonnull final CommandContext commandContext, - @Nonnull final Queue input) { + @Nonnull final Queue input) { final CompletableFuture> completableFuture = new CompletableFuture<>(); try { this.getCommandTree().parse(commandContext, input).ifPresent( diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java b/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java index 2ebec0af..b93c3a90 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/execution/CommandResult.java @@ -50,7 +50,8 @@ public class CommandResult { * * @return Command context */ - @Nonnull public CommandContext getCommandContext() { + @Nonnull + public CommandContext getCommandContext() { return this.commandContext; } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/execution/preprocessor/CommandPreprocessor.java b/cloud-core/src/main/java/com/intellectualsites/commands/execution/preprocessor/CommandPreprocessor.java index 0174da3e..34c4be43 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/execution/preprocessor/CommandPreprocessor.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/execution/preprocessor/CommandPreprocessor.java @@ -33,7 +33,7 @@ import com.intellectualsites.services.types.ConsumerService; * {@link ConsumerService#interrupt()} * * @param Command sender type - * {@inheritDoc} + * {@inheritDoc} */ public interface CommandPreprocessor extends ConsumerService> { } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java b/cloud-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java index ce755568..704b7bae 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java @@ -24,7 +24,6 @@ package com.intellectualsites.commands.internal; import com.intellectualsites.commands.Command; -import com.intellectualsites.commands.meta.CommandMeta; import javax.annotation.Nonnull; @@ -32,11 +31,19 @@ import javax.annotation.Nonnull; * Utility that registers commands natively for whatever * platform the library is used in. This can do nothing, if * the target platform does not have its own concept of commands - * - * @param Command meta type */ @FunctionalInterface -public interface CommandRegistrationHandler { +public interface CommandRegistrationHandler { + + /** + * Create a new {@link CommandRegistrationHandler} that does nothing + * + * @return Constructed registration + */ + @Nonnull + static CommandRegistrationHandler nullCommandRegistrationHandler() { + return new NullCommandRegistrationHandler(); + } /** * Attempt to register the command @@ -45,26 +52,15 @@ public interface CommandRegistrationHandler { * @return {@code true} if the command was registered successfully, * else {@code false} */ - boolean registerCommand(@Nonnull Command command); + boolean registerCommand(@Nonnull Command command); - /** - * Create a new {@link CommandRegistrationHandler} that does nothing - * - * @param Command meta type - * @return Constructed registration - */ - static CommandRegistrationHandler nullCommandRegistrationHandler() { - return new NullCommandRegistrationHandler<>(); - } - - - final class NullCommandRegistrationHandler implements CommandRegistrationHandler { + final class NullCommandRegistrationHandler implements CommandRegistrationHandler { private NullCommandRegistrationHandler() { } @Override - public boolean registerCommand(@Nonnull final Command command) { + public boolean registerCommand(@Nonnull final Command command) { return true; } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/meta/CommandMeta.java b/cloud-core/src/main/java/com/intellectualsites/commands/meta/CommandMeta.java index a732ba57..d99db69e 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/meta/CommandMeta.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/meta/CommandMeta.java @@ -24,6 +24,8 @@ package com.intellectualsites.commands.meta; import javax.annotation.Nonnull; +import java.util.Map; +import java.util.Optional; /** * Object that is associated with a {@link com.intellectualsites.commands.Command}. @@ -31,7 +33,7 @@ import javax.annotation.Nonnull; *

* Appropriate use for command meta would be fixed state, such as command descriptions. */ -public class CommandMeta { +public abstract class CommandMeta { /** * Create a new simple command meta builder @@ -49,4 +51,31 @@ public class CommandMeta { return ""; } + /** + * Get the value associated with a key + * + * @param key Key + * @return Optional that may contain the associated value + */ + @Nonnull + public abstract Optional getValue(@Nonnull String key); + + /** + * Get the value if it exists, else return the default value + * + * @param key Key + * @param defaultValue Default value + * @return Value, or default value + */ + @Nonnull + public abstract String getOrDefault(@Nonnull String key, @Nonnull String defaultValue); + + /** + * Get a copy of the meta map + * + * @return Copy of meta map + */ + @Nonnull + public abstract Map getAll(); + } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/meta/SimpleCommandMeta.java b/cloud-core/src/main/java/com/intellectualsites/commands/meta/SimpleCommandMeta.java index 6367d2d9..ee04530c 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/meta/SimpleCommandMeta.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/meta/SimpleCommandMeta.java @@ -57,40 +57,26 @@ public class SimpleCommandMeta extends CommandMeta { * * @return Empty instance */ - @Nonnull public static SimpleCommandMeta empty() { + @Nonnull + public static SimpleCommandMeta empty() { return SimpleCommandMeta.builder().build(); } - /** - * Get the value associated with a key - * - * @param key Key - * @return Optional that may contain the associated value - */ + @Override @Nonnull - public Optional getValue(@Nonnull final String key) { + public final Optional getValue(@Nonnull final String key) { return Optional.ofNullable(this.metaMap.get(key)); } - /** - * Get the value if it exists, else return the default value - * - * @param key Key - * @param defaultValue Default value - * @return Value, or default value - */ + @Override @Nonnull - public String getOrDefault(@Nonnull final String key, @Nonnull final String defaultValue) { + public final String getOrDefault(@Nonnull final String key, @Nonnull final String defaultValue) { return this.getValue(key).orElse(defaultValue); } - /** - * Get a copy of the meta map - * - * @return Copy of meta map - */ + @Override @Nonnull - public Map getAll() { + public final Map getAll() { return new HashMap<>(this.metaMap); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/package-info.java b/cloud-core/src/main/java/com/intellectualsites/commands/package-info.java index b3f44e0e..ed297f9d 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/package-info.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/package-info.java @@ -24,6 +24,7 @@ /** * cloud API main package + * * @see com.intellectualsites.commands.CommandManager Command manager class */ package com.intellectualsites.commands; diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/CommandPreProcessorTest.java b/cloud-core/src/test/java/com/intellectualsites/commands/CommandPreProcessorTest.java index a1521063..b865c4c8 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/CommandPreProcessorTest.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/CommandPreProcessorTest.java @@ -36,23 +36,23 @@ import javax.annotation.Nonnull; public class CommandPreProcessorTest { - private static CommandManager manager; + private static CommandManager manager; @BeforeAll static void newTree() { manager = new TestCommandManager(); manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty()) - .argument(EnumArgument.required(SampleEnum.class, "enum")) - .handler( - commandContext -> System.out.printf("enum = %s | integer = %d\n", - commandContext.get( - "enum").orElse( - SampleEnum.VALUE1), - commandContext.get( - "int").orElseThrow( - () -> new NullPointerException( - "int")))) - .build()); + .argument(EnumArgument.required(SampleEnum.class, "enum")) + .handler( + commandContext -> System.out.printf("enum = %s | integer = %d\n", + commandContext.get( + "enum").orElse( + SampleEnum.VALUE1), + commandContext.get( + "int").orElseThrow( + () -> new NullPointerException( + "int")))) + .build()); manager.registerCommandPreProcessor(new SamplePreprocessor()); } diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/CommandSuggestionsTest.java b/cloud-core/src/test/java/com/intellectualsites/commands/CommandSuggestionsTest.java index 86269376..b1708c04 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/CommandSuggestionsTest.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/CommandSuggestionsTest.java @@ -26,7 +26,6 @@ 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; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -37,7 +36,7 @@ import java.util.List; public class CommandSuggestionsTest { - private static CommandManager manager; + private static CommandManager manager; @BeforeAll static void setupManager() { @@ -47,18 +46,18 @@ public class CommandSuggestionsTest { manager.command(manager.commandBuilder("test") .literal("var") .argument(StringArgument.newBuilder("str") - .withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")) - .build()) + .withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")) + .build()) .argument(EnumArgument.required(TestEnum.class, "enum")) .build()); manager.command(manager.commandBuilder("test") - .literal("comb") - .argument(StringArgument.newBuilder("str") + .literal("comb") + .argument(StringArgument.newBuilder("str") .withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")) .build()) - .argument(IntegerArgument.newBuilder("num") + .argument(IntegerArgument.newBuilder("num") .withMin(1).withMax(95).asOptional().build()) - .build()); + .build()); } @Test @@ -68,7 +67,7 @@ public class CommandSuggestionsTest { Assertions.assertTrue(suggestions.isEmpty()); final String input2 = "test "; final List suggestions2 = manager.suggest(new TestCommandSender(), input2); - Assertions.assertEquals(Arrays.asList("comb", "one", "two","var"), suggestions2); + Assertions.assertEquals(Arrays.asList("comb", "one", "two", "var"), suggestions2); } @Test @@ -109,7 +108,8 @@ public class CommandSuggestionsTest { public enum TestEnum { - FOO, BAR + FOO, + BAR } } diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java b/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java index 254f09b2..3f7c75c8 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java @@ -40,7 +40,7 @@ import java.util.concurrent.CompletionException; class CommandTreeTest { private static final int EXPECTED_INPUT_NUMBER = 15; - private static CommandManager manager; + private static CommandManager manager; @BeforeAll static void newTree() { @@ -53,25 +53,25 @@ class CommandTreeTest { SimpleCommandMeta.empty()) .literal("opt", "öpt") .argument(IntegerArgument - .optional("num", EXPECTED_INPUT_NUMBER)) + .optional("num", EXPECTED_INPUT_NUMBER)) .build()) .command(manager.commandBuilder("req").withSenderType(SpecificCommandSender.class).build()); } @Test void parse() { - final Optional> command = manager.getCommandTree() - .parse(new CommandContext<>( - new TestCommandSender()), - new LinkedList<>( - Arrays.asList("test", - "one"))); + final Optional> command = manager.getCommandTree() + .parse(new CommandContext<>( + new TestCommandSender()), + new LinkedList<>( + Arrays.asList("test", + "one"))); Assertions.assertTrue(command.isPresent()); Assertions.assertThrows(NoPermissionException.class, () -> manager.getCommandTree() .parse(new CommandContext<>( - new TestCommandSender()), - new LinkedList<>( - Arrays.asList("test", "two")))); + new TestCommandSender()), + new LinkedList<>( + Arrays.asList("test", "two")))); manager.getCommandTree() .parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt"))) .ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()))); @@ -106,9 +106,9 @@ class CommandTreeTest { manager.commandBuilder("default") .argument(manager.argumentBuilder(Integer.class, "int").build()) .handler(context -> { - final int number = context.getRequired("int"); - System.out.printf("Supplied number is: %d\n", number); - }) + final int number = context.getRequired("int"); + System.out.printf("Supplied number is: %d\n", number); + }) .build() ); manager.executeCommand(new TestCommandSender(), "default 5").join(); diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/ParserRegistryTest.java b/cloud-core/src/test/java/com/intellectualsites/commands/ParserRegistryTest.java index 8a9420c0..8c707554 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/ParserRegistryTest.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/ParserRegistryTest.java @@ -72,12 +72,12 @@ public class ParserRegistryTest { Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MIN)); Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MAX)); final ArgumentParser parser = parserRegistry.createParser(parsedType, - parserParameters) - .orElseThrow( - () -> new NullPointerException("No parser found")); + parserParameters) + .orElseThrow( + () -> new NullPointerException( + "No parser found")); Assertions.assertTrue(parser instanceof IntegerArgument.IntegerParser); - @SuppressWarnings("unchecked") - final IntegerArgument.IntegerParser integerParser = + @SuppressWarnings("unchecked") final IntegerArgument.IntegerParser integerParser = (IntegerArgument.IntegerParser) parser; Assertions.assertEquals(RANGE_MIN, integerParser.getMin()); Assertions.assertEquals(RANGE_MAX, integerParser.getMax()); diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/TestCommandManager.java b/cloud-core/src/test/java/com/intellectualsites/commands/TestCommandManager.java index 2c8423c5..2b9ad31b 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/TestCommandManager.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/TestCommandManager.java @@ -29,7 +29,7 @@ import com.intellectualsites.commands.meta.SimpleCommandMeta; import javax.annotation.Nonnull; -public class TestCommandManager extends CommandManager { +public class TestCommandManager extends CommandManager { protected TestCommandManager() { super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler()); diff --git a/cloud-jline/src/main/java/com/intellectualsites/commands/jline/JLineCommandManager.java b/cloud-jline/src/main/java/com/intellectualsites/commands/jline/JLineCommandManager.java index ac02fa2d..8512a20d 100644 --- a/cloud-jline/src/main/java/com/intellectualsites/commands/jline/JLineCommandManager.java +++ b/cloud-jline/src/main/java/com/intellectualsites/commands/jline/JLineCommandManager.java @@ -47,15 +47,15 @@ import java.util.function.Function; /** * Command manager for use with JLine */ -public class JLineCommandManager extends CommandManager implements Completer { +public class JLineCommandManager extends CommandManager implements Completer { /** * Construct a new JLine command manager * * @param executionCoordinatorFunction Function producing a new coordinator */ - public JLineCommandManager(@Nonnull final Function, - CommandExecutionCoordinator> executionCoordinatorFunction) { + public JLineCommandManager(@Nonnull final Function, + CommandExecutionCoordinator> executionCoordinatorFunction) { super(executionCoordinatorFunction, CommandRegistrationHandler.nullCommandRegistrationHandler()); } @@ -78,35 +78,35 @@ public class JLineCommandManager extends CommandManager - shouldStop[0] = true) + shouldStop[0] = true) .build()) .command(jLineCommandManager.commandBuilder("echo", SimpleCommandMeta.empty()) - .argument(String.class, "string", builder -> - builder.asRequired() - .withParser(((commandContext, inputQueue) -> { - final StringBuilder stringBuilder = - new StringBuilder(); - while (!inputQueue.isEmpty()) { - stringBuilder.append(inputQueue.remove()); - if (!inputQueue.isEmpty()) { - stringBuilder.append(" "); - } - } - return ArgumentParseResult.success( - stringBuilder.toString()); - })).build()) - .handler(commandContext -> commandContext.get("string") - .ifPresent( - System.out::println)) - .build()) + .argument(String.class, "string", builder -> + builder.asRequired() + .withParser(((commandContext, inputQueue) -> { + final StringBuilder stringBuilder = + new StringBuilder(); + while (!inputQueue.isEmpty()) { + stringBuilder.append(inputQueue.remove()); + if (!inputQueue.isEmpty()) { + stringBuilder.append(" "); + } + } + return ArgumentParseResult.success( + stringBuilder.toString()); + })).build()) + .handler(commandContext -> commandContext.get("string") + .ifPresent( + System.out::println)) + .build()) .command(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty()) - .argument(StaticArgument.required("one")) - .handler(commandContext -> System.out.println("Test (1)")) - .build()) + .argument(StaticArgument.required("one")) + .handler(commandContext -> System.out.println("Test (1)")) + .build()) .command(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty()) - .argument(StaticArgument.required("two")) - .handler(commandContext -> System.out.println("Test (2)")) - .build()); + .argument(StaticArgument.required("two")) + .handler(commandContext -> System.out.println("Test (2)")) + .build()); System.out.println("Ready..."); while (!shouldStop[0]) { final String line = lineReader.readLine(); @@ -139,8 +139,8 @@ public class JLineCommandManager extends CommandManager list) { + @Nonnull final ParsedLine parsedLine, + @Nonnull final List list) { final String line = parsedLine.line(); if (line == null || line.isEmpty() || !line.startsWith("/")) { System.out.println("Cannot suggest: empty line"); @@ -157,7 +157,7 @@ public class JLineCommandManager extends CommandManager { ? extends ArgumentType>> mappers; private final Map, Supplier>> defaultArgumentTypeSuppliers; private final Supplier> dummyContextProvider; - private final CommandManager commandManager; + private final CommandManager commandManager; /** * Create a new cloud brigadier manager @@ -89,7 +89,7 @@ public final class CloudBrigadierManager { * @param commandManager Command manager * @param dummyContextProvider Provider of dummy context for completions */ - public CloudBrigadierManager(@Nonnull final CommandManager commandManager, + public CloudBrigadierManager(@Nonnull final CommandManager commandManager, @Nonnull final Supplier> dummyContextProvider) { this.mappers = Maps.newHashMap(); @@ -184,14 +184,14 @@ public final class CloudBrigadierManager { * Register a cloud-Brigadier mapping * * @param argumentType cloud argument type - * @param mapper mapper function - * @param cloud argument value type - * @param cloud argument type - * @param Brigadier argument type value + * @param mapper mapper function + * @param cloud argument value type + * @param cloud argument type + * @param Brigadier argument type value */ public , O> void registerMapping(@Nonnull final TypeToken argumentType, @Nonnull final Function> mapper) { + ? extends ArgumentType> mapper) { this.mappers.put(argumentType.getRawType(), mapper); } @@ -211,8 +211,8 @@ public final class CloudBrigadierManager { * * @param argumentType cloud argument type * @param argument cloud argument - * @param cloud argument value type (generic) - * @param cloud argument type (generic) + * @param cloud argument value type (generic) + * @param cloud argument type (generic) * @return Brigadier argument type */ @Nullable @@ -268,9 +268,9 @@ public final class CloudBrigadierManager { } private ArgumentBuilder constructCommandNode(@Nonnull final CommandTree.Node> root, - @Nonnull final BiPredicate permissionChecker, - @Nonnull final com.mojang.brigadier.Command executor, - @Nonnull final SuggestionProvider suggestionProvider) { + @Nonnull final BiPredicate permissionChecker, + @Nonnull final com.mojang.brigadier.Command executor, + @Nonnull final SuggestionProvider suggestionProvider) { ArgumentBuilder argumentBuilder; if (root.getValue() instanceof StaticArgument) { diff --git a/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java b/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java index 28a9bd6d..cef4b288 100644 --- a/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java +++ b/cloud-minecraft/cloud-bukkit-test/src/main/java/com/intellectualsites/commands/BukkitTest.java @@ -37,11 +37,11 @@ import com.intellectualsites.commands.arguments.standard.EnumArgument; import com.intellectualsites.commands.arguments.standard.FloatArgument; import com.intellectualsites.commands.arguments.standard.IntegerArgument; import com.intellectualsites.commands.arguments.standard.StringArgument; -import com.intellectualsites.commands.bukkit.BukkitCommandMeta; import com.intellectualsites.commands.bukkit.BukkitCommandMetaBuilder; -import com.intellectualsites.commands.execution.CommandExecutionCoordinator; -import com.intellectualsites.commands.paper.PaperCommandManager; import com.intellectualsites.commands.bukkit.parsers.WorldArgument; +import com.intellectualsites.commands.execution.CommandExecutionCoordinator; +import com.intellectualsites.commands.meta.SimpleCommandMeta; +import com.intellectualsites.commands.paper.PaperCommandManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; @@ -54,7 +54,6 @@ 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; @@ -68,46 +67,43 @@ public final class BukkitTest extends JavaPlugin { @Override public void onEnable() { try { - final PaperCommandManager mgr = new PaperCommandManager<>( + final CommandManager mgr = new PaperCommandManager<>( this, - CommandExecutionCoordinator - .simpleCoordinator(), + CommandExecutionCoordinator + .simpleCoordinator(), Function.identity(), Function.identity() ); - final AnnotationParser annotationParser + final AnnotationParser 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"), - BukkitCommandMetaBuilder.builder() - .withDescription("Your ugli") - .build()) + //noinspection all + ((PaperCommandManager) mgr).registerBrigadier(); + mgr.command(mgr.commandBuilder("gamemode", this.metaWithDescription("Your ugli"), "gajmöde") .argument(EnumArgument.required(GameMode.class, "gamemode")) .argument(StringArgument.newBuilder("player") - .withSuggestionsProvider((v1, v2) -> { - final List suggestions = - new ArrayList<>( - Bukkit.getOnlinePlayers() - .stream() - .map(Player::getName) - .collect(Collectors.toList())); - suggestions.add("dog"); - suggestions.add("cat"); - return suggestions; - }).build()) + .withSuggestionsProvider((v1, v2) -> { + final List suggestions = + new ArrayList<>( + Bukkit.getOnlinePlayers() + .stream() + .map(Player::getName) + .collect(Collectors.toList())); + suggestions.add("dog"); + suggestions.add("cat"); + return suggestions; + }).build()) .handler(c -> ((Player) c.getSender()) - .setGameMode(c.get("gamemode") - .orElse(GameMode.SURVIVAL))) + .setGameMode(c.get("gamemode") + .orElse(GameMode.SURVIVAL))) .build()) .command(mgr.commandBuilder("kenny") .literal("sux") .argument(IntegerArgument - .newBuilder("perc") - .withMin(PERC_MIN).withMax(PERC_MAX).build()) + .newBuilder("perc") + .withMin(PERC_MIN).withMax(PERC_MAX).build()) .handler(context -> { ((Player) context.getSender()).sendMessage(String.format( "Kenny sux %d%%", @@ -182,4 +178,9 @@ public final class BukkitTest extends JavaPlugin { player.sendMessage(ChatColor.GOLD + "Your input was: " + ChatColor.AQUA + input + ChatColor.GREEN + " (" + number + ")"); } + @Nonnull + private SimpleCommandMeta metaWithDescription(@Nonnull final String description) { + return BukkitCommandMetaBuilder.builder().withDescription(description).build(); + } + } diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java index c14c9681..fc499312 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommand.java @@ -48,10 +48,10 @@ final class BukkitCommand extends org.bukkit.command.Command implements Plugi private final CommandArgument command; private final BukkitCommandManager bukkitCommandManager; - private final Command cloudCommand; + private final Command cloudCommand; @SuppressWarnings("unchecked") - BukkitCommand(@Nonnull final Command cloudCommand, + BukkitCommand(@Nonnull final Command cloudCommand, @Nonnull final CommandArgument command, @Nonnull final BukkitCommandManager bukkitCommandManager) { super(command.getName(), @@ -76,9 +76,9 @@ final class BukkitCommand extends org.bukkit.command.Command implements Plugi if (throwable != null) { if (throwable instanceof InvalidSyntaxException) { commandSender.sendMessage(ChatColor.RED + "Invalid Command Syntax. " - + "Correct command syntax is: " - + ChatColor.GRAY + "/" - + ((InvalidSyntaxException) throwable).getCorrectSyntax()); + + "Correct command syntax is: " + + ChatColor.GRAY + "/" + + ((InvalidSyntaxException) throwable).getCorrectSyntax()); } else if (throwable instanceof InvalidCommandSenderException) { commandSender.sendMessage(ChatColor.RED + throwable.getMessage()); } else if (throwable instanceof NoPermissionException) { @@ -87,7 +87,8 @@ final class BukkitCommand extends org.bukkit.command.Command implements Plugi commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND); } else if (throwable instanceof ArgumentParseException) { commandSender.sendMessage(ChatColor.RED + "Invalid Command Argument: " - + ChatColor.GRAY + throwable.getCause().getMessage()); + + ChatColor.GRAY + throwable.getCause() + .getMessage()); } else { commandSender.sendMessage(throwable.getMessage()); throwable.printStackTrace(); diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java index a95e72b9..35902233 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandManager.java @@ -43,8 +43,7 @@ import java.util.function.Function; * * @param Command sender type */ -public class BukkitCommandManager - extends CommandManager { +public class BukkitCommandManager extends CommandManager { private final Plugin owningPlugin; @@ -61,8 +60,8 @@ public class BukkitCommandManager * @throws Exception If the construction of the manager fails */ public BukkitCommandManager(@Nonnull final Plugin owningPlugin, - @Nonnull final Function, - CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final Function, + CommandExecutionCoordinator> commandExecutionCoordinator, @Nonnull final Function commandSenderMapper, @Nonnull final Function backwardsCommandSenderMapper) throws Exception { diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandMetaBuilder.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandMetaBuilder.java index 2a646dcc..cb47c508 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandMetaBuilder.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitCommandMetaBuilder.java @@ -37,7 +37,8 @@ public final class BukkitCommandMetaBuilder { * * @return Builder instance */ - @Nonnull public static BuilderStage1 builder() { + @Nonnull + public static BuilderStage1 builder() { return new BuilderStage1(); } @@ -53,7 +54,8 @@ public final class BukkitCommandMetaBuilder { * @param description Command description * @return Builder instance */ - @Nonnull public BuilderStage2 withDescription(@Nonnull final String description) { + @Nonnull + public BuilderStage2 withDescription(@Nonnull final String description) { return new BuilderStage2(description); } @@ -73,7 +75,8 @@ public final class BukkitCommandMetaBuilder { * * @return Meta instance */ - @Nonnull public BukkitCommandMeta build() { + @Nonnull + public BukkitCommandMeta build() { return new BukkitCommandMeta(CommandMeta.simple().with("description", this.description).build()); } diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java index f7fbe294..cbac2f8d 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/BukkitPluginRegistrationHandler.java @@ -37,7 +37,7 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -final class BukkitPluginRegistrationHandler implements CommandRegistrationHandler { +final class BukkitPluginRegistrationHandler implements CommandRegistrationHandler { private final Map, org.bukkit.command.Command> registeredCommands = new HashMap<>(); @@ -62,7 +62,7 @@ final class BukkitPluginRegistrationHandler implements CommandRegistrationHan } @Override - public boolean registerCommand(@Nonnull final Command command) { + public boolean registerCommand(@Nonnull final Command command) { /* We only care about the root command argument */ final CommandArgument commandArgument = command.getArguments().get(0); if (this.registeredCommands.containsKey(commandArgument)) { @@ -75,7 +75,7 @@ final class BukkitPluginRegistrationHandler implements CommandRegistrationHan label = commandArgument.getName(); } @SuppressWarnings("unchecked") final BukkitCommand bukkitCommand = new BukkitCommand<>( - (Command) command, + (Command) command, (CommandArgument) commandArgument, this.bukkitCommandManager); this.registeredCommands.put(commandArgument, bukkitCommand); diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/MaterialArgument.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/MaterialArgument.java index b812c2c3..85571f60 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/MaterialArgument.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/MaterialArgument.java @@ -88,7 +88,7 @@ public class MaterialArgument extends CommandArgument { * * @param name Argument name * @param material Default value - * @param Command sender type + * @param Command sender type * @return Created argument */ @Nonnull diff --git a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/WorldArgument.java b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/WorldArgument.java index 81bc75ce..e85a54b8 100644 --- a/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/WorldArgument.java +++ b/cloud-minecraft/cloud-bukkit/src/main/java/com/intellectualsites/commands/bukkit/parsers/WorldArgument.java @@ -94,7 +94,7 @@ public class WorldArgument extends CommandArgument { */ @Nonnull public static CommandArgument optional(@Nonnull final String name, - @Nonnull final String defaultValue) { + @Nonnull final String defaultValue) { return WorldArgument.newBuilder(name).asOptionalWithDefault(defaultValue).build(); } diff --git a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommand.java b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommand.java index d8c0e110..ad5cb993 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommand.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommand.java @@ -30,7 +30,6 @@ import com.intellectualsites.commands.exceptions.InvalidCommandSenderException; import com.intellectualsites.commands.exceptions.InvalidSyntaxException; import com.intellectualsites.commands.exceptions.NoPermissionException; import com.intellectualsites.commands.exceptions.NoSuchCommandException; -import com.intellectualsites.commands.meta.SimpleCommandMeta; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.chat.ComponentBuilder; @@ -42,16 +41,16 @@ import javax.annotation.Nonnull; public final class BungeeCommand extends Command implements TabExecutor { private static final String MESSAGE_NO_PERMS = - "I'm sorry, but you do not have permission to perform this command. " - + "Please contact the server administrators if you believe that this is in error."; + "I'm sorry, but you do not have permission to perform this command. " + + "Please contact the server administrators if you believe that this is in error."; private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help."; private final BungeeCommandManager bungeeCommandManager; private final CommandArgument command; - private final com.intellectualsites.commands.Command cloudCommand; + private final com.intellectualsites.commands.Command cloudCommand; @SuppressWarnings("unchecked") - BungeeCommand(@Nonnull final com.intellectualsites.commands.Command cloudCommand, + BungeeCommand(@Nonnull final com.intellectualsites.commands.Command cloudCommand, @Nonnull final CommandArgument command, @Nonnull final BungeeCommandManager bungeeCommandManager) { super(command.getName(), @@ -71,41 +70,41 @@ public final class BungeeCommand extends Command implements TabExecutor { } this.bungeeCommandManager.executeCommand(this.bungeeCommandManager.getCommandSenderMapper().apply(commandSender), builder.toString()) - .whenComplete(((commandResult, throwable) -> { - if (throwable != null) { - if (throwable instanceof InvalidSyntaxException) { - commandSender.sendMessage( - new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ") - .color(ChatColor.RED) - .append("/") - .color(ChatColor.GRAY) - .append(((InvalidSyntaxException) throwable).getCorrectSyntax()) - .color(ChatColor.GRAY) - .create() - ); - } else if (throwable instanceof InvalidCommandSenderException) { - commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()) - .color(ChatColor.RED) - .create()); - } else if (throwable instanceof NoPermissionException) { - commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS) - .color(ChatColor.WHITE) - .create()); - } else if (throwable instanceof NoSuchCommandException) { - commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND) - .color(ChatColor.WHITE) - .create()); - } else if (throwable instanceof ArgumentParseException) { - commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ") - .color(ChatColor.GRAY) - .append(throwable.getCause().getMessage()) - .create()); - } else { - commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create()); - throwable.printStackTrace(); - } - } - })); + .whenComplete(((commandResult, throwable) -> { + if (throwable != null) { + if (throwable instanceof InvalidSyntaxException) { + commandSender.sendMessage( + new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ") + .color(ChatColor.RED) + .append("/") + .color(ChatColor.GRAY) + .append(((InvalidSyntaxException) throwable).getCorrectSyntax()) + .color(ChatColor.GRAY) + .create() + ); + } else if (throwable instanceof InvalidCommandSenderException) { + commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()) + .color(ChatColor.RED) + .create()); + } else if (throwable instanceof NoPermissionException) { + commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS) + .color(ChatColor.WHITE) + .create()); + } else if (throwable instanceof NoSuchCommandException) { + commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND) + .color(ChatColor.WHITE) + .create()); + } else if (throwable instanceof ArgumentParseException) { + commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ") + .color(ChatColor.GRAY) + .append(throwable.getCause().getMessage()) + .create()); + } else { + commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create()); + throwable.printStackTrace(); + } + } + })); } @Override diff --git a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java index 68f78ad8..996a2f71 100644 --- a/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java +++ b/cloud-minecraft/cloud-bungee/src/main/java/com/intellectualsites/commands/bungee/BungeeCommandManager.java @@ -33,7 +33,7 @@ import net.md_5.bungee.api.plugin.Plugin; import javax.annotation.Nonnull; import java.util.function.Function; -public class BungeeCommandManager extends CommandManager { +public class BungeeCommandManager extends CommandManager { private final Plugin owningPlugin; private final Function commandSenderMapper; @@ -49,8 +49,8 @@ public class BungeeCommandManager extends CommandManager, - CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final Function, + CommandExecutionCoordinator> commandExecutionCoordinator, @Nonnull final Function commandSenderMapper, @Nonnull final Function backwardsCommandSenderMapper) throws Exception { @@ -63,7 +63,7 @@ public class BungeeCommandManager extends CommandManager implements - CommandRegistrationHandler { +final class BungeePluginRegistrationHandler implements CommandRegistrationHandler { private final Map, net.md_5.bungee.api.plugin.Command> registeredCommands = new HashMap<>(); @@ -47,19 +45,19 @@ final class BungeePluginRegistrationHandler implements } @Override - public boolean registerCommand(@Nonnull final Command command) { + public boolean registerCommand(@Nonnull final Command command) { /* We only care about the root command argument */ final CommandArgument commandArgument = command.getArguments().get(0); if (this.registeredCommands.containsKey(commandArgument)) { return false; } @SuppressWarnings("unchecked") final BungeeCommand bungeeCommand = new BungeeCommand<>( - (Command) command, + (Command) command, (CommandArgument) commandArgument, this.bungeeCommandManager); this.registeredCommands.put(commandArgument, bungeeCommand); this.bungeeCommandManager.getOwningPlugin().getProxy().getPluginManager() - .registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand); + .registerCommand(this.bungeeCommandManager.getOwningPlugin(), bungeeCommand); return true; } diff --git a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperBrigadierListener.java b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperBrigadierListener.java index 614a8f53..552e1d0b 100644 --- a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperBrigadierListener.java +++ b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperBrigadierListener.java @@ -25,10 +25,9 @@ package com.intellectualsites.commands.paper; import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; -import com.intellectualsites.commands.bukkit.BukkitCommandMeta; import com.intellectualsites.commands.CommandTree; -import com.intellectualsites.commands.brigadier.CloudBrigadierManager; import com.intellectualsites.commands.arguments.CommandArgument; +import com.intellectualsites.commands.brigadier.CloudBrigadierManager; import com.intellectualsites.commands.context.CommandContext; import com.mojang.brigadier.arguments.ArgumentType; import org.bukkit.Bukkit; @@ -52,8 +51,9 @@ class PaperBrigadierListener implements Listener { PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception { this.paperCommandManager = paperCommandManager; this.brigadierManager = new CloudBrigadierManager<>(this.paperCommandManager, - () -> new CommandContext<>(this.paperCommandManager.getCommandSenderMapper() - .apply(Bukkit.getConsoleSender()))); + () -> new CommandContext<>( + this.paperCommandManager.getCommandSenderMapper() + .apply(Bukkit.getConsoleSender()))); /* Register default mappings */ final String version = Bukkit.getServer().getClass().getPackage().getName(); this.nmsVersion = version.substring(version.lastIndexOf(".") + 1); @@ -112,7 +112,7 @@ class PaperBrigadierListener implements Listener { @EventHandler public void onCommandRegister(@Nonnull final CommandRegisteredEvent event) { - final CommandTree commandTree = this.paperCommandManager.getCommandTree(); + final CommandTree commandTree = this.paperCommandManager.getCommandTree(); final CommandTree.Node> node = commandTree.getNamedNode(event.getCommandLabel()); if (node == null) { return; diff --git a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java index c25c06b9..e0fc237c 100644 --- a/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java +++ b/cloud-minecraft/cloud-paper/src/main/java/com/intellectualsites/commands/paper/PaperCommandManager.java @@ -23,9 +23,8 @@ // package com.intellectualsites.commands.paper; -import com.intellectualsites.commands.bukkit.BukkitCommandManager; -import com.intellectualsites.commands.bukkit.BukkitCommandMeta; import com.intellectualsites.commands.CommandTree; +import com.intellectualsites.commands.bukkit.BukkitCommandManager; import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -46,15 +45,15 @@ public class PaperCommandManager /** * Construct a new Paper command manager * - * @param owningPlugin Plugin that is constructing the manager - * @param commandExecutionCoordinator Coordinator provider - * @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type + * @param owningPlugin Plugin that is constructing the manager + * @param commandExecutionCoordinator Coordinator provider + * @param commandSenderMapper Function that maps {@link CommandSender} to the command sender type * @param backwardsCommandSenderMapper Function that maps the command sender type to {@link CommandSender} * @throws Exception If the construction of the manager fails */ public PaperCommandManager(@Nonnull final Plugin owningPlugin, - @Nonnull final Function, - CommandExecutionCoordinator> commandExecutionCoordinator, + @Nonnull final Function, + CommandExecutionCoordinator> commandExecutionCoordinator, @Nonnull final Function commandSenderMapper, @Nonnull final Function backwardsCommandSenderMapper) throws Exception { @@ -65,7 +64,7 @@ public class PaperCommandManager * Attempt to register the Brigadier mapper, and return it. * * @return {@link PaperBrigadierListener} instance, if it could be created. If it cannot - * be created {@code null} is returned + * be created {@code null} is returned */ @Nullable public PaperBrigadierListener registerBrigadier() {