refactor: 调整方法和流程

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
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

@ -1,80 +1,53 @@
package pw.yumc.YumCore.commands; 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 20158228:29:44
/** * @author
* */
* public class CommandArgument {
* @since 20158228:29:44 private final CommandSender sender;
* @author private final Command command;
*/ private final String alias;
public class CommandArgument { private final String[] args;
private final CommandSender sender;
private final Command command; public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) {
private final String alias; this.sender = sender;
private final String[] args; this.command = command;
this.alias = alias;
public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) { this.args = args;
this.sender = sender; }
this.command = command;
this.alias = alias; /**
this.args = args; * @return
} */
public String getAlias() {
public boolean check(final CommandInfo info) { return alias;
if (sender instanceof Player) { }
if (info.getCommand().onlyConsoleExecutable()) {
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!"); /**
} * @return
} else if (info.getCommand().onlyPlayerExecutable()) { */
throw new IllegalSenderException("§c玩家无法使用此命令(§4请使用控制台执行§c)!"); public String[] getArgs() {
} return args;
final String perm = info.getCommand().permission(); }
if (perm != null && !sender.hasPermission(perm)) {
throw new IllegalPermissionException("§c你需要有 " + perm + " 的权限才能执行此命令!"); /**
} * @return
return true; */
} public Command getCommand() {
return command;
/** }
*
* /**
* @return alias * @return
*/ */
public String getAlias() { public CommandSender getSender() {
return alias; return sender;
} }
/** }
*
*
* @return args
*/
public String[] getArgs() {
return args;
}
/**
*
*
* @return command
*/
public Command getCommand() {
return command;
}
/**
*
*
* @return sender
*/
public CommandSender getSender() {
return sender;
}
}

View File

@ -1,11 +1,11 @@
package pw.yumc.YumCore.commands.api; package pw.yumc.YumCore.commands;
/** /**
* *
* *
* @since 2016723 9:55:51 * @since 2016723 9:55:51
* @author * @author
*/ */
public interface CommandExecutor { public interface CommandExecutor {
} }

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;
}
}
if (!cmdcache.containsKey(subcmd)) {
cmdcache.put(subcmd, CommandInfo.Unknow);
}
} }
return cmdcache.get(subcmd).execute(sender, command, label, subargs); return ci.execute(cmdArgs);
} }
@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 2016723 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 2016723 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 2016723 11:00:34
* @author
*/
public class IllegalSenderException extends CommandException {
public IllegalSenderException(final String string) {
super(string);
}
}