fix: 修复命令执行时抛出CommandException未拦截的问题

Signed-off-by: MiaoWoo <admin@yumc.pw>
merge/16/MERGE
MiaoWoo 2019-10-26 10:15:06 +08:00
parent 300dc0d607
commit be1ad18954
2 changed files with 267 additions and 264 deletions

View File

@ -13,7 +13,7 @@ import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
/**
*
*
*
* @author
* @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 cmdDes = "§6命令描述: §3%s";
private static String argErr = "§c参数错误: §4%s";
private static String execErr = "§c命令执行失败: §4%s";
@Override
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) {
Log.sender(sender, argErr, e.getMessage());
} else {
throw e;
Log.sender(sender, execErr, e.getMessage());
}
}
}

View File

@ -1,262 +1,264 @@
package pw.yumc.YumCore.commands.info;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
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;
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.CommandException;
import pw.yumc.YumCore.commands.exception.PermissionException;
import pw.yumc.YumCore.commands.exception.SenderException;
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
/**
*
*
* @author
* @since 2016723 9:56:42
*/
public class CommandInfo {
public static CommandInfo Unknow = new CommandInfo();
private static Help defHelp = new Help() {
@Override
public Class<? extends Annotation> annotationType() {
return Help.class;
}
@Override
public String possibleArguments() {
return "这家伙很懒";
}
@Override
public String value() {
return "没写帮助信息";
}
};
private Object origin;
private Method method;
private String name;
private List<String> aliases;
private List<Executor> executors;
private String executorStr;
private boolean async;
private boolean main;
private Cmd command;
private Help help;
private Integer 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;
this.origin = origin;
this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value();
this.aliases = Arrays.asList(command.aliases());
this.executors = Arrays.asList(command.executor());
this.executorStr = eS(executors);
this.command = command;
this.help = help != null ? help : defHelp;
this.async = async;
this.main = method.getReturnType().equals(boolean.class);
this.sort = sort;
this.parse = parse;
}
private CommandInfo() {
this.method = null;
this.origin = null;
this.name = "unknow";
this.aliases = null;
this.executors = null;
this.executorStr = null;
this.command = null;
this.help = null;
this.async = false;
this.main = false;
this.sort = 0;
this.parse = null;
}
/**
* CommandInfo
*
* @param method
*
* @param origin
*
* @return {@link CommandInfo}
*/
public static CommandInfo parse(Method method, Object origin) {
Cmd command = method.getAnnotation(Cmd.class);
if (command != null) {
method.setAccessible(true);
Help help = method.getAnnotation(Help.class);
Async async = method.getAnnotation(Async.class);
Sort sort = method.getAnnotation(Sort.class);
CommandParse cp = CommandParse.get(method);
return new CommandInfo(method, origin, command, help, async != null, sort != null ? sort.value() : 50, cp);
}
return null;
}
/**
*
*
* @param sender
*
* @param label
*
* @param args
*
* @return
*/
public boolean execute(final CommandSender sender, final String label, final String[] args) {
if (method == null) { return false; }
Runnable runnable = () -> {
try {
check(sender, label, args);
method.invoke(origin, parse.parse(sender, label, args));
} catch (CommandException e) {
commandErrorHandler.error(e, sender, this, label, args);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
};
if (async) {
Bukkit.getScheduler().runTaskAsynchronously(P.instance, runnable);
} else {
runnable.run();
}
return true;
}
/**
* @return
*/
public Cmd getCommand() {
return command;
}
/**
* @return
*/
public Help getHelp() {
return help;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @return
*/
public List<String> getAliases() {
return aliases;
}
/**
* @return
*/
public Integer getSort() {
return sort;
}
/**
* @return
*/
public boolean isAsync() {
return async;
}
/**
* @return
*/
public boolean isMain() {
return main;
}
/**
* @return
*/
public String getExecutorStr() {
return executorStr;
}
/**
*
*
* @param cmd
*
* @return
*/
public boolean isValid(String cmd) {
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
}
/**
*
*/
public void setMain() {
this.main = true;
}
/**
*
*
* @param commandErrorHandler
*
*/
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
this.commandErrorHandler = commandErrorHandler;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
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);
}
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()); }
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) {
str.append(executor.getName());
str.append(", ");
}
return str.toString().substring(0, str.length() - 2);
}
}
package pw.yumc.YumCore.commands.info;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
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;
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.CommandException;
import pw.yumc.YumCore.commands.exception.PermissionException;
import pw.yumc.YumCore.commands.exception.SenderException;
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
/**
*
*
* @author
* @since 2016723 9:56:42
*/
public class CommandInfo {
public static CommandInfo Unknow = new CommandInfo();
private static Help defHelp = new Help() {
@Override
public Class<? extends Annotation> annotationType() {
return Help.class;
}
@Override
public String possibleArguments() {
return "这家伙很懒";
}
@Override
public String value() {
return "没写帮助信息";
}
};
private Object origin;
private Method method;
private String name;
private List<String> aliases;
private List<Executor> executors;
private String executorStr;
private boolean async;
private boolean main;
private Cmd command;
private Help help;
private Integer 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;
this.origin = origin;
this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value();
this.aliases = Arrays.asList(command.aliases());
this.executors = Arrays.asList(command.executor());
this.executorStr = eS(executors);
this.command = command;
this.help = help != null ? help : defHelp;
this.async = async;
this.main = method.getReturnType().equals(boolean.class);
this.sort = sort;
this.parse = parse;
}
private CommandInfo() {
this.method = null;
this.origin = null;
this.name = "unknow";
this.aliases = null;
this.executors = null;
this.executorStr = null;
this.command = null;
this.help = null;
this.async = false;
this.main = false;
this.sort = 0;
this.parse = null;
}
/**
* CommandInfo
*
* @param method
*
* @param origin
*
* @return {@link CommandInfo}
*/
public static CommandInfo parse(Method method, Object origin) {
Cmd command = method.getAnnotation(Cmd.class);
if (command != null) {
method.setAccessible(true);
Help help = method.getAnnotation(Help.class);
Async async = method.getAnnotation(Async.class);
Sort sort = method.getAnnotation(Sort.class);
CommandParse cp = CommandParse.get(method);
return new CommandInfo(method, origin, command, help, async != null, sort != null ? sort.value() : 50, cp);
}
return null;
}
/**
*
*
* @param sender
*
* @param label
*
* @param args
*
* @return
*/
public boolean execute(final CommandSender sender, final String label, final String[] args) {
if (method == null) { return false; }
Runnable runnable = () -> {
try {
check(sender, label, args);
method.invoke(origin, parse.parse(sender, label, args));
} catch (Exception e) {
if (e.getCause() instanceof CommandException) { e = (Exception) e.getCause(); }
if (e instanceof CommandException) {
commandErrorHandler.error((CommandException) e, sender, this, label, args);
return;
}
throw new RuntimeException(e);
}
};
if (async) {
Bukkit.getScheduler().runTaskAsynchronously(P.instance, runnable);
} else {
runnable.run();
}
return true;
}
/**
* @return
*/
public Cmd getCommand() {
return command;
}
/**
* @return
*/
public Help getHelp() {
return help;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @return
*/
public List<String> getAliases() {
return aliases;
}
/**
* @return
*/
public Integer getSort() {
return sort;
}
/**
* @return
*/
public boolean isAsync() {
return async;
}
/**
* @return
*/
public boolean isMain() {
return main;
}
/**
* @return
*/
public String getExecutorStr() {
return executorStr;
}
/**
*
*
* @param cmd
*
* @return
*/
public boolean isValid(String cmd) {
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
}
/**
*
*/
public void setMain() {
this.main = true;
}
/**
*
*
* @param commandErrorHandler
*
*/
public void setCommandErrorHandler(ErrorHanlder commandErrorHandler) {
this.commandErrorHandler = commandErrorHandler;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
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);
}
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()); }
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) {
str.append(executor.getName());
str.append(", ");
}
return str.toString().substring(0, str.length() - 2);
}
}