From 5c2831bbb7f31fed964ac0aefaba62c2cbce4468 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 14 Jan 2022 11:45:02 +0100 Subject: [PATCH] Improves output formatting and adds some missing messages --- .../command/CancelCommand.java | 6 +- .../command/CreateCommand.java | 15 +++-- .../command/ReloadCommand.java | 3 +- .../formatting/StringFormatter.java | 47 ++++++++++++++++ .../formatting/TranslatableMessage.java | 32 ++++++++++- .../listener/SignListener.java | 56 +++++++++++++++---- src/main/resources/strings.yml | 8 ++- 7 files changed, 141 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java index 1296635..22126d6 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java @@ -3,7 +3,6 @@ package net.knarcraft.permissionsigns.command; import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; -import net.knarcraft.permissionsigns.formatting.Translator; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -20,11 +19,12 @@ public class CancelCommand implements CommandExecutor { if (sender instanceof Player player) { if (sender.hasPermission("permissionsigns.admin.create")) { PermissionSigns.cancelSignCreationRequest(player.getUniqueId()); + sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CANCELLED)); } else { - sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED))); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); } } else { - sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PLAYER_ONLY))); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)); } return true; } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java index 0d5f38b..3e63bd2 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java @@ -4,7 +4,6 @@ import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; -import net.knarcraft.permissionsigns.formatting.Translator; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -23,15 +22,15 @@ public class CreateCommand implements CommandExecutor { // /ps create to create a new permission-sign //Name and permission(s) required, but duration and cost optional if (!(sender instanceof Player)) { - sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)); return false; } if (!sender.hasPermission("permissionsigns.admin.create")) { - sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED))); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); return false; } if (args.length < 2) { - sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.MISSING_CREATION_INFO)); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.MISSING_CREATION_INFO)); return false; } @@ -40,8 +39,8 @@ public class CreateCommand implements CommandExecutor { return false; } PermissionSigns.addSignCreationRequest((Player) sender, newSign); - //TODO: Tell the user that the creation request has been created and that they need to right-click an empty - // sign within 60 seconds to register the permissions sign + + sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CREATED)); return true; } @@ -60,13 +59,13 @@ public class CreateCommand implements CommandExecutor { try { cost = Double.parseDouble(args[2]); } catch (NumberFormatException exception) { - sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.COST_INVALID_NUMBER)); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COST_INVALID_NUMBER)); return null; } try { duration = Integer.parseInt(args[3]); } catch (NumberFormatException exception) { - sender.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.DURATION_INVALID_NUMBER)); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.DURATION_INVALID_NUMBER)); return null; } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java index 1dcfa7d..34c677d 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java @@ -2,7 +2,6 @@ package net.knarcraft.permissionsigns.command; import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; -import net.knarcraft.permissionsigns.formatting.Translator; import net.knarcraft.permissionsigns.manager.SignManager; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,7 +19,7 @@ public class ReloadCommand implements CommandExecutor { //TODO: Perform a reload from disk SignManager.loadSigns(); } else { - sender.sendMessage(StringFormatter.formatErrorMessage(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED))); + sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); } return true; } diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java b/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java index f9f6e48..734b632 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java @@ -7,6 +7,53 @@ import org.bukkit.ChatColor; */ public class StringFormatter { + /** + * Replaces a placeholder in a string + * + * @param input

The input string to replace in

+ * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement value

+ * @return

The input string with all placeholder instances replaced

+ */ + public static String replacePlaceholder(String input, String placeholder, String replacement) { + return input.replaceAll(placeholder, replacement); + } + + /** + * Replaces placeholders in a string + * + * @param input

The input string to replace in

+ * @param placeholders

The placeholders to replace

+ * @param replacements

The replacement values

+ * @return

The input string with all placeholder instances replaced

+ */ + public static String replacePlaceholders(String input, String[] placeholders, String[] replacements) { + for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) { + input = replacePlaceholder(input, placeholders[i], replacements[i]); + } + return input; + } + + /** + * Gets a translated and formatted info message + * + * @param translatableMessage

The translatable message to translate and format

+ * @return

The translated and formatted message

+ */ + public static String getTranslatedInfoMessage(TranslatableMessage translatableMessage) { + return formatInfoMessage(Translator.getTranslatedMessage(translatableMessage)); + } + + /** + * Gets a translated and formatted error message + * + * @param translatableMessage

The translatable message to translate and format

+ * @return

The translated and formatted message

+ */ + public static String getTranslatedErrorMessage(TranslatableMessage translatableMessage) { + return formatErrorMessage(Translator.getTranslatedMessage(translatableMessage)); + } + /** * Formats an information message by adding the prefix and text color * diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java index 0a1983f..3bbff5e 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java @@ -63,6 +63,36 @@ public enum TranslatableMessage { /** * The message to be displayed when a player successfully destroys a permissions sign */ - PERMISSION_SIGN_REMOVED + PERMISSION_SIGN_REMOVED, + + /** + * The message displayed when a player has successfully used the /ps create command + */ + CREATION_REQUEST_CREATED, + + /** + * The message displayed when a creation request is manually cancelled + */ + CREATION_REQUEST_CANCELLED, + + /** + * The message to display if a player is unable to pay for using a permission sign + */ + CANNOT_AFFORD, + + /** + * The message to display is a player has all permissions given by a permission sign + */ + ALREADY_HAS_PERMISSIONS, + + /** + * The message to display when a permission sign successfully grants permissions to the player + */ + PERMISSIONS_GRANTED, + + /** + * The message to display when a permission sign has been successfully created + */ + PERMISSION_SIGN_CREATED } diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java index 9722b97..8efe385 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java @@ -3,6 +3,7 @@ package net.knarcraft.permissionsigns.listener; import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.SignCreationRequest; +import net.knarcraft.permissionsigns.formatting.StringFormatter; import net.knarcraft.permissionsigns.formatting.TranslatableMessage; import net.knarcraft.permissionsigns.formatting.Translator; import net.knarcraft.permissionsigns.manager.EconomyManager; @@ -66,10 +67,10 @@ public class SignListener implements Listener { } if (!player.hasPermission("permissionsigns.admin")) { event.setCancelled(true); - player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY)); + player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY)); } else { SignManager.removeSign(sign.getLocation()); - player.sendMessage(Translator.getTranslatedMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED)); + player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED)); } } @@ -101,16 +102,49 @@ public class SignListener implements Listener { * @param player

The player that interacted with the permission sign

*/ private void handlePermissionSignInteract(PermissionSign permissionSign, Player player) { - //TODO: Check if the player has the permissions for sale. If it has, just explain that it does - if (EconomyManager.canAfford(player, permissionSign.getCost())) { - EconomyManager.withdraw(player, permissionSign.getCost()); - for (String permissionString : permissionSign.getPermissionNodes()) { - PermissionManager.addPermission(player, permissionString, permissionSign.getDuration() == 0); + //Don't allow players to pay for permissions they already own + boolean hasAllPermissions = true; + for (String permissionNode : permissionSign.getPermissionNodes()) { + if (!player.hasPermission(permissionNode)) { + hasAllPermissions = false; } - //TODO: Tell the player that they've been granted the new permissions, possibly including the full nodes? - } else { - //TODO: Tell the player that they cannot afford the transaction } + if (hasAllPermissions) { + player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ALREADY_HAS_PERMISSIONS)); + return; + } + + if (EconomyManager.canAfford(player, permissionSign.getCost())) { + performPermissionTransaction(player, permissionSign); + } else { + player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.CANNOT_AFFORD)); + } + } + + /** + * Performs the permission transaction taking payment and granting permissions + * + * @param player

The player to give permissions to

+ * @param permissionSign

The permission sign the player clicked

+ */ + private void performPermissionTransaction(Player player, PermissionSign permissionSign) { + EconomyManager.withdraw(player, permissionSign.getCost()); + StringBuilder permissionsBuilder = new StringBuilder(); + for (String permissionNode : permissionSign.getPermissionNodes()) { + //Only grant the permission if not already owned + if (!player.hasPermission(permissionNode)) { + permissionsBuilder.append(permissionNode); + permissionsBuilder.append(", "); + PermissionManager.addPermission(player, permissionNode, permissionSign.getDuration() == 0); + } + } + + 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})); } /** @@ -139,7 +173,7 @@ public class SignListener implements Listener { sign.setLine(i, signLines[i]); } sign.update(); - //TODO: Display text in the chat explaining that the permission sign has been created + player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_CREATED)); } } diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index b3606c9..1f646a1 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -10,4 +10,10 @@ en: COMMAND_PLAYER_ONLY: "&7This command is only available to players" PERMISSION_SIGN_DESTROY_DENY: "&7You do not have permissions to delete a permissions sign" PERMISSION_SIGN_REMOVED: "&7Permissions sign removed" - COMMAND_PERMISSION_DENIED: "&7You do not have necessary permissions to perform this command" \ No newline at end of file + COMMAND_PERMISSION_DENIED: "&7You do not have necessary permissions to perform this command" + CREATION_REQUEST_CREATED: "&7Permission Sign request created. Right-click an empty sign within 60 seconds to create the new permission sign" + CANNOT_AFFORD: "&7You cannot afford to use this permission sign" + ALREADY_HAS_PERMISSIONS: "&7You already have all permissions sold by this permission sign" + PERMISSIONS_GRANTED: "&7You have been granted the following permission nodes: {permissions} for {time}" + PERMISSION_SIGN_CREATED: "&7Permission sign successfully created!" + CREATION_REQUEST_CANCELLED: "&7Your last permission sign creation request has been cancelled" \ No newline at end of file