1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

refactor: 调整方法和流程

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-25 15:06:22 +08:00
parent 7a60e673cc
commit 8f9597bcce
8 changed files with 179 additions and 154 deletions

View File

@ -52,6 +52,12 @@ public class Log {
logger.severe(msg); logger.severe(msg);
} }
public static void toSender(final CommandSender sender, final String... msg) {
for (final String str : msg) {
sender.sendMessage(prefix + str);
}
}
public static void warning(final String msg) { public static void warning(final String msg) {
logger.warning(msg); logger.warning(msg);
} }

View File

@ -2,13 +2,9 @@ package pw.yumc.YumCore.commands;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.commands.exception.IllegalPermissionException;
import pw.yumc.YumCore.commands.exception.IllegalSenderException;
/** /**
* 子命令调用事件 * 子命令参数类
* *
* @since 2015年8月22日上午8:29:44 * @since 2015年8月22日上午8:29:44
* @author * @author
@ -26,52 +22,29 @@ public class CommandArgument {
this.args = args; this.args = args;
} }
public boolean check(final CommandInfo info) {
if (sender instanceof Player) {
if (info.getCommand().onlyConsoleExecutable()) {
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!");
}
} else if (info.getCommand().onlyPlayerExecutable()) {
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!");
}
final String perm = info.getCommand().permission();
if (perm != null && !sender.hasPermission(perm)) {
throw new IllegalPermissionException("§c你需要有 " + perm + " 的权限才能执行此命令!");
}
return true;
}
/** /**
* 命令别名 * @return 命令别名
*
* @return alias
*/ */
public String getAlias() { public String getAlias() {
return alias; return alias;
} }
/** /**
* 命令参数 * @return 命令参数
*
* @return args
*/ */
public String[] getArgs() { public String[] getArgs() {
return args; return args;
} }
/** /**
* 命令实体 * @return 命令实体
*
* @return command
*/ */
public Command getCommand() { public Command getCommand() {
return command; return command;
} }
/** /**
* 命令发送者 * @return 命令发送者
*
* @return sender
*/ */
public CommandSender getSender() { public CommandSender getSender() {
return sender; return sender;

View File

@ -1,4 +1,4 @@
package pw.yumc.YumCore.commands.api; package pw.yumc.YumCore.commands;
/** /**
* 命令执行类 * 命令执行类

View File

@ -6,9 +6,10 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
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;
@ -23,7 +24,13 @@ import pw.yumc.YumCore.commands.exception.CommandException;
* @author * @author
*/ */
public class CommandInfo { public class CommandInfo {
public static CommandInfo Unknow = new CommandInfo(); public static final CommandInfo Unknow = new CommandInfo();
private static final String onlyPlayer = "§c控制台无法使用此命令(§4请在游戏内执行§c)!";
private static final String onlyConsole = "§c玩家无法使用此命令(§4请使用控制台执行§c)!";
private static final String losePerm = "§c你需要有 %s 的权限才能执行此命令!";
private static final String cmdErr = "§6错误原因: §4命令参数不正确!";
private static final String cmdUse = "§6使用方法: §e/%s %s %s";
private static final String cmdDes = "§6命令描述: §3%s";
private final Object origin; private final Object origin;
private final Method method; private final Method method;
private final String name; private final String name;
@ -55,6 +62,15 @@ public class CommandInfo {
this.sort = 0; this.sort = 0;
} }
/**
* 解析CommandInfo
*
* @param method
* 方法
* @param origin
* 源对象
* @return {@link CommandInfo}
*/
public static CommandInfo parse(final Method method, final Object origin) { public static CommandInfo parse(final Method method, final Object origin) {
final Cmd command = method.getAnnotation(Cmd.class); final Cmd command = method.getAnnotation(Cmd.class);
if (command != null) { if (command != null) {
@ -74,21 +90,28 @@ public class CommandInfo {
return super.equals(obj); return super.equals(obj);
} }
public boolean execute(final CommandSender sender, final Command command, final String label, final String[] args) { /**
* 执行命令
*
* @param sender
* 命令发送者
* @param command
* 主命令
* @param label
* 主命令别名
* @param args
* 子命令参数
* @return
*/
public boolean execute(final CommandArgument cmdArgs) {
if (method == null) { if (method == null) {
return false; return false;
} }
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, args); check(cmdArgs);
final Runnable runnable = new Runnable() { final Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
try {
cmdArgs.check(CommandInfo.this);
} catch (final CommandException e) {
sender.sendMessage(e.getMessage());
return;
}
method.invoke(origin, cmdArgs); method.invoke(origin, cmdArgs);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new CommandException(e); throw new CommandException(e);
@ -103,27 +126,75 @@ public class CommandInfo {
return true; return true;
} }
/**
* @return 命令注解
*/
public Cmd getCommand() { public Cmd getCommand() {
return command; return command;
} }
/**
* @return 帮助注解
*/
public Help getHelp() { public Help getHelp() {
return help; return help;
} }
/**
* @return 命令名称
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* @return 命令排序
*/
public int getSort() { public int getSort() {
return sort; return sort;
} }
/**
* @return 是否为异步命令
*/
public boolean isAsync() { public boolean isAsync() {
return async; return async;
} }
/**
* 验证命令是否匹配
*
* @param cmd
* 需验证命令
* @return 是否匹配
*/
public boolean isValid(final String cmd) { public boolean isValid(final String cmd) {
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd); return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
} }
/**
* 检查命令
*
* @param info
* 命令信息
* @return 是否验证通过
*/
private boolean check(final CommandArgument cmdArgs) {
final CommandSender sender = cmdArgs.getSender();
if (cmdArgs.getArgs().length < command.minimumArguments()) {
Log.toSender(sender, cmdErr);
Log.toSender(sender, String.format(cmdUse, cmdArgs.getAlias(), getName(), help.possibleArguments()));
Log.toSender(sender, String.format(cmdDes, help.description()));
}
if (sender instanceof Player && command.onlyConsoleExecutable()) {
Log.toSender(sender, onlyConsole);
} else if (command.onlyPlayerExecutable()) {
Log.toSender(sender, onlyPlayer);
}
final String perm = command.permission();
if (perm != null && !sender.hasPermission(perm)) {
Log.toSender(sender, String.format(losePerm, perm));
}
return true;
}
} }

View File

@ -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.api.CommandExecutor;
/** /**
* 命令管理类 * 命令管理类
@ -96,19 +95,13 @@ public class CommandManager implements TabExecutor {
help.send(sender, command, label, args); help.send(sender, command, label, args);
return true; return true;
} }
final String[] subargs = moveStrings(args, 1); final CommandArgument cmdArgs = new CommandArgument(sender, command, label, moveStrings(args, 1));
if (!cmdcache.containsKey(label)) { final CommandInfo ci = checkCache(subcmd);
for (final CommandInfo cmdinfo : cmdlist) { try {
if (cmdinfo.isValid(subcmd)) {
cmdcache.put(subcmd, cmdinfo); } catch (final Exception e) {
break;
} }
} return ci.execute(cmdArgs);
if (!cmdcache.containsKey(subcmd)) {
cmdcache.put(subcmd, CommandInfo.Unknow);
}
}
return cmdcache.get(subcmd).execute(sender, command, label, subargs);
} }
@Override @Override
@ -145,6 +138,28 @@ public class CommandManager implements TabExecutor {
help = new CommandHelp(cmdlist); help = new CommandHelp(cmdlist);
} }
/**
* 检查缓存并获得命令
*
* @param subcmd
* 子命令
* @return 命令信息
*/
private CommandInfo checkCache(final String subcmd) {
if (!cmdcache.containsKey(subcmd)) {
for (final CommandInfo cmdinfo : cmdlist) {
if (cmdinfo.isValid(subcmd)) {
cmdcache.put(subcmd, cmdinfo);
break;
}
}
if (!cmdcache.containsKey(subcmd)) {
cmdcache.put(subcmd, CommandInfo.Unknow);
}
}
return cmdcache.get(subcmd);
}
/** /**
* 获取玩家命令补全 * 获取玩家命令补全
* *

View File

@ -1,14 +0,0 @@
package pw.yumc.YumCore.commands.exception;
/**
*
* @since 2016年7月23日 上午11:00:34
* @author
*/
public class IllegalArgumentException extends CommandException {
public IllegalArgumentException(final String string) {
super(string);
}
}

View File

@ -1,12 +0,0 @@
package pw.yumc.YumCore.commands.exception;
/**
*
* @since 2016年7月23日 上午10:50:23
* @author
*/
public class IllegalPermissionException extends CommandException {
public IllegalPermissionException(final String string) {
super(string);
}
}

View File

@ -1,14 +0,0 @@
package pw.yumc.YumCore.commands.exception;
/**
*
* @since 2016年7月23日 上午11:00:34
* @author
*/
public class IllegalSenderException extends CommandException {
public IllegalSenderException(final String string) {
super(string);
}
}