mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-24 02:08:48 +00:00
fix: 修复命令异常处理流程
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
104c35a0cf
commit
c9bc929cec
@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import pw.yumc.YumCore.bukkit.Log;
|
import pw.yumc.YumCore.bukkit.Log;
|
||||||
import pw.yumc.YumCore.commands.exception.ArgumentException;
|
import pw.yumc.YumCore.commands.exception.ArgumentException;
|
||||||
import pw.yumc.YumCore.commands.exception.CommandException;
|
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.PermissionException;
|
||||||
import pw.yumc.YumCore.commands.exception.SenderException;
|
import pw.yumc.YumCore.commands.exception.SenderException;
|
||||||
import pw.yumc.YumCore.commands.info.CommandInfo;
|
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 cmdErr = "§6错误原因: §4命令参数不正确!";
|
||||||
private static String cmdUse = "§6使用方法: §e/%s %s%s";
|
private static String cmdUse = "§6使用方法: §e/%s %s%s";
|
||||||
private static String cmdDes = "§6命令描述: §3%s";
|
private static String cmdDes = "§6命令描述: §3%s";
|
||||||
|
private static String argErr = "§c参数错误: §4%s";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void error(CommandException e, CommandSender sender, CommandInfo info, String label, String[] args) {
|
public void error(CommandException e, CommandSender sender, CommandInfo info, String label, String[] args) {
|
||||||
|
if (e == null) { return; }
|
||||||
if (e instanceof SenderException) {
|
if (e instanceof SenderException) {
|
||||||
Log.sender(sender, onlyExecutor, info.getExecutorStr());
|
Log.sender(sender, onlyExecutor, info.getExecutorStr());
|
||||||
} else if (e instanceof PermissionException) {
|
} else if (e instanceof PermissionException) {
|
||||||
@ -33,6 +36,10 @@ public class CommandError implements ErrorHanlder {
|
|||||||
Log.sender(sender, cmdErr);
|
Log.sender(sender, cmdErr);
|
||||||
Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
|
Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
|
||||||
Log.sender(sender, cmdDes, info.getHelp().value());
|
Log.sender(sender, cmdDes, info.getHelp().value());
|
||||||
|
} else if (e instanceof ParseException) {
|
||||||
|
Log.sender(sender, argErr, e.getMessage());
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,7 @@ import org.bukkit.command.PluginCommand;
|
|||||||
import pw.yumc.YumCore.bukkit.Log;
|
import pw.yumc.YumCore.bukkit.Log;
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.bukkit.P;
|
||||||
import pw.yumc.YumCore.commands.annotation.Help;
|
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.info.CommandInfo;
|
||||||
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
|
|
||||||
import pw.yumc.YumCore.commands.interfaces.Executor;
|
import pw.yumc.YumCore.commands.interfaces.Executor;
|
||||||
import pw.yumc.YumCore.commands.interfaces.HelpGenerator;
|
import pw.yumc.YumCore.commands.interfaces.HelpGenerator;
|
||||||
|
|
||||||
@ -41,10 +39,6 @@ public class CommandMain implements CommandExecutor {
|
|||||||
* 命令帮助处理
|
* 命令帮助处理
|
||||||
*/
|
*/
|
||||||
private CommandHelp help;
|
private CommandHelp help;
|
||||||
/**
|
|
||||||
* 命令错误处理
|
|
||||||
*/
|
|
||||||
private ErrorHanlder commandErrorHanlder = new CommandError();
|
|
||||||
/**
|
/**
|
||||||
* 帮助页生成
|
* 帮助页生成
|
||||||
*/
|
*/
|
||||||
@ -133,12 +127,7 @@ public class CommandMain implements CommandExecutor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
CommandInfo manager = getByCache(label);
|
CommandInfo manager = getByCache(label);
|
||||||
try {
|
|
||||||
return manager != null && manager.execute(sender, label, args);
|
return manager != null && manager.execute(sender, label, args);
|
||||||
} catch (CommandException e) {
|
|
||||||
commandErrorHanlder.error(e, sender, manager, label, args);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MainHelpGenerator extends CommandHelp.DefaultHelpGenerator {
|
private class MainHelpGenerator extends CommandHelp.DefaultHelpGenerator {
|
||||||
|
@ -20,7 +20,6 @@ import org.bukkit.util.StringUtil;
|
|||||||
import pw.yumc.YumCore.bukkit.Log;
|
import pw.yumc.YumCore.bukkit.Log;
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.bukkit.P;
|
||||||
import pw.yumc.YumCore.bukkit.compatible.C;
|
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.CommandInfo;
|
||||||
import pw.yumc.YumCore.commands.info.CommandTabInfo;
|
import pw.yumc.YumCore.commands.info.CommandTabInfo;
|
||||||
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
|
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
|
||||||
@ -46,10 +45,6 @@ public class CommandSub implements TabExecutor {
|
|||||||
* 插件命令
|
* 插件命令
|
||||||
*/
|
*/
|
||||||
private PluginCommand cmd;
|
private PluginCommand cmd;
|
||||||
/**
|
|
||||||
* 命令错误处理
|
|
||||||
*/
|
|
||||||
private ErrorHanlder commandErrorHanlder = new CommandError();
|
|
||||||
/**
|
/**
|
||||||
* 插件实例类
|
* 插件实例类
|
||||||
*/
|
*/
|
||||||
@ -188,12 +183,7 @@ public class CommandSub implements TabExecutor {
|
|||||||
} else {
|
} else {
|
||||||
subargs = moveStrings(args);
|
subargs = moveStrings(args);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
return cmd.execute(sender, label, subargs);
|
return cmd.execute(sender, label, subargs);
|
||||||
} catch (CommandException e) {
|
|
||||||
commandErrorHanlder.error(e, sender, cmd, label, subargs);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -293,7 +283,7 @@ public class CommandSub implements TabExecutor {
|
|||||||
* @return {@link CommandSub}
|
* @return {@link CommandSub}
|
||||||
*/
|
*/
|
||||||
public CommandSub setCommandErrorHanlder(ErrorHanlder commandErrorHanlder) {
|
public CommandSub setCommandErrorHanlder(ErrorHanlder commandErrorHanlder) {
|
||||||
this.commandErrorHanlder = commandErrorHanlder;
|
cmds.forEach(commandInfo -> commandInfo.setCommandErrorHandler(commandErrorHanlder));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@ import java.util.Objects;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import pw.yumc.YumCore.bukkit.Log;
|
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.bukkit.P;
|
||||||
|
import pw.yumc.YumCore.commands.CommandError;
|
||||||
import pw.yumc.YumCore.commands.CommandParse;
|
import pw.yumc.YumCore.commands.CommandParse;
|
||||||
import pw.yumc.YumCore.commands.annotation.Async;
|
import pw.yumc.YumCore.commands.annotation.Async;
|
||||||
import pw.yumc.YumCore.commands.annotation.Cmd;
|
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.Help;
|
||||||
import pw.yumc.YumCore.commands.annotation.Sort;
|
import pw.yumc.YumCore.commands.annotation.Sort;
|
||||||
import pw.yumc.YumCore.commands.exception.ArgumentException;
|
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.PermissionException;
|
||||||
import pw.yumc.YumCore.commands.exception.SenderException;
|
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 class CommandInfo {
|
||||||
public static CommandInfo Unknow = new CommandInfo();
|
public static CommandInfo Unknow = new CommandInfo();
|
||||||
private static String argErr = "§c参数错误: §4%s";
|
|
||||||
private static Help defHelp = new Help() {
|
private static Help defHelp = new Help() {
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends Annotation> annotationType() {
|
public Class<? extends Annotation> annotationType() {
|
||||||
@ -60,6 +60,10 @@ public class CommandInfo {
|
|||||||
private Help help;
|
private Help help;
|
||||||
private int sort;
|
private int sort;
|
||||||
private CommandParse parse;
|
private CommandParse parse;
|
||||||
|
/**
|
||||||
|
* 命令错误处理
|
||||||
|
*/
|
||||||
|
private ErrorHanlder commandErrorHandler = new CommandError();
|
||||||
|
|
||||||
public CommandInfo(Method method, Object origin, Cmd command, Help help, boolean async, int sort, CommandParse parse) {
|
public CommandInfo(Method method, Object origin, Cmd command, Help help, boolean async, int sort, CommandParse parse) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
@ -126,12 +130,12 @@ public class CommandInfo {
|
|||||||
*/
|
*/
|
||||||
public boolean execute(final CommandSender sender, final String label, final String[] args) {
|
public boolean execute(final CommandSender sender, final String label, final String[] args) {
|
||||||
if (method == null) { return false; }
|
if (method == null) { return false; }
|
||||||
check(sender, label, args);
|
|
||||||
Runnable runnable = () -> {
|
Runnable runnable = () -> {
|
||||||
try {
|
try {
|
||||||
|
check(sender, label, args);
|
||||||
method.invoke(origin, parse.parse(sender, label, args));
|
method.invoke(origin, parse.parse(sender, label, args));
|
||||||
} catch (ParseException | ArgumentException e) {
|
} catch (CommandException e) {
|
||||||
Log.sender(sender, argErr, e.getMessage());
|
commandErrorHandler.error(e, sender, this, label, args);
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -218,6 +222,16 @@ public class CommandInfo {
|
|||||||
this.main = true;
|
this.main = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置命令错误处理器
|
||||||
|
*
|
||||||
|
* @param commandErrorHandler
|
||||||
|
* 命令错误处理器
|
||||||
|
*/
|
||||||
|
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
|
||||||
|
this.commandErrorHandler = commandErrorHandler;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user