From 560724dd52ba9a5f49ba459c5d1abb056333c423 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 10 Nov 2016 16:34:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=B0=83=E7=94=A8=E9=80=BB=E8=BE=91=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/commands/CommandManager.java | 10 +-- .../YumCore/commands/info/CommandInfo.java | 65 +++++++++---------- .../YumCore/commands/info/CommandTabInfo.java | 39 +++++------ 3 files changed, 50 insertions(+), 64 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java index 53e6a53..42f5cb6 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java @@ -150,9 +150,6 @@ public class CommandManager implements TabExecutor { break; } } - if (defCmd != null) { - cmdCache.put(subcmd, defCmd); - } if (!cmdCache.containsKey(subcmd)) { cmdCache.put(subcmd, CommandInfo.Unknow); } @@ -209,7 +206,12 @@ public class CommandManager implements TabExecutor { } String subcmd = args[0].toLowerCase(); if (subcmd.equalsIgnoreCase("help")) { return help.send(sender, command, label, args); } - return getByCache(subcmd).execute(new CommandArgument(sender, command, label, moveStrings(args, 1))); + CommandInfo cmd = getByCache(subcmd); + if (cmd.equals(CommandInfo.Unknow) && defCmd != null) { + return defCmd.execute(new CommandArgument(sender, command, label, args)); + } else { + return cmd.execute(new CommandArgument(sender, command, label, moveStrings(args, 1))); + } } @Override diff --git a/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java index ed50aa3..2ce9404 100644 --- a/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java @@ -1,14 +1,7 @@ package pw.yumc.YumCore.commands.info; -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; - import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; - import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.commands.CommandArgument; @@ -22,6 +15,13 @@ import pw.yumc.YumCore.commands.exception.CommandArgumentException; import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.CommandParseException; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + /** * 命令信息存储类 * @@ -95,8 +95,10 @@ public class CommandInfo { /** * 解析CommandInfo * - * @param method 方法 - * @param origin 源对象 + * @param method + * 方法 + * @param origin + * 源对象 * @return {@link CommandInfo} */ public static CommandInfo parse(Method method, Object origin) { @@ -111,27 +113,15 @@ public class CommandInfo { return null; } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof CommandInfo) { - return name.equalsIgnoreCase(((CommandInfo) obj).getName()); - } - return super.equals(obj); - } - /** * 执行命令 * - * @param cmdArgs 命令参数 + * @param cmdArgs + * 命令参数 * @return 是否执行成功 */ public boolean execute(final CommandArgument cmdArgs) { - if (method == null) { - return false; - } + if (method == null) { return false; } if (check(cmdArgs)) { Runnable runnable = new Runnable() { @Override @@ -182,11 +172,6 @@ public class CommandInfo { return sort; } - @Override - public int hashCode() { - return name.toLowerCase().hashCode() + method.hashCode() + origin.hashCode(); - } - /** * @return 是否为异步命令 */ @@ -197,19 +182,27 @@ public class CommandInfo { /** * 验证命令是否匹配 * - * @param cmd 需验证命令 + * @param cmd + * 需验证命令 * @return 是否匹配 */ public boolean isValid(String cmd) { return name.equalsIgnoreCase(cmd) || aliases.contains(cmd); } - /** - * 检查命令 - * - * @param cmdArgs 命令信息 - * @return 是否验证通过 - */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CommandInfo that = (CommandInfo) o; + return Objects.equals(origin, that.origin) && Objects.equals(method, that.method) && Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(origin, method, name); + } + private boolean check(CommandArgument cmdArgs) { CommandSender sender = cmdArgs.getSender(); return checkSender(sender) && checkArgs(sender, cmdArgs) && checkPerm(sender); diff --git a/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java b/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java index 8ce6e3a..8bee362 100644 --- a/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java @@ -1,16 +1,16 @@ package pw.yumc.YumCore.commands.info; +import org.bukkit.command.CommandSender; +import pw.yumc.YumCore.bukkit.P; +import pw.yumc.YumCore.commands.CommandArgument; +import pw.yumc.YumCore.commands.annotation.Tab; +import pw.yumc.YumCore.commands.exception.CommandException; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; - -import org.bukkit.command.CommandSender; - -import pw.yumc.YumCore.bukkit.P; -import pw.yumc.YumCore.commands.CommandArgument; -import pw.yumc.YumCore.commands.annotation.Tab; -import pw.yumc.YumCore.commands.exception.CommandException; +import java.util.Objects; /** * Tab补全 @@ -38,23 +38,10 @@ public class CommandTabInfo { */ public static CommandTabInfo parse(Method method, Object origin) { Tab tab = method.getAnnotation(Tab.class); - if (tab != null) { - return new CommandTabInfo(method, origin); - } + if (tab != null) { return new CommandTabInfo(method, origin); } return null; } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof CommandTabInfo) { - return method.equals(((CommandTabInfo) obj).getMethod()); - } - return super.equals(obj); - } - /** * 获得补全List * @@ -78,12 +65,16 @@ public class CommandTabInfo { } } - public Method getMethod() { - return method; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CommandTabInfo that = (CommandTabInfo) o; + return Objects.equals(origin, that.origin) && Objects.equals(method, that.method); } @Override public int hashCode() { - return method.hashCode() + origin.hashCode(); + return Objects.hash(origin, method); } }