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.");
} catch (InvalidConfigurationException e) {
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) {
e.printStackTrace();
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.");
} catch (InvalidConfigurationException e) {
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) {
e.printStackTrace();
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.google.common.collect.Maps;
// This is not a real YAML file ;)
public class AsciiPlaceholders {
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 boolean update_notifications = true;
public boolean use_console_colors = true;
public String default_color__name = "&f";
public String default_color__lore = "&7";
public String multiple_commands_separator = ";";
public boolean update_notifications = true;
public Settings(PluginConfig 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);
}

View File

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

View File

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