Fix #296 Reloading doesn't update settings

- Replace Settings to encapsulate a YamlConfiguration instance provided by AuthMe, rather than loading the config file again.
This commit is contained in:
ljacqu 2015-12-02 21:42:39 +01:00
parent 75d4f73f3e
commit cafad5b088

View File

@ -17,7 +17,7 @@ import java.util.regex.Pattern;
/** /**
*/ */
public final class Settings extends YamlConfiguration { public final class Settings {
public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder(); public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder();
public static final File MODULE_FOLDER = new File(PLUGIN_FOLDER, "modules"); public static final File MODULE_FOLDER = new File(PLUGIN_FOLDER, "modules");
@ -105,18 +105,13 @@ public final class Settings extends YamlConfiguration {
configFile = (YamlConfiguration) plugin.getConfig(); configFile = (YamlConfiguration) plugin.getConfig();
} }
/**
* Method reload.
*
* @throws Exception
*/
public static void reload() throws Exception { public static void reload() throws Exception {
plugin.getLogger().info("Loading Configuration File..."); plugin.getLogger().info("Loading Configuration File...");
boolean exist = SETTINGS_FILE.exists(); boolean exist = SETTINGS_FILE.exists();
if (!exist) { if (!exist) {
plugin.saveDefaultConfig(); plugin.saveDefaultConfig();
} }
instance.load(SETTINGS_FILE); configFile.load(SETTINGS_FILE);
if (exist) { if (exist) {
instance.mergeConfig(); instance.mergeConfig();
} }
@ -290,22 +285,11 @@ public final class Settings extends YamlConfiguration {
} }
/**
* Method setValue.
*
* @param key String
* @param value Object
*/
public static void setValue(String key, Object value) { public static void setValue(String key, Object value) {
instance.set(key, value); instance.set(key, value);
save(); save();
} }
/**
* Method getPasswordHash.
*
* @return HashAlgorithm
*/
private static HashAlgorithm getPasswordHash() { private static HashAlgorithm getPasswordHash() {
String key = "settings.security.passwordHash"; String key = "settings.security.passwordHash";
try { try {
@ -316,11 +300,6 @@ public final class Settings extends YamlConfiguration {
} }
} }
/**
* Method getDataSource.
*
* @return DataSourceType
*/
private static DataSourceType getDataSource() { private static DataSourceType getDataSource() {
String key = "DataSource.backend"; String key = "DataSource.backend";
try { try {
@ -367,20 +346,13 @@ public final class Settings extends YamlConfiguration {
*/ */
public static boolean save() { public static boolean save() {
try { try {
instance.save(SETTINGS_FILE); configFile.save(SETTINGS_FILE);
return true; return true;
} catch (Exception ex) { } catch (IOException ex) {
return false; return false;
} }
} }
/**
* Method checkLang.
*
* @param lang String
*
* @return String
*/
public static String checkLang(String lang) { public static String checkLang(String lang) {
if (new File(PLUGIN_FOLDER, "messages" + File.separator + "messages_" + lang + ".yml").exists()) { if (new File(PLUGIN_FOLDER, "messages" + File.separator + "messages_" + lang + ".yml").exists()) {
ConsoleLogger.info("Set Language to: " + lang); ConsoleLogger.info("Set Language to: " + lang);
@ -394,11 +366,6 @@ public final class Settings extends YamlConfiguration {
return "en"; return "en";
} }
/**
* Method switchAntiBotMod.
*
* @param mode boolean
*/
public static void switchAntiBotMod(boolean mode) { public static void switchAntiBotMod(boolean mode) {
if (mode) { if (mode) {
isKickNonRegisteredEnabled = true; isKickNonRegisteredEnabled = true;
@ -440,13 +407,6 @@ public final class Settings extends YamlConfiguration {
} }
} }
/**
* Method isEmailCorrect.
*
* @param email String
*
* @return boolean
*/
public static boolean isEmailCorrect(String email) { public static boolean isEmailCorrect(String email) {
if (!email.contains("@")) if (!email.contains("@"))
return false; return false;
@ -587,7 +547,7 @@ public final class Settings extends YamlConfiguration {
set("VeryGames.enableIpCheck", false); set("VeryGames.enableIpCheck", false);
changes = true; changes = true;
} }
if (getString("settings.restrictions.allowedNicknameCharacters").equals("[a-zA-Z0-9_?]*")) { if (configFile.getString("settings.restrictions.allowedNicknameCharacters").equals("[a-zA-Z0-9_?]*")) {
set("settings.restrictions.allowedNicknameCharacters", "[a-zA-Z0-9_]*"); set("settings.restrictions.allowedNicknameCharacters", "[a-zA-Z0-9_]*");
changes = true; changes = true;
} }
@ -682,6 +642,15 @@ public final class Settings extends YamlConfiguration {
} }
} }
private static boolean contains(String path) {
return configFile.contains(path);
}
// public because it's used in AuthMe at one place
public void set(String path, Object value) {
configFile.set(path, value);
}
/** /**
* Saves current configuration (plus defaults) to disk. * Saves current configuration (plus defaults) to disk.
* <p/> * <p/>
@ -690,11 +659,13 @@ public final class Settings extends YamlConfiguration {
* @return True if saved successfully * @return True if saved successfully
*/ */
public final boolean saveDefaults() { public final boolean saveDefaults() {
options().copyDefaults(true); configFile.options()
options().copyHeader(true); .copyDefaults(true)
.copyHeader(true);
boolean success = save(); boolean success = save();
options().copyDefaults(false); configFile.options()
options().copyHeader(false); .copyDefaults(false)
.copyHeader(false);
return success; return success;
} }
} }