Improved and updated the custom configuration class

This commit is contained in:
Tim Visée 2015-11-25 01:32:11 +01:00
parent 2c2dd8e0d5
commit d7328466b8
6 changed files with 60 additions and 13 deletions

View File

@ -14,7 +14,7 @@ public class RoyalAuthYamlReader extends CustomConfiguration {
* @param file File
*/
public RoyalAuthYamlReader(File file) {
super(file);
super(file, true);
load();
save();
}

View File

@ -13,7 +13,7 @@ public class EssSpawn extends CustomConfiguration {
private static EssSpawn spawn;
public EssSpawn() {
super(new File("." + File.separator + "plugins" + File.separator + "Essentials" + File.separator + "spawn.yml"));
super(new File("." + File.separator + "plugins" + File.separator + "Essentials" + File.separator + "spawn.yml"), true);
spawn = this;
load();
}

View File

@ -12,36 +12,70 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
*/
public abstract class CustomConfiguration extends YamlConfiguration {
/**
* The file of the configuration.
*/
private final File configFile;
/**
* Constructor for CustomConfiguration.
* Constructor.
* This loads the configuration file.
*
* @param file the config file
* @param file The file of the configuration.
*/
public CustomConfiguration(File file) {
this.configFile = file;
load();
this(file, true);
}
/**
* Constructor.
*
* @param file The file of the configuration.
* @param load True to load the configuration file.
*/
public CustomConfiguration(File file, boolean load) {
// Set the configuration file
this.configFile = file;
// Load the configuration file
if(load)
load();
}
/**
* Load the configuration.
*/
public void load() {
// Try to load the configuration, catch exceptions
try {
// Load the configuration
super.load(configFile);
} catch (FileNotFoundException e) {
// Show an error message
ConsoleLogger.showError("Could not find " + configFile.getName() + ", creating new one...");
reLoad();
// Reload the configuration and create a new file
reload();
} catch (IOException e) {
// Show an error message
ConsoleLogger.showError("Could not load " + configFile.getName());
} catch (InvalidConfigurationException e) {
// Show an error message
ConsoleLogger.showError(configFile.getName() + " is no valid configuration file");
}
}
public boolean reLoad() {
/**
* Reload the configuration.
*
* @return
*/
public boolean reload() {
boolean out = true;
if (!configFile.exists()) {
out = loadResource(configFile);
@ -51,14 +85,27 @@ public abstract class CustomConfiguration extends YamlConfiguration {
return out;
}
/**
* Save the configuration.
*/
public void save() {
// Try to save the configuration, catch exceptions
try {
// Save the configuration
super.save(configFile);
} catch (IOException ex) {
// Show an error message
ConsoleLogger.showError("Could not save config to " + configFile.getName());
}
}
/**
* Get the configuration file.
*
* @return File.
*/
public File getConfigFile() {
return configFile;
}

View File

@ -19,7 +19,7 @@ public class Messages extends CustomConfiguration {
* @param lang the code of the language to use
*/
public Messages(File file, String lang) {
super(file);
super(file, true);
load();
singleton = this;
this.lang = lang;

View File

@ -17,7 +17,7 @@ public class OtherAccounts extends CustomConfiguration {
private static OtherAccounts others = null;
public OtherAccounts() {
super(new File("." + File.separator + "plugins" + File.separator + "AuthMe" + File.separator + "otheraccounts.yml"));
super(new File("." + File.separator + "plugins" + File.separator + "AuthMe" + File.separator + "otheraccounts.yml"), true);
others = this;
load();
save();

View File

@ -14,7 +14,7 @@ public class Spawn extends CustomConfiguration {
private static Spawn spawn;
public Spawn() {
super(new File("." + File.separator + "plugins" + File.separator + "AuthMe" + File.separator + "spawn.yml"));
super(new File("." + File.separator + "plugins" + File.separator + "AuthMe" + File.separator + "spawn.yml"), true);
spawn = this;
load();
save();