Improves the tab completer to be consistent with PEX and LuckPerms

This commit is contained in:
Kristian Knarvik 2022-01-15 19:02:26 +01:00
parent 6f3829f44f
commit 65a8ccdb88
7 changed files with 46 additions and 38 deletions

View File

@ -57,10 +57,9 @@ public class CreateCommand implements CommandExecutor {
*/ */
private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] args) { private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] args) {
String name = args[0]; String name = args[0];
String[] permissions = args[1].split(","); String[] permissions = args[1].replaceAll("\\?", " ").split(",");
for (String permission : permissions) { for (String permission : permissions) {
if (permission.contains(":")) { if (permission.contains(":")) {
//TODO: Might need to account for world names with spaces in the name
String world = permission.split(":")[0]; String world = permission.split(":")[0];
if (!world.equalsIgnoreCase("all") && Bukkit.getWorld(world) == null) { if (!world.equalsIgnoreCase("all") && Bukkit.getWorld(world) == null) {
sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage( sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage(

View File

@ -5,13 +5,13 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner;
/** /**
* The tab completer for the create command * The tab completer for the create command
@ -121,38 +121,31 @@ public class CreateTabCompleter implements TabCompleter {
private static void loadAvailablePermissions() { private static void loadAvailablePermissions() {
plugins = new ArrayList<>(); plugins = new ArrayList<>();
permissions = new HashMap<>(); permissions = new HashMap<>();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
for (Permission permission : plugin.getDescription().getPermissions()) { for (Permission permission : Bukkit.getPluginManager().getPermissions()) {
loadPermission(permission); loadPermission(permission.getName());
}
} }
} }
/** /**
* Loads a given permission into the proper lists and maps * Loads a given permission into the proper lists and maps
* *
* @param permission <p>The permission to load</p> * @param permissionName <p>The permission to load</p>
*/ */
private static void loadPermission(Permission permission) { private static void loadPermission(String permissionName) {
String permissionName = permission.getName();
String[] permissionParts = permissionName.split("\\."); String[] permissionParts = permissionName.split("\\.");
for (int i = 0; i < permissionParts.length; i++) { if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) {
if (i == 0) { plugins.add(permissionParts[0]);
plugins.add(permissionParts[0]); } else if (permissionParts.length > 1) {
} else { StringJoiner pathJoiner = new StringJoiner(".");
StringBuilder pathBuilder = new StringBuilder(); for (int j = 0; j < permissionParts.length - 1; j++) {
for (int j = 0; j < i; j++) { pathJoiner.add(permissionParts[j]);
pathBuilder.append(".");
pathBuilder.append(permissionParts[j]);
}
String path = pathBuilder.substring(1);
List<String> permissionList = permissions.get(path);
if (permissionList == null) {
permissionList = new ArrayList<>();
permissions.put(pathBuilder.substring(1), permissionList);
}
permissionList.add(permissionName);
} }
String path = pathJoiner.toString();
List<String> permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>());
permissionList.add(permissionName);
loadPermission(path);
} }
} }

View File

@ -21,6 +21,7 @@ public class ReloadCommand implements CommandExecutor {
SignManager.loadSigns(); SignManager.loadSigns();
PermissionManager.loadTemporaryPermissions(); PermissionManager.loadTemporaryPermissions();
PermissionSigns.getInstance().reloadConfig(); PermissionSigns.getInstance().reloadConfig();
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.RELOAD_SUCCESSFUL));
} else { } else {
sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED));
} }

View File

@ -16,7 +16,7 @@ public class SignCreationRequest implements Comparable<SignCreationRequest> {
* *
* @param permissionSign <p>The sign which is about to be created</p> * @param permissionSign <p>The sign which is about to be created</p>
* @param player <p>The player starting to create the permission sign</p> * @param player <p>The player starting to create the permission sign</p>
* @param initiationTime <p></p> * @param initiationTime <p>The time the request was initiated</p>
*/ */
public SignCreationRequest(PermissionSign permissionSign, Player player, long initiationTime) { public SignCreationRequest(PermissionSign permissionSign, Player player, long initiationTime) {
this.permissionSign = permissionSign; this.permissionSign = permissionSign;

View File

@ -90,6 +90,11 @@ public enum TranslatableMessage {
*/ */
PERMISSIONS_GRANTED, PERMISSIONS_GRANTED,
/**
* The message to display when a permission sign successfully grants permissions to the player permanently
*/
PERMISSIONS_GRANTED_PERMANENTLY,
/** /**
* The message to display if a player has specified an invalid world to set a permission for * The message to display if a player has specified an invalid world to set a permission for
*/ */
@ -103,6 +108,11 @@ public enum TranslatableMessage {
/** /**
* The message to display when a permission sign has been successfully created * The message to display when a permission sign has been successfully created
*/ */
PERMISSION_SIGN_CREATED PERMISSION_SIGN_CREATED,
/**
* The message to display when a reload action has been successfully performed
*/
RELOAD_SUCCESSFUL
} }

View File

@ -6,6 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -56,7 +57,7 @@ public final class Translator {
BufferedReader reader; BufferedReader reader;
try { try {
reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml");
} catch (FileNotFoundException e) { } catch (FileNotFoundException | UnsupportedEncodingException e) {
PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages");
return null; return null;
} }

View File

@ -19,6 +19,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import java.util.Arrays; import java.util.Arrays;
import java.util.StringJoiner;
/** /**
* A listener for all events related to signs * A listener for all events related to signs
@ -113,7 +114,7 @@ public class SignListener implements Listener {
*/ */
private void performPermissionTransaction(Player player, PermissionSign permissionSign) { private void performPermissionTransaction(Player player, PermissionSign permissionSign) {
EconomyManager.withdraw(player, permissionSign.getCost()); EconomyManager.withdraw(player, permissionSign.getCost());
StringBuilder permissionsBuilder = new StringBuilder(); StringJoiner permissionsJoiner = new StringJoiner(", ");
for (String permissionNode : permissionSign.getPermissionNodes()) { for (String permissionNode : permissionSign.getPermissionNodes()) {
//Only grant the permission if not already owned //Only grant the permission if not already owned
boolean hasPermission; boolean hasPermission;
@ -124,8 +125,7 @@ public class SignListener implements Listener {
hasPermission = player.hasPermission(permissionNode); hasPermission = player.hasPermission(permissionNode);
} }
if (!hasPermission) { if (!hasPermission) {
permissionsBuilder.append(permissionNode); permissionsJoiner.add(permissionNode);
permissionsBuilder.append(", ");
if (permissionSign.getDuration() == 0) { if (permissionSign.getDuration() == 0) {
PermissionManager.addPermission(player, permissionNode); PermissionManager.addPermission(player, permissionNode);
} else { } else {
@ -134,12 +134,16 @@ public class SignListener implements Listener {
} }
} }
String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED); String grantedPermissions = permissionsJoiner.toString();
String grantedPermissions = permissionsBuilder.toString(); if (permissionSign.getDuration() > 0) {
grantedPermissions = grantedPermissions.substring(0, grantedPermissions.length() - 2); String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED);
String timeUnit = Translator.getTranslatedMessage(TranslatableMessage.SIGN_TIME_UNIT); String timeUnit = Translator.getTranslatedMessage(TranslatableMessage.SIGN_TIME_UNIT);
player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"}, player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"},
new String[]{grantedPermissions, permissionSign.getDuration() + " " + timeUnit})); new String[]{grantedPermissions, permissionSign.getDuration() + " " + timeUnit}));
} else {
player.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedInfoMessage(
TranslatableMessage.PERMISSIONS_GRANTED_PERMANENTLY), "{permissions}", grantedPermissions));
}
} }
/** /**