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

feat: 添加默认命令处理

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-08-09 17:33:21 +08:00
parent 91e0cc4b0e
commit a567e2eb03
2 changed files with 55 additions and 6 deletions

View File

@ -36,6 +36,10 @@ public class CommandHelp {
String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix);
String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s";
String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s";
/**
* 默认命令
*/
CommandInfo defCmd;
/**
* 已排序的命令列表
*/
@ -53,10 +57,45 @@ public class CommandHelp {
*/
private final Map<String, String[]> cacheHelp = new HashMap<>();
/**
* 命令帮助
*
* @param list
* 子命令列表
*/
public CommandHelp(final Collection<? extends CommandInfo> list) {
this(null, list);
}
/**
* 命令帮助
*
* @param defCmd
* 默认命令
* @param list
* 子命令列表
*/
public CommandHelp(final CommandInfo defCmd, final Collection<? extends CommandInfo> list) {
this.defCmd = defCmd;
cmdlist = new LinkedList<>(list);
Collections.sort(cmdlist, new CommandComparator());
this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE);
HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE);
}
/**
* 格式化命令信息
*
* @param ci
* 命令信息
* @param label
* 命令
* @return 格式化后的字串
*/
public String formatCommand(final CommandInfo ci, final String label) {
final String aliases = Arrays.toString(ci.getCommand().aliases());
final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
final Help help = ci.getHelp();
return String.format(helpBody, label, cmd, help.possibleArguments(), help.value());
}
/**
@ -85,16 +124,15 @@ public class CommandHelp {
} else {
// 帮助标题
helpList.add(helpTitle);
if (page == 1 && defCmd != null) {
helpList.add(formatCommand(defCmd, label));
}
final int start = this.LINES_PER_PAGE * (page - 1);
final int end = start + this.LINES_PER_PAGE;
for (int i = start; i < end; i++) {
if (this.cmdlist.size() > i) {
final CommandInfo ci = cmdlist.get(i);
final String aliases = Arrays.toString(ci.getCommand().aliases());
final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
final Help help = ci.getHelp();
// 帮助列表
helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.value()));
helpList.add(formatCommand(cmdlist.get(i), label));
}
}
}

View File

@ -35,6 +35,10 @@ public class CommandManager implements TabExecutor {
* 插件实例类
*/
JavaPlugin plugin = P.instance;
/**
* 默认命令
*/
CommandInfo defCmd = null;
/**
* 命令列表
*/
@ -91,6 +95,9 @@ public class CommandManager implements TabExecutor {
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
if (args.length == 0) {
if (defCmd != null) {
return defCmd.execute(new CommandArgument(sender, command, label, args));
}
help.send(sender, command, label, args);
return true;
}
@ -229,6 +236,10 @@ public class CommandManager implements TabExecutor {
if (ci != null) {
final Class<?>[] params = method.getParameterTypes();
if (params.length == 1 && params[0].equals(CommandArgument.class)) {
if (method.getReturnType() == boolean.class) {
defCmd = ci;
return true;
}
cmds.add(ci);
cmdCache.put(ci.getName(), ci);
return true;