mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-21 01:38:51 +00:00
refactor: 调整方法和流程
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
7a60e673cc
commit
8f9597bcce
@ -52,6 +52,12 @@ public class Log {
|
||||
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) {
|
||||
logger.warning(msg);
|
||||
}
|
||||
|
@ -1,80 +1,53 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
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
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandArgument {
|
||||
private final CommandSender sender;
|
||||
private final Command command;
|
||||
private final String alias;
|
||||
private final String[] args;
|
||||
|
||||
public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) {
|
||||
this.sender = sender;
|
||||
this.command = command;
|
||||
this.alias = alias;
|
||||
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 alias
|
||||
*/
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令参数
|
||||
*
|
||||
* @return args
|
||||
*/
|
||||
public String[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令实体
|
||||
*
|
||||
* @return command
|
||||
*/
|
||||
public Command getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令发送者
|
||||
*
|
||||
* @return sender
|
||||
*/
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
}
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/**
|
||||
* 子命令参数类
|
||||
*
|
||||
* @since 2015年8月22日上午8:29:44
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class CommandArgument {
|
||||
private final CommandSender sender;
|
||||
private final Command command;
|
||||
private final String alias;
|
||||
private final String[] args;
|
||||
|
||||
public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) {
|
||||
this.sender = sender;
|
||||
this.command = command;
|
||||
this.alias = alias;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令别名
|
||||
*/
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令参数
|
||||
*/
|
||||
public String[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令实体
|
||||
*/
|
||||
public Command getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令发送者
|
||||
*/
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package pw.yumc.YumCore.commands.api;
|
||||
|
||||
/**
|
||||
* 命令执行类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:55:51
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public interface CommandExecutor {
|
||||
|
||||
}
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
/**
|
||||
* 命令执行类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:55:51
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public interface CommandExecutor {
|
||||
|
||||
}
|
@ -6,9 +6,10 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
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.commands.annotation.Async;
|
||||
import pw.yumc.YumCore.commands.annotation.Cmd;
|
||||
@ -23,7 +24,13 @@ import pw.yumc.YumCore.commands.exception.CommandException;
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
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 Method method;
|
||||
private final String name;
|
||||
@ -55,6 +62,15 @@ public class CommandInfo {
|
||||
this.sort = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析CommandInfo
|
||||
*
|
||||
* @param method
|
||||
* 方法
|
||||
* @param origin
|
||||
* 源对象
|
||||
* @return {@link CommandInfo}
|
||||
*/
|
||||
public static CommandInfo parse(final Method method, final Object origin) {
|
||||
final Cmd command = method.getAnnotation(Cmd.class);
|
||||
if (command != null) {
|
||||
@ -74,21 +90,28 @@ public class CommandInfo {
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, args);
|
||||
check(cmdArgs);
|
||||
final Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
try {
|
||||
cmdArgs.check(CommandInfo.this);
|
||||
} catch (final CommandException e) {
|
||||
sender.sendMessage(e.getMessage());
|
||||
return;
|
||||
}
|
||||
method.invoke(origin, cmdArgs);
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
throw new CommandException(e);
|
||||
@ -103,27 +126,75 @@ public class CommandInfo {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令注解
|
||||
*/
|
||||
public Cmd getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 帮助注解
|
||||
*/
|
||||
public Help getHelp() {
|
||||
return help;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令名称
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 命令排序
|
||||
*/
|
||||
public int getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否为异步命令
|
||||
*/
|
||||
public boolean isAsync() {
|
||||
return async;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证命令是否匹配
|
||||
*
|
||||
* @param cmd
|
||||
* 需验证命令
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public boolean isValid(final String 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;
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ 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.api.CommandExecutor;
|
||||
|
||||
/**
|
||||
* 命令管理类
|
||||
@ -96,19 +95,13 @@ public class CommandManager implements TabExecutor {
|
||||
help.send(sender, command, label, args);
|
||||
return true;
|
||||
}
|
||||
final String[] subargs = moveStrings(args, 1);
|
||||
if (!cmdcache.containsKey(label)) {
|
||||
for (final CommandInfo cmdinfo : cmdlist) {
|
||||
if (cmdinfo.isValid(subcmd)) {
|
||||
cmdcache.put(subcmd, cmdinfo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!cmdcache.containsKey(subcmd)) {
|
||||
cmdcache.put(subcmd, CommandInfo.Unknow);
|
||||
}
|
||||
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, moveStrings(args, 1));
|
||||
final CommandInfo ci = checkCache(subcmd);
|
||||
try {
|
||||
|
||||
} catch (final Exception e) {
|
||||
}
|
||||
return cmdcache.get(subcmd).execute(sender, command, label, subargs);
|
||||
return ci.execute(cmdArgs);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -145,6 +138,28 @@ public class CommandManager implements TabExecutor {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家命令补全
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user