Fix configuration not saving default values, added config header.

This commit is contained in:
filoghost 2014-11-08 09:36:57 +01:00
parent 2861cc2969
commit f41e79e1d9
6 changed files with 21 additions and 11 deletions

View File

@ -150,7 +150,7 @@ public class ChestCommands extends JavaPlugin {
getLogger().warning("I/O error while using the configuration. Default values will be used."); getLogger().warning("I/O error while using the configuration. Default values will be used.");
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
getLogger().warning("The config.yml was not valid, please look at the error above. Default values will be used."); getLogger().warning("The config.yml was not a valid YAML, please look at the error above. Default values will be used.");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer."); getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer.");
@ -163,7 +163,7 @@ public class ChestCommands extends JavaPlugin {
getLogger().warning("I/O error while using the language file. Default values will be used."); getLogger().warning("I/O error while using the language file. Default values will be used.");
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
getLogger().warning("The lang.yml was not valid, please look at the error above. Default values will be used."); getLogger().warning("The lang.yml was not a valid YAML, please look at the error above. Default values will be used.");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer."); getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer.");

View File

@ -13,6 +13,7 @@ import com.gmail.filoghost.chestcommands.util.ErrorLogger;
import com.gmail.filoghost.chestcommands.util.Utils; import com.gmail.filoghost.chestcommands.util.Utils;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
// This is not a real YAML file ;)
public class AsciiPlaceholders { public class AsciiPlaceholders {
private static Map<String, String> placeholders = Maps.newHashMap(); private static Map<String, String> placeholders = Maps.newHashMap();

View File

@ -5,14 +5,15 @@ import com.gmail.filoghost.chestcommands.config.yaml.SpecialConfig;
public class Settings extends SpecialConfig { public class Settings extends SpecialConfig {
public boolean update_notifications = true;
public boolean use_console_colors = true; public boolean use_console_colors = true;
public String default_color__name = "&f"; public String default_color__name = "&f";
public String default_color__lore = "&7"; public String default_color__lore = "&7";
public String multiple_commands_separator = ";"; public String multiple_commands_separator = ";";
public boolean update_notifications = true;
public Settings(PluginConfig config) { public Settings(PluginConfig config) {
super(config); super(config);
setHeader("ChestCommands configuration file.\nTutorial: http://dev.bukkit.org/bukkit-plugins/chest-commands\n");
} }
} }

View File

@ -38,6 +38,10 @@ public class PluginConfig extends YamlConfiguration {
} }
} }
// To reset all the values when loading.
for (String section : this.getKeys(false)) {
set(section, null);
}
load(file); load(file);
} }

View File

@ -18,12 +18,17 @@ import com.gmail.filoghost.chestcommands.util.Utils;
public class SpecialConfig { public class SpecialConfig {
private transient PluginConfig config; private transient PluginConfig config;
private transient String header;
private transient Map<String, Object> defaultValuesMap; private transient Map<String, Object> defaultValuesMap;
public SpecialConfig(PluginConfig config) { public SpecialConfig(PluginConfig config) {
this.config = config; this.config = config;
} }
public void setHeader(String header) {
this.header = header;
}
public void load() throws IOException, InvalidConfigurationException, Exception { public void load() throws IOException, InvalidConfigurationException, Exception {
// Check if the configuration was initialized. // Check if the configuration was initialized.
@ -57,7 +62,6 @@ public class SpecialConfig {
// Save default values not set. // Save default values not set.
boolean needsSave = false; boolean needsSave = false;
for (Entry<String, Object> entry : defaultValuesMap.entrySet()) { for (Entry<String, Object> entry : defaultValuesMap.entrySet()) {
if (!config.isSet(entry.getKey())) { if (!config.isSet(entry.getKey())) {
needsSave = true; needsSave = true;
config.set(entry.getKey(), entry.getValue()); config.set(entry.getKey(), entry.getValue());
@ -65,6 +69,7 @@ public class SpecialConfig {
} }
if (needsSave) { if (needsSave) {
config.options().header(header);
config.save(); config.save();
} }

View File

@ -95,9 +95,7 @@ public class Utils {
} }
public static String colorizeName(String input) { public static String colorizeName(String input) {
if (input == null) return null; if (input == null || input.isEmpty()) return input;
if (input.isEmpty()) return input;
if (input.charAt(0) != ChatColor.COLOR_CHAR) { if (input.charAt(0) != ChatColor.COLOR_CHAR) {
return ChestCommands.getSettings().default_color__name + addColors(input); return ChestCommands.getSettings().default_color__name + addColors(input);
@ -107,7 +105,8 @@ public class Utils {
} }
public static List<String> colorizeLore(List<String> input) { public static List<String> colorizeLore(List<String> input) {
if (input == null) return null; if (input == null || input.isEmpty()) return input;
for (int i = 0; i < input.size(); i++) { for (int i = 0; i < input.size(); i++) {
String line = input.get(i); String line = input.get(i);
@ -124,12 +123,12 @@ public class Utils {
} }
public static String addColors(String input) { public static String addColors(String input) {
if (input == null) return null; if (input == null || input.isEmpty()) return input;
return ChatColor.translateAlternateColorCodes('&', input); return ChatColor.translateAlternateColorCodes('&', input);
} }
public static List<String> addColors(List<String> input) { public static List<String> addColors(List<String> input) {
if (input == null) return null; if (input == null || input.isEmpty()) return input;
for (int i = 0; i < input.size(); i++) { for (int i = 0; i < input.size(); i++) {
input.set(i, addColors(input.get(i))); input.set(i, addColors(input.get(i)));
} }