From a7787caa849b7128e82c0c3fc4311925065a5d28 Mon Sep 17 00:00:00 2001 From: Esophose Date: Wed, 1 May 2019 20:17:06 -0600 Subject: [PATCH] Songoda Updater and Locale system --- UltimateTimber/Plugin/build.gradle | 15 ++ .../ultimatetimber/UltimateTimber.java | 37 ++- .../ultimatetimber/locale/LocaleModule.java | 30 +++ .../manager/ChoppingManager.java | 2 +- .../manager/CommandManager.java | 50 ++-- .../manager/ConfigurationManager.java | 15 +- .../ultimatetimber/manager/LocaleManager.java | 213 ++++++++++++++++++ .../src/main/resources/config-current.yml | 4 + .../src/main/resources/config-legacy.yml | 4 + .../src/main/resources/locale/en_US.lang | 17 ++ 10 files changed, 348 insertions(+), 39 deletions(-) create mode 100644 UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/locale/LocaleModule.java create mode 100644 UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/LocaleManager.java create mode 100644 UltimateTimber/Plugin/src/main/resources/locale/en_US.lang diff --git a/UltimateTimber/Plugin/build.gradle b/UltimateTimber/Plugin/build.gradle index 1714f1f..717a290 100644 --- a/UltimateTimber/Plugin/build.gradle +++ b/UltimateTimber/Plugin/build.gradle @@ -1,5 +1,9 @@ import org.apache.tools.ant.filters.ReplaceTokens +configurations { + extraLibs +} + dependencies { compile project(':UltimateTimber-Core') compile project(':UltimateTimber-CurrentAdapter') @@ -10,6 +14,9 @@ dependencies { compile project(':UltimateTimber-McMMOClassic12') compile project(':UltimateTimber-McMMOClassic8') compileOnly 'org.spigotmc:spigot:1.14' + extraLibs 'com.songoda:songodaupdater:1' + + configurations.compileOnly.extendsFrom(configurations.extraLibs) } processResources { @@ -18,3 +25,11 @@ processResources { filter ReplaceTokens, tokens: ["version": project.property("version")] } } + +jar { + from { + configurations.extraLibs.collect { + it.isDirectory() ? it : zipTree(it) + } + } +} diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/UltimateTimber.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/UltimateTimber.java index 2358f64..05e9769 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/UltimateTimber.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/UltimateTimber.java @@ -3,10 +3,12 @@ package com.songoda.ultimatetimber; import com.songoda.ultimatetimber.adapter.VersionAdapter; import com.songoda.ultimatetimber.adapter.current.CurrentAdapter; import com.songoda.ultimatetimber.adapter.legacy.LegacyAdapter; +import com.songoda.ultimatetimber.locale.LocaleModule; import com.songoda.ultimatetimber.manager.ChoppingManager; import com.songoda.ultimatetimber.manager.CommandManager; import com.songoda.ultimatetimber.manager.ConfigurationManager; import com.songoda.ultimatetimber.manager.HookManager; +import com.songoda.ultimatetimber.manager.LocaleManager; import com.songoda.ultimatetimber.manager.Manager; import com.songoda.ultimatetimber.manager.PlacedBlockManager; import com.songoda.ultimatetimber.manager.SaplingManager; @@ -17,6 +19,8 @@ import com.songoda.ultimatetimber.manager.TreeFallManager; import com.songoda.ultimatetimber.utils.Methods; import com.songoda.ultimatetimber.utils.Metrics; import com.songoda.ultimatetimber.utils.NMSUtil; +import com.songoda.update.Plugin; +import com.songoda.update.SongodaUpdate; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; @@ -31,7 +35,6 @@ public class UltimateTimber extends JavaPlugin { private static UltimateTimber INSTANCE; - private final String prefix = "&8[&6UltimateTimber&8]"; private final CommandSender console = Bukkit.getConsoleSender(); private Set managers; @@ -41,6 +44,7 @@ public class UltimateTimber extends JavaPlugin { private CommandManager commandManager; private ConfigurationManager configurationManager; private HookManager hookManager; + private LocaleManager localeManager; private PlacedBlockManager placedBlockManager; private SaplingManager saplingManager; private TreeAnimationManager treeAnimationManager; @@ -60,11 +64,21 @@ public class UltimateTimber extends JavaPlugin { this.console.sendMessage(Methods.formatText("&7" + this.getDescription().getName() + " " + this.getDescription().getVersion() + " by &5Songoda <3&7!")); this.console.sendMessage(Methods.formatText("&7Action: &aEnabling&7...")); + // Songoda Updater + Plugin plugin = new Plugin(this, 18); + plugin.addModule(new LocaleModule()); + SongodaUpdate.load(plugin); + + // bStats Metrics + new Metrics(this); + + // Register managers this.managers = new HashSet<>(); this.choppingManager = this.registerManager(ChoppingManager.class); this.commandManager = this.registerManager(CommandManager.class); this.configurationManager = new ConfigurationManager(this); this.hookManager = this.registerManager(HookManager.class); + this.localeManager = this.registerManager(LocaleManager.class); this.placedBlockManager = this.registerManager(PlacedBlockManager.class); this.saplingManager = this.registerManager(SaplingManager.class); this.treeAnimationManager = this.registerManager(TreeAnimationManager.class); @@ -72,11 +86,10 @@ public class UltimateTimber extends JavaPlugin { this.treeDetectionManager = this.registerManager(TreeDetectionManager.class); this.treeFallManager = this.registerManager(TreeFallManager.class); + // Load version adapter and managers this.setupVersionAdapter(); this.reload(); - new Metrics(this); - this.console.sendMessage(Methods.formatText("&a=============================")); } @@ -136,15 +149,6 @@ public class UltimateTimber extends JavaPlugin { } } - /** - * Gets the plugin prefix for chat - * - * @return The plugin prefix - */ - public String getPrefix() { - return this.prefix; - } - /** * Gets the active version adapter being used * @@ -190,6 +194,15 @@ public class UltimateTimber extends JavaPlugin { return this.hookManager; } + /** + * Gets the locale manager + * + * @return The LocaleManager instance + */ + public LocaleManager getLocaleManager() { + return this.localeManager; + } + /** * Gets the placed block manager * diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/locale/LocaleModule.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/locale/LocaleModule.java new file mode 100644 index 0000000..1cc9dc7 --- /dev/null +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/locale/LocaleModule.java @@ -0,0 +1,30 @@ +package com.songoda.ultimatetimber.locale; + +import com.songoda.ultimatetimber.manager.LocaleManager; +import com.songoda.update.Module; +import com.songoda.update.Plugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.io.IOException; +import java.net.URL; + +public class LocaleModule implements Module { + + @Override + public void run(Plugin plugin) { + JSONObject json = plugin.getJson(); + try { + JSONArray files = (JSONArray) json.get("neededFiles"); + for (Object o : files) { + JSONObject file = (JSONObject) o; + + if (file.get("type").equals("locale")) + LocaleManager.saveDefaultLocale(new URL((String) file.get("link")), (String) file.get("name")); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } +} + diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ChoppingManager.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ChoppingManager.java index a056ce1..f5a8a45 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ChoppingManager.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ChoppingManager.java @@ -86,7 +86,7 @@ public class ChoppingManager extends Manager { public boolean isInCooldown(Player player) { boolean cooldowned = this.useCooldown && this.cooldownedPlayers.containsKey(player.getUniqueId()); if (cooldowned && !this.cooldownedPlayers.get(player.getUniqueId())) { - player.sendMessage(ChatColor.translateAlternateColorCodes('&', UltimateTimber.getInstance().getPrefix() + ChatColor.YELLOW + "You are on cooldown and cannot topple trees right now.")); + this.ultimateTimber.getLocaleManager().sendPrefixedMessage(player, LocaleManager.Locale.ON_COOLDOWN); this.cooldownedPlayers.replace(player.getUniqueId(), true); } return cooldowned; diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/CommandManager.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/CommandManager.java index fb26943..ece5f8d 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/CommandManager.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/CommandManager.java @@ -21,8 +21,10 @@ public class CommandManager extends Manager implements CommandExecutor, TabCompl super(ultimateTimber); PluginCommand command = ultimateTimber.getCommand("ultimatetimber"); - command.setExecutor(this); - command.setTabCompleter(this); + if (command != null) { + command.setExecutor(this); + command.setTabCompleter(this); + } } @Override @@ -37,13 +39,15 @@ public class CommandManager extends Manager implements CommandExecutor, TabCompl @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { + LocaleManager localeManager = this.ultimateTimber.getLocaleManager(); + if (args.length > 0) { if (args[0].equalsIgnoreCase("reload")) { - if (commandSender instanceof Player && !this.permCheck((Player) commandSender, "ultimatetimber.reload")) + if (commandSender instanceof Player && this.doesntHavePermission(commandSender, "ultimatetimber.reload", localeManager)) return true; UltimateTimber.getInstance().reload(); - commandSender.sendMessage(Methods.formatText(UltimateTimber.getInstance().getPrefix() + " &7Configuration reloaded")); + localeManager.sendPrefixedMessage(commandSender, LocaleManager.Locale.COMMAND_RELOAD_RELOADED); return true; } else if (args[0].equalsIgnoreCase("toggle")) { if (!(commandSender instanceof Player)) { @@ -51,13 +55,13 @@ public class CommandManager extends Manager implements CommandExecutor, TabCompl return true; } - if (!this.permCheck((Player) commandSender, "ultimatetimber.toggle")) + if (this.doesntHavePermission(commandSender, "ultimatetimber.toggle", localeManager)) return true; - if (UltimateTimber.getInstance().getChoppingManager().togglePlayer((Player)commandSender)) { - commandSender.sendMessage(Methods.formatText(UltimateTimber.getInstance().getPrefix() + " Chopping Mode: &aEnabled")); + if (UltimateTimber.getInstance().getChoppingManager().togglePlayer((Player) commandSender)) { + localeManager.sendPrefixedMessage(commandSender, LocaleManager.Locale.COMMAND_TOGGLE_ENABLED); } else { - commandSender.sendMessage(Methods.formatText(UltimateTimber.getInstance().getPrefix() + " Chopping Mode: &cDisabled")); + localeManager.sendPrefixedMessage(commandSender, LocaleManager.Locale.COMMAND_TOGGLE_DISABLED); } return true; @@ -65,22 +69,14 @@ public class CommandManager extends Manager implements CommandExecutor, TabCompl } commandSender.sendMessage(""); - commandSender.sendMessage(Methods.formatText(UltimateTimber.getInstance().getPrefix() + " &7Version " + UltimateTimber.getInstance().getDescription().getVersion() + " Created with <3 by &5&l&oSongoda")); - commandSender.sendMessage(Methods.formatText("&8 - &a/ut reload &7 - Reloads the config.")); - commandSender.sendMessage(Methods.formatText("&8 - &a/ut toggle &7 - Toggles your chopping mode")); + commandSender.sendMessage(Methods.formatText(LocaleManager.Locale.PREFIX.get() + " &7Version " + UltimateTimber.getInstance().getDescription().getVersion() + " Created with <3 by &5&l&oSongoda")); + localeManager.sendMessage(commandSender, LocaleManager.Locale.COMMAND_RELOAD_DESCRIPTION); + localeManager.sendMessage(commandSender, LocaleManager.Locale.COMMAND_TOGGLE_DESCRIPTION); commandSender.sendMessage(""); return true; } - private boolean permCheck(Player sender, String permission) { - if (!sender.hasPermission(permission)) { - sender.sendMessage(Methods.formatText("&cYou don't have permission for that!")); - return false; - } - return true; - } - @Override public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { List completions = new ArrayList<>(); @@ -101,4 +97,20 @@ public class CommandManager extends Manager implements CommandExecutor, TabCompl return completions; } + /** + * Checks if a player has a permission + * + * @param sender The CommandSender to check + * @param permission The permission to check for + * @param localeManager The LocaleManager instance + * @return True if the player has permission, otherwise false and sends a message + */ + private boolean doesntHavePermission(CommandSender sender, String permission, LocaleManager localeManager) { + if (!sender.hasPermission(permission)) { + localeManager.sendPrefixedMessage(sender, LocaleManager.Locale.NO_PERMISSION); + return true; + } + return false; + } + } diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ConfigurationManager.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ConfigurationManager.java index 94b8ace..e9e0561 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ConfigurationManager.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/ConfigurationManager.java @@ -12,6 +12,7 @@ public class ConfigurationManager extends Manager { public enum Setting { SERVER_TYPE(SettingType.STRING), + LOCALE(SettingType.STRING), DISABLED_WORLDS(SettingType.STRING_LIST), MAX_LOGS_PER_CHOP(SettingType.INT), DESTROY_LEAVES(SettingType.BOOLEAN), @@ -62,7 +63,7 @@ public class ConfigurationManager extends Manager { */ public boolean getBoolean() { this.loadValue(); - return (boolean)this.value; + return (boolean) this.value; } /** @@ -72,7 +73,7 @@ public class ConfigurationManager extends Manager { */ public int getInt() { this.loadValue(); - return (int)this.value; + return (int) this.value; } /** @@ -82,7 +83,7 @@ public class ConfigurationManager extends Manager { */ public double getDouble() { this.loadValue(); - return (double)this.value; + return (double) this.value; } /** @@ -92,7 +93,7 @@ public class ConfigurationManager extends Manager { */ public String getString() { this.loadValue(); - return (String)this.value; + return (String) this.value; } /** @@ -103,7 +104,7 @@ public class ConfigurationManager extends Manager { @SuppressWarnings("unchecked") public List getStringList() { this.loadValue(); - return (List)this.value; + return (List) this.value; } /** @@ -141,9 +142,9 @@ public class ConfigurationManager extends Manager { } /** - * Gets the name of this Setting as a config-compatible key + * Gets the name of this Setting as a FileConfiguration-compatible key * - * @return The key in the config + * @return The key for a FileConfiguration */ private String getNameAsKey() { return this.name().replace("_", "-").toLowerCase(); diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/LocaleManager.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/LocaleManager.java new file mode 100644 index 0000000..ce1ac53 --- /dev/null +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/LocaleManager.java @@ -0,0 +1,213 @@ +package com.songoda.ultimatetimber.manager; + +import com.songoda.ultimatetimber.UltimateTimber; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class LocaleManager extends Manager { + + public enum Locale { + PREFIX, + NO_PERMISSION, + + COMMAND_RELOAD_DESCRIPTION, + COMMAND_RELOAD_RELOADED, + + COMMAND_TOGGLE_DESCRIPTION, + COMMAND_TOGGLE_ENABLED, + COMMAND_TOGGLE_DISABLED, + + ON_COOLDOWN; + + private String message; + + /** + * Gets a Locale message + * + * @return A message formatted for chat + */ + public String get() { + if (this.message == null) + this.loadMessage(); + return this.message; + } + + /** + * Loads the locale message and caches it + */ + private void loadMessage() { + String message = UltimateTimber.getInstance().getLocaleManager().getLocale().getString(this.getNameAsKey()); + if (message != null) + this.message = ChatColor.translateAlternateColorCodes('&', message); + } + + /** + * Resets the cached message + */ + private void reset() { + this.message = null; + } + + /** + * Gets the name of this Setting as a FileConfiguration-compatible key + * + * @return The key for a FileConfiguration + */ + private String getNameAsKey() { + return this.name().replace("_", "-").toLowerCase(); + } + } + + public LocaleManager(UltimateTimber ultimateTimber) { + super(ultimateTimber); + } + + private FileConfiguration locale; + + @Override + public void reload() { + for (Locale value : Locale.values()) + value.reset(); + + String targetLocaleName = ConfigurationManager.Setting.LOCALE.getString() + ".lang"; + File targetLocaleFile = new File(UltimateTimber.getInstance().getDataFolder() + "/locale", targetLocaleName); + if (!targetLocaleFile.exists()) { + targetLocaleFile = new File(UltimateTimber.getInstance().getDataFolder() + "/locale", "en_US.lang"); + if (!targetLocaleFile.exists()) { + UltimateTimber.getInstance().saveResource("locale/en_US.lang", false); + } + } + + this.locale = YamlConfiguration.loadConfiguration(targetLocaleFile); + } + + @Override + public void disable() { + + } + + /** + * Gets the FileConfiguration that contains the locale messages + * + * @return A FileConfiguration of the messages + */ + public FileConfiguration getLocale() { + return this.locale; + } + + /** + * Sends a message to a CommandSender with the prefix + * + * @param sender The CommandSender to send to + * @param locale The Locale to send + */ + public void sendPrefixedMessage(CommandSender sender, Locale locale) { + sender.sendMessage(Locale.PREFIX.get() + locale.get()); + } + + /** + * Sends a message to a CommandSender + * + * @param sender The CommandSender to send to + * @param locale The Locale to send + */ + public void sendMessage(CommandSender sender, Locale locale) { + sender.sendMessage(locale.get()); + } + + /** + * Saves a locale to disk in the /locale folder if it doesn't already exist + * If it does exist, it checks to see if anything needs to be updated + * + * @param fileUrl The URL of the file to download + * @param fileName The name of the file to save + */ + public static void saveDefaultLocale(URL fileUrl, String fileName) { + File localeFolder = new File(UltimateTimber.getInstance().getDataFolder() + "/locale"); + if (!localeFolder.exists()) + localeFolder.mkdirs(); + + File targetFile = new File(localeFolder, fileName); + if (targetFile.exists()) { + checkExistingFile(fileUrl, targetFile); + return; + } + + try (OutputStream outputStream = new FileOutputStream(targetFile)) { + copy(fileUrl.openStream(), outputStream); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + /** + * Checks and updates a locale file with additions if any exist + * + * @param fileUrl The URL of the file to download + * @param targetFile The target file + */ + private static void checkExistingFile(URL fileUrl, File targetFile) { + UltimateTimber ultimateTimber = UltimateTimber.getInstance(); + Bukkit.broadcastMessage("Checking file: " + targetFile.getName()); + + List keysToUpdate = new ArrayList<>(); + FileConfiguration existingConfiguration = YamlConfiguration.loadConfiguration(targetFile); + for (Locale locale : Locale.values()) + if (existingConfiguration.get(locale.getNameAsKey()) == null) + keysToUpdate.add(locale.getNameAsKey()); + Bukkit.broadcastMessage("Missing keys: " + keysToUpdate.size()); + if (keysToUpdate.isEmpty()) + return; + + try (Reader reader = new InputStreamReader(fileUrl.openStream()); + BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile, true))) { + FileConfiguration newFileConfiguration = YamlConfiguration.loadConfiguration(reader); + + writer.newLine(); + writer.newLine(); + writer.write("# Changes since " + ultimateTimber.getName() + " v" + ultimateTimber.getDescription().getVersion()); + + for (String key : keysToUpdate) { + Bukkit.broadcastMessage("Writing to file: " + key + ": " + "\"" + newFileConfiguration.getString(key) + "\""); + writer.newLine(); + writer.write(key + ": " + "\"" + newFileConfiguration.getString(key) + "\""); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + /** + * Copies a file from an InputStream to an OutputStream + * + * @param input The InputStream to copy + * @param output The OutputStream to copy to + */ + private static void copy(InputStream input, OutputStream output) { + try { + byte[] buffer = new byte[1024 * 4]; + int n; + while ((n = input.read(buffer)) != -1) + output.write(buffer, 0, n); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/UltimateTimber/Plugin/src/main/resources/config-current.yml b/UltimateTimber/Plugin/src/main/resources/config-current.yml index 1203ce1..0630d93 100644 --- a/UltimateTimber/Plugin/src/main/resources/config-current.yml +++ b/UltimateTimber/Plugin/src/main/resources/config-current.yml @@ -9,6 +9,10 @@ # Default: CURRENT server-type: CURRENT +# The locale to use in the /locale folder +# Default: en_US +locale: en_US + # A list of worlds that the plugin is disabled in # Default: # - disabled_world_name diff --git a/UltimateTimber/Plugin/src/main/resources/config-legacy.yml b/UltimateTimber/Plugin/src/main/resources/config-legacy.yml index 448bcab..a025c63 100644 --- a/UltimateTimber/Plugin/src/main/resources/config-legacy.yml +++ b/UltimateTimber/Plugin/src/main/resources/config-legacy.yml @@ -10,6 +10,10 @@ # Default: LEGACY server-type: LEGACY +# The locale to use in the /locale folder +# Default: en_US +locale: en_US + # A list of worlds that the plugin is disabled in # Default: # - disabled_world_name diff --git a/UltimateTimber/Plugin/src/main/resources/locale/en_US.lang b/UltimateTimber/Plugin/src/main/resources/locale/en_US.lang new file mode 100644 index 0000000..0bf4a10 --- /dev/null +++ b/UltimateTimber/Plugin/src/main/resources/locale/en_US.lang @@ -0,0 +1,17 @@ +# General Messages + +prefix: "&8[&6UltimateTimber&8] " +no-permission: "&cYou don't have permission for that!" + +# Command Messages + +command-reload-description: "&8 - &a/ut reload &7 - Reloads the config." +command-reload-reloaded: "&7Configuration and locale files have been reloaded." + +command-toggle-description: "&8 - &a/ut toggle &7 - Toggles your chopping mode" +command-toggle-enabled: "&7Chopping Mode: &aEnabled" +command-toggle-disabled: "&7Chopping Mode: &cDisabled" + +# Misc + +on-cooldown: "&eYou are on cooldown and cannot topple trees right now."