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

fix: 修复默认命令调用逻辑错误

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-11-10 16:34:00 +08:00
parent 9d45715190
commit 560724dd52
3 changed files with 50 additions and 64 deletions

View File

@ -150,9 +150,6 @@ public class CommandManager implements TabExecutor {
break; break;
} }
} }
if (defCmd != null) {
cmdCache.put(subcmd, defCmd);
}
if (!cmdCache.containsKey(subcmd)) { if (!cmdCache.containsKey(subcmd)) {
cmdCache.put(subcmd, CommandInfo.Unknow); cmdCache.put(subcmd, CommandInfo.Unknow);
} }
@ -209,7 +206,12 @@ public class CommandManager implements TabExecutor {
} }
String subcmd = args[0].toLowerCase(); String subcmd = args[0].toLowerCase();
if (subcmd.equalsIgnoreCase("help")) { return help.send(sender, command, label, args); } if (subcmd.equalsIgnoreCase("help")) { return help.send(sender, command, label, args); }
return getByCache(subcmd).execute(new CommandArgument(sender, command, label, moveStrings(args, 1))); CommandInfo cmd = getByCache(subcmd);
if (cmd.equals(CommandInfo.Unknow) && defCmd != null) {
return defCmd.execute(new CommandArgument(sender, command, label, args));
} else {
return cmd.execute(new CommandArgument(sender, command, label, moveStrings(args, 1)));
}
} }
@Override @Override

View File

@ -1,14 +1,7 @@
package pw.yumc.YumCore.commands.info; package pw.yumc.YumCore.commands.info;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
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.commands.CommandArgument; import pw.yumc.YumCore.commands.CommandArgument;
@ -22,6 +15,13 @@ import pw.yumc.YumCore.commands.exception.CommandArgumentException;
import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.CommandException;
import pw.yumc.YumCore.commands.exception.CommandParseException; import pw.yumc.YumCore.commands.exception.CommandParseException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/** /**
* 命令信息存储类 * 命令信息存储类
* *
@ -95,8 +95,10 @@ public class CommandInfo {
/** /**
* 解析CommandInfo * 解析CommandInfo
* *
* @param method 方法 * @param method
* @param origin 源对象 * 方法
* @param origin
* 源对象
* @return {@link CommandInfo} * @return {@link CommandInfo}
*/ */
public static CommandInfo parse(Method method, Object origin) { public static CommandInfo parse(Method method, Object origin) {
@ -111,27 +113,15 @@ public class CommandInfo {
return null; return null;
} }
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof CommandInfo) {
return name.equalsIgnoreCase(((CommandInfo) obj).getName());
}
return super.equals(obj);
}
/** /**
* 执行命令 * 执行命令
* *
* @param cmdArgs 命令参数 * @param cmdArgs
* 命令参数
* @return 是否执行成功 * @return 是否执行成功
*/ */
public boolean execute(final CommandArgument cmdArgs) { public boolean execute(final CommandArgument cmdArgs) {
if (method == null) { if (method == null) { return false; }
return false;
}
if (check(cmdArgs)) { if (check(cmdArgs)) {
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
@ -182,11 +172,6 @@ public class CommandInfo {
return sort; return sort;
} }
@Override
public int hashCode() {
return name.toLowerCase().hashCode() + method.hashCode() + origin.hashCode();
}
/** /**
* @return 是否为异步命令 * @return 是否为异步命令
*/ */
@ -197,19 +182,27 @@ public class CommandInfo {
/** /**
* 验证命令是否匹配 * 验证命令是否匹配
* *
* @param cmd 需验证命令 * @param cmd
* 需验证命令
* @return 是否匹配 * @return 是否匹配
*/ */
public boolean isValid(String cmd) { public boolean isValid(String cmd) {
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd); return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
} }
/** @Override
* 检查命令 public boolean equals(Object o) {
* if (this == o) return true;
* @param cmdArgs 命令信息 if (o == null || getClass() != o.getClass()) return false;
* @return 是否验证通过 CommandInfo that = (CommandInfo) o;
*/ return Objects.equals(origin, that.origin) && Objects.equals(method, that.method) && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(origin, method, name);
}
private boolean check(CommandArgument cmdArgs) { private boolean check(CommandArgument cmdArgs) {
CommandSender sender = cmdArgs.getSender(); CommandSender sender = cmdArgs.getSender();
return checkSender(sender) && checkArgs(sender, cmdArgs) && checkPerm(sender); return checkSender(sender) && checkArgs(sender, cmdArgs) && checkPerm(sender);

View File

@ -1,16 +1,16 @@
package pw.yumc.YumCore.commands.info; package pw.yumc.YumCore.commands.info;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.annotation.Tab;
import pw.yumc.YumCore.commands.exception.CommandException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.annotation.Tab;
import pw.yumc.YumCore.commands.exception.CommandException;
/** /**
* Tab补全 * Tab补全
@ -38,23 +38,10 @@ public class CommandTabInfo {
*/ */
public static CommandTabInfo parse(Method method, Object origin) { public static CommandTabInfo parse(Method method, Object origin) {
Tab tab = method.getAnnotation(Tab.class); Tab tab = method.getAnnotation(Tab.class);
if (tab != null) { if (tab != null) { return new CommandTabInfo(method, origin); }
return new CommandTabInfo(method, origin);
}
return null; return null;
} }
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof CommandTabInfo) {
return method.equals(((CommandTabInfo) obj).getMethod());
}
return super.equals(obj);
}
/** /**
* 获得补全List * 获得补全List
* *
@ -78,12 +65,16 @@ public class CommandTabInfo {
} }
} }
public Method getMethod() { @Override
return method; public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandTabInfo that = (CommandTabInfo) o;
return Objects.equals(origin, that.origin) && Objects.equals(method, that.method);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return method.hashCode() + origin.hashCode(); return Objects.hash(origin, method);
} }
} }