From d245d21a8443cb075056191250b6f7c6711fe88a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 15 Feb 2019 23:48:15 +0100 Subject: [PATCH] Command system fixes Removed Vault repo as it seems to be offline and wasn't even used Fixed CommandSender type, no subcommands handling Made the default handler use the correct sender type (actually, the generic type might be completely unnecessary but oh well) No longer checking param types to match methods (needed for generic types) --- BuildConfigUpdater/BuildConfigUpdater.iml | 1 + ButtonCore/pom.xml | 8 ++++---- .../main/java/buttondevteam/lib/chat/Command2.java | 14 ++++++++++---- .../java/buttondevteam/lib/chat/ICommand2.java | 11 +++++------ .../java/buttondevteam/lib/chat/ICommand2MC.java | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/BuildConfigUpdater/BuildConfigUpdater.iml b/BuildConfigUpdater/BuildConfigUpdater.iml index 274b3de..f14440c 100644 --- a/BuildConfigUpdater/BuildConfigUpdater.iml +++ b/BuildConfigUpdater/BuildConfigUpdater.iml @@ -12,6 +12,7 @@ + diff --git a/ButtonCore/pom.xml b/ButtonCore/pom.xml index e044b9a..013fe73 100755 --- a/ButtonCore/pom.xml +++ b/ButtonCore/pom.xml @@ -103,10 +103,10 @@ jitpack.io https://jitpack.io/ - - vault-repo - http://nexus.hc.to/content/repositories/pub_releases - + ess-repo http://repo.ess3.net/content/repositories/essrel/ diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java index ba6d8c5..3cd2a35 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/Command2.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.function.Function; -import java.util.stream.Collectors; /** * The method name is the subcommand, use underlines (_) to add further subcommands. @@ -106,6 +105,8 @@ public abstract class Command2 final ChromaGamerBase cg; if (sendertype.isAssignableFrom(sender.getClass())) params.add(sender); //The command either expects a CommandSender or it is a Player, or some other expected type + else if (CommandSender.class.isAssignableFrom(sendertype) && sender instanceof Command2MCSender) + params.add(((Command2MCSender) sender).getSender()); else if (ChromaGamerBase.class.isAssignableFrom(sendertype) && sender instanceof Command2MCSender && (cg = ChromaGamerBase.getFromSender(((Command2MCSender) sender).getSender())) != null @@ -167,8 +168,9 @@ public abstract class Command2 //var scmdmap = subcommandStrings.computeIfAbsent(mainPath, k -> new HashSet<>()); //Used to display subcommands val scmdHelpList = new ArrayList(); Method mainMethod = null; + boolean nosubs = true; try { //Register the default handler first so it can be reliably overwritten - mainMethod = command.getClass().getMethod("def", CommandSender.class, String.class); + mainMethod = command.getClass().getMethod("def", Command2Sender.class, String.class); val cc = command.getClass().getAnnotation(CommandClass.class); var ht = cc == null ? new String[0] : cc.helpText(); if (ht.length > 0) @@ -188,8 +190,11 @@ public abstract class Command2 ht = getHelpText(method, ht, subcommand); subcommands.put(subcommand, new SubcommandData<>(method, command, ht)); //Result of the above (def) is that it will show the help text scmdHelpList.add(subcommand); + nosubs = false; } } + if (nosubs && scmdHelpList.size() > 0) + scmdHelpList.remove(scmdHelpList.size() - 1); //Remove Subcommands header if (mainMethod != null && !subcommands.containsKey(commandChar + path)) //Command specified by the class subcommands.put(commandChar + path, new SubcommandData<>(mainMethod, command, scmdHelpList.toArray(new String[0]))); if (mainMethod != null && !subcommands.containsKey(mainPath)) //Main command, typically the same as the above @@ -210,8 +215,9 @@ public abstract class Command2 if (cs != null) { val mname = cs.getString("method"); val params = cs.getString("params"); - val goodname = method.getName() + "(" + Arrays.stream(method.getParameterTypes()).map(cl -> cl.getCanonicalName()).collect(Collectors.joining(",")) + ")"; - if (goodname.equals(mname) && params != null) { + //val goodname = method.getName() + "(" + Arrays.stream(method.getGenericParameterTypes()).map(cl -> cl.getTypeName()).collect(Collectors.joining(",")) + ")"; + int i = mname.indexOf('('); //Check only the name - the whole method is still stored for backwards compatibility and in case it may be useful + if (i != -1 && method.getName().equals(mname.substring(0, i)) && params != null) { String[] both = Arrays.copyOf(ht, ht.length + 1); both[ht.length] = "§6Usage:§r " + subcommand + " " + params; ht = both; diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java index 6ee2264..588c744 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2.java @@ -1,11 +1,10 @@ package buttondevteam.lib.chat; import lombok.Getter; -import org.bukkit.command.CommandSender; import java.util.function.Function; -public abstract class ICommand2 { +public abstract class ICommand2 { /** * Default handler for commands, can be used to copy the args too. * @@ -13,7 +12,7 @@ public abstract class ICommand2 { * @param args All of the arguments passed as is * @return The success of the command */ - public boolean def(CommandSender sender, @Command2.TextArg String args) { + public boolean def(TP sender, @Command2.TextArg String args) { return false; } @@ -24,16 +23,16 @@ public abstract class ICommand2 { * @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) { + protected boolean respond(TP sender, String message) { sender.sendMessage(message); return true; } private final String path; @Getter - private final Command2 manager; //TIL that if I use a raw type on a variable then none of the type args will work (including what's defined on a method, not on the type) + private final Command2 manager; //TIL that if I use a raw type on a variable then none of the type args will work (including what's defined on a method, not on the type) - public ICommand2(Command2 manager) { + public ICommand2(Command2 manager) { path = getcmdpath(); this.manager = manager; } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java index d051597..4ad997b 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/ICommand2MC.java @@ -2,7 +2,7 @@ package buttondevteam.lib.chat; import buttondevteam.lib.architecture.ButtonPlugin; -public class ICommand2MC extends ICommand2 { +public class ICommand2MC extends ICommand2 { public ICommand2MC() { super(ButtonPlugin.getCommand2MC()); }