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-10-09 20:36:10 +08:00
parent ea1ddd6385
commit 6ac2b8cf78
7 changed files with 36 additions and 21 deletions

View File

@ -14,6 +14,8 @@ import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.info.CommandInfo;
import pw.yumc.YumCore.commands.interfaces.CommandHelpParse;
/** /**
* 命令帮助生成类 * 命令帮助生成类

View File

@ -28,6 +28,10 @@ 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.info.CommandInfo;
import pw.yumc.YumCore.commands.info.CommandTabInfo;
import pw.yumc.YumCore.commands.interfaces.CommandExecutor;
import pw.yumc.YumCore.commands.interfaces.CommandHelpParse;
/** /**
* 命令管理类 * 命令管理类
@ -57,7 +61,7 @@ public class CommandManager implements TabExecutor {
knownCommandsField.setAccessible(true); knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap); knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
PluginCommandConstructor = PluginCommand.class.getConstructor(String.class, Plugin.class); PluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
} catch (NoSuchMethodException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { } catch (NoSuchMethodException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
Log.d("初始化命令管理器失败!"); Log.d("初始化命令管理器失败!");
Log.debug(e); Log.debug(e);
@ -78,7 +82,7 @@ public class CommandManager implements TabExecutor {
/** /**
* Tab列表 * Tab列表
*/ */
private final Set<TabInfo> tabs = new HashSet<>(); private final Set<CommandTabInfo> tabs = new HashSet<>();
/** /**
* 命令缓存列表 * 命令缓存列表
*/ */
@ -156,7 +160,7 @@ public class CommandManager implements TabExecutor {
if (args.length == 1) { if (args.length == 1) {
StringUtil.copyPartialMatches(args[0], cmdNameCache, completions); StringUtil.copyPartialMatches(args[0], cmdNameCache, completions);
} else if (args.length >= 2) { } else if (args.length >= 2) {
for (final TabInfo tab : tabs) { for (final CommandTabInfo tab : tabs) {
StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions); StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions);
} }
StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions); StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions);
@ -283,13 +287,14 @@ public class CommandManager implements TabExecutor {
final CommandInfo ci = CommandInfo.parse(method, clazz); final CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) { if (ci != null) {
final Class<?>[] params = method.getParameterTypes(); final Class<?>[] params = method.getParameterTypes();
if (params.length > 0 && params[0].equals(CommandArgument.class)) { Log.d("命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params));
if (params.length > 0 && params[0].isAssignableFrom(CommandSender.class)) {
if (method.getReturnType() == boolean.class) { if (method.getReturnType() == boolean.class) {
defCmd = ci; defCmd = ci;
return true; } else {
cmds.add(ci);
cmdCache.put(ci.getName(), ci);
} }
cmds.add(ci);
cmdCache.put(ci.getName(), ci);
return true; return true;
} }
Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName())); Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName()));
@ -307,7 +312,7 @@ public class CommandManager implements TabExecutor {
* @return 是否成功 * @return 是否成功
*/ */
private boolean registerTab(final Method method, final CommandExecutor clazz) { private boolean registerTab(final Method method, final CommandExecutor clazz) {
final TabInfo ti = TabInfo.parse(method, clazz); final CommandTabInfo ti = CommandTabInfo.parse(method, clazz);
if (ti != null) { if (ti != null) {
if (method.getReturnType().equals(List.class)) { if (method.getReturnType().equals(List.class)) {
tabs.add(ti); tabs.add(ti);

View File

@ -2,11 +2,13 @@ package pw.yumc.YumCore.commands;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.CommandException;
@ -19,7 +21,7 @@ import pw.yumc.YumCore.commands.exception.CommandParseException;
* @since 2016年10月5日 下午4:02:04 * @since 2016年10月5日 下午4:02:04
*/ */
public class CommandParse { public class CommandParse {
private static Map<Class, Parse> allparses; private static Map<Class, Parse> allparses = new HashMap<>();
static { static {
new IntegerParse(); new IntegerParse();
new LongParse(); new LongParse();
@ -33,6 +35,9 @@ public class CommandParse {
public CommandParse(final Class[] classes, final Annotation[][] annons) { public CommandParse(final Class[] classes, final Annotation[][] annons) {
for (final Class classe : classes) { for (final Class classe : classes) {
final Class clazz = classe; final Class clazz = classe;
if (clazz.isAssignableFrom(CommandSender.class)) {
continue;
}
if (!allparses.containsKey(clazz)) { if (!allparses.containsKey(clazz)) {
throw new CommandParseException(String.format("无法解析的参数类型 %s !", clazz.getName())); throw new CommandParseException(String.format("无法解析的参数类型 %s !", clazz.getName()));
} }

View File

@ -1,4 +1,4 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands.info;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -11,6 +11,8 @@ import org.bukkit.entity.Player;
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.CommandParse;
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;
import pw.yumc.YumCore.commands.annotation.Cmd.Executor; import pw.yumc.YumCore.commands.annotation.Cmd.Executor;
@ -121,7 +123,7 @@ public class CommandInfo {
@Override @Override
public void run() { public void run() {
try { try {
method.invoke(origin, cmdArgs.getSender(), parse.parse(cmdArgs)); method.invoke(origin, parse.parse(cmdArgs));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new CommandException(e); throw new CommandException(e);
} }

View File

@ -1,4 +1,4 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands.info;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -8,6 +8,7 @@ import java.util.List;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.annotation.Tab; import pw.yumc.YumCore.commands.annotation.Tab;
import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.CommandException;
@ -17,11 +18,11 @@ import pw.yumc.YumCore.commands.exception.CommandException;
* @since 2016年7月23日 上午9:56:42 * @since 2016年7月23日 上午9:56:42
* @author * @author
*/ */
public class TabInfo { public class CommandTabInfo {
private final Object origin; private final Object origin;
private final Method method; private final Method method;
public TabInfo(final Method method, final Object origin) { public CommandTabInfo(final Method method, final Object origin) {
this.method = method; this.method = method;
this.origin = origin; this.origin = origin;
} }
@ -33,12 +34,12 @@ public class TabInfo {
* 方法 * 方法
* @param origin * @param origin
* 对象 * 对象
* @return {@link TabInfo} * @return {@link CommandTabInfo}
*/ */
public static TabInfo parse(final Method method, final Object origin) { public static CommandTabInfo parse(final Method method, final Object origin) {
final Tab tab = method.getAnnotation(Tab.class); final Tab tab = method.getAnnotation(Tab.class);
if (tab != null) { if (tab != null) {
return new TabInfo(method, origin); return new CommandTabInfo(method, origin);
} }
return null; return null;
} }
@ -48,8 +49,8 @@ public class TabInfo {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj instanceof TabInfo) { if (obj instanceof CommandTabInfo) {
return method.equals(((TabInfo) obj).getMethod()); return method.equals(((CommandTabInfo) obj).getMethod());
} }
return super.equals(obj); return super.equals(obj);
} }

View File

@ -1,4 +1,4 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands.interfaces;
/** /**
* 命令执行类 * 命令执行类

View File

@ -1,4 +1,4 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands.interfaces;
/** /**
* 命令解析接口 * 命令解析接口