1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-21 01:38:51 +00:00

fix: 修复主命令错误拦截处理

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-11-27 21:50:11 +08:00
parent 8feb8fead0
commit d6a530d74b
3 changed files with 48 additions and 23 deletions

View File

@ -0,0 +1,35 @@
package pw.yumc.YumCore.commands;
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.PermissionException;
import pw.yumc.YumCore.commands.exception.SenderException;
import pw.yumc.YumCore.commands.info.CommandInfo;
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
/**
* @author
* @since 2016/11/27 0027
*/
public class CommandError implements ErrorHanlder {
private static String onlyExecutor = "§c当前命令仅允许 §b%s §c执行!";
private static String losePerm = "§c你需要有 %s 的权限才能执行此命令!";
private static String cmdErr = "§6错误原因: §4命令参数不正确!";
private static String cmdUse = "§6使用方法: §e/%s %s%s";
private static String cmdDes = "§6命令描述: §3%s";
@Override
public void error(CommandException e, CommandSender sender, CommandInfo info, String label, String[] args) {
if (e instanceof SenderException) {
Log.sender(sender, onlyExecutor, info.getExecutorStr());
} else if (e instanceof PermissionException) {
Log.sender(sender, losePerm, info.getCommand().permission());
} else if (e instanceof ArgumentException) {
Log.sender(sender, cmdErr);
Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
Log.sender(sender, cmdDes, info.getHelp().value());
}
}
}

View File

@ -7,7 +7,9 @@ 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;
@ -34,6 +36,10 @@ public class CommandMain implements CommandExecutor {
* 命令帮助处理
*/
private CommandHelp help;
/**
* 命令错误处理
*/
private ErrorHanlder commandErrorHanlder = new CommandError();
/**
* 帮助页生成
*/
@ -122,7 +128,12 @@ public class CommandMain implements CommandExecutor {
return true;
}
CommandInfo manager = getByCache(label);
return manager != null && manager.execute(sender, label, args);
try {
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 {

View File

@ -10,10 +10,7 @@ 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.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.info.CommandInfo;
import pw.yumc.YumCore.commands.info.CommandTabInfo;
import pw.yumc.YumCore.commands.interfaces.ErrorHanlder;
@ -33,11 +30,6 @@ import java.util.*;
public class CommandSub implements TabExecutor {
private static String argumentTypeError = "注解命令方法 %s 位于 %s 的参数错误 第一个参数应实现 CommandSender 接口!";
private static String returnTypeError = "注解命令补全 %s 位于 %s 的返回值错误 应实现 List 接口!";
private static String onlyExecutor = "§c当前命令仅允许 §b%s §c执行!";
private static String losePerm = "§c你需要有 %s 的权限才能执行此命令!";
private static String cmdErr = "§6错误原因: §4命令参数不正确!";
private static String cmdUse = "§6使用方法: §e/%s %s%s";
private static String cmdDes = "§6命令描述: §3%s";
/**
* 命令帮助
@ -50,20 +42,7 @@ public class CommandSub implements TabExecutor {
/**
* 命令错误处理
*/
private ErrorHanlder commandErrorHanlder = new ErrorHanlder() {
@Override
public void error(CommandException e, CommandSender sender, CommandInfo info, String label, String[] args) {
if (e instanceof SenderException) {
Log.sender(sender, onlyExecutor, info.getExecutorStr());
} else if (e instanceof PermissionException) {
Log.sender(sender, losePerm, info.getCommand().permission());
} else if (e instanceof ArgumentException) {
Log.sender(sender, cmdErr);
Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
Log.sender(sender, cmdDes, info.getHelp().value());
}
}
};
private ErrorHanlder commandErrorHanlder = new CommandError();
/**
* 插件实例类
*/