The translator
+ */ + public @NotNull Translator getTranslator() { + return translator; + } + + /** + * Gets the string formatter + * + * @returnThe string formatter
+ */ + public @NotNull StringFormatter stringFormatter() { + return stringFormatter; + } + + /** + * Gets this plugin's configuration + * + * @returnThis plugin's configuration
+ */ + public Configuration getConfiguration() { + return configuration; + } + /** * Gets the version of this plugin * * @returnThis plugin's version
*/ - public static String getPluginVersion() { - return pluginVersion; - } - - /** - * Adds a new sign creation request - * - * @param playerThe player that initiated the sign creation
- * @param signThe 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 uuidThe UUID to get a sign creation request for
- * @returnA sign creation request, or null if the UUID is not found
- */ - public static SignCreationRequest getSignCreationRequest(UUID uuid) { - StreamThe UUID of the player to cancel the request for
- */ - public static void cancelSignCreationRequest(UUID uuid) { - StreamWhether permissions should be set for the current world
- */ - public static boolean usePerWorldPermissions() { - return perWorldPermissions; - } - - /** - * Gets whether permission signs on falling blocks should be protected - * - * @returnWhether permission signs on falling blocks should be protected
- */ - public static boolean extensiveSignProtectionEnabled() { - return enableExtensiveSignProtection; - } - - /** - * Gets whether permission sign explosion and piston protection is enabled - * - * @returnWhether 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 - * - * @returnWhether 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 - * - * @returnThe 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 createThe sender that initiated the command
- * @param argsThe given command arguments
+ * @param senderThe sender that initiated the command
+ * @param argumentsThe given command arguments
* @returnThe 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 fileConfigurationThe file configuration to load
+ * @param translatorThe translator to load translations for
+ * @param dataFolderThis 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 fileConfigurationThe 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 + * + * @returnWhether permissions should be set for the current world
+ */ + public boolean usePerWorldPermissions() { + return perWorldPermissions; + } + + /** + * Gets whether permission signs on falling blocks should be protected + * + * @returnWhether permission signs on falling blocks should be protected
+ */ + public boolean extensiveSignProtectionEnabled() { + return enableExtensiveSignProtection; + } + + /** + * Gets whether permission sign explosion and piston protection is enabled + * + * @returnWhether 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 + * + * @returnWhether 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 MapThe 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 durationThe duration to display
- * @param translatableMessageThe time unit to display
- * @param castToIntWhether to cast the duration to an int
+ * @param durationThe duration to display
+ * @param translatableThe time unit to display
+ * @param castToIntWhether to cast the duration to an int
* @returnThe 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 inputThe input string to replace in
- * @param placeholderThe placeholder to replace
- * @param replacementThe replacement value
- * @returnThe 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 inputThe input string to replace in
- * @param placeholdersThe placeholders to replace
- * @param replacementsThe replacement values
- * @returnThe 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 translatableMessageThe translatable message to translate and format
- * @returnThe 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 translatableMessageThe translatable message to translate and format
- * @returnThe 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 messageThe message to format
- * @returnThe 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 messageThe message to format
- * @returnThe 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 messageThe message to format
- * @returnThe 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 messageThe string to search for color codes
- * @returnThe 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 MapThe message to translate
- * @returnThe 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 languageThe language chosen by the user
- * @returnA mapping of all strings for the given language
- */ - public static @Nullable MapThe selected language
- * @returnThe loaded translated strings, or null if no custom language file exists
- */ - public static @Nullable MapThe selected language
- * @param readerThe buffered reader to read from
- * @returnThe loaded translated strings
- */ - private static MapThe 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(); ListThe player that initiated the sign creation
+ * @param signThe 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 uuidThe UUID to get a sign creation request for
+ * @returnA sign creation request, or null if the UUID is not found
+ */ + public static @Nullable SignCreationRequest getSignCreationRequest(@NotNull UUID uuid) { + StreamThe UUID of the player to cancel the request for
+ */ + public static void cancelSignCreationRequest(UUID uuid) { + StreamA buffered read for reading the file
- * @throws FileNotFoundExceptionIf 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 inputStreamThe input stream to read
- * @returnA 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 SupplierThe new available plugin version
- * @param oldVersionThe old (current) plugin version
- * @returnThe 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 oldVersionThe old version to check
- * @param newVersionThe new version to check
- * @returnTrue 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; - } - -}