1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-27 07:28:52 +00:00

feat: 主命令帮助页面生成调整

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-11-25 20:11:13 +08:00
parent 022d4ed866
commit f6b0142238
2 changed files with 23 additions and 4 deletions

View File

@ -87,7 +87,7 @@ public class CommandHelp {
int page = 1; int page = 1;
try { try {
page = Integer.parseInt(args[1]); page = Integer.parseInt(args[1]);
page = page == 0 ? 1 : page; page = page < 1 ? 1 : page;
} catch (Exception ignored) { } catch (Exception ignored) {
} }
String helpkey = label + page; String helpkey = label + page;

View File

@ -6,8 +6,10 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
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.annotation.Help;
import pw.yumc.YumCore.commands.info.CommandInfo; import pw.yumc.YumCore.commands.info.CommandInfo;
import pw.yumc.YumCore.commands.interfaces.Executor; import pw.yumc.YumCore.commands.interfaces.Executor;
import pw.yumc.YumCore.commands.interfaces.HelpGenerator;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
@ -32,6 +34,10 @@ public class CommandMain implements CommandExecutor {
* 命令帮助处理 * 命令帮助处理
*/ */
private CommandHelp help; private CommandHelp help;
/**
* 帮助页生成
*/
private HelpGenerator helpGenerator = new MainHelpGenerator();
/** /**
* 主类命令管理类 * 主类命令管理类
@ -58,6 +64,7 @@ public class CommandMain implements CommandExecutor {
} }
} }
help = new CommandHelp(cmds); help = new CommandHelp(cmds);
help.setHelpGenerator(helpGenerator);
return this; return this;
} }
@ -70,7 +77,6 @@ public class CommandMain implements CommandExecutor {
try { try {
Class<? extends CommandSender> sender = params[0]; Class<? extends CommandSender> sender = params[0];
cmds.add(ci); cmds.add(ci);
cmdCache.put(ci.getName(), ci);
return true; return true;
} catch (ArrayIndexOutOfBoundsException | ClassCastException ignored) { } catch (ArrayIndexOutOfBoundsException | ClassCastException ignored) {
} }
@ -84,7 +90,6 @@ public class CommandMain implements CommandExecutor {
if (cmd == null) { if (cmd == null) {
if ((cmd = CommandKit.create(ci.getName(), ci.getAliases().toArray(new String[] {}))) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!"); } if ((cmd = CommandKit.create(ci.getName(), ci.getAliases().toArray(new String[] {}))) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!"); }
} }
cmd.setAliases(ci.getAliases());
cmd.setExecutor(this); cmd.setExecutor(this);
} }
@ -99,6 +104,7 @@ public class CommandMain implements CommandExecutor {
if (!cmdCache.containsKey(cmd)) { if (!cmdCache.containsKey(cmd)) {
for (CommandInfo cmdinfo : cmds) { for (CommandInfo cmdinfo : cmds) {
if (cmdinfo.isValid(cmd)) { if (cmdinfo.isValid(cmd)) {
Log.d("匹配命令: %s => %s 已缓存...", cmd, cmdinfo);
cmdCache.put(cmd, cmdinfo); cmdCache.put(cmd, cmdinfo);
break; break;
} }
@ -110,11 +116,24 @@ public class CommandMain implements CommandExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 1 && args[0].equalsIgnoreCase("help")) { Log.d("执行者: %s 触发主命令: %s 参数: %s", sender.getName(), label, Arrays.toString(args));
if (args.length > 0 && args[0].equalsIgnoreCase("?")) {
help.send(sender, label, args); help.send(sender, label, args);
return true; return true;
} }
CommandInfo manager = getByCache(label); CommandInfo manager = getByCache(label);
return manager != null && manager.execute(sender, label, args); return manager != null && manager.execute(sender, label, args);
} }
private class MainHelpGenerator extends CommandHelp.DefaultHelpGenerator {
private String helpBody = "§6/%1$s §e%2$s §6- §b%3$s";
@Override
public String body(String label, CommandInfo ci) {
String aliases = Arrays.toString(ci.getCommand().aliases());
String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
Help help = ci.getHelp();
return String.format(helpBody, cmd, help.possibleArguments(), parse(help.value()));
}
}
} }