mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-07 02:59:48 +01:00
#293 Improve handling of disabled sections
This commit is contained in:
parent
f453a5b4f5
commit
edfd833198
@ -30,8 +30,8 @@ import static java.util.Collections.singletonList;
|
||||
public class HelpProvider implements SettingsDependent {
|
||||
|
||||
// --- Bit flags ---
|
||||
/** Set to <i>not</i> show the command. */
|
||||
public static final int HIDE_COMMAND = 0x001;
|
||||
/** Set to show a command overview. */
|
||||
public static final int SHOW_COMMAND = 0x001;
|
||||
/** Set to show the description of the command. */
|
||||
public static final int SHOW_DESCRIPTION = 0x002;
|
||||
/** 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. */
|
||||
public static final int SHOW_CHILDREN = 0x040;
|
||||
|
||||
/** Shortcut for setting all options apart from {@link HelpProvider#HIDE_COMMAND}. */
|
||||
public static final int ALL_OPTIONS = ~HIDE_COMMAND;
|
||||
/** Shortcut for setting all options. */
|
||||
public static final int ALL_OPTIONS = ~0;
|
||||
|
||||
private final PermissionsManager permissionsManager;
|
||||
private final HelpMessagesService helpMessagesService;
|
||||
private String helpHeader;
|
||||
/** int with bit flags set corresponding to the above constants for enabled sections. */
|
||||
private Integer enabledSections;
|
||||
|
||||
@Inject
|
||||
HelpProvider(PermissionsManager permissionsManager, HelpMessagesService helpMessagesService, Settings settings) {
|
||||
@ -65,27 +67,28 @@ public class HelpProvider implements SettingsDependent {
|
||||
}
|
||||
|
||||
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 ]==========");
|
||||
|
||||
CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription());
|
||||
List<String> labels = ImmutableList.copyOf(result.getLabels());
|
||||
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
|
||||
|
||||
if (!hasFlag(HIDE_COMMAND, options)) {
|
||||
lines.add(ChatColor.GOLD + "Command: " + CommandSyntaxHelper.getSyntax(command, correctLabels));
|
||||
if (hasFlag(SHOW_COMMAND, options)) {
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMAND) + ": "
|
||||
+ CommandSyntaxHelper.getSyntax(command, correctLabels));
|
||||
}
|
||||
if (hasFlag(SHOW_DESCRIPTION, options)) {
|
||||
String description = helpMessagesService.getMessage(SHORT_DESCRIPTION);
|
||||
if (!description.isEmpty()) {
|
||||
lines.add(ChatColor.GOLD + description + ": " + ChatColor.WHITE + command.getDescription());
|
||||
}
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(SHORT_DESCRIPTION) + ": "
|
||||
+ ChatColor.WHITE + command.getDescription());
|
||||
}
|
||||
if (hasFlag(SHOW_LONG_DESCRIPTION, options)) {
|
||||
String description = helpMessagesService.getMessage(DETAILED_DESCRIPTION);
|
||||
if (!description.isEmpty()) {
|
||||
lines.add(ChatColor.GOLD + description + ":");
|
||||
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
|
||||
}
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(DETAILED_DESCRIPTION) + ":");
|
||||
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
|
||||
}
|
||||
if (hasFlag(SHOW_ARGUMENTS, options)) {
|
||||
printArguments(command, lines);
|
||||
@ -120,18 +123,41 @@ public class HelpProvider implements SettingsDependent {
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
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) {
|
||||
if (command.getArguments().isEmpty()) {
|
||||
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();
|
||||
String optionalText = " (" + helpMessagesService.getMessage(HelpMessage.OPTIONAL) + ")";
|
||||
for (CommandArgumentDescription argument : command.getArguments()) {
|
||||
@ -150,12 +176,8 @@ public class HelpProvider implements SettingsDependent {
|
||||
if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) {
|
||||
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
|
||||
final String parentLabel = correctLabels.get(0);
|
||||
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) {
|
||||
PermissionNode permission = command.getPermission();
|
||||
String permissionsTitle = helpMessagesService.getMessage(HelpSection.PERMISSIONS);
|
||||
if (permission == null || permissionsTitle.isEmpty()) {
|
||||
if (permission == null) {
|
||||
return;
|
||||
}
|
||||
lines.add(ChatColor.GOLD + permissionsTitle + ":");
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.PERMISSIONS) + ":");
|
||||
|
||||
boolean hasPermission = permissionsManager.hasPermission(sender, permission);
|
||||
lines.add(String.format(" " + ChatColor.YELLOW + ChatColor.ITALIC + "%s" + ChatColor.GRAY + " (%s)",
|
||||
@ -215,7 +236,7 @@ public class HelpProvider implements SettingsDependent {
|
||||
return;
|
||||
}
|
||||
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMANDS) + ":");
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.CHILDREN) + ":");
|
||||
String parentCommandPath = String.join(" ", parentLabels);
|
||||
for (CommandDescription child : command.getChildren()) {
|
||||
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)
|
||||
@ -228,7 +249,7 @@ public class HelpProvider implements SettingsDependent {
|
||||
}
|
||||
|
||||
@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<String> correctLabels = new ArrayList<>();
|
||||
boolean foundIncorrectLabel = false;
|
||||
|
@ -5,19 +5,19 @@ package fr.xephi.authme.command.help;
|
||||
*/
|
||||
public enum HelpSection {
|
||||
|
||||
COMMAND("command"),
|
||||
|
||||
SHORT_DESCRIPTION("description.short"),
|
||||
|
||||
DETAILED_DESCRIPTION("description.detailed"),
|
||||
|
||||
USAGE("usage"),
|
||||
|
||||
ARGUMENTS("arguments"),
|
||||
|
||||
ALTERNATIVES("alternatives"),
|
||||
|
||||
PERMISSIONS("permissions"),
|
||||
|
||||
COMMANDS("commands");
|
||||
CHILDREN("children");
|
||||
|
||||
|
||||
private final String key;
|
||||
|
@ -9,13 +9,13 @@ common:
|
||||
opOnly: 'Nur OP''s'
|
||||
allowed: 'Allen erlaubt'
|
||||
section:
|
||||
command: 'Kommando'
|
||||
description.short: 'Beschreibung'
|
||||
description.detailed: 'Detaillierte Beschreibung'
|
||||
usage: 'Gebrauch'
|
||||
arguments: 'Argumente'
|
||||
permissions: 'Rechte'
|
||||
alternatives: 'Alternativen'
|
||||
commands: 'Kommandos'
|
||||
children: 'Kommandos'
|
||||
commands:
|
||||
authme.register:
|
||||
description: 'Registriert einen Benutzer'
|
||||
|
@ -9,13 +9,13 @@ common:
|
||||
opOnly: 'OP''s only'
|
||||
allowed: 'Everyone allowed'
|
||||
section:
|
||||
command: 'Command'
|
||||
description.short: 'Short description'
|
||||
description.detailed: 'Detailed description'
|
||||
arguments: 'Arguments'
|
||||
permissions: 'Permissions'
|
||||
alternatives: 'Alternatives'
|
||||
commands: 'Commands'
|
||||
usage: 'Usage'
|
||||
children: 'Commands'
|
||||
commands:
|
||||
authme.register:
|
||||
description: 'Register a player'
|
||||
|
@ -28,10 +28,10 @@ import java.util.Set;
|
||||
|
||||
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.HIDE_COMMAND;
|
||||
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_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_LONG_DESCRIPTION;
|
||||
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"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION);
|
||||
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_LONG_DESCRIPTION | SHOW_DESCRIPTION);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -103,7 +103,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -121,7 +121,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("email"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -137,7 +137,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ARGUMENTS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_ARGUMENTS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -154,7 +154,7 @@ public class HelpProviderTest {
|
||||
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(true);
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -176,7 +176,7 @@ public class HelpProviderTest {
|
||||
given(permissionsManager.hasPermission(sender, command.getPermission())).willReturn(false);
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -195,7 +195,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -211,7 +211,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("test"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_PERMISSIONS);
|
||||
helpProvider.outputHelp(sender, result, SHOW_PERMISSIONS);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -225,7 +225,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "reg"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ALTERNATIVES);
|
||||
helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -242,7 +242,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "login"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_ALTERNATIVES);
|
||||
helpProvider.outputHelp(sender, result, SHOW_ALTERNATIVES);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -256,12 +256,12 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_CHILDREN);
|
||||
helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
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(3)), containsString("/authme register: register cmd"));
|
||||
}
|
||||
@ -273,7 +273,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Collections.singletonList("authme"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, HIDE_COMMAND | SHOW_CHILDREN);
|
||||
helpProvider.outputHelp(sender, result, SHOW_CHILDREN);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
@ -284,7 +284,7 @@ public class HelpProviderTest {
|
||||
public void shouldHandleUnboundFoundCommandResult() {
|
||||
// given
|
||||
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
|
||||
helpProvider.outputHelp(sender, result, ALL_OPTIONS);
|
||||
@ -306,7 +306,7 @@ public class HelpProviderTest {
|
||||
FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "ragister"));
|
||||
|
||||
// when
|
||||
helpProvider.outputHelp(sender, result, 0);
|
||||
helpProvider.outputHelp(sender, result, SHOW_COMMAND);
|
||||
|
||||
// then
|
||||
List<String> lines = getLines(sender);
|
||||
|
Loading…
Reference in New Issue
Block a user