diff --git a/bukkit/src/main/java/co/aikar/commands/BukkitCommandHelp.java b/bukkit/src/main/java/co/aikar/commands/BukkitCommandHelp.java new file mode 100644 index 00000000..7a636e2e --- /dev/null +++ b/bukkit/src/main/java/co/aikar/commands/BukkitCommandHelp.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +import java.util.ArrayList; +import java.util.List; + +public class BukkitCommandHelp extends CommandHelp { + + protected BukkitCommandHelp(CommandManager manager, BaseCommand cmd) { + super(manager, cmd); + } + + @Override + void renderHelp(CommandIssuer issuer) { + List rendered = new ArrayList<>(); + getCommandHelp().forEach(h -> rendered.add("/" + h.getCommand() + " " + h.getSyntax() + ((h.getHelpText() != null && !h.getHelpText().isEmpty() ? " - " + h.getHelpText() : "")))); + rendered.forEach(issuer::sendMessage); + } +} diff --git a/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java b/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java index d81328bc..b3a608e9 100644 --- a/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java +++ b/bukkit/src/main/java/co/aikar/commands/BukkitCommandManager.java @@ -26,7 +26,6 @@ package co.aikar.commands; import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil; import co.aikar.timings.lib.MCTiming; import co.aikar.timings.lib.TimingManager; -import com.google.common.collect.SetMultimap; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Server; @@ -43,7 +42,6 @@ import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -279,23 +277,11 @@ public class BukkitCommandManager extends CommandManager { } @Override - List getHelp(String command) { - BukkitRootCommand cmd = (BukkitRootCommand) obtainRootCommand(command); + CommandHelp getHelp(String command) { + RootCommand cmd = obtainRootCommand(command); BaseCommand defCommand = cmd.getDefCommand(); - if (defCommand instanceof ForwardingCommand) { - command = defCommand.commandName; - ForwardingCommand fwdCmd = (ForwardingCommand) defCommand; - defCommand = fwdCmd.getCommand(); - } - SetMultimap cmds = defCommand.subCommands; - List help = new ArrayList<>(); - String finalCommand = command; - cmds.entries().forEach(e -> { - if (e.getKey().equals("__default")) - return; - help.add("/" + finalCommand + " " + e.getKey() + " " + e.getValue().syntaxText + ( - (e.getValue().helpText != null && !e.getValue().helpText.isEmpty())?" - " + e.getValue().helpText: "")); - }); - return help; + if (defCommand == null) + return null; + return new BukkitCommandHelp(this, defCommand); } } diff --git a/bukkit/src/main/java/co/aikar/commands/BukkitRootCommand.java b/bukkit/src/main/java/co/aikar/commands/BukkitRootCommand.java index f4524b4a..78abce7c 100644 --- a/bukkit/src/main/java/co/aikar/commands/BukkitRootCommand.java +++ b/bukkit/src/main/java/co/aikar/commands/BukkitRootCommand.java @@ -104,7 +104,8 @@ public class BukkitRootCommand extends Command implements RootCommand { return this.subCommands; } - BaseCommand getDefCommand(){ + @Override + public BaseCommand getDefCommand(){ return defCommand; } } diff --git a/core/src/main/java/co/aikar/commands/BaseCommand.java b/core/src/main/java/co/aikar/commands/BaseCommand.java index 245b6b32..9310a3ba 100644 --- a/core/src/main/java/co/aikar/commands/BaseCommand.java +++ b/core/src/main/java/co/aikar/commands/BaseCommand.java @@ -110,12 +110,7 @@ public abstract class BaseCommand { } this.commandName = cmd != null ? cmd : self.getSimpleName().toLowerCase(); - Description descAnn = self.getAnnotation(Description.class); - if(descAnn != null && !descAnn.value().isEmpty()){ - this.description = descAnn.value(); - } else { - this.description = this.commandName + " commands"; - } + this.description = this.commandName + " commands"; this.usageMessage = "/" + this.commandName; final CommandPermission perm = self.getAnnotation(CommandPermission.class); @@ -517,7 +512,7 @@ public abstract class BaseCommand { return false; } - List getHelp(){ + CommandHelp getCommandHelp(){ return manager.getHelp(this.getExecCommandLabel()); } diff --git a/core/src/main/java/co/aikar/commands/CommandHelp.java b/core/src/main/java/co/aikar/commands/CommandHelp.java new file mode 100644 index 00000000..003cff3e --- /dev/null +++ b/core/src/main/java/co/aikar/commands/CommandHelp.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +import com.google.common.collect.SetMultimap; + +import java.util.*; + +public abstract class CommandHelp { + private BaseCommand command; + + protected CommandHelp(CommandManager manager, BaseCommand command) { + this.command = command; + } + + abstract void renderHelp(CommandIssuer issuer); + + + Collection getCommandHelp() { + SetMultimap subCommands = command.subCommands; + Set help = new HashSet<>(); + List used = new ArrayList<>(); + subCommands.entries().forEach(e -> { + if(e.getKey().equals("__default") || e.getKey().equals("__unknown")){ + return; + } + RegisteredCommand regCommand = e.getValue(); + if(!used.contains(regCommand.getCommand())) { + help.add(new HelpEntry(regCommand)); + used.add(regCommand.getCommand()); + + } + }); + return help; + } + + public BaseCommand getCommand() { + return command; + } + + +} diff --git a/core/src/main/java/co/aikar/commands/CommandManager.java b/core/src/main/java/co/aikar/commands/CommandManager.java index 640d9c19..0764f8e1 100644 --- a/core/src/main/java/co/aikar/commands/CommandManager.java +++ b/core/src/main/java/co/aikar/commands/CommandManager.java @@ -83,8 +83,8 @@ public abstract class CommandManager { */ public abstract CommandCompletions getCommandCompletions(); - List getHelp(String command){ - return new ArrayList<>(0); + CommandHelp getHelp(String command){ + throw new IllegalStateException("Not implemented yet."); } /** @@ -194,6 +194,7 @@ public abstract class CommandManager { return getLocales().getDefaultLocale(); } + public CommandOperationContext createCommandOperationContext(BaseCommand command, CommandIssuer issuer, String commandLabel, String[] args) { return new CommandOperationContext( this, diff --git a/core/src/main/java/co/aikar/commands/HelpEntry.java b/core/src/main/java/co/aikar/commands/HelpEntry.java new file mode 100644 index 00000000..844ecff2 --- /dev/null +++ b/core/src/main/java/co/aikar/commands/HelpEntry.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package co.aikar.commands; + +public class HelpEntry { + + private final RegisteredCommand command; + + public HelpEntry(RegisteredCommand command) { + this.command = command; + } + + + public String getCommand(){ + return this.command.getCommand(); + } + + public String getSyntax(){ + return this.command.getSyntaxText(); + } + + public String getHelpText(){ + return this.command.helpText; + } +} diff --git a/core/src/main/java/co/aikar/commands/RootCommand.java b/core/src/main/java/co/aikar/commands/RootCommand.java index 0a01e555..651aab60 100644 --- a/core/src/main/java/co/aikar/commands/RootCommand.java +++ b/core/src/main/java/co/aikar/commands/RootCommand.java @@ -52,4 +52,8 @@ interface RootCommand { children.add(command); } + + default BaseCommand getDefCommand(){ + return null; + } }