From aa324f54db0ae2db53e053e9dc0045b8db000484 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 25 Jul 2016 19:16:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20=E4=BF=AE=E5=A4=8DSort=20Tab=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=8F=AF=E7=94=A8=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF?= 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 | 11 +++- .../yumc/YumCore/commands/CommandManager.java | 56 +++++++++++-------- .../YumCore/commands/annotation/Async.java | 35 ++++++------ .../YumCore/commands/annotation/Sort.java | 7 +++ .../yumc/YumCore/commands/annotation/Tab.java | 27 +++++---- 5 files changed, 83 insertions(+), 53 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index eb7f081..798e9f0 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -1,6 +1,5 @@ package pw.yumc.YumCore.commands; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -79,7 +78,7 @@ public class CommandHelp { } final String helpkey = label + page; if (!cacheHelp.containsKey(helpkey)) { - final List helpList = new ArrayList<>(); + final List helpList = new LinkedList<>(); if (page > this.HELPPAGECOUNT || page < 1) { // 帮助页面不存在 helpList.add(String.format(commandNotFound, HELPPAGECOUNT)); @@ -115,7 +114,13 @@ public class CommandHelp { static class CommandComparator implements Comparator { @Override public int compare(final CommandInfo o1, final CommandInfo o2) { - return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort())); + if (o1.getSort() > o2.getSort()) { + return 1; + } else if (o1.getSort() == o2.getSort()) { + return 0; + } else { + return -1; + } } } } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java index f68b8bf..26727ff 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java @@ -2,6 +2,7 @@ package pw.yumc.YumCore.commands; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -37,20 +38,23 @@ public class CommandManager implements TabExecutor { /** * 命令列表 */ - Set cmdlist = new HashSet<>(); + Set cmds = new HashSet<>(); /** * Tab列表 */ - Set tablist = new HashSet<>(); + Set tabs = new HashSet<>(); /** * 命令缓存列表 */ - Map cmdcache = new HashMap<>(); + Map cmdCache = new HashMap<>(); + /** + * 命令名称缓存 + */ + List cmdNameCache = new ArrayList<>(); /** * 命令帮助 */ - CommandHelp help = new CommandHelp(cmdlist); - + CommandHelp help; /** * 插件命令 */ @@ -97,10 +101,6 @@ public class CommandManager implements TabExecutor { } final CommandArgument cmdArgs = new CommandArgument(sender, command, label, moveStrings(args, 1)); final CommandInfo ci = checkCache(subcmd); - try { - - } catch (final Exception e) { - } return ci.execute(cmdArgs); } @@ -109,10 +109,9 @@ public class CommandManager implements TabExecutor { final List completions = new ArrayList<>(); final String token = args[args.length - 1]; if (args.length == 1) { - final Set commands = this.cmdcache.keySet(); - StringUtil.copyPartialMatches(args[0], commands, completions); + StringUtil.copyPartialMatches(args[0], cmdNameCache, completions); } else if (args.length >= 2) { - for (final TabInfo tab : tablist) { + for (final TabInfo tab : tabs) { StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions); } StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions); @@ -135,7 +134,20 @@ public class CommandManager implements TabExecutor { } registerTab(method, clazz); } - help = new CommandHelp(cmdlist); + help = new CommandHelp(cmds); + buildCmdNameCache(); + } + + /** + * 构建命令列表缓存 + */ + private void buildCmdNameCache() { + cmdNameCache.clear(); + for (final CommandInfo cmd : cmds) { + cmdNameCache.add(cmd.getName()); + cmdNameCache.addAll(Arrays.asList(cmd.getCommand().aliases())); + } + cmdNameCache.add("help"); } /** @@ -146,18 +158,18 @@ public class CommandManager implements TabExecutor { * @return 命令信息 */ private CommandInfo checkCache(final String subcmd) { - if (!cmdcache.containsKey(subcmd)) { - for (final CommandInfo cmdinfo : cmdlist) { + if (!cmdCache.containsKey(subcmd)) { + for (final CommandInfo cmdinfo : cmds) { if (cmdinfo.isValid(subcmd)) { - cmdcache.put(subcmd, cmdinfo); + cmdCache.put(subcmd, cmdinfo); break; } } - if (!cmdcache.containsKey(subcmd)) { - cmdcache.put(subcmd, CommandInfo.Unknow); + if (!cmdCache.containsKey(subcmd)) { + cmdCache.put(subcmd, CommandInfo.Unknow); } } - return cmdcache.get(subcmd); + return cmdCache.get(subcmd); } /** @@ -215,8 +227,8 @@ public class CommandManager implements TabExecutor { if (ci != null) { final Class[] params = method.getParameterTypes(); if (params.length == 1 && params[0].equals(CommandArgument.class)) { - cmdlist.add(ci); - cmdcache.put(ci.getName(), ci); + cmds.add(ci); + cmdCache.put(ci.getName(), ci); return true; } Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName())); @@ -237,7 +249,7 @@ public class CommandManager implements TabExecutor { final TabInfo ti = TabInfo.parse(method, clazz); if (ti != null) { if (method.getReturnType().equals(List.class)) { - tablist.add(ti); + tabs.add(ti); return true; } Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName())); diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Async.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Async.java index 645505d..348946f 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Async.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Async.java @@ -1,18 +1,17 @@ -package pw.yumc.YumCore.commands.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * 异步命令注解 - * - * @since 2016年7月23日 上午9:00:27 - * @author 喵♂呜 - */ -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface Async { - -} +package pw.yumc.YumCore.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 异步命令注解 + * + * @since 2016年7月23日 上午9:00:27 + * @author 喵♂呜 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Async { +} diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java index 624f0a9..71c1296 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java @@ -1,11 +1,18 @@ package pw.yumc.YumCore.commands.annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + /** * 命令排序注解 * * @since 2016年7月23日 上午9:04:56 * @author 喵♂呜 */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) public @interface Sort { /** * @return 命令排序 diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java index a501ad1..c63d680 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Tab.java @@ -1,10 +1,17 @@ -package pw.yumc.YumCore.commands.annotation; - -/** - * 自动补全注解 - * - * @since 2016年7月23日 上午9:43:40 - * @author 喵♂呜 - */ -public @interface Tab { -} +package pw.yumc.YumCore.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 自动补全注解 + * + * @since 2016年7月23日 上午9:43:40 + * @author 喵♂呜 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Tab { +}