diff --git a/build.gradle b/build.gradle index 95ecb71..23036cf 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '4.77' +version = '4.78' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 9b5fc14..0be6abc 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -173,6 +173,7 @@ COMMANDS: # COMMAND-CREATE: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册到服务器' # COMMAND-REGISTER: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册 &f{2} &7条子命令' COMMAND-HELP: ' §f/{0} {1} {2}§6- §e{3}' + COMMAND-HELP-EMPTY: ' §f/{0} {1} {2}' COMMAND-ARGUMENT: '§7<§8{0}§7>' COMMAND-ARGUMENT-REQUIRE: '§7[§8{0}§7]' PARAMETER: diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java index 32cd2d8..6210fa7 100644 --- a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java @@ -31,13 +31,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { private List> linkClasses = new CopyOnWriteArrayList<>(); private List subCommands = new CopyOnWriteArrayList<>(); - /** - * 指令标题 - * - * @return 文本 - */ - abstract public String getCommandTitle(); - public static BaseMainCommand createCommandExecutor(String command, BaseMainCommand baseMainCommand) { Preconditions.checkArgument(Bukkit.getPluginCommand(command) != null, "PluginCommand \"" + command + "\" not found"); Preconditions.checkArgument(baseMainCommand != null, "Executor cannot be null"); @@ -71,14 +64,16 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { fields.forEach(commandField -> { try { commandField.getField().setAccessible(true); - baseMainCommand.registerSubCommand((BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance())); + BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance()); + subCommand.setLabel(commandField.getField().getName()); + baseMainCommand.registerSubCommand(subCommand); } catch (Exception ignored) { } }); } if (methods.size() + fields.size() > 0) { TabooLib.debug("Registered " + (methods.size() + fields.size()) + " sub-command with " + baseMainCommand.getRegisterCommand().getName() + " (" + baseMainCommand.getRegisterCommand().getPlugin().getName() + ")"); -// TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-REGISTER", baseMainCommand.getRegisterCommand().getPlugin().getName(), baseMainCommand.getRegisterCommand().getName(), String.valueOf(methods.size() + fields.size())); +// TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-REGISTER", baseMainCommand.getRegisterCommand().getPlugin().getName(), baseMainCommand.getRegisterCommand().getName(), String.valueOf(methods.size() + fields.size())); } } @@ -107,6 +102,16 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { subCommands.add(subCommand); } + public void onCommandHelp(CommandSender sender, Command command, String label, String[] args) { + sender.sendMessage(getEmptyLine()); + sender.sendMessage(getCommandTitle()); + sender.sendMessage(getEmptyLine()); + subCommands.stream().filter(subCommands -> !hideInHelp(subCommands) && hasPermission(sender, subCommands)).map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage); + sender.sendMessage(getEmptyLine()); + } + + abstract public String getCommandTitle(); + @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { return args.length == 1 ? subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand) && (args[0].isEmpty() || subCommand.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()) : null; @@ -115,10 +120,10 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (args.length == 0) { - helpCommand(sender, label); + onCommandHelp(sender, command, label, args); } else { for (BaseSubCommand subCommand : subCommands) { - if (subCommand == null || !args[0].equalsIgnoreCase(subCommand.getLabel()) || !hasPermission(sender, subCommand)) { + if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !hasPermission(sender, subCommand)) { continue; } if (!isConfirmType(sender, subCommand.getType())) { @@ -185,14 +190,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { || (sender instanceof ConsoleCommandSender && commandType == CommandType.CONSOLE); } - private void helpCommand(CommandSender sender, String label) { - sender.sendMessage(getEmptyLine()); - sender.sendMessage(getCommandTitle()); - sender.sendMessage(getEmptyLine()); - subCommands.stream().filter(subCommands -> !hideInHelp(subCommands) && hasPermission(sender, subCommands)).map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage); - sender.sendMessage(getEmptyLine()); - } - private void disguisedPlugin() { linkClasses.forEach(clazz -> disguisedPlugin(clazz, (JavaPlugin) registerCommand.getPlugin())); } diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/BaseSubCommand.java b/src/main/scala/me/skymc/taboolib/commands/internal/BaseSubCommand.java index 903f81b..a244d06 100644 --- a/src/main/scala/me/skymc/taboolib/commands/internal/BaseSubCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/internal/BaseSubCommand.java @@ -16,99 +16,55 @@ import java.util.stream.IntStream; */ public abstract class BaseSubCommand { - /** - * 指令名 - * - * @return 文本 - */ - abstract public String getLabel(); + private String label; - /** - * 指令描述 - * - * @return 文本 - */ - abstract public String getDescription(); + public void setLabel(String label) { + this.label = label; + } - /** - * 指令参数 - * - * @return {@link CommandArgument} - */ - abstract public CommandArgument[] getArguments(); + public String getLabel() { + return label; + } - /** - * 指令执行方法 - * - * @param sender 指令使用者 - * @param command 指令对象 - * @param label 主命令 - * @param args 参数(不含主命令及子命令) - */ - abstract public void onCommand(CommandSender sender, Command command, String label, String[] args); + public String getDescription() { + return null; + } + + public String[] getAliases() { + return new String[0]; + } + + public CommandArgument[] getArguments() { + return new CommandArgument[0]; + } - /** - * 指令执行者 - * - * @return {@link CommandType} - */ public CommandType getType() { return CommandType.ALL; } - /** - * 参数是否屏蔽子命令名 - * - * @return boolean - */ public boolean ignoredLabel() { return true; } - /** - * 是否需要玩家在线 - * - * @return boolean - */ public boolean requiredPlayer() { return false; } - /** - * 需要权限 - * - * @return boolean - */ public String getPermission() { return null; } - /** - * 参数是否符合 - * - * @param args 参数 - * @return boolean - */ + public boolean hideInHelp() { + return false; + } + public boolean isParameterConform(String[] args) { return IntStream.range(0, getArguments().length).noneMatch(i -> getArguments()[i].isRequired() && (args == null || args.length <= i)); } - /** - * 获取帮助文本 - * - * @param label 子命令标题 - * @return String - */ public String getCommandString(String label) { - return TLocale.asString("COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription()); + return TLocale.asString(getDescription() == null ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription()); } - /** - * 是否在命令帮助中隐藏 - * - * @return boolean - */ - public boolean hideInHelp() { - return false; - } + abstract public void onCommand(CommandSender sender, Command command, String label, String[] args); } \ No newline at end of file