fix: 修复命令异常处理流程

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2017-02-23 20:33:21 +08:00
parent 104c35a0cf
commit c9bc929cec
4 changed files with 30 additions and 30 deletions

View File

@ -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;
}
}
}

View File

@ -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 {

View File

@ -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;
}

View File

@ -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<? extends Annotation> 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;