From b6c3111b3dee6b2fbf668d930315f47b1a2255e0 Mon Sep 17 00:00:00 2001 From: Reflxction Dev Date: Mon, 6 Feb 2023 19:52:17 +0300 Subject: [PATCH] Add ExecutableCommand#execute() methods --- .../commands/command/ExecutableCommand.java | 17 ++++ .../commands/core/CommandExecutable.java | 82 +++++++++++++------ 2 files changed, 76 insertions(+), 23 deletions(-) diff --git a/common/src/main/java/revxrsal/commands/command/ExecutableCommand.java b/common/src/main/java/revxrsal/commands/command/ExecutableCommand.java index 9f91a5a7..719f0f90 100644 --- a/common/src/main/java/revxrsal/commands/command/ExecutableCommand.java +++ b/common/src/main/java/revxrsal/commands/command/ExecutableCommand.java @@ -36,6 +36,7 @@ import revxrsal.commands.core.CommandPath; import revxrsal.commands.process.ResponseHandler; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -137,6 +138,22 @@ public interface ExecutableCommand extends CommandAnnotationHolder, PermissionHo */ @NotNull ResponseHandler getResponseHandler(); + /** + * Executes the command on the behalf of the given sender + * + * @param actor Actor to execute as + * @param input The command input + */ + void execute(@NotNull CommandActor actor, @Nullable Collection input); + + /** + * Executes the command on the behalf of the given sender + * + * @param actor Actor to execute as + * @param input The command input + */ + void execute(@NotNull CommandActor actor, @Nullable String... input); + /** * Returns whether is this command marked as secret or not *

diff --git a/common/src/main/java/revxrsal/commands/core/CommandExecutable.java b/common/src/main/java/revxrsal/commands/core/CommandExecutable.java index 9e129a6d..58fa177e 100644 --- a/common/src/main/java/revxrsal/commands/core/CommandExecutable.java +++ b/common/src/main/java/revxrsal/commands/core/CommandExecutable.java @@ -28,15 +28,15 @@ import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Unmodifiable; import revxrsal.commands.CommandHandler; -import revxrsal.commands.command.CommandCategory; -import revxrsal.commands.command.CommandParameter; -import revxrsal.commands.command.CommandPermission; -import revxrsal.commands.command.ExecutableCommand; +import revxrsal.commands.command.*; import revxrsal.commands.core.reflect.MethodCaller.BoundMethodCaller; import revxrsal.commands.process.ResponseHandler; +import revxrsal.commands.util.Preconditions; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -55,73 +55,109 @@ class CommandExecutable implements ExecutableCommand { boolean secret; BoundMethodCaller methodCaller; BaseCommandCategory parent; - @SuppressWarnings("rawtypes") ResponseHandler responseHandler = CommandParser.VOID_HANDLER; + @SuppressWarnings("rawtypes") + ResponseHandler responseHandler = CommandParser.VOID_HANDLER; private CommandPermission permission = CommandPermission.ALWAYS_TRUE; @Unmodifiable List parameters; @Unmodifiable Map resolveableParameters; - @Override public @NotNull String getName() { + @Override + public @NotNull String getName() { return name; } - @Override public @Range(from = 0, to = Long.MAX_VALUE) int getId() { + @Override + public @Range(from = 0, to = Long.MAX_VALUE) int getId() { return id; } - @Override public @NotNull String getUsage() { + @Override + public @NotNull String getUsage() { return usage; } - @Override public @Nullable String getDescription() { + @Override + public @Nullable String getDescription() { return description; } - @Override public @NotNull CommandPath getPath() { + @Override + public @NotNull CommandPath getPath() { return path; } - @Override public @Nullable CommandCategory getParent() { + @Override + public @Nullable CommandCategory getParent() { return parent; } - @Override public @NotNull @Unmodifiable List getParameters() { + @Override + public @NotNull @Unmodifiable List getParameters() { return parameters; } - @Override public @NotNull @Unmodifiable Map getValueParameters() { + @Override + public @NotNull @Unmodifiable Map getValueParameters() { return resolveableParameters; } - @Override public @NotNull CommandPermission getPermission() { + @Override + public @NotNull CommandPermission getPermission() { return permission; } - @Override public @NotNull CommandHandler getCommandHandler() { + @Override + public @NotNull CommandHandler getCommandHandler() { return handler; } - @Override public @NotNull ResponseHandler getResponseHandler() { + @Override + public @NotNull ResponseHandler getResponseHandler() { return responseHandler; } - @Override public boolean isSecret() { + @Override + public void execute(@NotNull CommandActor actor, @Nullable Collection input) { + Preconditions.notNull(actor, "actor"); + ArgumentStack arguments = ArgumentStack.copyExact(path.path); + if (input != null) + arguments.addAll(input); + getCommandHandler().dispatch(actor, arguments); + } + + @Override + public void execute(@NotNull CommandActor actor, @Nullable String... input) { + Preconditions.notNull(actor, "actor"); + ArgumentStack arguments = ArgumentStack.copyExact(path.path); + if (input != null) + Collections.addAll(arguments, input); + getCommandHandler().dispatch(actor, arguments); + } + + @Override + public boolean isSecret() { return secret; } - @Override public A getAnnotation(@NotNull Class annotation) { + @Override + public A getAnnotation(@NotNull Class annotation) { return reader.get(annotation); } - @Override public boolean hasAnnotation(@NotNull Class annotation) { + @Override + public boolean hasAnnotation(@NotNull Class annotation) { return reader.contains(annotation); } public void parent(BaseCommandCategory cat, boolean isDefault) { parent = cat; - if (isDefault) - cat.defaultAction = this; - else { - if (cat != null) + if (cat != null) { + if (isDefault) { + if (cat.defaultAction != null && cat.defaultAction.method != method) + throw new IllegalArgumentException("Category '" + cat.getPath().toRealString() + "' has more than one default" + + " action! (" + cat.defaultAction.method.toGenericString() + " and " + method.toGenericString() + ")"); + cat.defaultAction = this; + } else cat.commands.put(path, this); } }