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

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