From ba080a658487640d3de32adff7716cda960953df Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sun, 20 Nov 2016 23:21:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=B8=BB=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- pom.xml | 2 +- .../pw/yumc/YumCore/commands/CommandMain.java | 98 +++++++++++++++++++ .../yumc/YumCore/commands/CommandManager.java | 23 +++-- 3 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/main/java/pw/yumc/YumCore/commands/CommandMain.java diff --git a/pom.xml b/pom.xml index be6d3d6..6861308 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 pw.yumc YumCore - 1.3 + 1.5 ${project.artifactId} diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandMain.java b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java new file mode 100644 index 0000000..b24524c --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java @@ -0,0 +1,98 @@ +package pw.yumc.YumCore.commands; + +import java.lang.reflect.Method; +import java.util.*; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.commands.info.CommandInfo; +import pw.yumc.YumCore.commands.interfaces.Executor; + +/** + * 主类命令管理 + * + * @author 喵♂呜 + * @since 2016/11/18 0018 + */ +public class CommandMain { + private static String argumentTypeError = "注解命令方法 %s 位于 %s 的参数错误 第一个参数应实现 CommandSender 接口!"; + /** + * 命令列表 + */ + private Set cmds = new HashSet<>(); + /** + * 命令缓存列表 + */ + private Map cmdCache = new HashMap<>(); + + /** + * 主类命令管理类 + * + * @param clazzs + * 命令类 + */ + public CommandMain(Executor... clazzs) { + register(clazzs); + } + + /** + * 注册命令 + * + * @param clazzs + * 命令类 + * @return {@link CommandMain} + */ + public CommandMain register(Executor... clazzs) { + for (Executor clazz : clazzs) { + Method[] methods = clazz.getClass().getDeclaredMethods(); + for (Method method : methods) { + registerCommand(method, clazz); + } + } + return this; + } + + public boolean execute(CommandSender sender, Command command, String label, String[] args) { + CommandInfo manager = getByCache(label); + return manager != null && manager.execute(new CommandArgument(sender, command, label, args)); + } + + private boolean registerCommand(Method method, Executor clazz) { + CommandInfo ci = CommandInfo.parse(method, clazz); + if (ci != null) { + Class[] params = method.getParameterTypes(); + Log.d("命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params)); + try { + Class sender = params[0]; + cmds.add(ci); + cmdCache.put(ci.getName(), ci); + return true; + } catch (ArrayIndexOutOfBoundsException | ClassCastException ignored) { + } + Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName())); + } + return false; + } + + /** + * 检查缓存并获得命令 + * + * @param cmd + * 子命令 + * @return 命令信息 + */ + private CommandInfo getByCache(String cmd) { + if (!cmdCache.containsKey(cmd)) { + for (CommandInfo cmdinfo : cmds) { + if (cmdinfo.isValid(cmd)) { + cmdCache.put(cmd, cmdinfo); + break; + } + } + cmdCache.put(cmd, null); + } + return cmdCache.get(cmd); + } +} diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java index 67aec15..7ee0d10 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandManager.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandManager.java @@ -1,5 +1,11 @@ package pw.yumc.YumCore.commands; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + import org.bukkit.Bukkit; import org.bukkit.command.*; import org.bukkit.entity.Player; @@ -7,6 +13,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.StringUtil; + import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.compatible.C; @@ -21,12 +28,6 @@ import pw.yumc.YumCore.commands.interfaces.Executor; import pw.yumc.YumCore.commands.interfaces.HelpGenerator; import pw.yumc.YumCore.commands.interfaces.HelpParse; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; - /** * 命令管理类 * @@ -118,6 +119,16 @@ public class CommandManager implements TabExecutor { */ private List cmdNameCache = new ArrayList<>(); + /** + * 命令管理器 用于主类 + * + * @param executor + * 命令执行类 + */ + public CommandManager(Executor... executor) { + register(executor); + } + /** * 命令管理器 *