From f46e0b171f732697cf68bc5aea878318b34131f0 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 24 Apr 2019 20:35:19 -0400 Subject: [PATCH] Almost full 1.14 support. --- .../java/com/songoda/epicheads/EpicHeads.java | 40 ++- main/java/com/songoda/epicheads/Locale.java | 271 ++++++++++-------- .../epicheads/menu/ui/InventoryMenu.java | 6 +- pom.xml | 106 +------ 4 files changed, 196 insertions(+), 227 deletions(-) diff --git a/main/java/com/songoda/epicheads/EpicHeads.java b/main/java/com/songoda/epicheads/EpicHeads.java index d5932de..321d39c 100644 --- a/main/java/com/songoda/epicheads/EpicHeads.java +++ b/main/java/com/songoda/epicheads/EpicHeads.java @@ -33,10 +33,16 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitRunnable; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; import java.util.function.Consumer; public class EpicHeads extends JavaPlugin implements Listener { @@ -87,6 +93,8 @@ public class EpicHeads extends JavaPlugin implements Listener { Locale.saveDefaultLocale("en_US"); this.locale = Locale.getLocale(getConfig().getString("Locale", "en_US")); + this.update(); + this.references = new References(); this.menus = new Menus(); this.menus.reload(); @@ -123,6 +131,36 @@ public class EpicHeads extends JavaPlugin implements Listener { this.tryHookBlockStore(); } + private void update() { + try { + URL url = new URL("http://update.songoda.com/index.php?plugin=" + getDescription().getName() + "&version=" + getDescription().getVersion()); + URLConnection urlConnection = url.openConnection(); + InputStream is = urlConnection.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + + int numCharsRead; + char[] charArray = new char[1024]; + StringBuilder sb = new StringBuilder(); + while ((numCharsRead = isr.read(charArray)) > 0) { + sb.append(charArray, 0, numCharsRead); + } + String jsonString = sb.toString(); + JSONObject json = (JSONObject) new JSONParser().parse(jsonString); + + JSONArray files = (JSONArray) json.get("neededFiles"); + for (Object o : files) { + JSONObject file = (JSONObject) o; + + if ("locale".equals(file.get("type"))) { + InputStream in = new URL((String) file.get("link")).openStream(); + Locale.saveDefaultLocale(in, (String) file.get("name")); + } + } + } catch (Exception e) { + Bukkit.getLogger().warning("Failed to update."); + } + } + public File getCacheFile() { if (!getDataFolder().exists() && !getDataFolder().mkdirs()) throw new RuntimeException("Unable to create the data folder to save plugin files"); diff --git a/main/java/com/songoda/epicheads/Locale.java b/main/java/com/songoda/epicheads/Locale.java index e1bfdf5..9681aa3 100644 --- a/main/java/com/songoda/epicheads/Locale.java +++ b/main/java/com/songoda/epicheads/Locale.java @@ -3,7 +3,7 @@ package com.songoda.epicheads; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; -import org.apache.commons.io.IOUtils; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; @@ -23,11 +23,10 @@ import java.util.stream.Collectors; */ public class Locale { - private static JavaPlugin plugin; private static final List LOCALES = Lists.newArrayList(); - - private static final Pattern NODE_PATTERN = Pattern.compile("(\\w+(?:\\.{1}\\w+)*)\\s*=\\s*\"(.*)\""); + private static final Pattern NODE_PATTERN = Pattern.compile("(\\w+(?:\\.\\w+)*)\\s*=\\s*\"(.*)\""); private static final String FILE_EXTENSION = ".lang"; + private static JavaPlugin plugin; private static File localeFolder; private static String defaultLocale; @@ -49,123 +48,7 @@ public class Locale { if (this.reloadMessages()) return; - plugin.getLogger().info("Loaded locale " + fileName); - } - - /** - * Get the name of the language that this locale is based on. - * (i.e. "en" for English, or "fr" for French) - * - * @return the name of the language - */ - public String getName() { - return name; - } - - /** - * Get the name of the region that this locale is from. - * (i.e. "US" for United States or "CA" for Canada) - * - * @return the name of the region - */ - public String getRegion() { - return region; - } - - /** - * Return the entire locale tag (i.e. "en_US") - * - * @return the language tag - */ - public String getLanguageTag() { - return name + "_" + region; - } - - /** - * Get the file that represents this locale - * - * @return the locale file (.lang) - */ - public File getFile() { - return file; - } - - /** - * Get a message set for a specific node - * - * @param node the node to get - * @return the message for the specified node - */ - public String getMessage(String node) { - return ChatColor.translateAlternateColorCodes('&', this.getMessageOrDefault(node, node)); - } - - /** - * Get a message set for a specific node and replace its params with a supplied arguments. - * - * @param node the node to get - * @param args the replacement arguments - * @return the message for the specified node - */ - public String getMessage(String node, Object... args) { - String message = getMessage(node); - for (Object arg : args) { - message = message.replaceFirst("\\%.*?\\%", arg.toString()); - } - return message; - } - - /** - * Get a message set for a specific node - * - * @param node the node to get - * @param defaultValue the default value given that a value for the node was not found - * @return the message for the specified node. Default if none found - */ - public String getMessageOrDefault(String node, String defaultValue) { - return this.nodes.getOrDefault(node, defaultValue); - } - - /** - * Get the key-value map of nodes to messages - * - * @return node-message map - */ - public Map getMessageNodeMap() { - return ImmutableMap.copyOf(nodes); - } - - /** - * Clear the previous message cache and load new messages directly from file - * - * @return reload messages from file - */ - public boolean reloadMessages() { - if (!this.file.exists()) { - plugin.getLogger().warning("Could not find file for locale " + this.name); - return false; - } - - this.nodes.clear(); // Clear previous data (if any) - - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line; - for (int lineNumber = 0; (line = reader.readLine()) != null; lineNumber++) { - if (line.isEmpty() || line.startsWith("#") /* Comment */) continue; - - Matcher matcher = NODE_PATTERN.matcher(line); - if (!matcher.find()) { - System.err.println("Invalid locale syntax at (line=" + lineNumber + ")"); - continue; - } - - nodes.put(matcher.group(1), matcher.group(2)); - } - } catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; + Bukkit.getConsoleSender().sendMessage("Loaded locale " + fileName); } /** @@ -191,7 +74,9 @@ public class Locale { * Find all .lang file locales under the "locales" folder */ public static void searchForLocales() { - if (!localeFolder.exists()) localeFolder.mkdirs(); + if (!localeFolder.exists()) { + localeFolder.mkdirs(); + } for (File file : localeFolder.listFiles()) { String name = file.getName(); @@ -204,7 +89,7 @@ public class Locale { if (localeExists(localeValues[0] + "_" + localeValues[1])) continue; LOCALES.add(new Locale(localeValues[0], localeValues[1])); - plugin.getLogger().info("Found and loaded locale \"" + fileName + "\""); + Bukkit.getConsoleSender().sendMessage("Found and loaded locale \"" + fileName + "\""); } } @@ -268,11 +153,11 @@ public class Locale { /** * Save a default locale file from the project source directory, to the locale folder * - * @param path the path to the file to save + * @param in file to save * @param fileName the name of the file to save * @return true if the operation was successful, false otherwise */ - public static boolean saveDefaultLocale(String path, String fileName) { + public static boolean saveDefaultLocale(InputStream in, String fileName) { if (!localeFolder.exists()) localeFolder.mkdirs(); if (!fileName.endsWith(FILE_EXTENSION)) @@ -284,7 +169,7 @@ public class Locale { } try (OutputStream outputStream = new FileOutputStream(destinationFile)) { - IOUtils.copy(plugin.getResource(fileName), outputStream); + copy(in == null ? plugin.getResource(fileName) : in, outputStream); fileName = fileName.substring(0, fileName.lastIndexOf('.')); String[] localeValues = fileName.split("_"); @@ -307,7 +192,7 @@ public class Locale { * @return true if the operation was successful, false otherwise */ public static boolean saveDefaultLocale(String fileName) { - return saveDefaultLocale("", fileName); + return saveDefaultLocale(null, fileName); } /** @@ -361,4 +246,136 @@ public class Locale { return changed; } + private static void copy(InputStream input, OutputStream output) { + int n; + byte[] buffer = new byte[1024 * 4]; + + try { + while ((n = input.read(buffer)) != -1) { + output.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Get the name of the language that this locale is based on. + * (i.e. "en" for English, or "fr" for French) + * + * @return the name of the language + */ + public String getName() { + return name; + } + + /** + * Get the name of the region that this locale is from. + * (i.e. "US" for United States or "CA" for Canada) + * + * @return the name of the region + */ + public String getRegion() { + return region; + } + + /** + * Return the entire locale tag (i.e. "en_US") + * + * @return the language tag + */ + public String getLanguageTag() { + return name + "_" + region; + } + + /** + * Get the file that represents this locale + * + * @return the locale file (.lang) + */ + public File getFile() { + return file; + } + + /** + * Get a message set for a specific node + * + * @param node the node to get + * @return the message for the specified node + */ + public String getMessage(String node) { + return ChatColor.translateAlternateColorCodes('&', this.getMessageOrDefault(node, node)); + } + + /** + * Get a message set for a specific node and replace its params with a supplied arguments. + * + * @param node the node to get + * @param args the replacement arguments + * @return the message for the specified node + */ + public String getMessage(String node, Object... args) { + String message = getMessage(node); + for (Object arg : args) { + message = message.replaceFirst("%.*?%", arg.toString()); + } + return message; + } + + /** + * Get a message set for a specific node + * + * @param node the node to get + * @param defaultValue the default value given that a value for the node was not found + * @return the message for the specified node. Default if none found + */ + public String getMessageOrDefault(String node, String defaultValue) { + return this.nodes.getOrDefault(node, defaultValue); + } + + /** + * Get the key-value map of nodes to messages + * + * @return node-message map + */ + public Map getMessageNodeMap() { + return ImmutableMap.copyOf(nodes); + } + + /** + * Clear the previous message cache and load new messages directly from file + * + * @return reload messages from file + */ + public boolean reloadMessages() { + if (!this.file.exists()) { + plugin.getLogger().warning("Could not find file for locale " + this.name); + return false; + } + + this.nodes.clear(); // Clear previous data (if any) + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line; + for (int lineNumber = 0; (line = reader.readLine()) != null; lineNumber++) { + if (line.isEmpty() || line.startsWith("#") /* Comment */) continue; + + Matcher matcher = NODE_PATTERN.matcher(line); + if (!matcher.find()) { + System.err.println("Invalid locale syntax at (line=" + lineNumber + ")"); + continue; + } + + nodes.put(matcher.group(1), matcher.group(2)); + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } + + public String getPrefix() { + return getMessage("general.nametag.prefix") + " "; + } } \ No newline at end of file diff --git a/main/java/com/songoda/epicheads/menu/ui/InventoryMenu.java b/main/java/com/songoda/epicheads/menu/ui/InventoryMenu.java index 1df24ac..549c900 100644 --- a/main/java/com/songoda/epicheads/menu/ui/InventoryMenu.java +++ b/main/java/com/songoda/epicheads/menu/ui/InventoryMenu.java @@ -26,7 +26,9 @@ public class InventoryMenu implements InventoryHolder { private Container container; private Inventory inventory; + private String title; private Inventory newInventory; + private String newTitle; public InventoryMenu(Player player, String title, int rows) { Checks.ensureNonNull(player, "player"); @@ -83,10 +85,11 @@ public class InventoryMenu implements InventoryHolder { public void setTitle(String title) { Checks.ensureNonNull(title, "title"); - if (inventory != null && title.equals(inventory.getTitle())) + if (inventory != null && title.equals(this.title)) return; title = (title.length() > 32 ? title.substring(0, 32) : title); + this.title = title; this.newInventory = Bukkit.createInventory(this, bounds.getVolume(), title); } @@ -96,6 +99,7 @@ public class InventoryMenu implements InventoryHolder { return false; inventory = newInventory; + title = newTitle; newInventory = null; return true; diff --git a/pom.xml b/pom.xml index ad6a7b2..4875a62 100644 --- a/pom.xml +++ b/pom.xml @@ -46,119 +46,29 @@ private - http://repo.songoda.com/repository/private + http://repo.songoda.com/artifactory/private/ org.spigotmc spigot - 1.13.1 - - - com.songoda - arconix - LATEST - - - org - kingdoms - LATEST + 1.14-pre5-2 net.milkbowl vault - LATEST - - - me.ryanhamshire - GriefPrevention - LATEST - - - com.sk89q - worldedit - LATEST - - - com.sk89q - worldguard - 7.0.0 - - - com - plotsquared - RELEASE - - - com.palmergames.bukkit - towny - LATEST - - - com.wasteofplastic - askyblock - 3.0.6.8 - - - us.talabrek - ultimateskyblock - LATEST - - - me.markeh - factionsframework - 1.2.0 - - - br.net.fabiozumbi12 - RedProtect - 7.3.0 - - - com.songoda - epicspawners - LATEST - - - com.gmail.nossr50 - mcmmo - LATEST - - - com.gamingmesh - jobs - LATEST - - - me.botsko - prism - LATEST - - - de.diddiz - logblock - LATEST - - - net - coreprotect - LATEST - - - uk.antiperson - stackmob - LATEST - - - net.sothatsit - blockstore - LATEST + 1.7.1 org.black_ixx playerpoints 2.1.4 + + net.sothatsit + blockstore + 1.5.0 +