Search in sources :

Example 1 with CommandDescription

use of fr.xephi.authme.command.CommandDescription in project AuthMeReloaded by AuthMe.

the class HelpProvider method buildHelpOutput.

/**
     * Builds the help messages based on the provided arguments.
     *
     * @param sender the sender to evaluate permissions with
     * @param result the command result to create help for
     * @param options output options
     * @return the generated help messages
     */
private List<String> buildHelpOutput(CommandSender sender, FoundCommandResult result, int options) {
    if (result.getCommandDescription() == null) {
        return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
    }
    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;
    }
    String header = helpMessagesService.getMessage(HelpMessage.HEADER);
    if (!header.isEmpty()) {
        lines.add(ChatColor.GOLD + header);
    }
    CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription());
    List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, result.getLabels()));
    if (hasFlag(SHOW_COMMAND, options)) {
        lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMAND) + ": " + CommandUtils.buildSyntax(command, correctLabels));
    }
    if (hasFlag(SHOW_DESCRIPTION, options)) {
        lines.add(ChatColor.GOLD + helpMessagesService.getMessage(SHORT_DESCRIPTION) + ": " + ChatColor.WHITE + command.getDescription());
    }
    if (hasFlag(SHOW_LONG_DESCRIPTION, options)) {
        lines.add(ChatColor.GOLD + helpMessagesService.getMessage(DETAILED_DESCRIPTION) + ":");
        lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
    }
    if (hasFlag(SHOW_ARGUMENTS, options)) {
        addArgumentsInfo(command, lines);
    }
    if (hasFlag(SHOW_PERMISSIONS, options) && sender != null) {
        addPermissionsInfo(command, sender, lines);
    }
    if (hasFlag(SHOW_ALTERNATIVES, options)) {
        addAlternativesInfo(command, correctLabels, lines);
    }
    if (hasFlag(SHOW_CHILDREN, options)) {
        addChildrenInfo(command, correctLabels, lines);
    }
    return lines;
}
Also used : CommandDescription(fr.xephi.authme.command.CommandDescription) ArrayList(java.util.ArrayList)

Example 2 with CommandDescription

use of fr.xephi.authme.command.CommandDescription in project AuthMeReloaded by AuthMe.

the class HelpProvider method addChildrenInfo.

/**
     * Adds help info about the given command's child command into the provided list.
     *
     * @param command the command for which to generate info about its child commands
     * @param correctLabels the labels used to access the given command (sanitized)
     * @param lines the output collection to add the info to
     */
private void addChildrenInfo(CommandDescription command, List<String> correctLabels, List<String> lines) {
    if (command.getChildren().isEmpty()) {
        return;
    }
    lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.CHILDREN) + ":");
    String parentCommandPath = String.join(" ", correctLabels);
    for (CommandDescription child : command.getChildren()) {
        lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0) + ChatColor.GRAY + ChatColor.ITALIC + ": " + helpMessagesService.getDescription(child));
    }
}
Also used : CommandDescription(fr.xephi.authme.command.CommandDescription)

Example 3 with CommandDescription

use of fr.xephi.authme.command.CommandDescription in project AuthMeReloaded by AuthMe.

the class HelpMessagesService method buildLocalizedDescription.

/**
     * Creates a copy of the supplied command description with localized messages where present.
     *
     * @param command the command to build a localized version of
     * @return the localized description
     */
public CommandDescription buildLocalizedDescription(CommandDescription command) {
    final String path = getCommandPath(command);
    if (!messageFileHandler.hasSection(path)) {
        // Messages file does not have a section for this command - return the provided command
        return command;
    }
    CommandDescription.CommandBuilder builder = CommandDescription.builder().description(getText(path + DESCRIPTION_SUFFIX, command::getDescription)).detailedDescription(getText(path + DETAILED_DESCRIPTION_SUFFIX, command::getDetailedDescription)).executableCommand(command.getExecutableCommand()).parent(command.getParent()).labels(command.getLabels()).permission(command.getPermission());
    int i = 1;
    for (CommandArgumentDescription argument : command.getArguments()) {
        String argPath = path + ".arg" + i;
        String label = getText(argPath + ".label", argument::getName);
        String description = getText(argPath + ".description", argument::getDescription);
        builder.withArgument(label, description, argument.isOptional());
        ++i;
    }
    CommandDescription localCommand = builder.build();
    localCommand.getChildren().addAll(command.getChildren());
    return localCommand;
}
Also used : CommandDescription(fr.xephi.authme.command.CommandDescription) CommandArgumentDescription(fr.xephi.authme.command.CommandArgumentDescription)

Example 4 with CommandDescription

use of fr.xephi.authme.command.CommandDescription in project AuthMeReloaded by AuthMe.

the class HelpProviderTest method shouldReplaceIncorrectLabels.

@Test
public void shouldReplaceIncorrectLabels() {
    // given
    List<String> labels = Arrays.asList("authme", "wrong");
    CommandDescription command = getCommandWithLabel(commands, "authme", "register");
    // when
    List<String> result = HelpProvider.filterCorrectLabels(command, labels);
    // then
    assertThat(result, contains("authme", "register"));
}
Also used : CommandDescription(fr.xephi.authme.command.CommandDescription) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with CommandDescription

use of fr.xephi.authme.command.CommandDescription in project AuthMeReloaded by AuthMe.

the class HelpProviderTest method shouldShowCommandSyntaxWithCorrectLabels.

/**
     * Since command parts may be mapped to a command description with labels that don't completely correspond to it,
     * (e.g. suggest "register command" for /authme ragister), we need to check the labels and construct a correct list
     */
@Test
public void shouldShowCommandSyntaxWithCorrectLabels() {
    // given
    CommandDescription command = getCommandWithLabel(commands, "authme", "register");
    FoundCommandResult result = newFoundResult(command, Arrays.asList("authme", "ragister"));
    // when
    helpProvider.outputHelp(sender, result, SHOW_COMMAND);
    // then
    List<String> lines = getLines(sender);
    assertThat(lines, hasSize(2));
    assertThat(lines.get(0), containsString("Header"));
    assertThat(lines.get(1), containsString("Command: /authme register <password> <confirmation>"));
}
Also used : FoundCommandResult(fr.xephi.authme.command.FoundCommandResult) CommandDescription(fr.xephi.authme.command.CommandDescription) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

CommandDescription (fr.xephi.authme.command.CommandDescription)38 Test (org.junit.Test)30 Matchers.containsString (org.hamcrest.Matchers.containsString)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)22 FoundCommandResult (fr.xephi.authme.command.FoundCommandResult)20 CommandInitializer (fr.xephi.authme.command.CommandInitializer)4 CommandSender (org.bukkit.command.CommandSender)3 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)2 TestHelper (fr.xephi.authme.TestHelper)1 CommandArgumentDescription (fr.xephi.authme.command.CommandArgumentDescription)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 MemorySection (org.bukkit.configuration.MemorySection)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 Matchers.contains (org.hamcrest.Matchers.contains)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1