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

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
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.CloseableResult;
import net.frankheijden.serverutils.common.entities.LoadResult;
@ -48,29 +49,29 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?>
manager.command(builder
.handler(this::handleHelpCommand));
manager.command(parseSubcommand(builder, "help")
manager.command(buildSubcommand(builder, "help")
.handler(this::handleHelpCommand));
manager.command(parseSubcommand(builder, "reload")
manager.command(buildSubcommand(builder, "reload")
.handler(this::handleReload));
manager.command(parseSubcommand(builder, "loadplugin")
manager.command(buildSubcommand(builder, "loadplugin")
.argument(getArgument("jarFile"))
.handler(this::handleLoadPlugin));
manager.command(parseSubcommand(builder, "unloadplugin")
manager.command(buildSubcommand(builder, "unloadplugin")
.argument(getArgument("plugin"))
.handler(this::handleUnloadPlugin));
manager.command(parseSubcommand(builder, "reloadplugin")
manager.command(buildSubcommand(builder, "reloadplugin")
.argument(getArgument("plugin"))
.handler(this::handleReloadPlugin));
manager.command(parseSubcommand(builder, "watchplugin")
manager.command(buildSubcommand(builder, "watchplugin")
.argument(getArgument("plugin"))
.handler(this::handleWatchPlugin));
manager.command(parseSubcommand(builder, "unwatchplugin")
manager.command(buildSubcommand(builder, "unwatchplugin")
.argument(getArgument("plugin"))
.handler(this::handleUnwatchPlugin));
manager.command(parseSubcommand(builder, "plugininfo")
manager.command(buildSubcommand(builder, "plugininfo")
.argument(getArgument("plugin"))
.handler(this::handlePluginInfo));
manager.command(parseSubcommand(builder, "commandinfo")
manager.command(buildSubcommand(builder, "commandinfo")
.argument(getArgument("command"))
.handler(this::handleCommandInfo));
}
@ -80,28 +81,67 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?>
plugin.getMessagesResource().sendMessage(sender, "serverutils.help.header");
FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format"))
.orderedKeys("%command%", "%subcommand%", "%help%");
.orderedKeys("%command%", "%help%");
for (Command<C> command : plugin.getCommands()) {
List<CommandArgument<C, ?>> arguments = command.getArguments();
if (arguments.size() < 2) continue;
ServerUtilsConfig config = plugin.getCommandsResource().getConfig();
for (String commandName : config.getKeys()) {
ServerUtilsConfig commandConfig = (ServerUtilsConfig) config.get(commandName);
CommandElement commandElement = parseElement(commandConfig);
String shortestCommandAlias = determineShortestAlias(commandElement);
String commandName = arguments.get(0).getName();
StringBuilder sb = new StringBuilder();
for (int i = 1; i < arguments.size(); i++) {
CommandArgument<C, ?> argument = arguments.get(i);
sb.append(" ").append(argument.getName());
if (commandElement.shouldDisplayInHelp()) {
builder.add(shortestCommandAlias, commandElement.getDescription().getDescription());
}
String subcommand = sb.toString();
String description = command.getComponents().get(1).getArgumentDescription().getDescription();
Object subcommandsObject = commandConfig.get("subcommands");
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));
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) {
C sender = context.getSender();
plugin.reload();

View file

@ -5,6 +5,7 @@ import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.Permission;
import java.util.HashMap;
import java.util.Map;
@ -51,29 +52,48 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?>
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.
*/
public Command.Builder<C> parseSubcommand(Command.Builder<C> builder, String subcommand) {
ServerUtilsConfig subcommandConfig = (ServerUtilsConfig) commandConfig.get("subcommands." + subcommand);
return builder
.literal(
subcommandConfig.getString("main"),
ArgumentDescription.of(subcommandConfig.getString("description")),
subcommandConfig.getStringList("aliases").toArray(new String[0])
)
.permission(subcommandConfig.getString("permission"));
public CommandElement parseSubcommand(String subcommandName) {
return parseElement((ServerUtilsConfig) commandConfig.get("subcommands." + subcommandName));
}
/**
* Parses a flag from the config.
*/
public CommandFlag<Void> parseFlag(String flag) {
ServerUtilsConfig flagConfig = (ServerUtilsConfig) commandConfig.get("flags." + flag);
return CommandFlag.newBuilder(flagConfig.getString("main"))
.withAliases(flagConfig.getStringList("aliases").toArray(new String[0]))
.withPermission(Permission.of(flagConfig.getString("permission")))
.withDescription(ArgumentDescription.of(flagConfig.getString("description")))
public CommandFlag<Void> parseFlag(String flagName) {
CommandElement flag = parseElement((ServerUtilsConfig) commandConfig.get("flags." + flagName));
return CommandFlag.newBuilder(flag.getMain())
.withAliases(flag.getAliases())
.withPermission(flag.getPermission())
.withDescription(flag.getDescription())
.build();
}
@ -95,4 +115,47 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?>
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 List<String[]> valueList;
private String[] orderedKeys;
private boolean skipEmpty;
private FormatBuilder(String format) {
this.format = format;
this.valueList = new ArrayList<>();
this.orderedKeys = new String[0];
this.skipEmpty = true;
}
public static FormatBuilder create(String format) {
@ -29,18 +31,24 @@ public class FormatBuilder {
return this;
}
public FormatBuilder skipEmpty(boolean skipEmpty) {
this.skipEmpty = skipEmpty;
return this;
}
/**
* Builds the current FormatBuilder instance into a list of strings.
*/
public List<String> build() {
List<String> strings = new ArrayList<>();
loop:
for (String[] values : valueList) {
String str = format;
for (int i = 0; i < Math.min(values.length, orderedKeys.length); i++) {
String value = values[i];
if (value == null || value.isEmpty()) break;
str = str.replace(orderedKeys[i], value);
if ((value == null || value.isEmpty()) && skipEmpty) continue loop;
str = str.replace(orderedKeys[i], String.valueOf(value));
}
strings.add(str);
}

View file

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

View file

@ -26,7 +26,7 @@
},
"help": {
"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-------------------------------------------------"
},
"plugins": {