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);
}
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);
}

View File

@ -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 20158228: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 20158228: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;
}
}

View File

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

View File

@ -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;
}
}

View File

@ -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);
}
/**
*
*

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);
}
}