diff --git a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java index 1d56239ef..214ea0fdb 100644 --- a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java +++ b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java @@ -1,6 +1,6 @@ package com.gmail.nossr50.config; -public class AdvancedConfig extends ConfigLoader { +public class AdvancedConfig extends AutoUpdateConfigLoader { private static AdvancedConfig instance; private AdvancedConfig() { diff --git a/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java b/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java new file mode 100644 index 000000000..74e84cc4f --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/AutoUpdateConfigLoader.java @@ -0,0 +1,128 @@ +package com.gmail.nossr50.config; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import com.gmail.nossr50.util.metrics.MetricsManager; + +public abstract class AutoUpdateConfigLoader extends ConfigLoader { + public AutoUpdateConfigLoader(String relativePath, String fileName) { + super(relativePath, fileName); + } + + public AutoUpdateConfigLoader(String fileName) { + super(fileName); + } + + @Override + protected void loadFile() { + super.loadFile(); + FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(plugin.getResource(fileName)); + + Set configKeys = config.getKeys(true); + Set internalConfigKeys = internalConfig.getKeys(true); + + boolean needSave = false; + + Set oldKeys = new HashSet(configKeys); + oldKeys.removeAll(internalConfigKeys); + + Set newKeys = new HashSet(internalConfigKeys); + newKeys.removeAll(configKeys); + + // Don't need a re-save if we have old keys sticking around? + // Would be less saving, but less... correct? + if (!newKeys.isEmpty() || !oldKeys.isEmpty()) { + needSave = true; + } + + for (String key : oldKeys) { + plugin.debug("Removing unused key: " + key); + config.set(key, null); + } + + for (String key : newKeys) { + plugin.debug("Adding new key: " + key + " = " + internalConfig.get(key)); + config.set(key, internalConfig.get(key)); + } + + if (needSave) { + // Get Bukkit's version of an acceptable config with new keys, and no old keys + String output = config.saveToString(); + + // Convert to the superior 4 space indentation + output = output.replace(" ", " "); + + // Rip out Bukkit's attempt to save comments at the top of the file + while (output.indexOf('#') != -1) { + output = output.substring(output.indexOf('\n', output.indexOf('#'))+1); + } + + // Read the internal config to get comments, then put them in the new one + try { + // Read internal + BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName))); + HashMap comments = new HashMap(); + String temp = ""; + + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("#")) { + temp += line + "\n"; + } + else if (line.contains(":")) { + line = line.substring(0, line.indexOf(":") + 1); + if(!temp.isEmpty()) { + comments.put(line, temp); + temp = ""; + } + } + } + + // Dump to the new one + for (String key : comments.keySet()) { + if (output.indexOf(key) != -1) { + output = output.substring(0, output.indexOf(key)) + comments.get(key) + output.substring(output.indexOf(key)); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Save it + try { + String saveName = fileName; + // At this stage we cannot guarantee that Config has been loaded, so we do the check directly here + if (!plugin.getConfig().getBoolean("General.Config_Update_Overwrite", true)) { + saveName += ".new"; + } + + BufferedWriter writer = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), saveName))); + writer.write(output); + writer.flush(); + writer.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + else { + for (String key : configKeys) { + if (!config.isConfigurationSection(key) && !config.get(key).equals(internalConfig.get(key))) { + MetricsManager.customConfig(); + break; + } + } + } + } +} diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index 3a03bbbe6..18f337de0 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -10,7 +10,7 @@ import com.gmail.nossr50.skills.utilities.AbilityType; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.StringUtils; -public class Config extends ConfigLoader { +public class Config extends AutoUpdateConfigLoader { private static Config instance; private Config() { @@ -45,6 +45,7 @@ public class Config extends ConfigLoader { public boolean getBackupsEnabled() { return config.getBoolean("General.Generate_Backups", true); } public boolean getVerboseLoggingEnabled() { return config.getBoolean("General.Verbose_Logging", false); } public boolean getConfigOverwriteEnabled() { return config.getBoolean("General.Config_Update_Overwrite", true); } + public boolean getPartyDisplayNames() { return config.getBoolean("Commands.p.Use_Display_Names", true); } public boolean getAdminDisplayNames() { return config.getBoolean("Commands.a.Use_Display_Names", true); } diff --git a/src/main/java/com/gmail/nossr50/config/ConfigLoader.java b/src/main/java/com/gmail/nossr50/config/ConfigLoader.java index f5d18a530..b85bf6a53 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigLoader.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigLoader.java @@ -46,104 +46,6 @@ public abstract class ConfigLoader { } config = YamlConfiguration.loadConfiguration(configFile); - FileConfiguration internalConfig = YamlConfiguration.loadConfiguration(plugin.getResource(fileName)); - - Set configKeys = config.getKeys(true); - Set internalConfigKeys = internalConfig.getKeys(true); - - boolean needSave = false; - - Set oldKeys = new HashSet(configKeys); - oldKeys.removeAll(internalConfigKeys); - - Set newKeys = new HashSet(internalConfigKeys); - newKeys.removeAll(configKeys); - - // Don't need a re-save if we have old keys sticking around? - // Would be less saving, but less... correct? - if (!newKeys.isEmpty() || !oldKeys.isEmpty()) { - needSave = true; - } - - for (String key : oldKeys) { - plugin.debug("Removing unused key: " + key); - config.set(key, null); - } - - for (String key : newKeys) { - plugin.debug("Adding new key: " + key + " = " + internalConfig.get(key)); - config.set(key, internalConfig.get(key)); - } - - if (needSave) { - // Get Bukkit's version of an acceptable config with new keys, and no old keys - String output = config.saveToString(); - - // Convert to the superior 4 space indentation - output = output.replace(" ", " "); - - // Rip out Bukkit's attempt to save comments at the top of the file - while (output.indexOf('#') != -1) { - output = output.substring(output.indexOf('\n', output.indexOf('#'))+1); - } - - // Read the internal config to get comments, then put them in the new one - try { - // Read internal - BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(fileName))); - HashMap comments = new HashMap(); - String temp = ""; - - String line; - while ((line = reader.readLine()) != null) { - if (line.contains("#")) { - temp += line + "\n"; - } - else if (line.contains(":")) { - line = line.substring(0, line.indexOf(":") + 1); - if(!temp.isEmpty()) { - comments.put(line, temp); - temp = ""; - } - } - } - - // Dump to the new one - for (String key : comments.keySet()) { - if (output.indexOf(key) != -1) { - output = output.substring(0, output.indexOf(key)) + comments.get(key) + output.substring(output.indexOf(key)); - } - } - } - catch (Exception e) { - e.printStackTrace(); - } - - // Save it - try { - String saveName = fileName; - // At this stage we cannot guarantee that Config has been loaded, so we do the check directly here - if (!plugin.getConfig().getBoolean("General.Config_Update_Overwrite", true)) { - saveName += ".new"; - } - - BufferedWriter writer = new BufferedWriter(new FileWriter(new File(plugin.getDataFolder(), saveName))); - writer.write(output); - writer.flush(); - writer.close(); - } - catch (Exception e) { - e.printStackTrace(); - } - } - else { - for (String key : configKeys) { - if (!config.isConfigurationSection(key) && !config.get(key).equals(internalConfig.get(key))) { - MetricsManager.customConfig(); - break; - } - } - } } protected abstract void loadKeys();