Added support for viewing in-depth help via /plan ?

This commit is contained in:
Risto Lahtela 2020-09-06 11:54:30 +03:00
parent 6c8a9f9294
commit 38bb87b9b2
3 changed files with 59 additions and 3 deletions

View File

@ -47,14 +47,29 @@ public class CommandWithSubcommands extends Subcommand {
return new Builder(locale);
}
private List<Subcommand> getPermittedSubcommands(CMDSender sender) {
return subcommands.stream().filter(sender::hasAllPermissionsFor).collect(Collectors.toList());
}
public void onHelp(CMDSender sender, Arguments arguments) {
List<Subcommand> hasPermissionFor = subcommands.stream().filter(sender::hasAllPermissionsFor).collect(Collectors.toList());
List<Subcommand> hasPermissionFor = getPermittedSubcommands(sender);
sender.buildMessage()
.addPart(locale.getString(CommandLang.HEADER_HELP, getPrimaryAlias()))
.newLine().newLine()
.apply(new HelpFormatter(sender, colors, getPrimaryAlias(), hasPermissionFor)::addSubcommands)
.newLine().newLine()
.addPart(locale.getString(CommandLang.FOOTER_HELP))
.addPart(locale.getString(CommandLang.FOOTER_HELP, getPrimaryAlias()))
.send();
}
public void onInDepthHelp(CMDSender sender, Arguments arguments) {
List<Subcommand> hasPermissionFor = getPermittedSubcommands(sender);
sender.buildMessage()
.addPart(locale.getString(CommandLang.HEADER_HELP, getPrimaryAlias()))
.newLine().newLine()
.apply(new HelpFormatter(sender, colors, getPrimaryAlias(), hasPermissionFor)::addInDepthSubcommands)
.newLine().newLine()
.addPart(locale.getString(CommandLang.FOOTER_HELP, getPrimaryAlias()))
.send();
}
@ -76,6 +91,10 @@ public class CommandWithSubcommands extends Subcommand {
String alias = gotAlias.get();
if ("help".equals(alias)) {
onHelp(sender, arguments);
return;
} else if ("?".equals(alias)) {
onInDepthHelp(sender, arguments);
return;
} else {
for (Subcommand subcommand : subcommands) {
if (subcommand.getAliases().contains(alias)) {

View File

@ -78,6 +78,43 @@ public class HelpFormatter {
return toReturn;
}
public MessageBuilder addInDepthSubcommands(MessageBuilder message) {
MessageBuilder toReturn = message;
String m = colors.getMainColor();
String s = colors.getSecondaryColor();
String asString = subcommands.stream()
.filter(cmd -> cmd.getDescription() != null)
.map(cmd -> {
TextStringBuilder builder = new TextStringBuilder(
m + mainCommand + " " + cmd.getPrimaryAlias()
);
for (String description : cmd.getInDepthDescription()) {
builder.append("***").append(s).append(description).append('\n');
}
for (Subcommand.ArgumentDescriptor argument : cmd.getArguments()) {
builder.append("***").append(m).append(argument.isRequired() ? '<' + argument.getName() + '>' : '[' + argument.getName() + ']')
.append(s).append(" ").append(argument.getDescription()).append('\n');
}
return builder.toString();
}
).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
List<String[]> table = sender.getFormatter().tableAsParts(asString, "***");
for (String[] row : table) {
if (sender.isPlayer()) {
toReturn = toReturn.addPart(m + "/");
}
toReturn = toReturn.addPart(row[0]);
if (row.length > 1) toReturn = toReturn.addPart(row[1]);
toReturn = toReturn.newLine();
}
return toReturn;
}
private List<String> argumentsAndAliases(Subcommand subcommand, List<Subcommand.ArgumentDescriptor> descriptors, Set<String> aliases) {
List<String> lines = new ArrayList<>();
lines.add(colors.getMainColor() + subcommand.getPrimaryAlias() + colors.getTertiaryColor() + " Arguments:" + (descriptors.isEmpty() ? " none" : ""));

View File

@ -72,7 +72,7 @@ public enum CommandLang implements Lang {
LINK_REGISTER("Cmd - Link Register", "Register page: "),
HEADER_HELP("Cmd Header - Help", "> §2/${0} Help"),
FOOTER_HELP("Cmd Footer - Help", "§7Hover over command or arguments to learn more about them."),
FOOTER_HELP("Cmd Footer - Help", "§7Hover over command or arguments or use '/${0} ?' to learn more about them."),
HEADER_SEARCH("Cmd Header - Search", "> §2${0} Results for §f${1}§2:"),
HEADER_ANALYSIS("Cmd Header - Analysis", "> §2Analysis Results"),
HEADER_INFO("Cmd Header - Info", "> §2Player Analytics"),