Search in sources :

Example 1 with DefaultPermission

use of fr.xephi.authme.permission.DefaultPermission in project AuthMeReloaded by AuthMe.

the class HelpProvider method addPermissionsInfo.

/**
 * Adds help info about the given command's permissions into the provided list.
 *
 * @param command the command to generate permissions info for
 * @param sender the command sender, used to evaluate permissions
 * @param lines the output collection to add the info to
 */
private void addPermissionsInfo(CommandDescription command, CommandSender sender, List<String> lines) {
    PermissionNode permission = command.getPermission();
    if (permission == null) {
        return;
    }
    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)", permission.getNode(), getLocalPermissionText(hasPermission)));
    // Addendum to the line to specify whether the sender has permission or not when default is OP_ONLY
    final DefaultPermission defaultPermission = permission.getDefaultPermission();
    String addendum = "";
    if (DefaultPermission.OP_ONLY.equals(defaultPermission)) {
        addendum = " (" + getLocalPermissionText(defaultPermission.evaluate(sender)) + ")";
    }
    lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpMessage.DEFAULT) + ": " + ChatColor.GRAY + ChatColor.ITALIC + helpMessagesService.getMessage(defaultPermission) + addendum);
    // Evaluate if the sender has permission to the command
    ChatColor permissionColor;
    String permissionText;
    if (permissionsManager.hasPermission(sender, command.getPermission())) {
        permissionColor = ChatColor.GREEN;
        permissionText = getLocalPermissionText(true);
    } else {
        permissionColor = ChatColor.DARK_RED;
        permissionText = getLocalPermissionText(false);
    }
    lines.add(String.format(ChatColor.GOLD + " %s: %s" + ChatColor.ITALIC + "%s", helpMessagesService.getMessage(HelpMessage.RESULT), permissionColor, permissionText));
}
Also used : PermissionNode(fr.xephi.authme.permission.PermissionNode) DefaultPermission(fr.xephi.authme.permission.DefaultPermission) ChatColor(org.bukkit.ChatColor)

Example 2 with DefaultPermission

use of fr.xephi.authme.permission.DefaultPermission in project AuthMeReloaded by AuthMe.

the class HelpProviderTest method setDefaultHelpMessages.

private static void setDefaultHelpMessages(HelpMessagesService helpMessagesService) {
    given(helpMessagesService.buildLocalizedDescription(any(CommandDescription.class))).willAnswer(new ReturnsArgumentAt(0));
    for (HelpMessage key : HelpMessage.values()) {
        String text = key.name().replace("_", " ").toLowerCase();
        given(helpMessagesService.getMessage(key)).willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
    }
    for (DefaultPermission permission : DefaultPermission.values()) {
        String text = permission.name().replace("_", " ").toLowerCase();
        given(helpMessagesService.getMessage(permission)).willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
    }
    for (HelpSection section : HelpSection.values()) {
        String text = section.name().replace("_", " ").toLowerCase();
        given(helpMessagesService.getMessage(section)).willReturn(text.substring(0, 1).toUpperCase() + text.substring(1));
    }
}
Also used : CommandDescription(fr.xephi.authme.command.CommandDescription) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultPermission(fr.xephi.authme.permission.DefaultPermission) ReturnsArgumentAt(org.mockito.internal.stubbing.answers.ReturnsArgumentAt)

Example 3 with DefaultPermission

use of fr.xephi.authme.permission.DefaultPermission in project AuthMeReloaded by AuthMe.

the class HelpTranslationGenerator method generateHelpMessageEntries.

/**
 * Generates entries for a complete help text file.
 *
 * @return help text entries to save
 */
private Map<String, Object> generateHelpMessageEntries() {
    Map<String, Object> messageEntries = new LinkedHashMap<>(HelpMessage.values().length);
    for (HelpMessage message : HelpMessage.values()) {
        messageEntries.put(message.getEntryKey(), helpMessagesService.getMessage(message));
    }
    Map<String, String> defaultPermissions = new LinkedHashMap<>();
    for (DefaultPermission defaultPermission : DefaultPermission.values()) {
        defaultPermissions.put(HelpMessagesService.getDefaultPermissionsSubPath(defaultPermission), helpMessagesService.getMessage(defaultPermission));
    }
    messageEntries.put("defaultPermissions", defaultPermissions);
    Map<String, String> sectionEntries = new LinkedHashMap<>(HelpSection.values().length);
    for (HelpSection section : HelpSection.values()) {
        sectionEntries.put(section.getEntryKey(), helpMessagesService.getMessage(section));
    }
    Map<String, Object> commandEntries = new LinkedHashMap<>();
    for (CommandDescription command : commandInitializer.getCommands()) {
        generateCommandEntries(command, commandEntries);
    }
    return ImmutableMap.of("common", messageEntries, "section", sectionEntries, "commands", commandEntries);
}
Also used : HelpSection(fr.xephi.authme.command.help.HelpSection) CommandDescription(fr.xephi.authme.command.CommandDescription) HelpMessage(fr.xephi.authme.command.help.HelpMessage) DefaultPermission(fr.xephi.authme.permission.DefaultPermission) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

DefaultPermission (fr.xephi.authme.permission.DefaultPermission)3 CommandDescription (fr.xephi.authme.command.CommandDescription)2 HelpMessage (fr.xephi.authme.command.help.HelpMessage)1 HelpSection (fr.xephi.authme.command.help.HelpSection)1 PermissionNode (fr.xephi.authme.permission.PermissionNode)1 LinkedHashMap (java.util.LinkedHashMap)1 ChatColor (org.bukkit.ChatColor)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ReturnsArgumentAt (org.mockito.internal.stubbing.answers.ReturnsArgumentAt)1