feat: 添加默认命令处理

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