Stop using the YamlConfiguration in bukkit for our config handling. We

can now support periods in world names.
This commit is contained in:
ElgarL 2012-08-03 10:36:26 +01:00
parent 6064b9760b
commit 7b482b612e
2 changed files with 73 additions and 25 deletions

View file

@ -190,4 +190,5 @@ v 2.0:
- Throw a better error than 'null' when someone removes all groups from a yml.
- Stop force removing attachments and let Bukkit handle it's own mess.
- Change to our own Yaml parsing for globalgroups instead of using the YAMLConfiguration class in bukkit.
- Fix a cases sensitivity bug in world loading.
- Fix a cases sensitivity bug in world loading.
- Stop using the YamlConfiguration in bukkit for our config handling. We can now support periods in world names.

View file

@ -5,22 +5,32 @@
package org.anjocaido.groupmanager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import org.anjocaido.groupmanager.utils.Tasks;
import org.bukkit.configuration.file.YamlConfiguration;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.reader.UnicodeReader;
/**
*
* @author gabrielcouto
*/
public class GMConfiguration {
private boolean opOverride;
private boolean toggleValidate;
private Integer saveInterval;
private Integer backupDuration;
private String loggerLevel;
private Map<String, Object> mirrorsMap;
private GroupManager plugin;
private File configFile;
private YamlConfiguration GMconfig;
private Map<String, Object> GMconfig;
public GMConfiguration(GroupManager plugin) {
@ -28,12 +38,14 @@ public class GMConfiguration {
load();
}
@SuppressWarnings("unchecked")
public void load() {
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdirs();
}
configFile = new File(plugin.getDataFolder(), "config.yml");
File configFile = new File(plugin.getDataFolder(), "config.yml");
if (!configFile.exists()) {
try {
@ -43,59 +55,94 @@ public class GMConfiguration {
}
}
GMconfig = new YamlConfiguration();
Yaml configYAML = new Yaml(new SafeConstructor());
try {
GMconfig.load(configFile);
FileInputStream configInputStream = new FileInputStream(configFile);
GMconfig = (Map<String, Object>) configYAML.load(new UnicodeReader(configInputStream));
configInputStream.close();
} catch (Exception ex) {
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + configFile.getPath(), ex);
}
/*
* Read our config settings ands store them for reading later.
*/
Map<String, Object> config = getElement("config", getElement("settings", GMconfig));
opOverride = (Boolean) config.get("opOverrides");
toggleValidate = (Boolean) config.get("validate_toggle");
/*
* data node for save/backup timers.
*/
Map<String, Object> save = getElement("save", getElement("data", getElement("settings", GMconfig)));
saveInterval = (Integer) save.get("minutes");
backupDuration = (Integer) save.get("hours");
loggerLevel = ((Map<String, String>) getElement("settings", GMconfig).get("logging")).get("level");
/*
* Store our mirrors map for parsing later.
*/
mirrorsMap = (Map<String, Object>) ((Map<String, Object>) GMconfig.get("settings")).get("mirrors");
// Setup defaults
adjustLoggerLevel();
plugin.setValidateOnlinePlayer(isToggleValidate());
}
@SuppressWarnings("unchecked")
private Map<String, Object> getElement(String element, Map<String, Object> map) {
if (!map.containsKey(element)) {
throw new IllegalArgumentException("The config.yml has no '" + element + ".\n");
}
return (Map<String, Object>) map.get(element);
}
public boolean isOpOverride() {
return GMconfig.getBoolean("settings.config.opOverrides", true);
return opOverride;
}
public boolean isToggleValidate() {
return GMconfig.getBoolean("settings.config.validate_toggle", true);
}
public Map<String, Object> getMirrorsMap() {
// Try to fetch the old mirror path first
if (GMconfig.isConfigurationSection("settings.permission.world.mirror")) {
return (Map<String, Object>) GMconfig.getConfigurationSection("settings.permission.world.mirror").getValues(false);
} else if (GMconfig.isConfigurationSection("settings.mirrors")) {
return (Map<String, Object>) GMconfig.getConfigurationSection("settings.mirrors").getValues(false);
}
return null;
return toggleValidate;
}
public Integer getSaveInterval() {
return GMconfig.getInt("settings.data.save.minutes", 10);
return saveInterval;
}
public Integer getBackupDuration() {
return GMconfig.getInt("settings.data.save.hours", 24);
return backupDuration;
}
public void adjustLoggerLevel() {
try {
GroupManager.logger.setLevel(Level.parse(GMconfig.getString("settings.logging.level", "INFO")));
GroupManager.logger.setLevel(Level.parse(loggerLevel));
return;
} catch (Exception e) {
}
GroupManager.logger.setLevel(Level.INFO);
}
public Map<String, Object> getMirrorsMap() {
if (!mirrorsMap.isEmpty()) {
return mirrorsMap;
}
return null;
}
}