From ab24480f898130d31946a68ee5658f2351056145 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 2 Feb 2019 02:45:32 +0100 Subject: [PATCH] Separated commands and handlers WIP --- BuildConfigUpdater/BuildConfigUpdater.iml | 1 - .../buttondevteam/core/ComponentCommand.java | 8 +-- .../lib/architecture/ButtonPlugin.java | 3 + .../lib/architecture/Component.java | 6 +- .../java/buttondevteam/lib/chat/Command2.java | 67 +++---------------- .../buttondevteam/lib/chat/Command2MC.java | 10 +-- .../buttondevteam/lib/chat/ICommand2.java | 63 +++++++++++++++++ .../buttondevteam/lib/chat/ICommand2MC.java | 9 +++ 8 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java create mode 100644 ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index f14440c..274b3de 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -12,7 +12,6 @@ - diff --git a/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java b/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java index 4aaceac..78aa6ea 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java +++ b/ButtonCore/src/main/java/buttondevteam/core/ComponentCommand.java @@ -2,8 +2,9 @@ package buttondevteam.core; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.Component; -import buttondevteam.lib.chat.Command2MC; +import buttondevteam.lib.chat.Command2.Subcommand; import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.ICommand2MC; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -15,10 +16,9 @@ import java.util.Optional; "ยง6---- Component command ----", "Can be used to enable/disable/list components" }) -public class ComponentCommand extends Command2MC { +public class ComponentCommand extends ICommand2MC { public ComponentCommand() { - addParamConverter(Plugin.class, arg -> Bukkit.getPluginManager().getPlugin(arg)); - + getManager().addParamConverter(Plugin.class, arg -> Bukkit.getPluginManager().getPlugin(arg)); } @Subcommand diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java index 5b26d9d..8ca1109 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -2,6 +2,7 @@ package buttondevteam.lib.architecture; import buttondevteam.core.ComponentManager; import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.chat.Command2MC; import buttondevteam.lib.chat.TBMCChatAPI; import lombok.AccessLevel; import lombok.Getter; @@ -11,6 +12,8 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.Stack; public abstract class ButtonPlugin extends JavaPlugin { + @Getter + private static Command2MC command2MC = new Command2MC(); @Getter(AccessLevel.PROTECTED) private IHaveConfig iConfig; /** diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index e052555..7d8a2d4 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -3,7 +3,7 @@ package buttondevteam.lib.architecture; import buttondevteam.core.ComponentManager; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException; -import buttondevteam.lib.chat.Command2MC; +import buttondevteam.lib.chat.ICommand2; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCCommandBase; import lombok.Getter; @@ -197,8 +197,8 @@ public abstract class Component { * * @param commandBase Custom coded command class */ - protected final void registerCommand(Command2MC commandBase) { - Command2MC.registerCommand(commandBase); + protected final void registerCommand(ICommand2 commandBase) { + ButtonPlugin.getCommand2MC().registerCommand(commandBase); } /** diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java index c16d318..5e3cf01 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java @@ -25,29 +25,6 @@ import java.util.stream.Collectors; * The args may be null if the conversion failed. */ public abstract class Command2 { - /** - * Default handler for commands, can be used to copy the args too. - * - * @param sender The sender which ran the command - * @param args All of the arguments passed as is - * @return The success of the command - */ - public boolean def(CommandSender sender, @TextArg String args) { - return false; - } - - /** - * Convenience method. Return with this. - * - * @param sender The sender of the command - * @param message The message to send to the sender - * @return Always true so that the usage isn't shown - */ - protected boolean respond(CommandSender sender, String message) { - sender.sendMessage(message); - return true; - } - /** * TODO: @CommandClass(helpText=...) * Parameters annotated with this receive all of the remaining arguments @@ -70,16 +47,11 @@ public abstract class Command2 { } @RequiredArgsConstructor - protected static class SubcommandData { + protected static class SubcommandData { public final Method method; public final T command; public final String[] helpText; } - - public Command2() { - path = getcmdpath(); - } - /** * Adds a param converter that obtains a specific object from a string parameter. * The converter may return null. @@ -88,11 +60,15 @@ public abstract class Command2 { * @param converter The converter to use * @param The type of the result */ - protected static void addParamConverter(Class cl, Function converter, HashMap, Function> map) { + public abstract void addParamConverter(Class cl, Function converter); + + protected void addParamConverter(Class cl, Function converter, HashMap, Function> map) { map.put(cl, converter); } - protected static boolean handleCommand(CommandSender sender, String commandline, + public abstract boolean handleCommand(CommandSender sender, String commandLine) throws Exception; + + protected boolean handleCommand(CommandSender sender, String commandline, HashMap> subcommands, HashMap, Function> paramConverters) throws Exception { for (int i = commandline.length(); i != -1; i = commandline.lastIndexOf(' ', i - 1)) { String subcommand = commandline.substring(0, i).toLowerCase(); @@ -147,7 +123,9 @@ public abstract class Command2 { return false; //Didn't handle } //TODO: Add to the help - protected static void registerCommand(T command, HashMap> subcommands, char commandChar) { + public abstract void registerCommand(ICommand2 command); + + protected void registerCommand(T command, HashMap> subcommands, char commandChar) { val path = command.getCommandPath(); try { //Register the default handler first so it can be reliably overwritten val method = command.getClass().getMethod("def", CommandSender.class, String.class); @@ -200,28 +178,5 @@ public abstract class Command2 { return ht; } - private final String path; - - /** - * The command's path, or name if top-level command.
- * For example:
- * "u admin updateplugin" or "u" for the top level one
- * The path must be lowercase!
- * - * @return The command path, which is the command class name by default (removing any "command" from it) - Change via the {@link CommandClass} annotation - */ - public final String getCommandPath() { - return path; - } - - private String getcmdpath() { - if (!getClass().isAnnotationPresent(CommandClass.class)) - throw new RuntimeException( - "No @CommandClass annotation on command class " + getClass().getSimpleName() + "!"); - Function, String> getFromClass = cl -> cl.getSimpleName().toLowerCase().replace("commandbase", "") // <-- ... - .replace("command", ""); - String path = getClass().getAnnotation(CommandClass.class).path(); - path = path.length() == 0 ? getFromClass.apply(getClass()) : path; - return path; - } + public abstract boolean hasPermission(CommandSender sender, ICommand2 command); } //TODO: Test support of Player instead of CommandSender diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java index 715d078..e49ca0d 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2MC.java @@ -7,18 +7,18 @@ import java.util.function.Function; public class Command2MC extends Command2 { - private static HashMap> subcommands = new HashMap<>(); - private static HashMap, Function> paramConverters = new HashMap<>(); + private HashMap> subcommands = new HashMap<>(); + private HashMap, Function> paramConverters = new HashMap<>(); - public static boolean handleCommand(CommandSender sender, String commandLine) throws Exception { + public boolean handleCommand(CommandSender sender, String commandLine) throws Exception { return handleCommand(sender, commandLine, subcommands, paramConverters); } - public static void registerCommand(Command2MC command) { + public void registerCommand(ICommand2 command) { registerCommand(command, subcommands, '/'); } - public static void addParamConverter(Class cl, Function converter) { + public void addParamConverter(Class cl, Function converter) { addParamConverter(cl, converter, paramConverters); } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java new file mode 100644 index 0000000..3aa0a96 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java @@ -0,0 +1,63 @@ +package buttondevteam.lib.chat; + +import lombok.Getter; +import org.bukkit.command.CommandSender; + +import java.util.function.Function; + +public abstract class ICommand2 { + /** + * Default handler for commands, can be used to copy the args too. + * + * @param sender The sender which ran the command + * @param args All of the arguments passed as is + * @return The success of the command + */ + public boolean def(CommandSender sender, @Command2.TextArg String args) { + return false; + } + + /** + * Convenience method. Return with this. + * + * @param sender The sender of the command + * @param message The message to send to the sender + * @return Always true so that the usage isn't shown + */ + protected boolean respond(CommandSender sender, String message) { + sender.sendMessage(message); + return true; + } + + private final String path; + @Getter + private final Command2 manager; + + public ICommand2(Command2 manager) { + path = getcmdpath(); + this.manager = manager; + } + + /** + * The command's path, or name if top-level command.
+ * For example:
+ * "u admin updateplugin" or "u" for the top level one
+ * The path must be lowercase!
+ * + * @return The command path, which is the command class name by default (removing any "command" from it) - Change via the {@link CommandClass} annotation + */ + public final String getCommandPath() { + return path; + } + + private String getcmdpath() { + if (!getClass().isAnnotationPresent(CommandClass.class)) + throw new RuntimeException( + "No @CommandClass annotation on command class " + getClass().getSimpleName() + "!"); + Function, String> getFromClass = cl -> cl.getSimpleName().toLowerCase().replace("commandbase", "") // <-- ... + .replace("command", ""); + String path = getClass().getAnnotation(CommandClass.class).path(); + path = path.length() == 0 ? getFromClass.apply(getClass()) : path; + return path; + } +} diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java new file mode 100644 index 0000000..d051597 --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java @@ -0,0 +1,9 @@ +package buttondevteam.lib.chat; + +import buttondevteam.lib.architecture.ButtonPlugin; + +public class ICommand2MC extends ICommand2 { + public ICommand2MC() { + super(ButtonPlugin.getCommand2MC()); + } +}