mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2025-09-02 11:36:59 +00:00
fix: 修复命令执行时抛出CommandException未拦截的问题
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
@ -24,6 +24,7 @@ public class CommandError implements ErrorHanlder {
|
|||||||
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";
|
private static String argErr = "§c参数错误: §4%s";
|
||||||
|
private static String execErr = "§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) {
|
||||||
@ -39,7 +40,7 @@ public class CommandError implements ErrorHanlder {
|
|||||||
} else if (e instanceof ParseException) {
|
} else if (e instanceof ParseException) {
|
||||||
Log.sender(sender, argErr, e.getMessage());
|
Log.sender(sender, argErr, e.getMessage());
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
Log.sender(sender, execErr, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package pw.yumc.YumCore.commands.info;
|
package pw.yumc.YumCore.commands.info;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -99,9 +98,9 @@ public class CommandInfo {
|
|||||||
* 解析CommandInfo
|
* 解析CommandInfo
|
||||||
*
|
*
|
||||||
* @param method
|
* @param method
|
||||||
* 方法
|
* 方法
|
||||||
* @param origin
|
* @param origin
|
||||||
* 源对象
|
* 源对象
|
||||||
* @return {@link CommandInfo}
|
* @return {@link CommandInfo}
|
||||||
*/
|
*/
|
||||||
public static CommandInfo parse(Method method, Object origin) {
|
public static CommandInfo parse(Method method, Object origin) {
|
||||||
@ -121,11 +120,11 @@ public class CommandInfo {
|
|||||||
* 执行命令
|
* 执行命令
|
||||||
*
|
*
|
||||||
* @param sender
|
* @param sender
|
||||||
* 命令发送者
|
* 命令发送者
|
||||||
* @param label
|
* @param label
|
||||||
* 命令标签
|
* 命令标签
|
||||||
* @param args
|
* @param args
|
||||||
* 参数
|
* 参数
|
||||||
* @return 是否执行成功
|
* @return 是否执行成功
|
||||||
*/
|
*/
|
||||||
public boolean execute(final CommandSender sender, final String label, final String[] args) {
|
public boolean execute(final CommandSender sender, final String label, final String[] args) {
|
||||||
@ -134,9 +133,12 @@ public class CommandInfo {
|
|||||||
try {
|
try {
|
||||||
check(sender, label, args);
|
check(sender, label, args);
|
||||||
method.invoke(origin, parse.parse(sender, label, args));
|
method.invoke(origin, parse.parse(sender, label, args));
|
||||||
} catch (CommandException e) {
|
} catch (Exception e) {
|
||||||
commandErrorHandler.error(e, sender, this, label, args);
|
if (e.getCause() instanceof CommandException) { e = (Exception) e.getCause(); }
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
if (e instanceof CommandException) {
|
||||||
|
commandErrorHandler.error((CommandException) e, sender, this, label, args);
|
||||||
|
return;
|
||||||
|
}
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -208,7 +210,7 @@ public class CommandInfo {
|
|||||||
* 验证命令是否匹配
|
* 验证命令是否匹配
|
||||||
*
|
*
|
||||||
* @param cmd
|
* @param cmd
|
||||||
* 需验证命令
|
* 需验证命令
|
||||||
* @return 是否匹配
|
* @return 是否匹配
|
||||||
*/
|
*/
|
||||||
public boolean isValid(String cmd) {
|
public boolean isValid(String cmd) {
|
||||||
@ -226,7 +228,7 @@ public class CommandInfo {
|
|||||||
* 设置命令错误处理器
|
* 设置命令错误处理器
|
||||||
*
|
*
|
||||||
* @param commandErrorHandler
|
* @param commandErrorHandler
|
||||||
* 命令错误处理器
|
* 命令错误处理器
|
||||||
*/
|
*/
|
||||||
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
|
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
|
||||||
this.commandErrorHandler = commandErrorHandler;
|
this.commandErrorHandler = commandErrorHandler;
|
||||||
|
Reference in New Issue
Block a user