From a3578fe754374572355da268083927d343c670f2 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 25 Jan 2024 15:08:56 +0100 Subject: [PATCH] Uses KnarLib methods --- .../permissionsigns/PermissionSigns.java | 184 ++++++------------ .../permissionsigns/command/AboutCommand.java | 12 +- .../command/CancelCommand.java | 15 +- .../command/CreateCommand.java | 62 +++--- .../command/ReloadCommand.java | 9 +- .../permissionsigns/config/Configuration.java | 85 ++++++++ .../Translatable.java} | 14 +- .../container/PermissionSign.java | 50 ++--- .../formatting/StringFormatter.java | 110 ----------- .../formatting/Translator.java | 122 ------------ .../listener/BlockListener.java | 21 +- .../listener/SignListener.java | 37 ++-- .../manager/PermissionManager.java | 4 +- .../manager/SignCreationManager.java | 89 +++++++++ .../permissionsigns/manager/SignManager.java | 2 +- .../permissionsigns/utility/FileHelper.java | 45 ----- .../utility/UpdateChecker.java | 96 --------- 17 files changed, 363 insertions(+), 594 deletions(-) create mode 100644 src/main/java/net/knarcraft/permissionsigns/config/Configuration.java rename src/main/java/net/knarcraft/permissionsigns/{formatting/TranslatableMessage.java => config/Translatable.java} (91%) delete mode 100644 src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java delete mode 100644 src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java create mode 100644 src/main/java/net/knarcraft/permissionsigns/manager/SignCreationManager.java delete mode 100644 src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java delete mode 100644 src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java diff --git a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java index 9b34563..5a88845 100644 --- a/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java +++ b/src/main/java/net/knarcraft/permissionsigns/PermissionSigns.java @@ -1,38 +1,33 @@ package net.knarcraft.permissionsigns; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.knarlib.util.UpdateChecker; import net.knarcraft.permissionsigns.command.PermissionSignsCommand; import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter; -import net.knarcraft.permissionsigns.container.PermissionSign; -import net.knarcraft.permissionsigns.container.SignCreationRequest; -import net.knarcraft.permissionsigns.formatting.Translator; +import net.knarcraft.permissionsigns.config.Configuration; +import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.listener.BlockListener; import net.knarcraft.permissionsigns.listener.PlayerListener; import net.knarcraft.permissionsigns.listener.SignListener; import net.knarcraft.permissionsigns.manager.EconomyManager; import net.knarcraft.permissionsigns.manager.PermissionManager; +import net.knarcraft.permissionsigns.manager.SignCreationManager; import net.knarcraft.permissionsigns.manager.SignManager; import net.knarcraft.permissionsigns.thread.PermissionTimeoutThread; -import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; -import net.knarcraft.permissionsigns.utility.UpdateChecker; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitScheduler; +import org.jetbrains.annotations.NotNull; import java.io.IOException; -import java.util.List; -import java.util.PriorityQueue; -import java.util.Queue; -import java.util.UUID; -import java.util.stream.Stream; /* PermissionSigns - A permission shop plugin for Spigot @@ -59,13 +54,10 @@ along with this program. If not, see . */ public final class PermissionSigns extends JavaPlugin { - private static final Queue signCreationRequests = new PriorityQueue<>(); - private static String pluginVersion; private static PermissionSigns instance; - private static boolean perWorldPermissions; - private static boolean enableExtensiveSignProtection; - private static boolean enableIndirectSignProtection; - private static boolean removePermissionSignIfMissing; + private Translator translator; + private StringFormatter stringFormatter; + private Configuration configuration; /** * Instantiates the permission signs class @@ -85,96 +77,46 @@ public final class PermissionSigns extends JavaPlugin { return instance; } + /** + * Gets the translator + * + * @return

The translator

+ */ + public @NotNull Translator getTranslator() { + return translator; + } + + /** + * Gets the string formatter + * + * @return

The string formatter

+ */ + public @NotNull StringFormatter stringFormatter() { + return stringFormatter; + } + + /** + * Gets this plugin's configuration + * + * @return

This plugin's configuration

+ */ + public Configuration getConfiguration() { + return configuration; + } + /** * Gets the version of this plugin * * @return

This plugin's version

*/ - public static String getPluginVersion() { - return pluginVersion; - } - - /** - * Adds a new sign creation request - * - * @param player

The player that initiated the sign creation

- * @param sign

The sign the player is about to create

- */ - public static void addSignCreationRequest(Player player, PermissionSign sign) { - signCreationRequests.add(new SignCreationRequest(sign, player, System.currentTimeMillis())); - } - - /** - * Gets the sign creation request for the player with the given UUID - * - * @param uuid

The UUID to get a sign creation request for

- * @return

A sign creation request, or null if the UUID is not found

- */ - public static SignCreationRequest getSignCreationRequest(UUID uuid) { - Stream matchingRequests = signCreationRequests.stream().filter( - (item) -> item.getPlayer().getUniqueId().equals(uuid)); - List requestList = matchingRequests.toList(); - if (!requestList.isEmpty()) { - return requestList.get(0); - } else { - return null; - } - } - - /** - * Cancels the sign creation request triggered by the user - * - * @param uuid

The UUID of the player to cancel the request for

- */ - public static void cancelSignCreationRequest(UUID uuid) { - Stream matchingRequests = signCreationRequests.stream().filter( - (item) -> item.getPlayer().getUniqueId().equals(uuid)); - List requestList = matchingRequests.toList(); - if (requestList.size() > 0) { - signCreationRequests.remove(requestList.get(0)); - } - } - - /** - * Checks whether permissions should always be set for the world the sign belongs to - * - * @return

Whether permissions should be set for the current world

- */ - public static boolean usePerWorldPermissions() { - return perWorldPermissions; - } - - /** - * Gets whether permission signs on falling blocks should be protected - * - * @return

Whether permission signs on falling blocks should be protected

- */ - public static boolean extensiveSignProtectionEnabled() { - return enableExtensiveSignProtection; - } - - /** - * Gets whether permission sign explosion and piston protection is enabled - * - * @return

Whether permission sign explosion and piston protection is enabled

- */ - public static boolean indirectProtectionEnabled() { - return enableIndirectSignProtection; - } - - /** - * Gets whether to remove permissions signs if their physical signs are missing - * - * @return

Whether tor remove permission signs if their signs are missing

- */ - public static boolean removePermissionSignIfMissing() { - return removePermissionSignIfMissing; + public String getPluginVersion() { + return this.getDescription().getVersion(); } @Override public void reloadConfig() { super.reloadConfig(); - Translator.loadLanguages(loadConfig()); + loadConfig(); } @Override @@ -182,15 +124,15 @@ public final class PermissionSigns extends JavaPlugin { //Check if vault is loaded setupVault(); - //Get plugin info - PluginDescriptionFile pluginDescriptionFile = this.getDescription(); - pluginVersion = pluginDescriptionFile.getVersion(); - //Load config and write the default config if necessary FileConfiguration config = this.getConfig(); this.saveDefaultConfig(); config.options().copyDefaults(true); - Translator.loadLanguages(loadConfig()); + + translator = new Translator(); + translator.registerMessageCategory(Translatable.ALREADY_HAS_PERMISSIONS); + stringFormatter = new StringFormatter(this.getDescription().getName(), translator); + configuration = new Configuration(this.getConfig(), translator, this.getDataFolder()); registerListeners(); registerCommands(); @@ -200,23 +142,26 @@ public final class PermissionSigns extends JavaPlugin { PermissionManager.loadTemporaryPermissions(); UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=99426", - () -> pluginVersion, null); + () -> this.getDescription().getVersion(), null); + } + + @Override + public void onDisable() { + try { + SignManager.saveSigns(); + } catch (IOException e) { + e.printStackTrace(); + } + SignCreationManager.stopTimeoutTimer(); } /** * Loads the config file - * - * @return

The currently selected language

*/ - private String loadConfig() { - FileConfiguration config = this.getConfig(); - String language = config.getString("language", "en"); - perWorldPermissions = config.getBoolean("perWorldPermissions", false); - enableExtensiveSignProtection = config.getBoolean("enableExtensiveSignProtection", false); - enableIndirectSignProtection = config.getBoolean("enableIndirectSignProtection", true); - removePermissionSignIfMissing = config.getBoolean("removePermissionSignIfMissing", true); + private void loadConfig() { + this.reloadConfig(); + configuration.loadConfig(this.getConfig()); saveConfig(); - return language; } /** @@ -224,7 +169,7 @@ public final class PermissionSigns extends JavaPlugin { */ private void runThreads() { BukkitScheduler scheduler = Bukkit.getScheduler(); - scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L); + SignCreationManager.runTimeoutTimer(); scheduler.runTaskTimer(this, new PermissionTimeoutThread(), 0L, 25L); } @@ -264,13 +209,4 @@ public final class PermissionSigns extends JavaPlugin { } } - @Override - public void onDisable() { - try { - SignManager.saveSigns(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/AboutCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/AboutCommand.java index bab3f5e..c674cb7 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/AboutCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/AboutCommand.java @@ -1,7 +1,7 @@ package net.knarcraft.permissionsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.permissionsigns.PermissionSigns; -import net.knarcraft.permissionsigns.formatting.StringFormatter; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -14,11 +14,13 @@ import org.jetbrains.annotations.NotNull; public class AboutCommand implements CommandExecutor { @Override - public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - sender.sendMessage(StringFormatter.formatInfoMessage("Permission Signs plugin created by " + ChatColor.GOLD + - "EpicKnarvik97")); - sender.sendMessage(StringFormatter.formatInfoMessage("Plugin version: " + PermissionSigns.getPluginVersion())); + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); + stringFormatter.displaySuccessMessage(sender, "Permission Signs plugin created by " + ChatColor.GOLD + + "EpicKnarvik97"); + stringFormatter.displaySuccessMessage(sender, "Plugin version: " + + PermissionSigns.getInstance().getPluginVersion()); return true; } diff --git a/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java b/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java index 18ab990..e4470d5 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CancelCommand.java @@ -1,8 +1,9 @@ package net.knarcraft.permissionsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.permissionsigns.PermissionSigns; -import net.knarcraft.permissionsigns.formatting.StringFormatter; -import net.knarcraft.permissionsigns.formatting.TranslatableMessage; +import net.knarcraft.permissionsigns.config.Translatable; +import net.knarcraft.permissionsigns.manager.SignCreationManager; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -17,15 +18,17 @@ public class CancelCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); + if (sender instanceof Player player) { if (sender.hasPermission("permissionsigns.admin.create")) { - PermissionSigns.cancelSignCreationRequest(player.getUniqueId()); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CANCELLED)); + SignCreationManager.cancelSignCreationRequest(player.getUniqueId()); + stringFormatter.displaySuccessMessage(sender, Translatable.CREATION_REQUEST_CANCELLED); } else { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); + stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PERMISSION_DENIED); } } else { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)); + stringFormatter.displayErrorMessage(sender, Translatable.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 63f7c1c..c8cf4f9 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/CreateCommand.java @@ -1,9 +1,10 @@ package net.knarcraft.permissionsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.permissionsigns.PermissionSigns; +import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.container.PermissionSign; -import net.knarcraft.permissionsigns.formatting.StringFormatter; -import net.knarcraft.permissionsigns.formatting.TranslatableMessage; +import net.knarcraft.permissionsigns.manager.SignCreationManager; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -25,16 +26,18 @@ public class CreateCommand implements CommandExecutor { //Name and permission(s) required, but duration and cost optional String usage = "/ps create [cost] [duration] - Used for creating" + " a new permission sign"; + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); + if (!(sender instanceof Player)) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PLAYER_ONLY)); + stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PLAYER_ONLY); return false; } if (!sender.hasPermission("permissionsigns.admin.create")) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); + stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PERMISSION_DENIED); return false; } if (args.length < 3) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.MISSING_CREATION_INFO)); + stringFormatter.displayErrorMessage(sender, Translatable.MISSING_CREATION_INFO); sender.sendMessage(usage); return true; } @@ -44,50 +47,55 @@ public class CreateCommand implements CommandExecutor { sender.sendMessage(usage); return true; } - PermissionSigns.addSignCreationRequest((Player) sender, newSign); + SignCreationManager.addSignCreationRequest((Player) sender, newSign); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CREATION_REQUEST_CREATED)); + stringFormatter.displaySuccessMessage(sender, Translatable.CREATION_REQUEST_CREATED); return true; } /** * Parses the permission sign given in user input * - * @param sender

The sender that initiated the command

- * @param args

The given command arguments

+ * @param sender

The sender that initiated the command

+ * @param arguments

The given command arguments

* @return

The parsed sign, or null if it could not be parsed

*/ - private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] args) { - String name = args[0]; - String description = replaceUnderscoresWithSpaces(args[1]); - String[] permissions = args[2].replaceAll("\\?", " ").split(","); + private PermissionSign parseSign(@NotNull CommandSender sender, @NotNull String[] arguments) { + String name = arguments[0]; + String description = replaceUnderscoresWithSpaces(arguments[1]); + String[] permissions = arguments[2].replaceAll("\\?", " ").split(","); + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); + for (String permission : permissions) { - if (permission.contains(":")) { - String world = permission.split(":")[0]; - if (!world.equalsIgnoreCase("all") && !world.equalsIgnoreCase("any") && - Bukkit.getWorld(world) == null) { - sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.PERMISSION_WORLD_INVALID), "{world}", world)); - return null; - } + if (!permission.contains(":")) { + continue; + } + + String world = permission.split(":")[0]; + if (!world.equalsIgnoreCase("all") && !world.equalsIgnoreCase("any") && + Bukkit.getWorld(world) == null) { + + stringFormatter.displayErrorMessage(sender, stringFormatter.replacePlaceholder( + Translatable.PERMISSION_WORLD_INVALID, "{world}", world)); + return null; } } double cost = 0; int duration = 0; - if (args.length > 3) { + if (arguments.length > 3) { try { - cost = Double.parseDouble(args[3]); + cost = Double.parseDouble(arguments[3]); } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COST_INVALID_NUMBER)); + stringFormatter.displayErrorMessage(sender, Translatable.COST_INVALID_NUMBER); return null; } } - if (args.length > 4) { + if (arguments.length > 4) { try { - duration = Integer.parseInt(args[4]); + duration = Integer.parseInt(arguments[4]); } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.DURATION_INVALID_NUMBER)); + stringFormatter.displayErrorMessage(sender, Translatable.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 796fb94..ba43667 100644 --- a/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/permissionsigns/command/ReloadCommand.java @@ -1,8 +1,8 @@ package net.knarcraft.permissionsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.permissionsigns.PermissionSigns; -import net.knarcraft.permissionsigns.formatting.StringFormatter; -import net.knarcraft.permissionsigns.formatting.TranslatableMessage; +import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.manager.PermissionManager; import net.knarcraft.permissionsigns.manager.SignManager; import org.bukkit.command.Command; @@ -18,13 +18,14 @@ public class ReloadCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); if (sender.hasPermission("permissionsigns.admin.reload")) { SignManager.loadSigns(); PermissionManager.loadTemporaryPermissions(); PermissionSigns.getInstance().reloadConfig(); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.RELOAD_SUCCESSFUL)); + stringFormatter.displaySuccessMessage(sender, Translatable.RELOAD_SUCCESSFUL); } else { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.COMMAND_PERMISSION_DENIED)); + stringFormatter.displayErrorMessage(sender, Translatable.COMMAND_PERMISSION_DENIED); } return true; } diff --git a/src/main/java/net/knarcraft/permissionsigns/config/Configuration.java b/src/main/java/net/knarcraft/permissionsigns/config/Configuration.java new file mode 100644 index 0000000..97f0530 --- /dev/null +++ b/src/main/java/net/knarcraft/permissionsigns/config/Configuration.java @@ -0,0 +1,85 @@ +package net.knarcraft.permissionsigns.config; + +import net.knarcraft.knarlib.formatting.Translator; +import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; + +import java.io.File; + +/** + * PermissionSigns' configuration file + */ +public class Configuration { + + private final Translator translator; + private final File dataFolder; + private boolean perWorldPermissions; + private boolean enableExtensiveSignProtection; + private boolean enableIndirectSignProtection; + private boolean removePermissionSignIfMissing; + + /** + * Instantiates a new configuration + * + * @param fileConfiguration

The file configuration to load

+ * @param translator

The translator to load translations for

+ * @param dataFolder

This plugin's data folder

+ */ + public Configuration(@NotNull FileConfiguration fileConfiguration, @NotNull Translator translator, + @NotNull File dataFolder) { + this.translator = translator; + this.dataFolder = dataFolder; + loadConfig(fileConfiguration); + } + + /** + * Loads the current configuration + * + * @param fileConfiguration

The file configuration to load

+ */ + public void loadConfig(@NotNull FileConfiguration fileConfiguration) { + String language = fileConfiguration.getString("language", "en"); + perWorldPermissions = fileConfiguration.getBoolean("perWorldPermissions", false); + enableExtensiveSignProtection = fileConfiguration.getBoolean("enableExtensiveSignProtection", false); + enableIndirectSignProtection = fileConfiguration.getBoolean("enableIndirectSignProtection", true); + removePermissionSignIfMissing = fileConfiguration.getBoolean("removePermissionSignIfMissing", true); + translator.loadLanguages(dataFolder, "en", language); + } + + /** + * Checks whether permissions should always be set for the world the sign belongs to + * + * @return

Whether permissions should be set for the current world

+ */ + public boolean usePerWorldPermissions() { + return perWorldPermissions; + } + + /** + * Gets whether permission signs on falling blocks should be protected + * + * @return

Whether permission signs on falling blocks should be protected

+ */ + public boolean extensiveSignProtectionEnabled() { + return enableExtensiveSignProtection; + } + + /** + * Gets whether permission sign explosion and piston protection is enabled + * + * @return

Whether permission sign explosion and piston protection is enabled

+ */ + public boolean indirectProtectionEnabled() { + return enableIndirectSignProtection; + } + + /** + * Gets whether to remove permissions signs if their physical signs are missing + * + * @return

Whether tor remove permission signs if their signs are missing

+ */ + public boolean removePermissionSignIfMissing() { + return removePermissionSignIfMissing; + } + +} diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/permissionsigns/config/Translatable.java similarity index 91% rename from src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java rename to src/main/java/net/knarcraft/permissionsigns/config/Translatable.java index d39726d..9e19ea9 100644 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/permissionsigns/config/Translatable.java @@ -1,9 +1,12 @@ -package net.knarcraft.permissionsigns.formatting; +package net.knarcraft.permissionsigns.config; + +import net.knarcraft.knarlib.formatting.TranslatableMessage; +import org.jetbrains.annotations.NotNull; /** * An enum representing all translatable messages */ -public enum TranslatableMessage { +public enum Translatable implements TranslatableMessage { /** * The prefix to display in messages @@ -173,6 +176,11 @@ public enum TranslatableMessage { /** * The format used for each permission displayed in a list when displaying permission sign info */ - SIGN_INFO_PERMISSION_FORMAT + SIGN_INFO_PERMISSION_FORMAT; + + @Override + public @NotNull TranslatableMessage[] getAllMessages() { + return Translatable.values(); + } } diff --git a/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java b/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java index bc4d316..71682ad 100644 --- a/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java +++ b/src/main/java/net/knarcraft/permissionsigns/container/PermissionSign.java @@ -1,7 +1,9 @@ package net.knarcraft.permissionsigns.container; -import net.knarcraft.permissionsigns.formatting.TranslatableMessage; -import net.knarcraft.permissionsigns.formatting.Translator; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.permissionsigns.PermissionSigns; +import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.manager.EconomyManager; import org.bukkit.Location; import org.jetbrains.annotations.NotNull; @@ -13,8 +15,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import static net.knarcraft.permissionsigns.formatting.StringFormatter.replacePlaceholder; - /** * This class represents a placed and active permission sign */ @@ -27,7 +27,7 @@ public class PermissionSign { private final int duration; private final double cost; - private static Map timeUnits; + private static Map timeUnits; private static List sortedUnits; /** @@ -147,10 +147,11 @@ public class PermissionSign { * @return

The lines used to draw this permission sign

*/ public @NotNull String[] getSignLines() { + Translator translator = PermissionSigns.getInstance().getTranslator(); String[] lines = new String[4]; - lines[0] = Translator.getTranslatedMessage(TranslatableMessage.SIGN_PREFIX); - lines[1] = replacePlaceholder(Translator.getTranslatedMessage( - TranslatableMessage.SIGN_NAME_FORMAT), "{name}", getName()); + lines[0] = translator.getTranslatedMessage(Translatable.SIGN_PREFIX); + lines[1] = StringFormatter.replacePlaceholder(translator.getTranslatedMessage( + Translatable.SIGN_NAME_FORMAT), "{name}", getName()); lines[2] = getDurationString(); lines[3] = getCostString(); return lines; @@ -175,7 +176,7 @@ public class PermissionSign { */ public @NotNull String getDurationString() { if (duration == 0) { - return Translator.getTranslatedMessage(TranslatableMessage.SIGN_PERMANENT); + return PermissionSigns.getInstance().getTranslator().getTranslatedMessage(Translatable.SIGN_PERMANENT); } else { if (sortedUnits == null) { initializeUnits(); @@ -187,7 +188,7 @@ public class PermissionSign { (units * 10) % 10 == 0); } } - return formatDurationString(duration, TranslatableMessage.UNIT_SECONDS, false); + return formatDurationString(duration, Translatable.UNIT_SECONDS, false); } } @@ -198,7 +199,7 @@ public class PermissionSign { */ public @NotNull String getCostString() { if (cost == 0) { - return Translator.getTranslatedMessage(TranslatableMessage.SIGN_COST_FREE); + return PermissionSigns.getInstance().getTranslator().getTranslatedMessage(Translatable.SIGN_COST_FREE); } else { return EconomyManager.format(cost); } @@ -217,18 +218,19 @@ public class PermissionSign { /** * Formats a duration string * - * @param duration

The duration to display

- * @param translatableMessage

The time unit to display

- * @param castToInt

Whether to cast the duration to an int

+ * @param duration

The duration to display

+ * @param translatable

The time unit to display

+ * @param castToInt

Whether to cast the duration to an int

* @return

The formatted duration string

*/ - private @NotNull String formatDurationString(double duration, @NotNull TranslatableMessage translatableMessage, + private @NotNull String formatDurationString(double duration, @NotNull Translatable translatable, boolean castToInt) { - String durationFormat = Translator.getTranslatedMessage(TranslatableMessage.SIGN_DURATION_FORMAT); - durationFormat = replacePlaceholder(durationFormat, "{unit}", - Translator.getTranslatedMessage(translatableMessage)); - return replacePlaceholder(durationFormat, "{duration}", castToInt ? String.valueOf((int) duration) : - String.valueOf(duration)); + Translator translator = PermissionSigns.getInstance().getTranslator(); + String durationFormat = translator.getTranslatedMessage(Translatable.SIGN_DURATION_FORMAT); + durationFormat = StringFormatter.replacePlaceholder(durationFormat, "{unit}", + translator.getTranslatedMessage(translatable)); + return StringFormatter.replacePlaceholder(durationFormat, "{duration}", + castToInt ? String.valueOf((int) duration) : String.valueOf(duration)); } /** @@ -240,10 +242,10 @@ public class PermissionSign { double day = hour * 24; timeUnits = new HashMap<>(); - timeUnits.put(day, new TranslatableMessage[]{TranslatableMessage.UNIT_DAY, TranslatableMessage.UNIT_DAYS}); - timeUnits.put(hour, new TranslatableMessage[]{TranslatableMessage.UNIT_HOUR, TranslatableMessage.UNIT_HOURS}); - timeUnits.put(minute, new TranslatableMessage[]{TranslatableMessage.UNIT_MINUTE, TranslatableMessage.UNIT_MINUTES}); - timeUnits.put(1D, new TranslatableMessage[]{TranslatableMessage.UNIT_SECOND, TranslatableMessage.UNIT_SECONDS}); + timeUnits.put(day, new Translatable[]{Translatable.UNIT_DAY, Translatable.UNIT_DAYS}); + timeUnits.put(hour, new Translatable[]{Translatable.UNIT_HOUR, Translatable.UNIT_HOURS}); + timeUnits.put(minute, new Translatable[]{Translatable.UNIT_MINUTE, Translatable.UNIT_MINUTES}); + timeUnits.put(1D, new Translatable[]{Translatable.UNIT_SECOND, Translatable.UNIT_SECONDS}); sortedUnits = new ArrayList<>(timeUnits.keySet()); Collections.sort(sortedUnits); diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java b/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java deleted file mode 100644 index 633d7ee..0000000 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/StringFormatter.java +++ /dev/null @@ -1,110 +0,0 @@ -package net.knarcraft.permissionsigns.formatting; - -import net.md_5.bungee.api.ChatColor; -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A formatter for formatting displayed messages - */ -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 the placeholder replaced

- */ - public static @NotNull String replacePlaceholder(@NotNull String input, @NotNull String placeholder, - @NotNull String replacement) { - return input.replace(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 placeholders replaced

- */ - public static @NotNull String replacePlaceholders(@NotNull String input, @NotNull String[] placeholders, - @NotNull 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 @NotNull String getTranslatedInfoMessage(@NotNull 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 @NotNull String getTranslatedErrorMessage(@NotNull TranslatableMessage translatableMessage) { - return formatErrorMessage(Translator.getTranslatedMessage(translatableMessage)); - } - - /** - * Formats an information message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - public static @NotNull String formatInfoMessage(@NotNull String message) { - return ChatColor.DARK_GREEN + formatMessage(message); - } - - /** - * Formats an error message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - public static @NotNull String formatErrorMessage(@NotNull String message) { - return ChatColor.DARK_RED + formatMessage(message); - } - - /** - * Formats a message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - private static @NotNull String formatMessage(@NotNull String message) { - return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " + - ChatColor.RESET + message; - } - - /** - * Translates all found color codes to formatting in a string - * - * @param message

The string to search for color codes

- * @return

The message with color codes translated

- */ - public static @NotNull String translateAllColorCodes(@NotNull String message) { - message = ChatColor.translateAlternateColorCodes('&', message); - Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); - Matcher matcher = pattern.matcher(message); - while (matcher.find()) { - message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); - } - return message; - } - -} diff --git a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java b/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java deleted file mode 100644 index 5b18fde..0000000 --- a/src/main/java/net/knarcraft/permissionsigns/formatting/Translator.java +++ /dev/null @@ -1,122 +0,0 @@ -package net.knarcraft.permissionsigns.formatting; - -import net.knarcraft.permissionsigns.PermissionSigns; -import net.knarcraft.permissionsigns.utility.FileHelper; -import org.bukkit.configuration.file.YamlConfiguration; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - -/** - * A tool to get strings translated to the correct language - */ -public final class Translator { - - private static Map translatedMessages; - private static Map backupTranslatedMessages; - - private Translator() { - - } - - /** - * Loads the languages used by this translator - */ - public static void loadLanguages(@NotNull String selectedLanguage) { - backupTranslatedMessages = loadTranslatedMessages("en"); - translatedMessages = loadCustomTranslatedMessages(selectedLanguage); - if (translatedMessages == null) { - translatedMessages = loadTranslatedMessages(selectedLanguage); - } - } - - /** - * Gets a translated version of the given translatable message - * - * @param translatableMessage

The message to translate

- * @return

The translated message

- */ - public static @NotNull String getTranslatedMessage(@NotNull TranslatableMessage translatableMessage) { - if (translatedMessages == null) { - return "Translated strings not loaded"; - } - String translatedMessage; - if (translatedMessages.containsKey(translatableMessage)) { - translatedMessage = translatedMessages.get(translatableMessage); - } else if (backupTranslatedMessages.containsKey(translatableMessage)) { - translatedMessage = backupTranslatedMessages.get(translatableMessage); - } else { - translatedMessage = translatableMessage.toString(); - } - return StringFormatter.translateAllColorCodes(translatedMessage); - } - - /** - * Loads all translated messages for the given language - * - * @param language

The language chosen by the user

- * @return

A mapping of all strings for the given language

- */ - public static @Nullable Map loadTranslatedMessages(@NotNull String language) { - try { - BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); - return loadTranslatableMessages(language, reader); - } catch (FileNotFoundException e) { - PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); - return null; - } - } - - /** - * Tries to load translated messages from a custom strings.yml file - * - * @param language

The selected language

- * @return

The loaded translated strings, or null if no custom language file exists

- */ - public static @Nullable Map loadCustomTranslatedMessages(@NotNull String language) { - File strings = new File(PermissionSigns.getInstance().getDataFolder(), "strings.yml"); - if (!strings.exists()) { - PermissionSigns.getInstance().getLogger().log(Level.FINEST, "Strings file not found"); - return null; - } - - try { - PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Loading custom strings..."); - return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader( - new FileInputStream(strings), StandardCharsets.UTF_8))); - } catch (FileNotFoundException e) { - PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Unable to load custom messages"); - return null; - } - } - - /** - * Loads translatable messages from the given reader - * - * @param language

The selected language

- * @param reader

The buffered reader to read from

- * @return

The loaded translated strings

- */ - private static Map loadTranslatableMessages(String language, BufferedReader reader) { - Map translatedMessages = new HashMap<>(); - YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader); - - for (TranslatableMessage message : TranslatableMessage.values()) { - String translated = configuration.getString(language + "." + message.toString()); - if (translated != null) { - translatedMessages.put(message, translated); - } - } - return translatedMessages; - } - -} \ No newline at end of file diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java index fd9ddb7..f336204 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/BlockListener.java @@ -1,8 +1,8 @@ package net.knarcraft.permissionsigns.listener; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.permissionsigns.PermissionSigns; -import net.knarcraft.permissionsigns.formatting.StringFormatter; -import net.knarcraft.permissionsigns.formatting.TranslatableMessage; +import net.knarcraft.permissionsigns.config.Translatable; import net.knarcraft.permissionsigns.manager.SignManager; import org.bukkit.GameMode; import org.bukkit.Material; @@ -27,8 +27,6 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; -import static net.knarcraft.permissionsigns.PermissionSigns.extensiveSignProtectionEnabled; - /** * A listener for relevant block events such as signs being broken */ @@ -66,7 +64,7 @@ public class BlockListener implements Listener { //Protect the permission sign itself protectBlockIfPermissionSign(event, block, player); - if (extensiveSignProtectionEnabled()) { + if (PermissionSigns.getInstance().getConfiguration().extensiveSignProtectionEnabled()) { protectSignsInDirection(event, block, player, BlockFace.UP); if (event.isCancelled()) { return; @@ -105,7 +103,7 @@ public class BlockListener implements Listener { */ private void preventDestruction(@NotNull List blocks, @NotNull Cancellable event) { //Don't do anything if indirect protection is disabled - if (!PermissionSigns.indirectProtectionEnabled()) { + if (!PermissionSigns.getInstance().getConfiguration().indirectProtectionEnabled()) { return; } for (Block block : blocks) { @@ -114,7 +112,7 @@ public class BlockListener implements Listener { return; } - if (extensiveSignProtectionEnabled()) { + if (PermissionSigns.getInstance().getConfiguration().extensiveSignProtectionEnabled()) { protectSignsInDirection(event, block, null, BlockFace.UP); if (event.isCancelled()) { return; @@ -186,21 +184,24 @@ public class BlockListener implements Listener { if (!registered) { return; } + if (player == null) { event.setCancelled(true); return; } + + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); if (!player.hasPermission("permissionsigns.admin.create")) { event.setCancelled(true); - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_DENY)); + stringFormatter.displayErrorMessage(player, Translatable.PERMISSION_SIGN_DESTROY_DENY); } else { if (!player.isSneaking() && player.getGameMode() == GameMode.CREATIVE) { event.setCancelled(true); - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.PERMISSION_SIGN_DESTROY_SNEAK)); + stringFormatter.displayErrorMessage(player, Translatable.PERMISSION_SIGN_DESTROY_SNEAK); return; } SignManager.removeSign(sign.getLocation()); - player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_REMOVED)); + stringFormatter.displaySuccessMessage(player, Translatable.PERMISSION_SIGN_REMOVED); } } diff --git a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java index 7ac1057..af1bec0 100644 --- a/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/permissionsigns/listener/SignListener.java @@ -1,13 +1,14 @@ package net.knarcraft.permissionsigns.listener; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.permissionsigns.PermissionSigns; +import net.knarcraft.permissionsigns.config.Translatable; 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; import net.knarcraft.permissionsigns.manager.PermissionManager; +import net.knarcraft.permissionsigns.manager.SignCreationManager; import net.knarcraft.permissionsigns.manager.SignManager; import org.bukkit.GameMode; import org.bukkit.Material; @@ -76,7 +77,8 @@ public class SignListener implements Listener { return; } if (!player.hasPermission("permissionsigns.use")) { - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.INTERACT_PERMISSION_MISSING)); + PermissionSigns.getInstance().stringFormatter().displayErrorMessage(player, + Translatable.INTERACT_PERMISSION_MISSING); return; } //Prevent placement of any blocks, and sign clicking @@ -90,7 +92,7 @@ public class SignListener implements Listener { } //Check if the player has a creation request that can be fulfilled - SignCreationRequest request = PermissionSigns.getSignCreationRequest(player.getUniqueId()); + SignCreationRequest request = SignCreationManager.getSignCreationRequest(player.getUniqueId()); if (request != null) { registerPermissionSign(sign, request, player); } @@ -103,14 +105,15 @@ public class SignListener implements Listener { * @return

The information to display

*/ private String getSignInfoText(@NotNull PermissionSign sign) { - String rawSignInfo = Translator.getTranslatedMessage(TranslatableMessage.SIGN_INFO); + Translator translator = PermissionSigns.getInstance().getTranslator(); + String rawSignInfo = translator.getTranslatedMessage(Translatable.SIGN_INFO); StringBuilder permissionString = new StringBuilder(); List permissionNodes = sign.getPermissionNodes(); if (permissionNodes.size() == 1) { permissionString = new StringBuilder(permissionNodes.get(0)); } else { for (String permissionNode : permissionNodes) { - String format = Translator.getTranslatedMessage(TranslatableMessage.SIGN_INFO_PERMISSION_FORMAT); + String format = translator.getTranslatedMessage(Translatable.SIGN_INFO_PERMISSION_FORMAT); permissionString.append(StringFormatter.replacePlaceholder(format, "{permission}", permissionNode)); } } @@ -139,15 +142,16 @@ public class SignListener implements Listener { hasAllPermissions = false; } } + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); if (hasAllPermissions) { - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ALREADY_HAS_PERMISSIONS)); + stringFormatter.displayErrorMessage(player, Translatable.ALREADY_HAS_PERMISSIONS); return; } if (EconomyManager.canAfford(player, permissionSign.getCost())) { performPermissionTransaction(player, permissionSign); } else { - player.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.CANNOT_AFFORD)); + stringFormatter.displayErrorMessage(player, Translatable.CANNOT_AFFORD); } } @@ -179,14 +183,15 @@ public class SignListener implements Listener { } } + StringFormatter stringFormatter = PermissionSigns.getInstance().stringFormatter(); String grantedPermissions = permissionsJoiner.toString(); if (permissionSign.getDuration() > 0) { - String successMessage = StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSIONS_GRANTED); - player.sendMessage(StringFormatter.replacePlaceholders(successMessage, new String[]{"{permissions}", "{time}"}, + stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholders( + Translatable.PERMISSIONS_GRANTED, new String[]{"{permissions}", "{time}"}, new String[]{grantedPermissions, permissionSign.getDurationString()})); } else { - player.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedInfoMessage( - TranslatableMessage.PERMISSIONS_GRANTED_PERMANENTLY), "{permissions}", grantedPermissions)); + stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder( + Translatable.PERMISSIONS_GRANTED_PERMANENTLY, "{permissions}", grantedPermissions)); } } @@ -210,7 +215,7 @@ public class SignListener implements Listener { PermissionSign permissionSign = request.getPermissionSign(); permissionSign.setSignLocation(sign.getLocation()); SignManager.addSign(permissionSign); - PermissionSigns.cancelSignCreationRequest(player.getUniqueId()); + SignCreationManager.cancelSignCreationRequest(player.getUniqueId()); //Display the permission sign information on the sign String[] signLines = permissionSign.getSignLines(); @@ -219,7 +224,9 @@ public class SignListener implements Listener { } sign.setWaxed(true); sign.update(); - player.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.PERMISSION_SIGN_CREATED)); + + PermissionSigns.getInstance().stringFormatter().displaySuccessMessage(player, + Translatable.PERMISSION_SIGN_CREATED); } } diff --git a/src/main/java/net/knarcraft/permissionsigns/manager/PermissionManager.java b/src/main/java/net/knarcraft/permissionsigns/manager/PermissionManager.java index 4ec9170..fbf67d1 100644 --- a/src/main/java/net/knarcraft/permissionsigns/manager/PermissionManager.java +++ b/src/main/java/net/knarcraft/permissionsigns/manager/PermissionManager.java @@ -61,7 +61,7 @@ public final class PermissionManager { if (permissionNode.contains(":")) { grantWorldPermission(player, permissionNode, false); } else { - boolean perWorldPermissions = PermissionSigns.usePerWorldPermissions(); + boolean perWorldPermissions = PermissionSigns.getInstance().getConfiguration().usePerWorldPermissions(); permission.playerAdd(perWorldPermissions ? player.getWorld().getName() : null, player, permissionNode); } } @@ -79,7 +79,7 @@ public final class PermissionManager { if (permissionNode.contains(":")) { grantWorldPermission(player, permissionNode, true); } else { - boolean perWorldPermissions = PermissionSigns.usePerWorldPermissions(); + boolean perWorldPermissions = PermissionSigns.getInstance().getConfiguration().usePerWorldPermissions(); permission.playerAddTransient(perWorldPermissions ? worldName : null, player, permissionNode); } } diff --git a/src/main/java/net/knarcraft/permissionsigns/manager/SignCreationManager.java b/src/main/java/net/knarcraft/permissionsigns/manager/SignCreationManager.java new file mode 100644 index 0000000..ade3769 --- /dev/null +++ b/src/main/java/net/knarcraft/permissionsigns/manager/SignCreationManager.java @@ -0,0 +1,89 @@ +package net.knarcraft.permissionsigns.manager; + +import net.knarcraft.permissionsigns.PermissionSigns; +import net.knarcraft.permissionsigns.container.PermissionSign; +import net.knarcraft.permissionsigns.container.SignCreationRequest; +import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.UUID; +import java.util.stream.Stream; + +/** + * A manager for keeping track of sign creation requests + */ +public final class SignCreationManager { + + private static final Queue signCreationRequests = new PriorityQueue<>(); + private static BukkitTask timeoutTask = null; + + private SignCreationManager() { + + } + + /** + * Runs the task for timing out permission sign creation requests + */ + public static void runTimeoutTimer() { + timeoutTask = Bukkit.getScheduler().runTaskTimer(PermissionSigns.getInstance(), + new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 1000L); + } + + /** + * Stops the task for timing out permission sign creation requests + */ + public static void stopTimeoutTimer() { + if (timeoutTask != null) { + timeoutTask.cancel(); + } + } + + /** + * Adds a new sign creation request + * + * @param player

The player that initiated the sign creation

+ * @param sign

The sign the player is about to create

+ */ + public static void addSignCreationRequest(@NotNull Player player, @NotNull PermissionSign sign) { + signCreationRequests.add(new SignCreationRequest(sign, player, System.currentTimeMillis())); + } + + /** + * Gets the sign creation request for the player with the given UUID + * + * @param uuid

The UUID to get a sign creation request for

+ * @return

A sign creation request, or null if the UUID is not found

+ */ + public static @Nullable SignCreationRequest getSignCreationRequest(@NotNull UUID uuid) { + Stream matchingRequests = signCreationRequests.stream().filter( + (item) -> item.getPlayer().getUniqueId().equals(uuid)); + List requestList = matchingRequests.toList(); + if (!requestList.isEmpty()) { + return requestList.get(0); + } else { + return null; + } + } + + /** + * Cancels the sign creation request triggered by the user + * + * @param uuid

The UUID of the player to cancel the request for

+ */ + public static void cancelSignCreationRequest(UUID uuid) { + Stream matchingRequests = signCreationRequests.stream().filter( + (item) -> item.getPlayer().getUniqueId().equals(uuid)); + List requestList = matchingRequests.toList(); + if (requestList.size() > 0) { + signCreationRequests.remove(requestList.get(0)); + } + } + +} diff --git a/src/main/java/net/knarcraft/permissionsigns/manager/SignManager.java b/src/main/java/net/knarcraft/permissionsigns/manager/SignManager.java index 507dcb8..b346d1b 100644 --- a/src/main/java/net/knarcraft/permissionsigns/manager/SignManager.java +++ b/src/main/java/net/knarcraft/permissionsigns/manager/SignManager.java @@ -125,7 +125,7 @@ public final class SignManager { Block signBlock = key.getBlock(); BlockState state = signBlock.getState(); if (!(state instanceof Sign sign)) { - if (PermissionSigns.removePermissionSignIfMissing()) { + if (PermissionSigns.getInstance().getConfiguration().removePermissionSignIfMissing()) { invalidPermissionSigns.add(permissionSign.getSignLocation()); } continue; diff --git a/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java b/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java deleted file mode 100644 index 5165415..0000000 --- a/src/main/java/net/knarcraft/permissionsigns/utility/FileHelper.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.knarcraft.permissionsigns.utility; - -import org.jetbrains.annotations.NotNull; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; - -/** - * A helper class for dealing with files - */ -public final class FileHelper { - - private FileHelper() { - - } - - /** - * Gets a buffered reader for - * - * @return

A buffered read for reading the file

- * @throws FileNotFoundException

If unable to get an input stream for the given file

- */ - public static @NotNull BufferedReader getBufferedReaderForInternalFile(@NotNull String file) throws FileNotFoundException { - InputStream inputStream = FileHelper.class.getResourceAsStream(file); - if (inputStream == null) { - throw new FileNotFoundException("Unable to read the given file"); - } - return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); - } - - /** - * Gets a buffered reader given an input stream - * - * @param inputStream

The input stream to read

- * @return

A buffered reader reading the input stream

- */ - public static @NotNull BufferedReader getBufferedReaderFromInputStream(@NotNull InputStream inputStream) { - InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); - return new BufferedReader(inputStreamReader); - } - -} diff --git a/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java b/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java deleted file mode 100644 index 9deac2b..0000000 --- a/src/main/java/net/knarcraft/permissionsigns/utility/UpdateChecker.java +++ /dev/null @@ -1,96 +0,0 @@ -package net.knarcraft.permissionsigns.utility; - -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitScheduler; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.logging.Level; - -/** - * The update checker is responsible for looking for new updates - */ -public final class UpdateChecker { - - private final static String updateNotice = "A new update is available: %s (You are still on %s)"; - - private UpdateChecker() { - - } - - /** - * Checks if there's a new update available, and alerts the user if necessary - */ - public static void checkForUpdate(@NotNull Plugin plugin, @NotNull String apiResourceURL, - @NotNull Supplier getVersionMethod, - @Nullable Consumer setVersionMethod) { - BukkitScheduler scheduler = plugin.getServer().getScheduler(); - scheduler.runTaskAsynchronously(plugin, () -> UpdateChecker.queryAPI(plugin, apiResourceURL, getVersionMethod, - setVersionMethod)); - } - - /** - * Queries the spigot API to check for a newer version, and informs the user - */ - private static void queryAPI(@NotNull Plugin plugin, @NotNull String APIResourceURL, - @NotNull Supplier getVersionMethod, - @Nullable Consumer setVersionMethod) { - try { - InputStream inputStream = new URL(APIResourceURL).openStream(); - BufferedReader reader = FileHelper.getBufferedReaderFromInputStream(inputStream); - //There should only be one line of output - String newVersion = reader.readLine(); - reader.close(); - - String oldVersion = getVersionMethod.get(); - //If there is a newer version, notify the user - if (isVersionHigher(oldVersion, newVersion)) { - plugin.getLogger().log(Level.INFO, getUpdateAvailableString(newVersion, oldVersion)); - if (setVersionMethod != null) { - setVersionMethod.accept(newVersion); - } - } - } catch (IOException e) { - plugin.getLogger().log(Level.WARNING, "Unable to get newest version."); - } - } - - /** - * Gets the string to display to a user to alert about a new update - * - * @param newVersion

The new available plugin version

- * @param oldVersion

The old (current) plugin version

- * @return

The string to display

- */ - public static @NotNull String getUpdateAvailableString(@NotNull String newVersion, @NotNull String oldVersion) { - return String.format(updateNotice, newVersion, oldVersion); - } - - /** - * Decides whether one version number is higher than another - * - * @param oldVersion

The old version to check

- * @param newVersion

The new version to check

- * @return

True if the new version is higher than the old one

- */ - public static boolean isVersionHigher(@NotNull String oldVersion, @NotNull String newVersion) { - String[] oldVersionParts = oldVersion.split("\\."); - String[] newVersionParts = newVersion.split("\\."); - int versionLength = Math.max(oldVersionParts.length, newVersionParts.length); - for (int i = 0; i < versionLength; i++) { - int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0; - int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0; - if (newVersionNumber != oldVersionNumber) { - return newVersionNumber > oldVersionNumber; - } - } - return false; - } - -}