From 6ac2b8cf78a2a7df56ffd5cfff5488bfc30e0765 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sun, 9 Oct 2016 20:36:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=A7=A3=E6=9E=90=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 | 2 ++ .../yumc/YumCore/commands/CommandManager.java | 21 ++++++++++++------- .../yumc/YumCore/commands/CommandParse.java | 7 ++++++- .../commands/{ => info}/CommandInfo.java | 6 ++++-- .../CommandTabInfo.java} | 17 ++++++++------- .../{ => interfaces}/CommandExecutor.java | 2 +- .../{ => interfaces}/CommandHelpParse.java | 2 +- 7 files changed, 36 insertions(+), 21 deletions(-) rename src/main/java/pw/yumc/YumCore/commands/{ => info}/CommandInfo.java (94%) rename src/main/java/pw/yumc/YumCore/commands/{TabInfo.java => info/CommandTabInfo.java} (78%) rename src/main/java/pw/yumc/YumCore/commands/{ => interfaces}/CommandExecutor.java (70%) rename src/main/java/pw/yumc/YumCore/commands/{ => interfaces}/CommandHelpParse.java (82%) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index de4f3c3..4499fb7 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -14,6 +14,8 @@ import org.bukkit.command.CommandSender; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.commands.annotation.Help; +import pw.yumc.YumCore.commands.info.CommandInfo; +import pw.yumc.YumCore.commands.interfaces.CommandHelpParse; /** * 命令帮助生成类 diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java index 701853c..981e5a6 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java @@ -28,6 +28,10 @@ import org.bukkit.util.StringUtil; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.compatible.C; +import pw.yumc.YumCore.commands.info.CommandInfo; +import pw.yumc.YumCore.commands.info.CommandTabInfo; +import pw.yumc.YumCore.commands.interfaces.CommandExecutor; +import pw.yumc.YumCore.commands.interfaces.CommandHelpParse; /** * 命令管理类 @@ -57,7 +61,7 @@ public class CommandManager implements TabExecutor { knownCommandsField.setAccessible(true); knownCommands = (Map) knownCommandsField.get(commandMap); - PluginCommandConstructor = PluginCommand.class.getConstructor(String.class, Plugin.class); + PluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class); } catch (NoSuchMethodException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { Log.d("初始化命令管理器失败!"); Log.debug(e); @@ -78,7 +82,7 @@ public class CommandManager implements TabExecutor { /** * Tab列表 */ - private final Set tabs = new HashSet<>(); + private final Set tabs = new HashSet<>(); /** * 命令缓存列表 */ @@ -156,7 +160,7 @@ public class CommandManager implements TabExecutor { if (args.length == 1) { StringUtil.copyPartialMatches(args[0], cmdNameCache, completions); } else if (args.length >= 2) { - for (final TabInfo tab : tabs) { + for (final CommandTabInfo tab : tabs) { StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions); } StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions); @@ -283,13 +287,14 @@ public class CommandManager implements TabExecutor { final CommandInfo ci = CommandInfo.parse(method, clazz); if (ci != null) { final Class[] params = method.getParameterTypes(); - if (params.length > 0 && params[0].equals(CommandArgument.class)) { + Log.d("命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params)); + if (params.length > 0 && params[0].isAssignableFrom(CommandSender.class)) { if (method.getReturnType() == boolean.class) { defCmd = ci; - return true; + } else { + cmds.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())); @@ -307,7 +312,7 @@ public class CommandManager implements TabExecutor { * @return 是否成功 */ private boolean registerTab(final Method method, final CommandExecutor clazz) { - final TabInfo ti = TabInfo.parse(method, clazz); + final CommandTabInfo ti = CommandTabInfo.parse(method, clazz); if (ti != null) { if (method.getReturnType().equals(List.class)) { tabs.add(ti); diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index e548b4a..b2e2aa5 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -2,11 +2,13 @@ package pw.yumc.YumCore.commands; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import pw.yumc.YumCore.commands.exception.CommandException; @@ -19,7 +21,7 @@ import pw.yumc.YumCore.commands.exception.CommandParseException; * @since 2016年10月5日 下午4:02:04 */ public class CommandParse { - private static Map allparses; + private static Map allparses = new HashMap<>(); static { new IntegerParse(); new LongParse(); @@ -33,6 +35,9 @@ public class CommandParse { public CommandParse(final Class[] classes, final Annotation[][] annons) { for (final Class classe : classes) { final Class clazz = classe; + if (clazz.isAssignableFrom(CommandSender.class)) { + continue; + } if (!allparses.containsKey(clazz)) { throw new CommandParseException(String.format("无法解析的参数类型 %s !", clazz.getName())); } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java similarity index 94% rename from src/main/java/pw/yumc/YumCore/commands/CommandInfo.java rename to src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java index af413fe..db75e1b 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java @@ -1,4 +1,4 @@ -package pw.yumc.YumCore.commands; +package pw.yumc.YumCore.commands.info; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -11,6 +11,8 @@ import org.bukkit.entity.Player; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; +import pw.yumc.YumCore.commands.CommandArgument; +import pw.yumc.YumCore.commands.CommandParse; import pw.yumc.YumCore.commands.annotation.Async; import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Cmd.Executor; @@ -121,7 +123,7 @@ public class CommandInfo { @Override public void run() { try { - method.invoke(origin, cmdArgs.getSender(), parse.parse(cmdArgs)); + method.invoke(origin, parse.parse(cmdArgs)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new CommandException(e); } diff --git a/src/main/java/pw/yumc/YumCore/commands/TabInfo.java b/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java similarity index 78% rename from src/main/java/pw/yumc/YumCore/commands/TabInfo.java rename to src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java index 0f6bd82..0efa3ce 100644 --- a/src/main/java/pw/yumc/YumCore/commands/TabInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandTabInfo.java @@ -1,4 +1,4 @@ -package pw.yumc.YumCore.commands; +package pw.yumc.YumCore.commands.info; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -8,6 +8,7 @@ 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; @@ -17,11 +18,11 @@ import pw.yumc.YumCore.commands.exception.CommandException; * @since 2016年7月23日 上午9:56:42 * @author 喵♂呜 */ -public class TabInfo { +public class CommandTabInfo { private final Object origin; private final Method method; - public TabInfo(final Method method, final Object origin) { + public CommandTabInfo(final Method method, final Object origin) { this.method = method; this.origin = origin; } @@ -33,12 +34,12 @@ public class TabInfo { * 方法 * @param origin * 对象 - * @return {@link TabInfo} + * @return {@link CommandTabInfo} */ - public static TabInfo parse(final Method method, final Object origin) { + public static CommandTabInfo parse(final Method method, final Object origin) { final Tab tab = method.getAnnotation(Tab.class); if (tab != null) { - return new TabInfo(method, origin); + return new CommandTabInfo(method, origin); } return null; } @@ -48,8 +49,8 @@ public class TabInfo { if (this == obj) { return true; } - if (obj instanceof TabInfo) { - return method.equals(((TabInfo) obj).getMethod()); + if (obj instanceof CommandTabInfo) { + return method.equals(((CommandTabInfo) obj).getMethod()); } return super.equals(obj); } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandExecutor.java b/src/main/java/pw/yumc/YumCore/commands/interfaces/CommandExecutor.java similarity index 70% rename from src/main/java/pw/yumc/YumCore/commands/CommandExecutor.java rename to src/main/java/pw/yumc/YumCore/commands/interfaces/CommandExecutor.java index f7b2b4d..869e0b8 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandExecutor.java +++ b/src/main/java/pw/yumc/YumCore/commands/interfaces/CommandExecutor.java @@ -1,4 +1,4 @@ -package pw.yumc.YumCore.commands; +package pw.yumc.YumCore.commands.interfaces; /** * 命令执行类 diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelpParse.java b/src/main/java/pw/yumc/YumCore/commands/interfaces/CommandHelpParse.java similarity index 82% rename from src/main/java/pw/yumc/YumCore/commands/CommandHelpParse.java rename to src/main/java/pw/yumc/YumCore/commands/interfaces/CommandHelpParse.java index bd4bb53..377539d 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelpParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/interfaces/CommandHelpParse.java @@ -1,4 +1,4 @@ -package pw.yumc.YumCore.commands; +package pw.yumc.YumCore.commands.interfaces; /** * 命令解析接口