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.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();

View File

@ -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");
}
}

View File

@ -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;
@ -150,24 +152,21 @@ public class FileUtils
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;
}
private static final String JAR = "plugins/MobArena.jar";
private static final String RES = "res/";
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;
/**
* 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;
}
}

View File

@ -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);
@ -28,11 +29,16 @@ public class ConfigUtils
section = config.getConfigSection(path);
}
try {
// Extract the yml file.
YamlConfiguration ymlConfig = FileUtils.getConfig(plugin, filename, plugin.getClass());
YamlConfiguration ymlConfig = FileUtils.getConfig(filename);
// 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) {