mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-24 02:08:48 +00:00
fix: 修复命令执行时抛出CommandException未拦截的问题
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
300dc0d607
commit
be1ad18954
@ -13,7 +13,7 @@ import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 命令错误处理
|
* 命令错误处理
|
||||||
*
|
*
|
||||||
* @author 喵♂呜
|
* @author 喵♂呜
|
||||||
* @since 2016/11/27 0027
|
* @since 2016/11/27 0027
|
||||||
*/
|
*/
|
||||||
@ -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,262 +1,264 @@
|
|||||||
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;
|
import java.util.Objects;
|
||||||
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.P;
|
||||||
import pw.yumc.YumCore.bukkit.P;
|
import pw.yumc.YumCore.commands.CommandError;
|
||||||
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;
|
import pw.yumc.YumCore.commands.annotation.Cmd.Executor;
|
||||||
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.CommandException;
|
||||||
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;
|
||||||
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
|
|
||||||
|
/**
|
||||||
/**
|
* 命令信息存储类
|
||||||
* 命令信息存储类
|
*
|
||||||
*
|
* @author 喵♂呜
|
||||||
* @author 喵♂呜
|
* @since 2016年7月23日 上午9:56:42
|
||||||
* @since 2016年7月23日 上午9:56:42
|
*/
|
||||||
*/
|
public class CommandInfo {
|
||||||
public class CommandInfo {
|
public static CommandInfo Unknow = new CommandInfo();
|
||||||
public static CommandInfo Unknow = new CommandInfo();
|
private static Help defHelp = new Help() {
|
||||||
private static Help defHelp = new Help() {
|
@Override
|
||||||
@Override
|
public Class<? extends Annotation> annotationType() {
|
||||||
public Class<? extends Annotation> annotationType() {
|
return Help.class;
|
||||||
return Help.class;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public String possibleArguments() {
|
||||||
public String possibleArguments() {
|
return "这家伙很懒";
|
||||||
return "这家伙很懒";
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public String value() {
|
||||||
public String value() {
|
return "没写帮助信息";
|
||||||
return "没写帮助信息";
|
}
|
||||||
}
|
};
|
||||||
};
|
private Object origin;
|
||||||
private Object origin;
|
private Method method;
|
||||||
private Method method;
|
private String name;
|
||||||
private String name;
|
private List<String> aliases;
|
||||||
private List<String> aliases;
|
private List<Executor> executors;
|
||||||
private List<Executor> executors;
|
private String executorStr;
|
||||||
private String executorStr;
|
private boolean async;
|
||||||
private boolean async;
|
private boolean main;
|
||||||
private boolean main;
|
private Cmd command;
|
||||||
private Cmd command;
|
private Help help;
|
||||||
private Help help;
|
private Integer sort;
|
||||||
private Integer sort;
|
private CommandParse parse;
|
||||||
private CommandParse parse;
|
/**
|
||||||
/**
|
* 命令错误处理
|
||||||
* 命令错误处理
|
*/
|
||||||
*/
|
private ErrorHanlder commandErrorHandler = new CommandError();
|
||||||
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;
|
this.origin = origin;
|
||||||
this.origin = origin;
|
this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value();
|
||||||
this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value();
|
this.aliases = Arrays.asList(command.aliases());
|
||||||
this.aliases = Arrays.asList(command.aliases());
|
this.executors = Arrays.asList(command.executor());
|
||||||
this.executors = Arrays.asList(command.executor());
|
this.executorStr = eS(executors);
|
||||||
this.executorStr = eS(executors);
|
this.command = command;
|
||||||
this.command = command;
|
this.help = help != null ? help : defHelp;
|
||||||
this.help = help != null ? help : defHelp;
|
this.async = async;
|
||||||
this.async = async;
|
this.main = method.getReturnType().equals(boolean.class);
|
||||||
this.main = method.getReturnType().equals(boolean.class);
|
this.sort = sort;
|
||||||
this.sort = sort;
|
this.parse = parse;
|
||||||
this.parse = parse;
|
}
|
||||||
}
|
|
||||||
|
private CommandInfo() {
|
||||||
private CommandInfo() {
|
this.method = null;
|
||||||
this.method = null;
|
this.origin = null;
|
||||||
this.origin = null;
|
this.name = "unknow";
|
||||||
this.name = "unknow";
|
this.aliases = null;
|
||||||
this.aliases = null;
|
this.executors = null;
|
||||||
this.executors = null;
|
this.executorStr = null;
|
||||||
this.executorStr = null;
|
this.command = null;
|
||||||
this.command = null;
|
this.help = null;
|
||||||
this.help = null;
|
this.async = false;
|
||||||
this.async = false;
|
this.main = false;
|
||||||
this.main = false;
|
this.sort = 0;
|
||||||
this.sort = 0;
|
this.parse = null;
|
||||||
this.parse = null;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* 解析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) {
|
Cmd command = method.getAnnotation(Cmd.class);
|
||||||
Cmd command = method.getAnnotation(Cmd.class);
|
if (command != null) {
|
||||||
if (command != null) {
|
method.setAccessible(true);
|
||||||
method.setAccessible(true);
|
Help help = method.getAnnotation(Help.class);
|
||||||
Help help = method.getAnnotation(Help.class);
|
Async async = method.getAnnotation(Async.class);
|
||||||
Async async = method.getAnnotation(Async.class);
|
Sort sort = method.getAnnotation(Sort.class);
|
||||||
Sort sort = method.getAnnotation(Sort.class);
|
CommandParse cp = CommandParse.get(method);
|
||||||
CommandParse cp = CommandParse.get(method);
|
return new CommandInfo(method, origin, command, help, async != null, sort != null ? sort.value() : 50, cp);
|
||||||
return new CommandInfo(method, origin, command, help, async != null, sort != null ? sort.value() : 50, cp);
|
}
|
||||||
}
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* 执行命令
|
||||||
* 执行命令
|
*
|
||||||
*
|
* @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) {
|
if (method == null) { return false; }
|
||||||
if (method == null) { return false; }
|
Runnable runnable = () -> {
|
||||||
Runnable runnable = () -> {
|
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 (Exception e) {
|
||||||
} catch (CommandException e) {
|
if (e.getCause() instanceof CommandException) { e = (Exception) e.getCause(); }
|
||||||
commandErrorHandler.error(e, sender, this, label, args);
|
if (e instanceof CommandException) {
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
commandErrorHandler.error((CommandException) e, sender, this, label, args);
|
||||||
throw new RuntimeException(e);
|
return;
|
||||||
}
|
}
|
||||||
};
|
throw new RuntimeException(e);
|
||||||
if (async) {
|
}
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(P.instance, runnable);
|
};
|
||||||
} else {
|
if (async) {
|
||||||
runnable.run();
|
Bukkit.getScheduler().runTaskAsynchronously(P.instance, runnable);
|
||||||
}
|
} else {
|
||||||
return true;
|
runnable.run();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
/**
|
}
|
||||||
* @return 命令注解
|
|
||||||
*/
|
/**
|
||||||
public Cmd getCommand() {
|
* @return 命令注解
|
||||||
return command;
|
*/
|
||||||
}
|
public Cmd getCommand() {
|
||||||
|
return command;
|
||||||
/**
|
}
|
||||||
* @return 帮助注解
|
|
||||||
*/
|
/**
|
||||||
public Help getHelp() {
|
* @return 帮助注解
|
||||||
return help;
|
*/
|
||||||
}
|
public Help getHelp() {
|
||||||
|
return help;
|
||||||
/**
|
}
|
||||||
* @return 命令名称
|
|
||||||
*/
|
/**
|
||||||
public String getName() {
|
* @return 命令名称
|
||||||
return name;
|
*/
|
||||||
}
|
public String getName() {
|
||||||
|
return name;
|
||||||
/**
|
}
|
||||||
* @return 命令别名
|
|
||||||
*/
|
/**
|
||||||
public List<String> getAliases() {
|
* @return 命令别名
|
||||||
return aliases;
|
*/
|
||||||
}
|
public List<String> getAliases() {
|
||||||
|
return aliases;
|
||||||
/**
|
}
|
||||||
* @return 命令排序
|
|
||||||
*/
|
/**
|
||||||
public Integer getSort() {
|
* @return 命令排序
|
||||||
return sort;
|
*/
|
||||||
}
|
public Integer getSort() {
|
||||||
|
return sort;
|
||||||
/**
|
}
|
||||||
* @return 是否为异步命令
|
|
||||||
*/
|
/**
|
||||||
public boolean isAsync() {
|
* @return 是否为异步命令
|
||||||
return async;
|
*/
|
||||||
}
|
public boolean isAsync() {
|
||||||
|
return async;
|
||||||
/**
|
}
|
||||||
* @return 是否为主命令
|
|
||||||
*/
|
/**
|
||||||
public boolean isMain() {
|
* @return 是否为主命令
|
||||||
return main;
|
*/
|
||||||
}
|
public boolean isMain() {
|
||||||
|
return main;
|
||||||
/**
|
}
|
||||||
* @return 允许的命令发送者
|
|
||||||
*/
|
/**
|
||||||
public String getExecutorStr() {
|
* @return 允许的命令发送者
|
||||||
return executorStr;
|
*/
|
||||||
}
|
public String getExecutorStr() {
|
||||||
|
return executorStr;
|
||||||
/**
|
}
|
||||||
* 验证命令是否匹配
|
|
||||||
*
|
/**
|
||||||
* @param cmd
|
* 验证命令是否匹配
|
||||||
* 需验证命令
|
*
|
||||||
* @return 是否匹配
|
* @param cmd
|
||||||
*/
|
* 需验证命令
|
||||||
public boolean isValid(String cmd) {
|
* @return 是否匹配
|
||||||
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
|
*/
|
||||||
}
|
public boolean isValid(String cmd) {
|
||||||
|
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
|
||||||
/**
|
}
|
||||||
* 设置为主命令
|
|
||||||
*/
|
/**
|
||||||
public void setMain() {
|
* 设置为主命令
|
||||||
this.main = true;
|
*/
|
||||||
}
|
public void setMain() {
|
||||||
|
this.main = true;
|
||||||
/**
|
}
|
||||||
* 设置命令错误处理器
|
|
||||||
*
|
/**
|
||||||
* @param commandErrorHandler
|
* 设置命令错误处理器
|
||||||
* 命令错误处理器
|
*
|
||||||
*/
|
* @param commandErrorHandler
|
||||||
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
|
* 命令错误处理器
|
||||||
this.commandErrorHandler = commandErrorHandler;
|
*/
|
||||||
}
|
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
|
||||||
|
this.commandErrorHandler = commandErrorHandler;
|
||||||
@Override
|
}
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
@Override
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
public boolean equals(Object o) {
|
||||||
CommandInfo that = (CommandInfo) o;
|
if (this == o) return true;
|
||||||
return Objects.equals(origin, that.origin) && Objects.equals(method, that.method) && Objects.equals(name, that.name);
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
}
|
CommandInfo that = (CommandInfo) o;
|
||||||
|
return Objects.equals(origin, that.origin) && Objects.equals(method, that.method) && Objects.equals(name, that.name);
|
||||||
@Override
|
}
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(origin, method, name);
|
@Override
|
||||||
}
|
public int hashCode() {
|
||||||
|
return Objects.hash(origin, method, name);
|
||||||
private void check(CommandSender sender, String label, String[] args) {
|
}
|
||||||
if (!executors.contains(Executor.ALL) && !executors.contains(Executor.valueOf(sender))) { throw new SenderException(executorStr); }
|
|
||||||
if (!"".equals(command.permission()) && !sender.hasPermission(command.permission())) { throw new PermissionException(command.permission()); }
|
private void check(CommandSender sender, String label, String[] args) {
|
||||||
if (args.length < command.minimumArguments()) { throw new ArgumentException(String.valueOf(command.minimumArguments())); }
|
if (!executors.contains(Executor.ALL) && !executors.contains(Executor.valueOf(sender))) { throw new SenderException(executorStr); }
|
||||||
}
|
if (!"".equals(command.permission()) && !sender.hasPermission(command.permission())) { throw new PermissionException(command.permission()); }
|
||||||
|
if (args.length < command.minimumArguments()) { throw new ArgumentException(String.valueOf(command.minimumArguments())); }
|
||||||
private String eS(List<Executor> executors) {
|
}
|
||||||
StringBuilder str = new StringBuilder();
|
|
||||||
for (Executor executor : executors) {
|
private String eS(List<Executor> executors) {
|
||||||
str.append(executor.getName());
|
StringBuilder str = new StringBuilder();
|
||||||
str.append(", ");
|
for (Executor executor : executors) {
|
||||||
}
|
str.append(executor.getName());
|
||||||
return str.toString().substring(0, str.length() - 2);
|
str.append(", ");
|
||||||
}
|
}
|
||||||
}
|
return str.toString().substring(0, str.length() - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user