From a567e2eb031554b1a6cbcce9d834d4d1007e10d1 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 9 Aug 2016 17:33:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/commands/CommandHelp.java | 50 ++++++++++++++++--- .../yumc/YumCore/commands/CommandManager.java | 11 ++++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index 798e9f0..26500ad 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -36,6 +36,10 @@ public class CommandHelp { String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix); String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s"; String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s"; + /** + * 默认命令 + */ + CommandInfo defCmd; /** * 已排序的命令列表 */ @@ -53,10 +57,45 @@ public class CommandHelp { */ private final Map cacheHelp = new HashMap<>(); + /** + * 命令帮助 + * + * @param list + * 子命令列表 + */ public CommandHelp(final Collection list) { + this(null, list); + } + + /** + * 命令帮助 + * + * @param defCmd + * 默认命令 + * @param list + * 子命令列表 + */ + public CommandHelp(final CommandInfo defCmd, final Collection list) { + this.defCmd = defCmd; cmdlist = new LinkedList<>(list); Collections.sort(cmdlist, new CommandComparator()); - this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE); + HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE); + } + + /** + * 格式化命令信息 + * + * @param ci + * 命令信息 + * @param label + * 命令 + * @return 格式化后的字串 + */ + public String formatCommand(final CommandInfo ci, final String label) { + final String aliases = Arrays.toString(ci.getCommand().aliases()); + final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases); + final Help help = ci.getHelp(); + return String.format(helpBody, label, cmd, help.possibleArguments(), help.value()); } /** @@ -85,16 +124,15 @@ public class CommandHelp { } else { // 帮助标题 helpList.add(helpTitle); + if (page == 1 && defCmd != null) { + helpList.add(formatCommand(defCmd, label)); + } final int start = this.LINES_PER_PAGE * (page - 1); final int end = start + this.LINES_PER_PAGE; for (int i = start; i < end; i++) { if (this.cmdlist.size() > i) { - final CommandInfo ci = cmdlist.get(i); - final String aliases = Arrays.toString(ci.getCommand().aliases()); - final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases); - final Help help = ci.getHelp(); // 帮助列表 - helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.value())); + helpList.add(formatCommand(cmdlist.get(i), label)); } } } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java index 71aeee3..3e7b564 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java @@ -35,6 +35,10 @@ public class CommandManager implements TabExecutor { * 插件实例类 */ JavaPlugin plugin = P.instance; + /** + * 默认命令 + */ + CommandInfo defCmd = null; /** * 命令列表 */ @@ -91,6 +95,9 @@ public class CommandManager implements TabExecutor { @Override public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { if (args.length == 0) { + if (defCmd != null) { + return defCmd.execute(new CommandArgument(sender, command, label, args)); + } help.send(sender, command, label, args); return true; } @@ -229,6 +236,10 @@ public class CommandManager implements TabExecutor { if (ci != null) { final Class[] params = method.getParameterTypes(); if (params.length == 1 && params[0].equals(CommandArgument.class)) { + if (method.getReturnType() == boolean.class) { + defCmd = ci; + return true; + } cmds.add(ci); cmdCache.put(ci.getName(), ci); return true;