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 * @param file File
*/ */
public RoyalAuthYamlReader(File file) { public RoyalAuthYamlReader(File file) {
super(file); super(file, true);
load(); load();
save(); save();
} }

View File

@ -13,7 +13,7 @@ public class EssSpawn extends CustomConfiguration {
private static EssSpawn spawn; private static EssSpawn spawn;
public EssSpawn() { 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; spawn = this;
load(); load();
} }

View File

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

View File

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

View File

@ -17,7 +17,7 @@ public class OtherAccounts extends CustomConfiguration {
private static OtherAccounts others = null; private static OtherAccounts others = null;
public OtherAccounts() { 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; others = this;
load(); load();
save(); save();

View File

@ -14,7 +14,7 @@ public class Spawn extends CustomConfiguration {
private static Spawn spawn; private static Spawn spawn;
public 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; spawn = this;
load(); load();
save(); save();