#293 Improve handling of disabled sections

This commit is contained in:
ljacqu 2016-10-09 00:50:03 +02:00
parent f453a5b4f5
commit edfd833198
5 changed files with 74 additions and 53 deletions

View File

@ -30,8 +30,8 @@ import static java.util.Collections.singletonList;
public class HelpProvider implements SettingsDependent { public class HelpProvider implements SettingsDependent {
// --- Bit flags --- // --- Bit flags ---
/** Set to <i>not</i> show the command. */ /** Set to show a command overview. */
public static final int HIDE_COMMAND = 0x001; public static final int SHOW_COMMAND = 0x001;
/** Set to show the description of the command. */ /** Set to show the description of the command. */
public static final int SHOW_DESCRIPTION = 0x002; public static final int SHOW_DESCRIPTION = 0x002;
/** Set to show the detailed description of the command. */ /** Set to show the detailed description of the command. */
@ -45,12 +45,14 @@ public class HelpProvider implements SettingsDependent {
/** Set to show the child commands of the command. */ /** Set to show the child commands of the command. */
public static final int SHOW_CHILDREN = 0x040; public static final int SHOW_CHILDREN = 0x040;
/** Shortcut for setting all options apart from {@link HelpProvider#HIDE_COMMAND}. */ /** Shortcut for setting all options. */
public static final int ALL_OPTIONS = ~HIDE_COMMAND; public static final int ALL_OPTIONS = ~0;
private final PermissionsManager permissionsManager; private final PermissionsManager permissionsManager;
private final HelpMessagesService helpMessagesService; private final HelpMessagesService helpMessagesService;
private String helpHeader; private String helpHeader;
/** int with bit flags set corresponding to the above constants for enabled sections. */
private Integer enabledSections;
@Inject @Inject
HelpProvider(PermissionsManager permissionsManager, HelpMessagesService helpMessagesService, Settings settings) { HelpProvider(PermissionsManager permissionsManager, HelpMessagesService helpMessagesService, Settings settings) {
@ -65,28 +67,29 @@ public class HelpProvider implements SettingsDependent {
} }
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
options = filterDisabledSections(options);
if (options == 0) {
// Return directly if no options are enabled so we don't include the help header
return lines;
}
lines.add(ChatColor.GOLD + "==========[ " + helpHeader + " HELP ]=========="); lines.add(ChatColor.GOLD + "==========[ " + helpHeader + " HELP ]==========");
CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription()); CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription());
List<String> labels = ImmutableList.copyOf(result.getLabels()); List<String> labels = ImmutableList.copyOf(result.getLabels());
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels)); List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
if (!hasFlag(HIDE_COMMAND, options)) { if (hasFlag(SHOW_COMMAND, options)) {
lines.add(ChatColor.GOLD + "Command: " + CommandSyntaxHelper.getSyntax(command, correctLabels)); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMAND) + ": "
+ CommandSyntaxHelper.getSyntax(command, correctLabels));
} }
if (hasFlag(SHOW_DESCRIPTION, options)) { if (hasFlag(SHOW_DESCRIPTION, options)) {
String description = helpMessagesService.getMessage(SHORT_DESCRIPTION); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(SHORT_DESCRIPTION) + ": "
if (!description.isEmpty()) { + ChatColor.WHITE + command.getDescription());
lines.add(ChatColor.GOLD + description + ": " + ChatColor.WHITE + command.getDescription());
}
} }
if (hasFlag(SHOW_LONG_DESCRIPTION, options)) { if (hasFlag(SHOW_LONG_DESCRIPTION, options)) {
String description = helpMessagesService.getMessage(DETAILED_DESCRIPTION); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(DETAILED_DESCRIPTION) + ":");
if (!description.isEmpty()) {
lines.add(ChatColor.GOLD + description + ":");
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription()); lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
} }
}
if (hasFlag(SHOW_ARGUMENTS, options)) { if (hasFlag(SHOW_ARGUMENTS, options)) {
printArguments(command, lines); printArguments(command, lines);
} }
@ -120,18 +123,41 @@ public class HelpProvider implements SettingsDependent {
@Override @Override
public void reload(Settings settings) { public void reload(Settings settings) {
helpHeader = settings.getProperty(PluginSettings.HELP_HEADER); helpHeader = settings.getProperty(PluginSettings.HELP_HEADER);
// We don't know about the reloading order of the classes, i.e. we cannot assume that HelpMessagesService
// has already been reloaded. So set the enabledSections flag to null and redefine it first time needed.
enabledSections = null;
}
/**
* Removes any disabled sections from the options. Sections are considered disabled
* if the translated text for the section is empty.
*
* @param options the options to process
* @return the options without any disabled sections
*/
private int filterDisabledSections(int options) {
if (enabledSections == null) {
enabledSections = flagFor(HelpSection.COMMAND, SHOW_COMMAND)
| flagFor(HelpSection.SHORT_DESCRIPTION, SHOW_DESCRIPTION)
| flagFor(HelpSection.DETAILED_DESCRIPTION, SHOW_LONG_DESCRIPTION)
| flagFor(HelpSection.ARGUMENTS, SHOW_ARGUMENTS)
| flagFor(HelpSection.PERMISSIONS, SHOW_PERMISSIONS)
| flagFor(HelpSection.ALTERNATIVES, SHOW_ALTERNATIVES)
| flagFor(HelpSection.CHILDREN, SHOW_CHILDREN);
}
return options & enabledSections;
}
private int flagFor(HelpSection section, int flag) {
return helpMessagesService.getMessage(section).isEmpty() ? 0 : flag;
} }
private void printArguments(CommandDescription command, List<String> lines) { private void printArguments(CommandDescription command, List<String> lines) {
if (command.getArguments().isEmpty()) { if (command.getArguments().isEmpty()) {
return; return;
} }
String arguments = helpMessagesService.getMessage(HelpSection.ARGUMENTS);
if (arguments.isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + arguments + ":"); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.ARGUMENTS) + ":");
StringBuilder argString = new StringBuilder(); StringBuilder argString = new StringBuilder();
String optionalText = " (" + helpMessagesService.getMessage(HelpMessage.OPTIONAL) + ")"; String optionalText = " (" + helpMessagesService.getMessage(HelpMessage.OPTIONAL) + ")";
for (CommandArgumentDescription argument : command.getArguments()) { for (CommandArgumentDescription argument : command.getArguments()) {
@ -150,12 +176,8 @@ public class HelpProvider implements SettingsDependent {
if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) { if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) {
return; return;
} }
String alternatives = helpMessagesService.getMessage(HelpSection.ALTERNATIVES);
if (alternatives.isEmpty()) {
return;
}
lines.add(ChatColor.GOLD + alternatives + ":"); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.ALTERNATIVES) + ":");
// Get the label used // Get the label used
final String parentLabel = correctLabels.get(0); final String parentLabel = correctLabels.get(0);
final String childLabel = correctLabels.get(1); final String childLabel = correctLabels.get(1);
@ -170,11 +192,10 @@ public class HelpProvider implements SettingsDependent {
private void printPermissions(CommandDescription command, CommandSender sender, List<String> lines) { private void printPermissions(CommandDescription command, CommandSender sender, List<String> lines) {
PermissionNode permission = command.getPermission(); PermissionNode permission = command.getPermission();
String permissionsTitle = helpMessagesService.getMessage(HelpSection.PERMISSIONS); if (permission == null) {
if (permission == null || permissionsTitle.isEmpty()) {
return; return;
} }
lines.add(ChatColor.GOLD + permissionsTitle + ":"); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.PERMISSIONS) + ":");
boolean hasPermission = permissionsManager.hasPermission(sender, permission); boolean hasPermission = permissionsManager.hasPermission(sender, permission);
lines.add(String.format(" " + ChatColor.YELLOW + ChatColor.ITALIC + "%s" + ChatColor.GRAY + " (%s)", lines.add(String.format(" " + ChatColor.YELLOW + ChatColor.ITALIC + "%s" + ChatColor.GRAY + " (%s)",
@ -215,7 +236,7 @@ public class HelpProvider implements SettingsDependent {
return; return;
} }
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMANDS) + ":"); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.CHILDREN) + ":");
String parentCommandPath = String.join(" ", parentLabels); String parentCommandPath = String.join(" ", parentLabels);
for (CommandDescription child : command.getChildren()) { for (CommandDescription child : command.getChildren()) {
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0) lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)
@ -228,7 +249,7 @@ public class HelpProvider implements SettingsDependent {
} }
@VisibleForTesting @VisibleForTesting
protected static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) { static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) {
List<CommandDescription> commands = CommandUtils.constructParentList(command); List<CommandDescription> commands = CommandUtils.constructParentList(command);
List<String> correctLabels = new ArrayList<>(); List<String> correctLabels = new ArrayList<>();
boolean foundIncorrectLabel = false; boolean foundIncorrectLabel = false;

View File

@ -5,19 +5,19 @@ package fr.xephi.authme.command.help;
*/ */
public enum HelpSection { public enum HelpSection {
COMMAND("command"),
SHORT_DESCRIPTION("description.short"), SHORT_DESCRIPTION("description.short"),
DETAILED_DESCRIPTION("description.detailed"), DETAILED_DESCRIPTION("description.detailed"),
USAGE("usage"),
ARGUMENTS("arguments"), ARGUMENTS("arguments"),
ALTERNATIVES("alternatives"), ALTERNATIVES("alternatives"),
PERMISSIONS("permissions"), PERMISSIONS("permissions"),
COMMANDS("commands"); CHILDREN("children");
private final String key; private final String key;

View File

@ -9,13 +9,13 @@ common:
opOnly: 'Nur OP''s' opOnly: 'Nur OP''s'
allowed: 'Allen erlaubt' allowed: 'Allen erlaubt'
section: section:
command: 'Kommando'
description.short: 'Beschreibung' description.short: 'Beschreibung'
description.detailed: 'Detaillierte Beschreibung' description.detailed: 'Detaillierte Beschreibung'
usage: 'Gebrauch'
arguments: 'Argumente' arguments: 'Argumente'
permissions: 'Rechte' permissions: 'Rechte'
alternatives: 'Alternativen' alternatives: 'Alternativen'
commands: 'Kommandos' children: 'Kommandos'
commands: commands:
authme.register: authme.register:
description: 'Registriert einen Benutzer' description: 'Registriert einen Benutzer'

View File

@ -9,13 +9,13 @@ common:
opOnly: 'OP''s only' opOnly: 'OP''s only'
allowed: 'Everyone allowed' allowed: 'Everyone allowed'
section: section:
command: 'Command'
description.short: 'Short description' description.short: 'Short description'
description.detailed: 'Detailed description' description.detailed: 'Detailed description'
arguments: 'Arguments' arguments: 'Arguments'
permissions: 'Permissions' permissions: 'Permissions'
alternatives: 'Alternatives' alternatives: 'Alternatives'
commands: 'Commands' children: 'Commands'
usage: 'Usage'
commands: commands:
authme.register: authme.register:
description: 'Register a player' description: 'Register a player'

View File

@ -28,10 +28,10 @@ import java.util.Set;
import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel; import static fr.xephi.authme.command.TestCommandsUtil.getCommandWithLabel;
import static fr.xephi.authme.command.help.HelpProvider.ALL_OPTIONS; import static fr.xephi.authme.command.help.HelpProvider.ALL_OPTIONS;
import static fr.xephi.authme.command.help.HelpProvider.HIDE_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES; import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_ARGUMENTS; import static fr.xephi.authme.command.help.HelpProvider.SHOW_ARGUMENTS;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN; import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_COMMAND;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION; import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_LONG_DESCRIPTION; import static fr.xephi.authme.command.help.HelpProvider.SHOW_LONG_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpProvider.SHOW_PERMISSIONS; import static fr.xephi.authme.command.help.HelpProvider.SHOW_PERMISSIONS;
@ -84,7 +84,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login")); FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
// when // when
helpProvider.outputHelp(sender, result, SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION); helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -103,7 +103,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg")); FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS); helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -121,7 +121,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("email")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("email"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS); helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -137,7 +137,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS); helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -154,7 +154,7 @@ public class HelpProviderTest {
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true); given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true);
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS); helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -176,7 +176,7 @@ public class HelpProviderTest {
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(false); given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(false);
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS); helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -195,7 +195,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS); helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -211,7 +211,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("test")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("test"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS); helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -225,7 +225,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg")); FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ALTERNATIVES); helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -242,7 +242,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login")); FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ALTERNATIVES); helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -256,12 +256,12 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_CHILDREN); helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
assertThat(lines, hasSize(4)); assertThat(lines, hasSize(4));
assertThat(removeColors(lines.get(1)), containsString("Commands:")); assertThat(removeColors(lines.get(1)), containsString("Children:"));
assertThat(removeColors(lines.get(2)), containsString("/authme login: login cmd")); assertThat(removeColors(lines.get(2)), containsString("/authme login: login cmd"));
assertThat(removeColors(lines.get(3)), containsString("/authme register: register cmd")); assertThat(removeColors(lines.get(3)), containsString("/authme register: register cmd"));
} }
@ -273,7 +273,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme")); FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
// when // when
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_CHILDREN); helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);
@ -284,7 +284,7 @@ public class HelpProviderTest {
public void shouldHandleUnboundFoundCommandResult() { public void shouldHandleUnboundFoundCommandResult() {
// given // given
FoundCommandResult result = new FoundCommandResult(null, Arrays.asList("authme", "test"), FoundCommandResult result = new FoundCommandResult(null, Arrays.asList("authme", "test"),
Collections.<String>emptyList(), 0.0, FoundResultStatus.UNKNOWN_LABEL); Collections.emptyList(), 0.0, FoundResultStatus.UNKNOWN_LABEL);
// when // when
helpProvider.outputHelp(sender, result, ALL_OPTIONS); helpProvider.outputHelp(sender, result, ALL_OPTIONS);
@ -306,7 +306,7 @@ public class HelpProviderTest {
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "ragister")); FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "ragister"));
// when // when
helpProvider.outputHelp(sender, result, 0); helpProvider.outputHelp(sender, result, SHOW_COMMAND);
// then // then
List<String> lines = getLines(sender); List<String> lines = getLines(sender);