Add support for arbitrary filenames...

This commit is contained in:
garbagemule 2013-08-15 04:45:07 +02:00
parent e99a2f3d44
commit 2475ae3633
4 changed files with 22 additions and 65 deletions

View File

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

View File

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

View File

@ -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<String> 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<String> result = new ArrayList<String>();
// 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<JarEntry> 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<String>(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;

View File

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