diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandError.java b/src/main/java/pw/yumc/YumCore/commands/CommandError.java index 4ec54dc..5dcc23d 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandError.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandError.java @@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.commands.exception.ArgumentException; import pw.yumc.YumCore.commands.exception.CommandException; +import pw.yumc.YumCore.commands.exception.ParseException; import pw.yumc.YumCore.commands.exception.PermissionException; import pw.yumc.YumCore.commands.exception.SenderException; import pw.yumc.YumCore.commands.info.CommandInfo; @@ -22,9 +23,11 @@ public class CommandError implements ErrorHanlder { private static String cmdErr = "§6错误原因: §4命令参数不正确!"; private static String cmdUse = "§6使用方法: §e/%s %s%s"; private static String cmdDes = "§6命令描述: §3%s"; + private static String argErr = "§c参数错误: §4%s"; @Override public void error(CommandException e, CommandSender sender, CommandInfo info, String label, String[] args) { + if (e == null) { return; } if (e instanceof SenderException) { Log.sender(sender, onlyExecutor, info.getExecutorStr()); } else if (e instanceof PermissionException) { @@ -33,6 +36,10 @@ public class CommandError implements ErrorHanlder { Log.sender(sender, cmdErr); Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments()); Log.sender(sender, cmdDes, info.getHelp().value()); + } else if (e instanceof ParseException) { + Log.sender(sender, argErr, e.getMessage()); + } else { + throw e; } } } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandMain.java b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java index b1f9edf..0a4349b 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandMain.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java @@ -15,9 +15,7 @@ import org.bukkit.command.PluginCommand; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.commands.annotation.Help; -import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.info.CommandInfo; -import pw.yumc.YumCore.commands.interfaces.ErrorHanlder; import pw.yumc.YumCore.commands.interfaces.Executor; import pw.yumc.YumCore.commands.interfaces.HelpGenerator; @@ -41,10 +39,6 @@ public class CommandMain implements CommandExecutor { * 命令帮助处理 */ private CommandHelp help; - /** - * 命令错误处理 - */ - private ErrorHanlder commandErrorHanlder = new CommandError(); /** * 帮助页生成 */ @@ -133,12 +127,7 @@ public class CommandMain implements CommandExecutor { return true; } CommandInfo manager = getByCache(label); - try { - return manager != null && manager.execute(sender, label, args); - } catch (CommandException e) { - commandErrorHanlder.error(e, sender, manager, label, args); - } - return false; + return manager != null && manager.execute(sender, label, args); } private class MainHelpGenerator extends CommandHelp.DefaultHelpGenerator { diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandSub.java b/src/main/java/pw/yumc/YumCore/commands/CommandSub.java index 4e3e19d..6adc0b3 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandSub.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandSub.java @@ -20,7 +20,6 @@ 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.exception.CommandException; import pw.yumc.YumCore.commands.info.CommandInfo; import pw.yumc.YumCore.commands.info.CommandTabInfo; import pw.yumc.YumCore.commands.interfaces.ErrorHanlder; @@ -46,10 +45,6 @@ public class CommandSub implements TabExecutor { * 插件命令 */ private PluginCommand cmd; - /** - * 命令错误处理 - */ - private ErrorHanlder commandErrorHanlder = new CommandError(); /** * 插件实例类 */ @@ -188,12 +183,7 @@ public class CommandSub implements TabExecutor { } else { subargs = moveStrings(args); } - try { - return cmd.execute(sender, label, subargs); - } catch (CommandException e) { - commandErrorHanlder.error(e, sender, cmd, label, subargs); - } - return false; + return cmd.execute(sender, label, subargs); } @Override @@ -293,7 +283,7 @@ public class CommandSub implements TabExecutor { * @return {@link CommandSub} */ public CommandSub setCommandErrorHanlder(ErrorHanlder commandErrorHanlder) { - this.commandErrorHanlder = commandErrorHanlder; + cmds.forEach(commandInfo -> commandInfo.setCommandErrorHandler(commandErrorHanlder)); return this; } 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 65b0d7a..b5f3e42 100644 --- a/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java @@ -10,8 +10,8 @@ import java.util.Objects; 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.CommandError; import pw.yumc.YumCore.commands.CommandParse; import pw.yumc.YumCore.commands.annotation.Async; import pw.yumc.YumCore.commands.annotation.Cmd; @@ -19,9 +19,10 @@ import pw.yumc.YumCore.commands.annotation.Cmd.Executor; import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Sort; import pw.yumc.YumCore.commands.exception.ArgumentException; -import pw.yumc.YumCore.commands.exception.ParseException; +import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.PermissionException; import pw.yumc.YumCore.commands.exception.SenderException; +import pw.yumc.YumCore.commands.interfaces.ErrorHanlder; /** * 命令信息存储类 @@ -31,7 +32,6 @@ import pw.yumc.YumCore.commands.exception.SenderException; */ public class CommandInfo { public static CommandInfo Unknow = new CommandInfo(); - private static String argErr = "§c参数错误: §4%s"; private static Help defHelp = new Help() { @Override public Class annotationType() { @@ -60,6 +60,10 @@ public class CommandInfo { private Help help; private int sort; private CommandParse parse; + /** + * 命令错误处理 + */ + private ErrorHanlder commandErrorHandler = new CommandError(); public CommandInfo(Method method, Object origin, Cmd command, Help help, boolean async, int sort, CommandParse parse) { this.method = method; @@ -126,12 +130,12 @@ public class CommandInfo { */ public boolean execute(final CommandSender sender, final String label, final String[] args) { if (method == null) { return false; } - check(sender, label, args); Runnable runnable = () -> { try { + check(sender, label, args); method.invoke(origin, parse.parse(sender, label, args)); - } catch (ParseException | ArgumentException e) { - Log.sender(sender, argErr, e.getMessage()); + } catch (CommandException e) { + commandErrorHandler.error(e, sender, this, label, args); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } @@ -218,6 +222,16 @@ public class CommandInfo { this.main = true; } + /** + * 设置命令错误处理器 + * + * @param commandErrorHandler + * 命令错误处理器 + */ + public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) { + this.commandErrorHandler = commandErrorHandler; + } + @Override public boolean equals(Object o) { if (this == o) return true;