2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.config;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-03-01 06:52:01 +01:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
2019-01-15 07:11:58 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.List;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
public abstract class ConfigLoader {
|
|
|
|
protected static final mcMMO plugin = mcMMO.p;
|
|
|
|
protected String fileName;
|
2013-10-07 19:09:46 +02:00
|
|
|
private File configFile;
|
2013-03-01 06:52:01 +01:00
|
|
|
protected FileConfiguration config;
|
|
|
|
|
|
|
|
public ConfigLoader(String relativePath, String fileName) {
|
|
|
|
this.fileName = fileName;
|
|
|
|
configFile = new File(plugin.getDataFolder(), relativePath + File.separator + fileName);
|
|
|
|
loadFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConfigLoader(String fileName) {
|
|
|
|
this.fileName = fileName;
|
|
|
|
configFile = new File(plugin.getDataFolder(), fileName);
|
|
|
|
loadFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void loadFile() {
|
|
|
|
if (!configFile.exists()) {
|
2013-04-13 21:53:00 +02:00
|
|
|
plugin.debug("Creating mcMMO " + fileName + " File...");
|
2013-10-18 16:12:55 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
plugin.saveResource(fileName, false); // Normal files
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
plugin.saveResource(configFile.getParentFile().getName() + File.separator + fileName, false); // Mod files
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-04-13 21:53:00 +02:00
|
|
|
plugin.debug("Loading mcMMO " + fileName + " File...");
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
config = YamlConfiguration.loadConfiguration(configFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void loadKeys();
|
|
|
|
|
2013-08-20 21:51:12 +02:00
|
|
|
protected boolean validateKeys() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-20 21:15:42 +02:00
|
|
|
protected boolean noErrorsInConfig(List<String> issues) {
|
|
|
|
for (String issue : issues) {
|
|
|
|
plugin.getLogger().warning(issue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues.isEmpty();
|
|
|
|
}
|
2013-08-20 21:51:12 +02:00
|
|
|
|
|
|
|
protected void validate() {
|
|
|
|
if (validateKeys()) {
|
|
|
|
plugin.debug("No errors found in " + fileName + "!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
plugin.getLogger().warning("Errors were found in " + fileName + "! mcMMO was disabled!");
|
|
|
|
plugin.getServer().getPluginManager().disablePlugin(plugin);
|
|
|
|
plugin.noErrorsInConfigFiles = false;
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 16:42:19 +02:00
|
|
|
|
|
|
|
public File getFile() {
|
|
|
|
return configFile;
|
|
|
|
}
|
2013-10-07 19:09:46 +02:00
|
|
|
|
|
|
|
public void backup() {
|
|
|
|
plugin.getLogger().warning("You are using an old version of the " + fileName + " file.");
|
|
|
|
plugin.getLogger().warning("Your old file has been renamed to " + fileName + ".old and has been replaced by an updated version.");
|
|
|
|
|
|
|
|
configFile.renameTo(new File(configFile.getPath() + ".old"));
|
|
|
|
|
|
|
|
if (plugin.getResource(fileName) != null) {
|
|
|
|
plugin.saveResource(fileName, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.getLogger().warning("Reloading " + fileName + " with new values...");
|
|
|
|
loadFile();
|
|
|
|
loadKeys();
|
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|