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) {
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(

View File

@ -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 <p>The permission to load</p>
* @param permissionName <p>The permission to load</p>
*/
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<String> 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<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();
PermissionManager.loadTemporaryPermissions();
PermissionSigns.getInstance().reloadConfig();
sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.RELOAD_SUCCESSFUL));
} else {
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 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) {
this.permissionSign = permissionSign;

View File

@ -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
}

View File

@ -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;
}

View File

@ -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));
}
}
/**