1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-24 02:08:48 +00:00

feat: 修复命令注册错误

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-25 13:54:13 +08:00
parent 1a7e967826
commit ad4126a810
4 changed files with 484 additions and 452 deletions

View File

@ -56,12 +56,11 @@ public class CommandInfo {
}
public static CommandInfo parse(final Method method, final Object origin) {
final Class<?> clazz = method.getClass();
final Cmd command = clazz.getAnnotation(Cmd.class);
final Cmd command = method.getAnnotation(Cmd.class);
if (command != null) {
final Help help = clazz.getAnnotation(Help.class);
final Async async = clazz.getAnnotation(Async.class);
final Sort sort = clazz.getAnnotation(Sort.class);
final Help help = method.getAnnotation(Help.class);
final Async async = method.getAnnotation(Async.class);
final Sort sort = method.getAnnotation(Sort.class);
return new CommandInfo(method, origin, command, help != null ? help : Help.DEFAULT, async != null, sort != null ? sort.sort() : 50);
}
return null;

View File

@ -52,6 +52,11 @@ public class CommandManager implements TabExecutor {
*/
CommandHelp help = new CommandHelp(cmdlist);
/**
* 插件命令
*/
PluginCommand cmd;
/**
* 命令管理器
*
@ -59,7 +64,7 @@ public class CommandManager implements TabExecutor {
* 注册的命令
*/
public CommandManager(final String name) {
final PluginCommand cmd = plugin.getCommand(name);
cmd = plugin.getCommand(name);
if (cmd == null) {
throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + name + " 命令!");
}
@ -77,7 +82,7 @@ public class CommandManager implements TabExecutor {
*/
public CommandManager(final String name, final CommandExecutor executor) {
this(name);
registerCommands(executor);
register(executor);
}
@Override
@ -94,14 +99,16 @@ public class CommandManager implements TabExecutor {
final String[] subargs = moveStrings(args, 1);
if (!cmdcache.containsKey(label)) {
for (final CommandInfo cmdinfo : cmdlist) {
if (cmdinfo.isValid(label)) {
cmdcache.put(label, cmdinfo);
if (cmdinfo.isValid(subcmd)) {
cmdcache.put(subcmd, cmdinfo);
break;
}
}
cmdcache.put(label, CommandInfo.Unknow);
if (!cmdcache.containsKey(subcmd)) {
cmdcache.put(subcmd, CommandInfo.Unknow);
}
return cmdcache.get(label).execute(sender, command, label, subargs);
}
return cmdcache.get(subcmd).execute(sender, command, label, subargs);
}
@Override
@ -127,29 +134,13 @@ public class CommandManager implements TabExecutor {
* @param clazz
* 子命令处理类
*/
public void registerCommands(final CommandExecutor clazz) {
public void register(final CommandExecutor clazz) {
final Method[] methods = clazz.getClass().getDeclaredMethods();
for (final Method method : methods) {
final CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) {
final Class<?>[] params = method.getParameterTypes();
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
if (cmdlist.add(ci)) {
cmdcache.put(ci.getName(), ci);
if (registerCommand(method, clazz)) {
continue;
}
} else {
Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName()));
}
}
final TabInfo ti = TabInfo.parse(method, clazz);
if (ti != null) {
if (!method.getReturnType().equals(List.class)) {
Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName()));
continue;
}
tablist.add(ti);
}
registerTab(method, clazz);
}
help = new CommandHelp(cmdlist);
}
@ -195,4 +186,47 @@ public class CommandManager implements TabExecutor {
return ret;
}
/**
* 注册命令
*
* @param method
* 方法
* @param clazz
* 调用对象
* @return 是否成功
*/
private boolean registerCommand(final Method method, final CommandExecutor clazz) {
final CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) {
final Class<?>[] params = method.getParameterTypes();
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
cmdlist.add(ci);
cmdcache.put(ci.getName(), ci);
return true;
}
Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName()));
}
return false;
}
/**
* 注册Tab补全
*
* @param method
* 方法
* @param clazz
* 调用对象
* @return 是否成功
*/
private boolean registerTab(final Method method, final CommandExecutor clazz) {
final TabInfo ti = TabInfo.parse(method, clazz);
if (ti != null) {
if (method.getReturnType().equals(List.class)) {
tablist.add(ti);
return true;
}
Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName()));
}
return false;
}
}

View File

@ -35,8 +35,7 @@ public class TabInfo {
* @return {@link TabInfo}
*/
public static TabInfo parse(final Method method, final Object origin) {
final Class<?> clazz = method.getClass();
final Tab tab = clazz.getAnnotation(Tab.class);
final Tab tab = method.getAnnotation(Tab.class);
if (tab != null) {
return new TabInfo(method, origin);
}

View File

@ -23,12 +23,12 @@ public @interface Help {
@Override
public String description() {
return "这家伙很懒";
return "没写帮助信息";
}
@Override
public String possibleArguments() {
return "没有帮助信息";
return "这家伙很懒";
}
};