From 52599319c1431e86348c52b45510247302315459 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Sat, 22 Jun 2013 12:02:45 +0200 Subject: [PATCH] Attempt at fixing reload issues. --- src/com/garbagemule/MobArena/MobArena.java | 4 +-- .../listeners/MagicSpellsListener.java | 2 +- .../garbagemule/MobArena/util/FileUtils.java | 29 +++++-------------- .../MobArena/util/config/ConfigUtils.java | 2 +- .../waves/ability/AbilityManager.java | 8 ++--- 5 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/com/garbagemule/MobArena/MobArena.java b/src/com/garbagemule/MobArena/MobArena.java index 47825cf..5c56050 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -65,7 +65,7 @@ public class MobArena extends JavaPlugin public void onEnable() { // Create default files and initialize config-file - FileUtils.extractResource(this.getDataFolder(), "config.yml"); + FileUtils.extractResource(this.getDataFolder(), "config.yml", getClass()); loadConfigFile(); // Load boss abilities @@ -205,7 +205,7 @@ public class MobArena extends JavaPlugin File dir = new File(this.getDataFolder(), "abilities"); if (!dir.exists()) dir.mkdir(); - AbilityManager.loadAbilities(dir); + AbilityManager.loadAbilities(dir, getClass()); } private void startMetrics() { diff --git a/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java b/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java index 2947740..b140d6d 100644 --- a/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java +++ b/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java @@ -25,7 +25,7 @@ public class MagicSpellsListener implements Listener this.plugin = plugin; // Set up the MagicSpells config-file. - File spellFile = FileUtils.extractResource(plugin.getDataFolder(), "magicspells.yml"); + File spellFile = FileUtils.extractResource(plugin.getDataFolder(), "magicspells.yml", plugin.getClass()); Config spellConfig = new Config(spellFile); spellConfig.load(); setupSpells(spellConfig); diff --git a/src/com/garbagemule/MobArena/util/FileUtils.java b/src/com/garbagemule/MobArena/util/FileUtils.java index 08e9d6c..9f11081 100644 --- a/src/com/garbagemule/MobArena/util/FileUtils.java +++ b/src/com/garbagemule/MobArena/util/FileUtils.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; @@ -25,11 +24,11 @@ public class FileUtils * @param resources a list of resources to extract * @return a list of all the files that were written */ - public static List extractResources(File dir, List resources) { - return extractResources(dir, "", resources); + public static List extractResources(File dir, List resources, Class cls) { + return extractResources(dir, "", resources, cls); } - public static List extractResources(File dir, String path, List filenames) { + public static List extractResources(File dir, String path, List filenames, Class cls) { List files = new ArrayList(); // If the path is empty, just forget about it. @@ -47,7 +46,7 @@ public class FileUtils // Extract each resource for (String filename : filenames) { - File file = extractResource(dir, path + filename); + File file = extractResource(dir, path + filename, cls); if (file != null) { files.add(file); @@ -55,18 +54,6 @@ public class FileUtils } return files; } - - /** - * Extracts all of the given resources to the given directory. - * Convenience method, used if one is too lazy to create a new list for - * the resource names. - * @param dir a directory - * @param resources an array of resources to extract - * @return a list of all the files that were written - */ - public static List extractResources(File dir, String... resources) { - return extractResources(dir, Arrays.asList(resources)); - } /** * Extracts the given resource to the given directory. @@ -74,7 +61,7 @@ public class FileUtils * @param resource a resource to extract * @return the file that was written, or null */ - public static File extractResource(File dir, String resource) { + public static File extractResource(File dir, String resource, Class cls) { if (!dir.exists()) dir.mkdirs(); // Set up our new file. @@ -85,7 +72,7 @@ public class FileUtils if (file.exists()) return file; // Grab the resource input stream. - InputStream in = MobArena.class.getResourceAsStream("/res/" + resource); + InputStream in = cls.getResourceAsStream("/res/" + resource); if (in == null) return null; try { @@ -163,8 +150,8 @@ public class FileUtils return (slash < 0 ? resource : resource.substring(slash + 1)); } - public static YamlConfiguration getConfig(MobArena plugin, String filename) { - InputStream in = MobArena.class.getResourceAsStream("/res/" + filename); + 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; diff --git a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java index f7627d2..4ff5a11 100644 --- a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java +++ b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java @@ -29,7 +29,7 @@ public class ConfigUtils } // Extract the yml file. - YamlConfiguration ymlConfig = FileUtils.getConfig(plugin, filename); + YamlConfiguration ymlConfig = FileUtils.getConfig(plugin, filename, plugin.getClass()); // Assert the nodes. assertNodes(section, ymlConfig, keepOthers); diff --git a/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java b/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java index 4b05e3a..2d75e9b 100644 --- a/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java +++ b/src/com/garbagemule/MobArena/waves/ability/AbilityManager.java @@ -38,7 +38,7 @@ public class AbilityManager * the specified directory. * @param dir a directory of .class (and/or .java) files */ - public static void loadAbilities(File classDir) { + public static void loadAbilities(File classDir, Class cls) { abilities = new HashMap(); // Grab the source directory. @@ -62,14 +62,14 @@ public class AbilityManager String[] files = classDir.list(); if (files.length == 0 || (files.length == 1 && files[0].equals("src"))) { Messenger.info("No boss abilities found. Extracting defaults..."); - extractDefaultAbilities(classDir); + extractDefaultAbilities(classDir, cls); } // Load all the custom abilities. loadClasses(classDir); } - private static void extractDefaultAbilities(File classDir) { + private static void extractDefaultAbilities(File classDir, Class cls) { // Grab a list of all the class files. List resources = FileUtils.listFilesOnPath("res/abilities/", ".class"); @@ -80,7 +80,7 @@ public class AbilityManager } // Extract everything. - List files = FileUtils.extractResources(classDir, "abilities/", resources); + List files = FileUtils.extractResources(classDir, "abilities/", resources, cls); Messenger.info("Extracted abilities: " + fileListToString(files, "$")); }