parent
417a189f9b
commit
aa324f54db
|
@ -1,6 +1,5 @@
|
||||||
package pw.yumc.YumCore.commands;
|
package pw.yumc.YumCore.commands;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -79,7 +78,7 @@ public class CommandHelp {
|
||||||
}
|
}
|
||||||
final String helpkey = label + page;
|
final String helpkey = label + page;
|
||||||
if (!cacheHelp.containsKey(helpkey)) {
|
if (!cacheHelp.containsKey(helpkey)) {
|
||||||
final List<String> helpList = new ArrayList<>();
|
final List<String> helpList = new LinkedList<>();
|
||||||
if (page > this.HELPPAGECOUNT || page < 1) {
|
if (page > this.HELPPAGECOUNT || page < 1) {
|
||||||
// 帮助页面不存在
|
// 帮助页面不存在
|
||||||
helpList.add(String.format(commandNotFound, HELPPAGECOUNT));
|
helpList.add(String.format(commandNotFound, HELPPAGECOUNT));
|
||||||
|
@ -115,7 +114,13 @@ public class CommandHelp {
|
||||||
static class CommandComparator implements Comparator<CommandInfo> {
|
static class CommandComparator implements Comparator<CommandInfo> {
|
||||||
@Override
|
@Override
|
||||||
public int compare(final CommandInfo o1, final CommandInfo o2) {
|
public int compare(final CommandInfo o1, final CommandInfo o2) {
|
||||||
return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort()));
|
if (o1.getSort() > o2.getSort()) {
|
||||||
|
return 1;
|
||||||
|
} else if (o1.getSort() == o2.getSort()) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pw.yumc.YumCore.commands;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -37,20 +38,23 @@ public class CommandManager implements TabExecutor {
|
||||||
/**
|
/**
|
||||||
* 命令列表
|
* 命令列表
|
||||||
*/
|
*/
|
||||||
Set<CommandInfo> cmdlist = new HashSet<>();
|
Set<CommandInfo> cmds = new HashSet<>();
|
||||||
/**
|
/**
|
||||||
* Tab列表
|
* Tab列表
|
||||||
*/
|
*/
|
||||||
Set<TabInfo> tablist = new HashSet<>();
|
Set<TabInfo> tabs = new HashSet<>();
|
||||||
/**
|
/**
|
||||||
* 命令缓存列表
|
* 命令缓存列表
|
||||||
*/
|
*/
|
||||||
Map<String, CommandInfo> cmdcache = new HashMap<>();
|
Map<String, CommandInfo> cmdCache = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* 命令名称缓存
|
||||||
|
*/
|
||||||
|
List<String> cmdNameCache = new ArrayList<>();
|
||||||
/**
|
/**
|
||||||
* 命令帮助
|
* 命令帮助
|
||||||
*/
|
*/
|
||||||
CommandHelp help = new CommandHelp(cmdlist);
|
CommandHelp help;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插件命令
|
* 插件命令
|
||||||
*/
|
*/
|
||||||
|
@ -97,10 +101,6 @@ public class CommandManager implements TabExecutor {
|
||||||
}
|
}
|
||||||
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, moveStrings(args, 1));
|
final CommandArgument cmdArgs = new CommandArgument(sender, command, label, moveStrings(args, 1));
|
||||||
final CommandInfo ci = checkCache(subcmd);
|
final CommandInfo ci = checkCache(subcmd);
|
||||||
try {
|
|
||||||
|
|
||||||
} catch (final Exception e) {
|
|
||||||
}
|
|
||||||
return ci.execute(cmdArgs);
|
return ci.execute(cmdArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,10 +109,9 @@ public class CommandManager implements TabExecutor {
|
||||||
final List<String> completions = new ArrayList<>();
|
final List<String> completions = new ArrayList<>();
|
||||||
final String token = args[args.length - 1];
|
final String token = args[args.length - 1];
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
final Set<String> commands = this.cmdcache.keySet();
|
StringUtil.copyPartialMatches(args[0], cmdNameCache, completions);
|
||||||
StringUtil.copyPartialMatches(args[0], commands, completions);
|
|
||||||
} else if (args.length >= 2) {
|
} else if (args.length >= 2) {
|
||||||
for (final TabInfo tab : tablist) {
|
for (final TabInfo 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);
|
||||||
|
@ -135,7 +134,20 @@ public class CommandManager implements TabExecutor {
|
||||||
}
|
}
|
||||||
registerTab(method, clazz);
|
registerTab(method, clazz);
|
||||||
}
|
}
|
||||||
help = new CommandHelp(cmdlist);
|
help = new CommandHelp(cmds);
|
||||||
|
buildCmdNameCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建命令列表缓存
|
||||||
|
*/
|
||||||
|
private void buildCmdNameCache() {
|
||||||
|
cmdNameCache.clear();
|
||||||
|
for (final CommandInfo cmd : cmds) {
|
||||||
|
cmdNameCache.add(cmd.getName());
|
||||||
|
cmdNameCache.addAll(Arrays.asList(cmd.getCommand().aliases()));
|
||||||
|
}
|
||||||
|
cmdNameCache.add("help");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,18 +158,18 @@ public class CommandManager implements TabExecutor {
|
||||||
* @return 命令信息
|
* @return 命令信息
|
||||||
*/
|
*/
|
||||||
private CommandInfo checkCache(final String subcmd) {
|
private CommandInfo checkCache(final String subcmd) {
|
||||||
if (!cmdcache.containsKey(subcmd)) {
|
if (!cmdCache.containsKey(subcmd)) {
|
||||||
for (final CommandInfo cmdinfo : cmdlist) {
|
for (final CommandInfo cmdinfo : cmds) {
|
||||||
if (cmdinfo.isValid(subcmd)) {
|
if (cmdinfo.isValid(subcmd)) {
|
||||||
cmdcache.put(subcmd, cmdinfo);
|
cmdCache.put(subcmd, cmdinfo);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!cmdcache.containsKey(subcmd)) {
|
if (!cmdCache.containsKey(subcmd)) {
|
||||||
cmdcache.put(subcmd, CommandInfo.Unknow);
|
cmdCache.put(subcmd, CommandInfo.Unknow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cmdcache.get(subcmd);
|
return cmdCache.get(subcmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -215,8 +227,8 @@ public class CommandManager implements TabExecutor {
|
||||||
if (ci != null) {
|
if (ci != null) {
|
||||||
final Class<?>[] params = method.getParameterTypes();
|
final Class<?>[] params = method.getParameterTypes();
|
||||||
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
|
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
|
||||||
cmdlist.add(ci);
|
cmds.add(ci);
|
||||||
cmdcache.put(ci.getName(), 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()));
|
||||||
|
@ -237,7 +249,7 @@ public class CommandManager implements TabExecutor {
|
||||||
final TabInfo ti = TabInfo.parse(method, clazz);
|
final TabInfo ti = TabInfo.parse(method, clazz);
|
||||||
if (ti != null) {
|
if (ti != null) {
|
||||||
if (method.getReturnType().equals(List.class)) {
|
if (method.getReturnType().equals(List.class)) {
|
||||||
tablist.add(ti);
|
tabs.add(ti);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName()));
|
Log.warning(String.format(returnTypeError, method.getName(), clazz.getClass().getName()));
|
||||||
|
|
|
@ -14,5 +14,4 @@ import java.lang.annotation.Target;
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Async {
|
public @interface Async {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
package pw.yumc.YumCore.commands.annotation;
|
package pw.yumc.YumCore.commands.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 命令排序注解
|
* 命令排序注解
|
||||||
*
|
*
|
||||||
* @since 2016年7月23日 上午9:04:56
|
* @since 2016年7月23日 上午9:04:56
|
||||||
* @author 喵♂呜
|
* @author 喵♂呜
|
||||||
*/
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Sort {
|
public @interface Sort {
|
||||||
/**
|
/**
|
||||||
* @return 命令排序
|
* @return 命令排序
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
package pw.yumc.YumCore.commands.annotation;
|
package pw.yumc.YumCore.commands.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动补全注解
|
* 自动补全注解
|
||||||
*
|
*
|
||||||
* @since 2016年7月23日 上午9:43:40
|
* @since 2016年7月23日 上午9:43:40
|
||||||
* @author 喵♂呜
|
* @author 喵♂呜
|
||||||
*/
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Tab {
|
public @interface Tab {
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue