Fix reload issues! Woooo!

This commit is contained in:
garbagemule 2013-06-24 14:48:33 +02:00
parent 3386ba3794
commit 4d95b67cb1
4 changed files with 44 additions and 43 deletions

View File

@ -19,8 +19,6 @@ import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager; 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.ArenaClass.ArmorType;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster; import com.garbagemule.MobArena.framework.ArenaMaster;
@ -252,7 +250,7 @@ public class ArenaMasterImpl implements ArenaMaster
* Load the global settings. * Load the global settings.
*/ */
public void loadSettings() { 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"); ConfigSection section = config.getConfigSection("global-settings");
// Grab the commands string // Grab the commands string
@ -295,7 +293,7 @@ public class ArenaMasterImpl implements ArenaMaster
* Loads the classes in res/classes.yml into the config-file. * Loads the classes in res/classes.yml into the config-file.
*/ */
public void loadDefaultClasses() { 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. // 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. // Create an Arena with the name and world.
Arena arena = new ArenaImpl(plugin, config, arenaname, world); Arena arena = new ArenaImpl(plugin, config, arenaname, world);
@ -578,14 +576,14 @@ public class ArenaMasterImpl implements ArenaMaster
throw new IllegalArgumentException("Arena already exists!"); throw new IllegalArgumentException("Arena already exists!");
// Extract the default settings and update the world-node. // 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()); config.set(path + ".settings.world", world.getName());
// Extract the default waves. // Extract the default waves.
ConfigUtils.replaceAllNodes(plugin, config, path + ".waves", "waves.yml"); ConfigUtils.replaceAllNodes(config, path + ".waves", "waves.yml");
// Extract the default rewards. // Extract the default rewards.
ConfigUtils.replaceAllNodes(plugin, config, path + ".rewards", "rewards.yml"); ConfigUtils.replaceAllNodes(config, path + ".rewards", "rewards.yml");
// Save the changes. // Save the changes.
config.save(); config.save();

View File

@ -18,8 +18,6 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin; 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.commands.CommandHandler;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.framework.ArenaMaster; import com.garbagemule.MobArena.framework.ArenaMaster;
@ -235,7 +233,7 @@ public class MobArena extends JavaPlugin
for (String arena : arenas) { for (String arena : arenas) {
String path = "arenas." + arena + ".settings"; String path = "arenas." + arena + ".settings";
ConfigUtils.replaceAllNodes(this, config, path, "settings.yml"); ConfigUtils.replaceAllNodes(config, path, "settings.yml");
} }
} }

View File

@ -1,14 +1,16 @@
package com.garbagemule.MobArena.util; package com.garbagemule.MobArena.util;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; 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 org.bukkit.configuration.file.YamlConfiguration;
import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.Messenger;
@ -149,25 +151,22 @@ public class FileUtils
int slash = resource.lastIndexOf("/"); int slash = resource.lastIndexOf("/");
return (slash < 0 ? resource : resource.substring(slash + 1)); 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 { private static final String JAR = "plugins/MobArena.jar";
YamlConfiguration result = new YamlConfiguration(); private static final String RES = "res/";
result.load(in);
/**
return result; * Get a YamlConfiguration of a given resource.
} * @param filename the name of the resource
catch (Exception e) { * @return a YamlConfiguration for the given resource
e.printStackTrace(); * @throws IOException if the resource does not exist
Messenger.warning("Couldn't load '" + filename + "' as stream!"); * @throws InvalidConfigurationException if the resource is not a valid config
} */
public static YamlConfiguration getConfig(String filename) throws IOException, InvalidConfigurationException {
return null; ZipFile zip = new ZipFile(JAR);
ZipEntry entry = zip.getEntry(RES + filename);
YamlConfiguration yaml = new YamlConfiguration();
yaml.load(zip.getInputStream(entry));
return yaml;
} }
} }

View File

@ -2,6 +2,7 @@ package com.garbagemule.MobArena.util.config;
import java.util.Set; import java.util.Set;
import com.garbagemule.MobArena.Messenger;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import com.garbagemule.MobArena.MobArena; import com.garbagemule.MobArena.MobArena;
@ -10,15 +11,15 @@ import com.garbagemule.MobArena.util.FileUtils;
public class ConfigUtils public class ConfigUtils
{ {
public static void addMissingNodes(MobArena plugin, Config config, String path, String filename) { public static void addMissingNodes(Config config, String path, String filename) {
assertNodes(plugin, config, path, filename, true); assertNodes(config, path, filename, true);
} }
public static void replaceAllNodes(MobArena plugin, Config config, String path, String filename) { public static void replaceAllNodes(Config config, String path, String filename) {
assertNodes(plugin, config, path, filename, false); 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. // Grab the section that the path is pointing to.
ConfigSection section = config.getConfigSection(path); ConfigSection section = config.getConfigSection(path);
@ -27,12 +28,17 @@ public class ConfigUtils
config.set(path, ""); config.set(path, "");
section = config.getConfigSection(path); section = config.getConfigSection(path);
} }
try {
// Extract the yml file.
YamlConfiguration ymlConfig = FileUtils.getConfig(filename);
// Extract the yml file. // Assert the nodes.
YamlConfiguration ymlConfig = FileUtils.getConfig(plugin, filename, plugin.getClass()); assertNodes(section, ymlConfig, keepOthers);
} catch (Exception e) {
// Assert the nodes. e.printStackTrace();
assertNodes(section, ymlConfig, keepOthers); Messenger.severe("Failed to load '" + filename + "'. Restart required!");
}
} }
private static void assertNodes(ConfigSection config, YamlConfiguration ymlConfig, boolean keepOthers) { private static void assertNodes(ConfigSection config, YamlConfiguration ymlConfig, boolean keepOthers) {