Add support for arbitrary filenames...
This commit is contained in:
@@ -252,7 +252,7 @@ public class ArenaMasterImpl implements ArenaMaster
|
|||||||
* Load the global settings.
|
* Load the global settings.
|
||||||
*/
|
*/
|
||||||
public void loadSettings() {
|
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");
|
ConfigSection section = config.getConfigSection("global-settings");
|
||||||
|
|
||||||
// Grab the commands string
|
// Grab the commands string
|
||||||
@@ -295,7 +295,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(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.
|
// 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.
|
// Create an Arena with the name and world.
|
||||||
Arena arena = new ArenaImpl(plugin, config, arenaname, world);
|
Arena arena = new ArenaImpl(plugin, config, arenaname, world);
|
||||||
@@ -596,14 +596,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(config, path + ".settings", "settings.yml");
|
ConfigUtils.replaceAllNodes(plugin.getFilename(), 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(config, path + ".waves", "waves.yml");
|
ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".waves", "waves.yml");
|
||||||
|
|
||||||
// Extract the default rewards.
|
// Extract the default rewards.
|
||||||
ConfigUtils.replaceAllNodes(config, path + ".rewards", "rewards.yml");
|
ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".rewards", "rewards.yml");
|
||||||
|
|
||||||
// Save the changes.
|
// Save the changes.
|
||||||
config.save();
|
config.save();
|
||||||
|
|||||||
@@ -231,7 +231,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(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;
|
double minor = item.getDurability() / 100D;
|
||||||
return major + minor;
|
return major + minor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFilename() {
|
||||||
|
return super.getFile().getName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -100,60 +100,13 @@ public class FileUtils
|
|||||||
return null;
|
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) {
|
private static String getFilename(String resource) {
|
||||||
int slash = resource.lastIndexOf("/");
|
int slash = resource.lastIndexOf("/");
|
||||||
return (slash < 0 ? resource : resource.substring(slash + 1));
|
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 RES = "res/";
|
||||||
|
private static final String PLUGINS = "plugins/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a YamlConfiguration of a given resource.
|
* Get a YamlConfiguration of a given resource.
|
||||||
@@ -162,9 +115,9 @@ public class FileUtils
|
|||||||
* @throws IOException if the resource does not exist
|
* @throws IOException if the resource does not exist
|
||||||
* @throws InvalidConfigurationException if the resource is not a valid config
|
* @throws InvalidConfigurationException if the resource is not a valid config
|
||||||
*/
|
*/
|
||||||
public static YamlConfiguration getConfig(String filename) throws IOException, InvalidConfigurationException {
|
public static YamlConfiguration getConfig(String filename, String resourcename) throws IOException, InvalidConfigurationException {
|
||||||
ZipFile zip = new ZipFile(JAR);
|
ZipFile zip = new ZipFile(PLUGINS + filename);
|
||||||
ZipEntry entry = zip.getEntry(RES + filename);
|
ZipEntry entry = zip.getEntry(RES + resourcename);
|
||||||
YamlConfiguration yaml = new YamlConfiguration();
|
YamlConfiguration yaml = new YamlConfiguration();
|
||||||
yaml.load(zip.getInputStream(entry));
|
yaml.load(zip.getInputStream(entry));
|
||||||
return yaml;
|
return yaml;
|
||||||
|
|||||||
@@ -11,15 +11,15 @@ import com.garbagemule.MobArena.util.FileUtils;
|
|||||||
|
|
||||||
public class ConfigUtils
|
public class ConfigUtils
|
||||||
{
|
{
|
||||||
public static void addMissingNodes(Config config, String path, String filename) {
|
public static void addMissingNodes(String filename, Config config, String path, String resourcename) {
|
||||||
assertNodes(config, path, filename, true);
|
assertNodes(filename, config, path, resourcename, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void replaceAllNodes(Config config, String path, String filename) {
|
public static void replaceAllNodes(String filename, Config config, String path, String resourcename) {
|
||||||
assertNodes(config, path, filename, false);
|
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.
|
// Grab the section that the path is pointing to.
|
||||||
ConfigSection section = config.getConfigSection(path);
|
ConfigSection section = config.getConfigSection(path);
|
||||||
|
|
||||||
@@ -31,13 +31,13 @@ public class ConfigUtils
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract the yml file.
|
// Extract the yml file.
|
||||||
YamlConfiguration ymlConfig = FileUtils.getConfig(filename);
|
YamlConfiguration ymlConfig = FileUtils.getConfig(filename, resourcename);
|
||||||
|
|
||||||
// Assert the nodes.
|
// Assert the nodes.
|
||||||
assertNodes(section, ymlConfig, keepOthers);
|
assertNodes(section, ymlConfig, keepOthers);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Messenger.severe("Failed to load '" + filename + "'. Restart required!");
|
Messenger.severe("Failed to load '" + resourcename + "'. Restart required!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user