From 2475ae3633eb7a7417863a0496a1b641b11ecb6b Mon Sep 17 00:00:00 2001 From: garbagemule Date: Thu, 15 Aug 2013 04:45:07 +0200 Subject: [PATCH] Add support for arbitrary filenames... --- .../garbagemule/MobArena/ArenaMasterImpl.java | 12 ++-- src/com/garbagemule/MobArena/MobArena.java | 6 +- .../garbagemule/MobArena/util/FileUtils.java | 55 ++----------------- .../MobArena/util/config/ConfigUtils.java | 14 ++--- 4 files changed, 22 insertions(+), 65 deletions(-) diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index 603229d..7ce5937 100644 --- a/src/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -252,7 +252,7 @@ public class ArenaMasterImpl implements ArenaMaster * Load the global settings. */ public void loadSettings() { - ConfigUtils.replaceAllNodes(config, "global-settings", "global-settings.yml"); + ConfigUtils.replaceAllNodes(plugin.getFilename(), config, "global-settings", "global-settings.yml"); ConfigSection section = config.getConfigSection("global-settings"); // Grab the commands string @@ -295,7 +295,7 @@ public class ArenaMasterImpl implements ArenaMaster * Loads the classes in res/classes.yml into the config-file. */ public void loadDefaultClasses() { - ConfigUtils.addMissingNodes(config, "classes", "classes.yml"); + ConfigUtils.addMissingNodes(plugin.getFilename(), config, "classes", "classes.yml"); } /** @@ -573,7 +573,7 @@ public class ArenaMasterImpl implements ArenaMaster } // Assert all settings nodes. - ConfigUtils.replaceAllNodes(config, path + ".settings", "settings.yml"); + ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".settings", "settings.yml"); // Create an Arena with the name and world. Arena arena = new ArenaImpl(plugin, config, arenaname, world); @@ -596,14 +596,14 @@ public class ArenaMasterImpl implements ArenaMaster throw new IllegalArgumentException("Arena already exists!"); // Extract the default settings and update the world-node. - ConfigUtils.replaceAllNodes(config, path + ".settings", "settings.yml"); + ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".settings", "settings.yml"); config.set(path + ".settings.world", world.getName()); // Extract the default waves. - ConfigUtils.replaceAllNodes(config, path + ".waves", "waves.yml"); + ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".waves", "waves.yml"); // Extract the default rewards. - ConfigUtils.replaceAllNodes(config, path + ".rewards", "rewards.yml"); + ConfigUtils.replaceAllNodes(plugin.getFilename(), 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 da70408..3e8f53f 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -231,7 +231,7 @@ public class MobArena extends JavaPlugin for (String arena : arenas) { String path = "arenas." + arena + ".settings"; - ConfigUtils.replaceAllNodes(config, path, "settings.yml"); + ConfigUtils.replaceAllNodes(getFilename(), config, path, "settings.yml"); } } @@ -303,4 +303,8 @@ public class MobArena extends JavaPlugin double minor = item.getDurability() / 100D; return major + minor; } + + public String getFilename() { + return super.getFile().getName(); + } } \ No newline at end of file diff --git a/src/com/garbagemule/MobArena/util/FileUtils.java b/src/com/garbagemule/MobArena/util/FileUtils.java index 0564d45..9b2e060 100644 --- a/src/com/garbagemule/MobArena/util/FileUtils.java +++ b/src/com/garbagemule/MobArena/util/FileUtils.java @@ -100,60 +100,13 @@ public class FileUtils return null; } - /** - * Lists all files in the MobArena jar file that exist on the given path. - * The resulting list contains only filenames (with extensions) - * @param path the path of the jar file to list files from - * @param ext the file extension of the file type, can be null or the empty string - * @return a list of file names - */ - public static List listFilesOnPath(String path, String ext) { - try { - // If the jar can't be found for some odd reason, escape. - File file = new File("plugins" + File.separator + "MobArena.jar"); - if (file == null || !file.exists()) return null; - - // Create a JarFile out of the.. jar file - JarFile jarFile = new JarFile(file); - List result = new ArrayList(); - - // JarEntry names never start with / - if (path.startsWith("/")) { - path = path.substring(1); - } - - // If null, replace with the empty string. - if (ext == null) { - ext = ""; - } - // Require that extensions start with a period - else if (!ext.startsWith(".")) { - ext = "." + ext; - } - - // Loop through all entries, add the ones that match the path and extension - for (Enumeration entries = jarFile.entries(); entries.hasMoreElements(); ) { - String name = entries.nextElement().getName(); - - if (name.startsWith(path) && name.endsWith(ext)) { - result.add(getFilename(name)); - } - } - return result; - } - catch (Exception e) { - e.printStackTrace(); - return new ArrayList(1); - } - } - private static String getFilename(String resource) { int slash = resource.lastIndexOf("/"); return (slash < 0 ? resource : resource.substring(slash + 1)); } - private static final String JAR = "plugins/MobArena.jar"; private static final String RES = "res/"; + private static final String PLUGINS = "plugins/"; /** * Get a YamlConfiguration of a given resource. @@ -162,9 +115,9 @@ public class FileUtils * @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); + public static YamlConfiguration getConfig(String filename, String resourcename) throws IOException, InvalidConfigurationException { + ZipFile zip = new ZipFile(PLUGINS + filename); + ZipEntry entry = zip.getEntry(RES + resourcename); 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 d5e592c..aeb3cda 100644 --- a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java +++ b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java @@ -11,15 +11,15 @@ import com.garbagemule.MobArena.util.FileUtils; public class ConfigUtils { - public static void addMissingNodes(Config config, String path, String filename) { - assertNodes(config, path, filename, true); + public static void addMissingNodes(String filename, Config config, String path, String resourcename) { + assertNodes(filename, config, path, resourcename, true); } - public static void replaceAllNodes(Config config, String path, String filename) { - assertNodes(config, path, filename, false); + public static void replaceAllNodes(String filename, Config config, String path, String resourcename) { + assertNodes(filename, config, path, resourcename, false); } - private static void assertNodes(Config config, String path, String filename, boolean keepOthers) { + private static void assertNodes(String filename, Config config, String path, String resourcename, boolean keepOthers) { // Grab the section that the path is pointing to. ConfigSection section = config.getConfigSection(path); @@ -31,13 +31,13 @@ public class ConfigUtils try { // Extract the yml file. - YamlConfiguration ymlConfig = FileUtils.getConfig(filename); + YamlConfiguration ymlConfig = FileUtils.getConfig(filename, resourcename); // Assert the nodes. assertNodes(section, ymlConfig, keepOthers); } catch (Exception e) { e.printStackTrace(); - Messenger.severe("Failed to load '" + filename + "'. Restart required!"); + Messenger.severe("Failed to load '" + resourcename + "'. Restart required!"); } }