Refactor help menu

This commit is contained in:
Frank van der Heijden 2021-07-24 17:23:42 +02:00
parent d96398f4a9
commit 5aeef212dc
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
6 changed files with 154 additions and 42 deletions

View file

@ -71,13 +71,13 @@ public class BukkitCommandServerUtils extends CommandServerUtils<BukkitPlugin, P
.withSuggestionsProvider((context, s) -> supportedConfigNames) .withSuggestionsProvider((context, s) -> supportedConfigNames)
.build()); .build());
manager.command(parseSubcommand(builder, "enableplugin") manager.command(buildSubcommand(builder, "enableplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleEnablePlugin)); .handler(this::handleEnablePlugin));
manager.command(parseSubcommand(builder, "disableplugin") manager.command(buildSubcommand(builder, "disableplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleDisablePlugin)); .handler(this::handleDisablePlugin));
manager.command(parseSubcommand(builder, "reloadconfig") manager.command(buildSubcommand(builder, "reloadconfig")
.argument(getArgument("config")) .argument(getArgument("config"))
.handler(this::handleReloadConfig)); .handler(this::handleReloadConfig));
} }

View file

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import net.frankheijden.serverutils.common.config.ServerUtilsConfig;
import net.frankheijden.serverutils.common.entities.AbstractResult; import net.frankheijden.serverutils.common.entities.AbstractResult;
import net.frankheijden.serverutils.common.entities.CloseableResult; import net.frankheijden.serverutils.common.entities.CloseableResult;
import net.frankheijden.serverutils.common.entities.LoadResult; import net.frankheijden.serverutils.common.entities.LoadResult;
@ -48,29 +49,29 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?>
manager.command(builder manager.command(builder
.handler(this::handleHelpCommand)); .handler(this::handleHelpCommand));
manager.command(parseSubcommand(builder, "help") manager.command(buildSubcommand(builder, "help")
.handler(this::handleHelpCommand)); .handler(this::handleHelpCommand));
manager.command(parseSubcommand(builder, "reload") manager.command(buildSubcommand(builder, "reload")
.handler(this::handleReload)); .handler(this::handleReload));
manager.command(parseSubcommand(builder, "loadplugin") manager.command(buildSubcommand(builder, "loadplugin")
.argument(getArgument("jarFile")) .argument(getArgument("jarFile"))
.handler(this::handleLoadPlugin)); .handler(this::handleLoadPlugin));
manager.command(parseSubcommand(builder, "unloadplugin") manager.command(buildSubcommand(builder, "unloadplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleUnloadPlugin)); .handler(this::handleUnloadPlugin));
manager.command(parseSubcommand(builder, "reloadplugin") manager.command(buildSubcommand(builder, "reloadplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleReloadPlugin)); .handler(this::handleReloadPlugin));
manager.command(parseSubcommand(builder, "watchplugin") manager.command(buildSubcommand(builder, "watchplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleWatchPlugin)); .handler(this::handleWatchPlugin));
manager.command(parseSubcommand(builder, "unwatchplugin") manager.command(buildSubcommand(builder, "unwatchplugin")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handleUnwatchPlugin)); .handler(this::handleUnwatchPlugin));
manager.command(parseSubcommand(builder, "plugininfo") manager.command(buildSubcommand(builder, "plugininfo")
.argument(getArgument("plugin")) .argument(getArgument("plugin"))
.handler(this::handlePluginInfo)); .handler(this::handlePluginInfo));
manager.command(parseSubcommand(builder, "commandinfo") manager.command(buildSubcommand(builder, "commandinfo")
.argument(getArgument("command")) .argument(getArgument("command"))
.handler(this::handleCommandInfo)); .handler(this::handleCommandInfo));
} }
@ -80,28 +81,67 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?>
plugin.getMessagesResource().sendMessage(sender, "serverutils.help.header"); plugin.getMessagesResource().sendMessage(sender, "serverutils.help.header");
FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format")) FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format"))
.orderedKeys("%command%", "%subcommand%", "%help%"); .orderedKeys("%command%", "%help%");
for (Command<C> command : plugin.getCommands()) { ServerUtilsConfig config = plugin.getCommandsResource().getConfig();
List<CommandArgument<C, ?>> arguments = command.getArguments(); for (String commandName : config.getKeys()) {
if (arguments.size() < 2) continue; ServerUtilsConfig commandConfig = (ServerUtilsConfig) config.get(commandName);
CommandElement commandElement = parseElement(commandConfig);
String shortestCommandAlias = determineShortestAlias(commandElement);
String commandName = arguments.get(0).getName(); if (commandElement.shouldDisplayInHelp()) {
StringBuilder sb = new StringBuilder(); builder.add(shortestCommandAlias, commandElement.getDescription().getDescription());
for (int i = 1; i < arguments.size(); i++) {
CommandArgument<C, ?> argument = arguments.get(i);
sb.append(" ").append(argument.getName());
} }
String subcommand = sb.toString(); Object subcommandsObject = commandConfig.get("subcommands");
String description = command.getComponents().get(1).getArgumentDescription().getDescription(); if (subcommandsObject instanceof ServerUtilsConfig) {
ServerUtilsConfig subcommandsConfig = (ServerUtilsConfig) subcommandsObject;
builder.add(commandName, subcommand, description); for (String subcommandName : subcommandsConfig.getKeys()) {
ServerUtilsConfig subcommandConfig = (ServerUtilsConfig) subcommandsConfig.get(subcommandName);
CommandElement subcommandElement = parseElement(subcommandConfig);
if (subcommandElement.shouldDisplayInHelp()) {
String shortestSubcommandAlias = determineShortestAlias(subcommandElement);
builder.add(
shortestCommandAlias + ' ' + shortestSubcommandAlias,
subcommandElement.getDescription().getDescription()
);
}
}
}
Object flagsObject = commandConfig.get("flags");
if (flagsObject instanceof ServerUtilsConfig) {
ServerUtilsConfig flagsConfig = (ServerUtilsConfig) flagsObject;
for (String flagName : flagsConfig.getKeys()) {
ServerUtilsConfig flagConfig = (ServerUtilsConfig) flagsConfig.get(flagName);
CommandElement flagElement = parseElement(flagConfig);
if (flagElement.shouldDisplayInHelp()) {
String shortestFlagAlias = determineShortestAlias(flagElement);
String flagPrefix = "-" + (flagElement.getMain().equals(shortestFlagAlias) ? "_" : "");
builder.add(
shortestCommandAlias + ' ' + flagPrefix + shortestFlagAlias,
flagElement.getDescription().getDescription()
);
}
}
}
} }
builder.build().forEach(msg -> plugin.getMessagesResource().sendRawMessage(sender, msg)); builder.build().forEach(msg -> plugin.getMessagesResource().sendRawMessage(sender, msg));
plugin.getMessagesResource().sendMessage(sender, "serverutils.help.footer"); plugin.getMessagesResource().sendMessage(sender, "serverutils.help.footer");
} }
private String determineShortestAlias(CommandElement element) {
String shortestAlias = element.getMain();
for (String alias : element.getAliases()) {
if (alias.length() < shortestAlias.length()) {
shortestAlias = alias;
}
}
return shortestAlias;
}
private void handleReload(CommandContext<C> context) { private void handleReload(CommandContext<C> context) {
C sender = context.getSender(); C sender = context.getSender();
plugin.reload(); plugin.reload();

View file

@ -5,6 +5,7 @@ import cloud.commandframework.Command;
import cloud.commandframework.CommandManager; import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.flags.CommandFlag; import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.Permission; import cloud.commandframework.permission.Permission;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -51,29 +52,48 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?>
return this.arguments.get(name).copy(); return this.arguments.get(name).copy();
} }
/**
* Builds a subcommand from the config.
*/
public Command.Builder<C> buildSubcommand(Command.Builder<C> builder, String subcommandName) {
CommandElement subcommand = parseSubcommand(subcommandName);
return builder
.literal(subcommand.getMain(), subcommand.getDescription(), subcommand.getAliases())
.permission(subcommand.getPermission());
}
/**
* Parses a command from the config.
*/
public CommandElement parseElement(ServerUtilsConfig elementConfig) {
String main = applyPrefix(elementConfig.getString("main"));
String descriptionString = elementConfig.getString("description");
ArgumentDescription description = descriptionString == null ? null : ArgumentDescription.of(descriptionString);
CommandPermission permission = Permission.of(elementConfig.getString("permission"));
boolean displayInHelp = elementConfig.getBoolean("display-in-help");
String[] aliases = elementConfig.getStringList("aliases").stream()
.map(this::applyPrefix)
.toArray(String[]::new);
return new CommandElement(main, description, permission, displayInHelp, aliases);
}
/** /**
* Parses a subcommand from the config. * Parses a subcommand from the config.
*/ */
public Command.Builder<C> parseSubcommand(Command.Builder<C> builder, String subcommand) { public CommandElement parseSubcommand(String subcommandName) {
ServerUtilsConfig subcommandConfig = (ServerUtilsConfig) commandConfig.get("subcommands." + subcommand); return parseElement((ServerUtilsConfig) commandConfig.get("subcommands." + subcommandName));
return builder
.literal(
subcommandConfig.getString("main"),
ArgumentDescription.of(subcommandConfig.getString("description")),
subcommandConfig.getStringList("aliases").toArray(new String[0])
)
.permission(subcommandConfig.getString("permission"));
} }
/** /**
* Parses a flag from the config. * Parses a flag from the config.
*/ */
public CommandFlag<Void> parseFlag(String flag) { public CommandFlag<Void> parseFlag(String flagName) {
ServerUtilsConfig flagConfig = (ServerUtilsConfig) commandConfig.get("flags." + flag); CommandElement flag = parseElement((ServerUtilsConfig) commandConfig.get("flags." + flagName));
return CommandFlag.newBuilder(flagConfig.getString("main")) return CommandFlag.newBuilder(flag.getMain())
.withAliases(flagConfig.getStringList("aliases").toArray(new String[0])) .withAliases(flag.getAliases())
.withPermission(Permission.of(flagConfig.getString("permission"))) .withPermission(flag.getPermission())
.withDescription(ArgumentDescription.of(flagConfig.getString("description"))) .withDescription(flag.getDescription())
.build(); .build();
} }
@ -95,4 +115,47 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?>
return str.replace("%prefix%", prefixChar); return str.replace("%prefix%", prefixChar);
} }
protected static class CommandElement {
private final String main;
private final ArgumentDescription description;
private final CommandPermission permission;
private final boolean displayInHelp;
private final String[] aliases;
public CommandElement(
String main,
ArgumentDescription description,
CommandPermission permission,
boolean displayInHelp,
String... aliases
) {
this.main = main;
this.description = description;
this.permission = permission;
this.displayInHelp = displayInHelp;
this.aliases = aliases;
}
public String getMain() {
return main;
}
public ArgumentDescription getDescription() {
return description;
}
public CommandPermission getPermission() {
return permission;
}
public boolean shouldDisplayInHelp() {
return displayInHelp;
}
public String[] getAliases() {
return aliases;
}
}
} }

View file

@ -8,11 +8,13 @@ public class FormatBuilder {
private final String format; private final String format;
private final List<String[]> valueList; private final List<String[]> valueList;
private String[] orderedKeys; private String[] orderedKeys;
private boolean skipEmpty;
private FormatBuilder(String format) { private FormatBuilder(String format) {
this.format = format; this.format = format;
this.valueList = new ArrayList<>(); this.valueList = new ArrayList<>();
this.orderedKeys = new String[0]; this.orderedKeys = new String[0];
this.skipEmpty = true;
} }
public static FormatBuilder create(String format) { public static FormatBuilder create(String format) {
@ -29,18 +31,24 @@ public class FormatBuilder {
return this; return this;
} }
public FormatBuilder skipEmpty(boolean skipEmpty) {
this.skipEmpty = skipEmpty;
return this;
}
/** /**
* Builds the current FormatBuilder instance into a list of strings. * Builds the current FormatBuilder instance into a list of strings.
*/ */
public List<String> build() { public List<String> build() {
List<String> strings = new ArrayList<>(); List<String> strings = new ArrayList<>();
loop:
for (String[] values : valueList) { for (String[] values : valueList) {
String str = format; String str = format;
for (int i = 0; i < Math.min(values.length, orderedKeys.length); i++) { for (int i = 0; i < Math.min(values.length, orderedKeys.length); i++) {
String value = values[i]; String value = values[i];
if (value == null || value.isEmpty()) break; if ((value == null || value.isEmpty()) && skipEmpty) continue loop;
str = str.replace(orderedKeys[i], value); str = str.replace(orderedKeys[i], String.valueOf(value));
} }
strings.add(str); strings.add(str);
} }

View file

@ -3,6 +3,7 @@
"main": "%prefix%plugins", "main": "%prefix%plugins",
"aliases": ["%prefix%pl"], "aliases": ["%prefix%pl"],
"permission": "serverutils.plugins", "permission": "serverutils.plugins",
"description": "Displays the enabled plugins.",
"display-in-help": true, "display-in-help": true,
"flags": { "flags": {
"version": { "version": {

View file

@ -26,7 +26,7 @@
}, },
"help": { "help": {
"header": "&8&m-------------=&r&8[ &b&lServerUtils Help&r &8]&m=---------------", "header": "&8&m-------------=&r&8[ &b&lServerUtils Help&r &8]&m=---------------",
"format": "&8/&3%command%&b%subcommand% &f(&7%help%&f)", "format": "&8/&3%command% &8(&b%help%&8)",
"footer": "&8&m-------------------------------------------------" "footer": "&8&m-------------------------------------------------"
}, },
"plugins": { "plugins": {