From bb75d50c065b01b0b8ef29e9a5335c42b81f8c04 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 18 Oct 2016 17:59:23 +0200 Subject: [PATCH] Set help command to short description and alternatives - As discussed in https://github.com/AuthMe/AuthMeReloaded/pull/169 --- .../command/executable/HelpCommand.java | 9 +++++-- .../authme/command/help/HelpProvider.java | 27 +++++++++++++------ .../command/executable/HelpCommandTest.java | 11 +++++--- .../authme/command/help/HelpProviderTest.java | 18 +++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java b/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java index 2076bcbc..0f7d90d2 100644 --- a/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/HelpCommand.java @@ -14,6 +14,11 @@ import java.util.List; import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND; import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; +import static fr.xephi.authme.command.help.HelpProvider.ALL_OPTIONS; +import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES; +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; public class HelpCommand implements ExecutableCommand { @@ -46,9 +51,9 @@ public class HelpCommand implements ExecutableCommand { int mappedCommandLevel = result.getCommandDescription().getLabelCount(); if (mappedCommandLevel == 1) { - helpProvider.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN); + helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES); } else { - helpProvider.outputHelp(sender, result, HelpProvider.ALL_OPTIONS); + helpProvider.outputHelp(sender, result, ALL_OPTIONS); } } diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 8fc94161..dfcbe554 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -15,11 +15,12 @@ import org.bukkit.command.CommandSender; import javax.inject.Inject; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.function.Function; import static fr.xephi.authme.command.help.HelpSection.DETAILED_DESCRIPTION; import static fr.xephi.authme.command.help.HelpSection.SHORT_DESCRIPTION; -import static java.util.Arrays.asList; import static java.util.Collections.singletonList; /** @@ -171,19 +172,29 @@ public class HelpProvider implements Reloadable { } private void printAlternatives(CommandDescription command, List correctLabels, List lines) { - if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) { + if (command.getLabels().size() <= 1) { return; } lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.ALTERNATIVES) + ":"); - // Get the label used - final String parentLabel = correctLabels.get(0); - final String childLabel = correctLabels.get(1); + + // Label with which the command was called -> don't show it as an alternative + final String usedLabel; + // Takes alternative label and constructs list of labels, e.g. "reg" -> [authme, reg] + final Function> commandLabelsFn; + + if (correctLabels.size() == 1) { + usedLabel = correctLabels.get(0); + commandLabelsFn = label -> singletonList(label); + } else { + usedLabel = correctLabels.get(1); + commandLabelsFn = label -> Arrays.asList(correctLabels.get(0), label); + } // Create a list of alternatives - for (String entry : command.getLabels()) { - if (!entry.equalsIgnoreCase(childLabel)) { - lines.add(" " + CommandSyntaxHelper.getSyntax(command, asList(parentLabel, entry))); + for (String label : command.getLabels()) { + if (!label.equalsIgnoreCase(usedLabel)) { + lines.add(" " + CommandSyntaxHelper.getSyntax(command, commandLabelsFn.apply(label))); } } } diff --git a/src/test/java/fr/xephi/authme/command/executable/HelpCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/HelpCommandTest.java index 143ae26b..3c57e214 100644 --- a/src/test/java/fr/xephi/authme/command/executable/HelpCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/HelpCommandTest.java @@ -20,6 +20,10 @@ import static fr.xephi.authme.command.FoundResultStatus.INCORRECT_ARGUMENTS; import static fr.xephi.authme.command.FoundResultStatus.MISSING_BASE_COMMAND; import static fr.xephi.authme.command.FoundResultStatus.SUCCESS; import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; +import static fr.xephi.authme.command.help.HelpProvider.SHOW_ALTERNATIVES; +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 java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.hamcrest.CoreMatchers.containsString; @@ -108,7 +112,7 @@ public class HelpCommandTest { CommandDescription commandDescription = mock(CommandDescription.class); given(commandDescription.getLabelCount()).willReturn(1); FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, singletonList("authme"), - Collections.emptyList(), 0.0, SUCCESS); + Collections.emptyList(), 0.0, SUCCESS); given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult); // when @@ -116,7 +120,8 @@ public class HelpCommandTest { // then verify(sender, never()).sendMessage(anyString()); - verify(helpProvider).outputHelp(sender, foundCommandResult, HelpProvider.SHOW_CHILDREN); + verify(helpProvider).outputHelp(sender, foundCommandResult, + SHOW_DESCRIPTION | SHOW_COMMAND | SHOW_CHILDREN | SHOW_ALTERNATIVES); } @Test @@ -126,7 +131,7 @@ public class HelpCommandTest { CommandDescription commandDescription = mock(CommandDescription.class); given(commandDescription.getLabelCount()).willReturn(2); FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, asList("authme", "getpos"), - Collections.emptyList(), 0.0, INCORRECT_ARGUMENTS); + Collections.emptyList(), 0.0, INCORRECT_ARGUMENTS); given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult); // when diff --git a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java index 5a0f06f2..0d3983ce 100644 --- a/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java +++ b/src/test/java/fr/xephi/authme/command/help/HelpProviderTest.java @@ -395,6 +395,24 @@ public class HelpProviderTest { assertThat(lines.get(0), equalTo("Command: /authme register ")); } + @Test + public void shouldShowAlternativesForRootCommand() { + // given + CommandDescription command = getCommandWithLabel(commands, "unregister"); + FoundCommandResult result = newFoundResult(command, Collections.singletonList("unreg")); + + // when + helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_ALTERNATIVES); + + // then + List lines = getLines(sender); + assertThat(lines, hasSize(4)); + assertThat(lines.get(0), equalTo("Header")); + assertThat(lines.get(1), equalTo("Command: /unreg ")); + assertThat(lines.get(2), equalTo("Alternatives:")); + assertThat(lines.get(3), equalTo(" /unregister ")); + } + /** * Generate an instance of {@link FoundCommandResult} with the given command and labels. All other fields aren't * retrieved by {@link HelpProvider} and so are initialized to default values for the tests.