From 65a8ccdb88dbe6fa8b06bcdf41fc8c5c0a08e433 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 15 Jan 2022 19:02:26 +0100 Subject: [PATCH] Improves the tab completer to be consistent with PEX and LuckPerms --- .../command/CreateCommand.java | 3 +- .../command/CreateTabCompleter.java | 41 ++++++++----------- .../command/ReloadCommand.java | 1 + .../container/SignCreationRequest.java | 2 +- .../formatting/TranslatableMessage.java | 12 +++++- .../formatting/Translator.java | 3 +- .../listener/SignListener.java | 22 ++++++---- 7 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java index 48cf117..58c8740 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java @@ -57,10 +57,9 @@ public class CreateCommand implements CommandExecutor { */ private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] args) { String name = args[0]; - String[] permissions = args[1].split(","); + String[] permissions = args[1].replaceAll("\\?", " ").split(","); for (String permission : permissions) { if (permission.contains(":")) { - //TODO: Might need to account for world names with spaces in the name String world = permission.split(":")[0]; if (!world.equalsIgnoreCase("all") && Bukkit.getWorld(world) == null) { sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage( diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CreateTabCompleter.java b/src/main/java/net/knarcraft/permissionsigns/command/CreateTabCompleter.java index e2e13ee..06f6a9e 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CreateTabCompleter.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CreateTabCompleter.java @@ -5,13 +5,13 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.permissions.Permission; -import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.StringJoiner; /** * The tab completer for the create command @@ -121,38 +121,31 @@ public class CreateTabCompleter implements TabCompleter { private static void loadAvailablePermissions() { plugins = new ArrayList<>(); permissions = new HashMap<>(); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - for (Permission permission : plugin.getDescription().getPermissions()) { - loadPermission(permission); - } + + for (Permission permission : Bukkit.getPluginManager().getPermissions()) { + loadPermission(permission.getName()); } } /** * Loads a given permission into the proper lists and maps * - * @param permission

The permission to load

+ * @param permissionName

The permission to load

*/ - private static void loadPermission(Permission permission) { - String permissionName = permission.getName(); + private static void loadPermission(String permissionName) { String[] permissionParts = permissionName.split("\\."); - for (int i = 0; i < permissionParts.length; i++) { - if (i == 0) { - plugins.add(permissionParts[0]); - } else { - StringBuilder pathBuilder = new StringBuilder(); - for (int j = 0; j < i; j++) { - pathBuilder.append("."); - pathBuilder.append(permissionParts[j]); - } - String path = pathBuilder.substring(1); - List permissionList = permissions.get(path); - if (permissionList == null) { - permissionList = new ArrayList<>(); - permissions.put(pathBuilder.substring(1), permissionList); - } - permissionList.add(permissionName); + if (permissionParts.length == 1 && !plugins.contains(permissionParts[0])) { + plugins.add(permissionParts[0]); + } else if (permissionParts.length > 1) { + StringJoiner pathJoiner = new StringJoiner("."); + for (int j = 0; j < permissionParts.length - 1; j++) { + pathJoiner.add(permissionParts[j]); } + String path = pathJoiner.toString(); + List permissionList = permissions.computeIfAbsent(path, k -> new ArrayList<>()); + permissionList.add(permissionName); + + loadPermission(path); } } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java index baf0461..2c197a3 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java @@ -21,6 +21,7 @@ public class ReloadCommand implements CommandExecutor { SignManager.loadSigns(); PermissionManager.loadTemporaryPermissions(); PermissionSigns.getInstance().reloadConfig(); + sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.RELOAD_SUCCESSFUL)); } else { sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); } diff --git a/src/main/java/net/knarcraft/permissionsigns/container/SignCreationRequest.java b/src/main/java/net/knarcraft/permissionsigns/container/SignCreationRequest.java index 9cb6025..be225ca 100644 --- a/src/main/java/net/knarcraft/permissionsigns/container/SignCreationRequest.java +++ b/src/main/java/net/knarcraft/permissionsigns/container/SignCreationRequest.java @@ -16,7 +16,7 @@ public class SignCreationRequest implements Comparable { * * @param permissionSign

The sign which is about to be created

* @param player

The player starting to create the permission sign

- * @param initiationTime

+ * @param initiationTime

The time the request was initiated

*/ public SignCreationRequest(PermissionSign permissionSign, Player player, long initiationTime) { this.permissionSign = permissionSign; diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java index 19c4228..16a0134 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java @@ -90,6 +90,11 @@ public enum TranslatableMessage { */ 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 */ @@ -103,6 +108,11 @@ public enum TranslatableMessage { /** * 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 } diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java index 6da4a9d..5565382 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java @@ -6,6 +6,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.BufferedReader; import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; @@ -56,7 +57,7 @@ public final class Translator { BufferedReader reader; try { reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); - } catch (FileNotFoundException e) { + } catch (FileNotFoundException | UnsupportedEncodingException e) { PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); return null; } diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java index b555627..9dbf814 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java @@ -19,6 +19,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; import java.util.Arrays; +import java.util.StringJoiner; /** * A listener for all events related to signs @@ -113,7 +114,7 @@ public class SignListener implements Listener { */ private void performPermissionTransaction(Player player, PermissionSign permissionSign) { EconomyManager.withdraw(player, permissionSign.getCost()); - StringBuilder permissionsBuilder = new StringBuilder(); + StringJoiner permissionsJoiner = new StringJoiner(", "); for (String permissionNode : permissionSign.getPermissionNodes()) { //Only grant the permission if not already owned boolean hasPermission; @@ -124,8 +125,7 @@ public class SignListener implements Listener { hasPermission = player.hasPermission(permissionNode); } if (!hasPermission) { - permissionsBuilder.append(permissionNode); - permissionsBuilder.append(", "); + permissionsJoiner.add(permissionNode); if (permissionSign.getDuration() == 0) { PermissionManager.addPermission(player, permissionNode); } else { @@ -134,12 +134,16 @@ public class SignListener implements Listener { } } - String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED); - String grantedPermissions = permissionsBuilder.toString(); - grantedPermissions = grantedPermissions.substring(0, grantedPermissions.length() - 2); - String timeUnit = Translator.getTranslatedMessage(TranslatableMessage.SIGN_TIME_UNIT); - player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"}, - new String[]{grantedPermissions, permissionSign.getDuration() + " " + timeUnit})); + String grantedPermissions = permissionsJoiner.toString(); + if (permissionSign.getDuration() > 0) { + String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED); + String timeUnit = Translator.getTranslatedMessage(TranslatableMessage.SIGN_TIME_UNIT); + player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"}, + new String[]{grantedPermissions, permissionSign.getDuration() + " " + timeUnit})); + } else { + player.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedInfoMessage( + TranslatableMessage.PERMISSIONS_GRANTED_PERMANENTLY), "{permissions}", grantedPermissions)); + } } /**