diff --git a/src/main/java/com/songoda/ultimaterepairing/Locale.java b/src/main/java/com/songoda/ultimaterepairing/Locale.java index f6f1f07..6fbe570 100644 --- a/src/main/java/com/songoda/ultimaterepairing/Locale.java +++ b/src/main/java/com/songoda/ultimaterepairing/Locale.java @@ -150,11 +150,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)) @@ -166,7 +166,7 @@ public class Locale { } try (OutputStream outputStream = new FileOutputStream(destinationFile)) { - copy(plugin.getResource(fileName), outputStream); + copy(in == null ? plugin.getResource(fileName) : in, outputStream); fileName = fileName.substring(0, fileName.lastIndexOf('.')); String[] localeValues = fileName.split("_"); @@ -189,7 +189,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); } /** diff --git a/src/main/java/com/songoda/ultimaterepairing/UltimateRepairing.java b/src/main/java/com/songoda/ultimaterepairing/UltimateRepairing.java index 8ecf13c..38cd7f6 100644 --- a/src/main/java/com/songoda/ultimaterepairing/UltimateRepairing.java +++ b/src/main/java/com/songoda/ultimaterepairing/UltimateRepairing.java @@ -21,6 +21,14 @@ import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; public final class UltimateRepairing extends JavaPlugin implements Listener { private static CommandSender console = Bukkit.getConsoleSender(); @@ -79,10 +87,14 @@ public final class UltimateRepairing extends JavaPlugin implements Listener { settingsManager.updateSettings(); setupConfig(); - // Locales + String langMode = getConfig().getString("System.Language Mode"); Locale.init(this); Locale.saveDefaultLocale("en_US"); - this.locale = Locale.getLocale(this.getConfig().getString("Locale", "en_US")); + this.locale = Locale.getLocale(getConfig().getString("System.Language Mode", langMode)); + + if (getConfig().getBoolean("System.Download Needed Data Files")) { + this.update(); + } this.editor = new Editor(this); this.anvilManager = new AnvilManager(); @@ -126,6 +138,39 @@ public final class UltimateRepairing extends JavaPlugin implements Listener { saveToFile(); } + 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]; + StringBuffer sb = new StringBuffer(); + 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; + + switch ((String) file.get("type")) { + case "locale": + InputStream in = new URL((String) file.get("link")).openStream(); + Locale.saveDefaultLocale(in, (String) file.get("name")); + break; + } + } + } catch (Exception e) { + System.out.println("Failed to update."); + //e.printStackTrace(); + } + } + /* * Saves registered kits to file. */ diff --git a/src/main/java/com/songoda/ultimaterepairing/utils/SettingsManager.java b/src/main/java/com/songoda/ultimaterepairing/utils/SettingsManager.java index e840ca5..5fc5e60 100644 --- a/src/main/java/com/songoda/ultimaterepairing/utils/SettingsManager.java +++ b/src/main/java/com/songoda/ultimaterepairing/utils/SettingsManager.java @@ -224,6 +224,8 @@ public class SettingsManager implements Listener { o21("-", "Main.Particle Type", "SPELL_WITCH"), + DOWNLOAD_FILES("-", "System.Download Needed Data Files", true), + LANGUGE_MODE("-", "System.Language Mode", "en_US"), o22("Debug-Mode", "System.Debugger Enabled", false); private final String setting, oldSetting;