Set help command to short description and alternatives

- As discussed in https://github.com/AuthMe/AuthMeReloaded/pull/169
This commit is contained in:
ljacqu 2016-10-18 17:59:23 +02:00
parent dc8d0b9b6b
commit bb75d50c06
4 changed files with 52 additions and 13 deletions

View File

@ -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.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; 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 { public class HelpCommand implements ExecutableCommand {
@ -46,9 +51,9 @@ public class HelpCommand implements ExecutableCommand {
int mappedCommandLevel = result.getCommandDescription().getLabelCount(); int mappedCommandLevel = result.getCommandDescription().getLabelCount();
if (mappedCommandLevel == 1) { if (mappedCommandLevel == 1) {
helpProvider.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN); helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES);
} else { } else {
helpProvider.outputHelp(sender, result, HelpProvider.ALL_OPTIONS); helpProvider.outputHelp(sender, result, ALL_OPTIONS);
} }
} }

View File

@ -15,11 +15,12 @@ import org.bukkit.command.CommandSender;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; 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.DETAILED_DESCRIPTION;
import static fr.xephi.authme.command.help.HelpSection.SHORT_DESCRIPTION; import static fr.xephi.authme.command.help.HelpSection.SHORT_DESCRIPTION;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
/** /**
@ -171,19 +172,29 @@ public class HelpProvider implements Reloadable {
} }
private void printAlternatives(CommandDescription command, List<String> correctLabels, List<String> lines) { private void printAlternatives(CommandDescription command, List<String> correctLabels, List<String> lines) {
if (command.getLabels().size() <= 1 || correctLabels.size() <= 1) { if (command.getLabels().size() <= 1) {
return; return;
} }
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.ALTERNATIVES) + ":"); lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.ALTERNATIVES) + ":");
// Get the label used
final String parentLabel = correctLabels.get(0); // Label with which the command was called -> don't show it as an alternative
final String childLabel = correctLabels.get(1); final String usedLabel;
// Takes alternative label and constructs list of labels, e.g. "reg" -> [authme, reg]
final Function<String, List<String>> 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 // Create a list of alternatives
for (String entry : command.getLabels()) { for (String label : command.getLabels()) {
if (!entry.equalsIgnoreCase(childLabel)) { if (!label.equalsIgnoreCase(usedLabel)) {
lines.add(" " + CommandSyntaxHelper.getSyntax(command, asList(parentLabel, entry))); lines.add(" " + CommandSyntaxHelper.getSyntax(command, commandLabelsFn.apply(label)));
} }
} }
} }

View File

@ -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.MISSING_BASE_COMMAND;
import static fr.xephi.authme.command.FoundResultStatus.SUCCESS; import static fr.xephi.authme.command.FoundResultStatus.SUCCESS;
import static fr.xephi.authme.command.FoundResultStatus.UNKNOWN_LABEL; 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.Arrays.asList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
@ -108,7 +112,7 @@ public class HelpCommandTest {
CommandDescription commandDescription = mock(CommandDescription.class); CommandDescription commandDescription = mock(CommandDescription.class);
given(commandDescription.getLabelCount()).willReturn(1); given(commandDescription.getLabelCount()).willReturn(1);
FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, singletonList("authme"), FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, singletonList("authme"),
Collections.<String>emptyList(), 0.0, SUCCESS); Collections.emptyList(), 0.0, SUCCESS);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult); given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when // when
@ -116,7 +120,8 @@ public class HelpCommandTest {
// then // then
verify(sender, never()).sendMessage(anyString()); 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 @Test
@ -126,7 +131,7 @@ public class HelpCommandTest {
CommandDescription commandDescription = mock(CommandDescription.class); CommandDescription commandDescription = mock(CommandDescription.class);
given(commandDescription.getLabelCount()).willReturn(2); given(commandDescription.getLabelCount()).willReturn(2);
FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, asList("authme", "getpos"), FoundCommandResult foundCommandResult = new FoundCommandResult(commandDescription, asList("authme", "getpos"),
Collections.<String>emptyList(), 0.0, INCORRECT_ARGUMENTS); Collections.emptyList(), 0.0, INCORRECT_ARGUMENTS);
given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult); given(commandMapper.mapPartsToCommand(sender, arguments)).willReturn(foundCommandResult);
// when // when

View File

@ -395,6 +395,24 @@ public class HelpProviderTest {
assertThat(lines.get(0), equalTo("Command: /authme register <password> <confirmation>")); assertThat(lines.get(0), equalTo("Command: /authme register <password> <confirmation>"));
} }
@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<String> lines = getLines(sender);
assertThat(lines, hasSize(4));
assertThat(lines.get(0), equalTo("Header"));
assertThat(lines.get(1), equalTo("Command: /unreg <player>"));
assertThat(lines.get(2), equalTo("Alternatives:"));
assertThat(lines.get(3), equalTo(" /unregister <player>"));
}
/** /**
* Generate an instance of {@link FoundCommandResult} with the given command and labels. All other fields aren't * 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. * retrieved by {@link HelpProvider} and so are initialized to default values for the tests.