From 4d95b67cb1f4d3a71d11e0ac8e4a89d8c914d731 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 24 Jun 2013 14:48:33 +0200 Subject: [PATCH] Fix reload issues! Woooo! --- .../garbagemule/MobArena/ArenaMasterImpl.java | 14 +++--- src/com/garbagemule/MobArena/MobArena.java | 4 +- .../garbagemule/MobArena/util/FileUtils.java | 43 +++++++++---------- .../MobArena/util/config/ConfigUtils.java | 26 ++++++----- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index c51fc93..9e88b68 100644 --- a/src/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -19,8 +19,6 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.PluginManager; -import com.garbagemule.MobArena.ArenaImpl; -import com.garbagemule.MobArena.MobArena; import com.garbagemule.MobArena.ArenaClass.ArmorType; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.ArenaMaster; @@ -252,7 +250,7 @@ public class ArenaMasterImpl implements ArenaMaster * Load the global settings. */ public void loadSettings() { - ConfigUtils.replaceAllNodes(plugin, config, "global-settings", "global-settings.yml"); + ConfigUtils.replaceAllNodes(config, "global-settings", "global-settings.yml"); ConfigSection section = config.getConfigSection("global-settings"); // Grab the commands string @@ -295,7 +293,7 @@ public class ArenaMasterImpl implements ArenaMaster * Loads the classes in res/classes.yml into the config-file. */ public void loadDefaultClasses() { - ConfigUtils.addMissingNodes(plugin, config, "classes", "classes.yml"); + ConfigUtils.addMissingNodes(config, "classes", "classes.yml"); } /** @@ -553,7 +551,7 @@ public class ArenaMasterImpl implements ArenaMaster } // Assert all settings nodes. - ConfigUtils.replaceAllNodes(plugin, config, path + ".settings", "settings.yml"); + ConfigUtils.replaceAllNodes(config, path + ".settings", "settings.yml"); // Create an Arena with the name and world. Arena arena = new ArenaImpl(plugin, config, arenaname, world); @@ -578,14 +576,14 @@ public class ArenaMasterImpl implements ArenaMaster throw new IllegalArgumentException("Arena already exists!"); // Extract the default settings and update the world-node. - ConfigUtils.replaceAllNodes(plugin, config, path + ".settings", "settings.yml"); + ConfigUtils.replaceAllNodes(config, path + ".settings", "settings.yml"); config.set(path + ".settings.world", world.getName()); // Extract the default waves. - ConfigUtils.replaceAllNodes(plugin, config, path + ".waves", "waves.yml"); + ConfigUtils.replaceAllNodes(config, path + ".waves", "waves.yml"); // Extract the default rewards. - ConfigUtils.replaceAllNodes(plugin, config, path + ".rewards", "rewards.yml"); + ConfigUtils.replaceAllNodes(config, path + ".rewards", "rewards.yml"); // Save the changes. config.save(); diff --git a/src/com/garbagemule/MobArena/MobArena.java b/src/com/garbagemule/MobArena/MobArena.java index 5c56050..077d716 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -18,8 +18,6 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; -import com.garbagemule.MobArena.ArenaMasterImpl; -import com.garbagemule.MobArena.MAMessages; import com.garbagemule.MobArena.commands.CommandHandler; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.ArenaMaster; @@ -235,7 +233,7 @@ public class MobArena extends JavaPlugin for (String arena : arenas) { String path = "arenas." + arena + ".settings"; - ConfigUtils.replaceAllNodes(this, config, path, "settings.yml"); + ConfigUtils.replaceAllNodes(config, path, "settings.yml"); } } diff --git a/src/com/garbagemule/MobArena/util/FileUtils.java b/src/com/garbagemule/MobArena/util/FileUtils.java index 9f11081..0564d45 100644 --- a/src/com/garbagemule/MobArena/util/FileUtils.java +++ b/src/com/garbagemule/MobArena/util/FileUtils.java @@ -1,14 +1,16 @@ package com.garbagemule.MobArena.util; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; +import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import com.garbagemule.MobArena.Messenger; @@ -149,25 +151,22 @@ public class FileUtils int slash = resource.lastIndexOf("/"); return (slash < 0 ? resource : resource.substring(slash + 1)); } - - public static YamlConfiguration getConfig(MobArena plugin, String filename, Class cls) { - InputStream in = cls.getResourceAsStream("/res/" + filename); - if (in == null) { - Messenger.severe("Failed to load '" + filename + "', the server must be restarted!"); - return null; - } - try { - YamlConfiguration result = new YamlConfiguration(); - result.load(in); - - return result; - } - catch (Exception e) { - e.printStackTrace(); - Messenger.warning("Couldn't load '" + filename + "' as stream!"); - } - - return null; + private static final String JAR = "plugins/MobArena.jar"; + private static final String RES = "res/"; + + /** + * Get a YamlConfiguration of a given resource. + * @param filename the name of the resource + * @return a YamlConfiguration for the given resource + * @throws IOException if the resource does not exist + * @throws InvalidConfigurationException if the resource is not a valid config + */ + public static YamlConfiguration getConfig(String filename) throws IOException, InvalidConfigurationException { + ZipFile zip = new ZipFile(JAR); + ZipEntry entry = zip.getEntry(RES + filename); + YamlConfiguration yaml = new YamlConfiguration(); + yaml.load(zip.getInputStream(entry)); + return yaml; } } diff --git a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java index 4ff5a11..d5e592c 100644 --- a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java +++ b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java @@ -2,6 +2,7 @@ package com.garbagemule.MobArena.util.config; import java.util.Set; +import com.garbagemule.MobArena.Messenger; import org.bukkit.configuration.file.YamlConfiguration; import com.garbagemule.MobArena.MobArena; @@ -10,15 +11,15 @@ import com.garbagemule.MobArena.util.FileUtils; public class ConfigUtils { - public static void addMissingNodes(MobArena plugin, Config config, String path, String filename) { - assertNodes(plugin, config, path, filename, true); + public static void addMissingNodes(Config config, String path, String filename) { + assertNodes(config, path, filename, true); } - public static void replaceAllNodes(MobArena plugin, Config config, String path, String filename) { - assertNodes(plugin, config, path, filename, false); + public static void replaceAllNodes(Config config, String path, String filename) { + assertNodes(config, path, filename, false); } - private static void assertNodes(MobArena plugin, Config config, String path, String filename, boolean keepOthers) { + private static void assertNodes(Config config, String path, String filename, boolean keepOthers) { // Grab the section that the path is pointing to. ConfigSection section = config.getConfigSection(path); @@ -27,12 +28,17 @@ public class ConfigUtils config.set(path, ""); section = config.getConfigSection(path); } + + try { + // Extract the yml file. + YamlConfiguration ymlConfig = FileUtils.getConfig(filename); - // Extract the yml file. - YamlConfiguration ymlConfig = FileUtils.getConfig(plugin, filename, plugin.getClass()); - - // Assert the nodes. - assertNodes(section, ymlConfig, keepOthers); + // Assert the nodes. + assertNodes(section, ymlConfig, keepOthers); + } catch (Exception e) { + e.printStackTrace(); + Messenger.severe("Failed to load '" + filename + "'. Restart required!"); + } } private static void assertNodes(ConfigSection config, YamlConfiguration ymlConfig, boolean keepOthers) {