Improvements to help formatter

This commit is contained in:
Aikar
2018-03-21 22:02:48 -04:00
parent 96238f9e06
commit 8335d580f6
7 changed files with 113 additions and 49 deletions
@@ -353,4 +353,9 @@ public class BukkitCommandManager extends CommandManager<
this.autoDetectFromClient = autoDetectFromClient;
return old;
}
@Override
public String getCommandPrefix(CommandIssuer issuer) {
return issuer.isPlayer() ? "/" : "";
}
}
@@ -207,4 +207,10 @@ public class BungeeCommandManager extends CommandManager<
}
}
}
@Override
public String getCommandPrefix(CommandIssuer issuer) {
return issuer.isPlayer() ? "/" : "";
}
}
@@ -50,7 +50,7 @@ public class CommandHelp {
this.manager = manager;
this.issuer = issuer;
this.perPage = manager.defaultHelpPerPage;
this.commandName = "/" + rootCommand.getCommandName();
this.commandName = manager.getCommandPrefix(issuer) + rootCommand.getCommandName();
SetMultimap<String, RegisteredCommand> subCommands = rootCommand.getSubCommands();
Set<RegisteredCommand> seen = new HashSet<>();
@@ -175,21 +175,24 @@ public class CommandHelp {
}
}
public void showDetailedHelp(HelpEntry page, CommandIssuer issuer) {
public void showDetailedHelp(HelpEntry entry, CommandIssuer issuer) {
// header
manager.getHelpFormatter().printDetailedHelpHeader(issuer, commandName, page);
CommandHelpFormatter formatter = manager.getHelpFormatter();
formatter.printDetailedHelpHeader(issuer, commandName, entry);
// normal help line
manager.getHelpFormatter().printHelpLine(issuer, commandName, page);
formatter.printHelpLine(issuer, commandName, entry);
// additionally detailed help for params
for (CommandParameter param : page.getParameters()) {
manager.getHelpFormatter().printDetailedHelpLine(issuer, commandName, page, param.getName(), param.getDescription());
for (CommandParameter param : entry.getParameters()) {
String description = param.getDescription();
if (description != null && !description.isEmpty()) {
formatter.printDetailedHelpLine(issuer, commandName, entry, param.getName(), description);
}
}
// footer
manager.getHelpFormatter().printDetailedHelpFooter(issuer, commandName, page);
formatter.printDetailedHelpFooter(issuer, commandName, entry);
}
public List<HelpEntry> getHelpEntries() {
@@ -39,23 +39,31 @@ public class CommandHelpFormatter {
// # help #
// ########
public void printHelpHeader(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, "{command}", commandName);
public void printHelpHeader(CommandIssuer issuer, String command, int page, int totalPages, int totalResults) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER,
"{page}", "" + page,
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults,
"{command}", "" + command
);
}
public void printHelpLine(CommandIssuer issuer, String commandName, HelpEntry page) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page));
public void printHelpLine(CommandIssuer issuer, String command, HelpEntry page) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page, command));
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printHelpFooter(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, boolean lastPage) {
if(lastPage)return;
public void printHelpFooter(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, boolean lastPage) {
if (lastPage) {
return;
}
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
"{page}", "" + page,
"{totalpages}", ""+ totalPages,
"{results}", "" + totalResults
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults,
"{command}", "" + command
);
}
@@ -63,57 +71,72 @@ public class CommandHelpFormatter {
// # search #
// ##########
public void printSearchHeader(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, List<String> search) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER,
"{command}", commandName,
"{search}", String.join(" ", search));
public void printSearchHeader(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, List<String> search) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getPaginationFormatReplacements(command, page, totalPages, totalResults, search));
}
public void printSearchLine(CommandIssuer issuer, String commandName, HelpEntry page, int score) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page));
public void printSearchLine(CommandIssuer issuer, String command, HelpEntry page, int score) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page, command));
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printSearchFooter(CommandIssuer issuer, String commandName, int page, int totalPages, int totalResults, List<String> search, boolean lastPage) {
if(lastPage)return;
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
"{page}", "" + page,
"{totalpages}", ""+ totalPages,
"{results}", "" + totalResults
public void printSearchFooter(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, List<String> search, boolean lastPage) {
if (lastPage) {
return;
}
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getPaginationFormatReplacements(command, page, totalPages, totalResults, search)
);
}
// ############
// # detailed #
// ############
public void printDetailedHelpHeader(CommandIssuer issuer, String commandName, HelpEntry page) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER, "{command}", page.getCommand());
public void printDetailedHelpHeader(CommandIssuer issuer, String command, HelpEntry entry) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER,
"{command}", entry.getCommand(),
"{command}", command
);
}
public void printDetailedHelpLine(CommandIssuer issuer, String commandName, HelpEntry page, String subCommand, String paramDescription) {
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_FORMAT, getDetailedFormatReplacements(subCommand, paramDescription, page));
public void printDetailedHelpLine(CommandIssuer issuer, String rootCommand, HelpEntry entry, String subCommand, String paramDescription) {
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_FORMAT, getDetailedFormatReplacements(subCommand, paramDescription, entry, rootCommand));
for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printDetailedHelpFooter(CommandIssuer issuer, String commandName, HelpEntry page) {
public void printDetailedHelpFooter(CommandIssuer issuer, String command, HelpEntry page) {
// default doesn't have a footer
}
@NotNull
public String[] getPaginationFormatReplacements(String command, int page, int totalPages, int totalResults, List<String> search) {
return new String[]{
"{search}", String.join(" ", search),
"{command}", command,
"{rootcommand}", command,
"{page}", "" + page,
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults
};
}
/**
* Override this to control replacements
*
* @param e
* @param command
* @return
*/
@NotNull
public String[] getFormatReplacements(HelpEntry e) {
public String[] getFormatReplacements(HelpEntry e, String command) {
//{command} {parameters} {separator} {description}
return new String[] {
return new String[]{
"{command}", e.getCommand(),
"{rootcommand}", command,
"{parameters}", e.getParameterSyntax(),
"{separator}", e.getDescription().isEmpty() ? "" : "-",
"{description}", e.getDescription()
@@ -122,17 +145,21 @@ public class CommandHelpFormatter {
/**
* Override this to control replacements
* @param cmd
* @param help
*
* @param name
* @param description
* @param page
* @param rootCommand
* @return
*/
@NotNull
public String[] getDetailedFormatReplacements(String cmd, String help, HelpEntry page) {
public String[] getDetailedFormatReplacements(String name, String description, HelpEntry page, String rootCommand) {
//{name} {description}
return new String[] {
"{name}", cmd,
"{description}", help
return new String[]{
"{name}", name,
"{command}", page.getCommand(),
"{rootcommand}", rootCommand,
"{description}", description
};
}
}
@@ -504,4 +504,8 @@ public abstract class CommandManager <
Annotations getAnnotations() {
return annotations;
}
public String getCommandPrefix(CommandIssuer issuer) {
return "";
}
}
@@ -220,13 +220,7 @@ public class JDACommandManager extends CommandManager<
Message message = event.getMessage();
String msg = message.getContentDisplay();
CommandConfig config = this.defaultConfig;
if (this.configProvider != null) {
CommandConfig provided = this.configProvider.provide(event);
if (provided != null) {
config = provided;
}
}
CommandConfig config = getCommandConfig(event);
String prefixFound = null;
for (String prefix : config.getCommandPrefixes()) {
@@ -255,4 +249,24 @@ public class JDACommandManager extends CommandManager<
}
rootCommand.execute(this.getCommandIssuer(event), cmd, args);
}
private CommandConfig getCommandConfig(MessageReceivedEvent event) {
CommandConfig config = this.defaultConfig;
if (this.configProvider != null) {
CommandConfig provided = this.configProvider.provide(event);
if (provided != null) {
config = provided;
}
}
return config;
}
@Override
public String getCommandPrefix(CommandIssuer issuer) {
MessageReceivedEvent event = ((JDACommandEvent) issuer).getEvent();
CommandConfig commandConfig = getCommandConfig(event);
List<String> prefixes = commandConfig.getCommandPrefixes();
return prefixes.isEmpty() ? "" : prefixes.get(0);
}
}
@@ -197,4 +197,9 @@ public class SpongeCommandManager extends CommandManager<
public SpongeConditionContext createConditionContext(CommandIssuer issuer, String config) {
return new SpongeConditionContext((SpongeCommandIssuer) issuer, config);
}
@Override
public String getCommandPrefix(CommandIssuer issuer) {
return issuer.isPlayer() ? "/" : "";
}
}