2016-03-01 03:15:02 +00:00
|
|
|
/*
|
|
|
|
* To change this template, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package org.anjocaido.groupmanager;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
import org.anjocaido.groupmanager.utils.Tasks;
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
|
|
|
import org.yaml.snakeyaml.reader.UnicodeReader;
|
|
|
|
|
|
|
|
/**
|
2016-03-01 12:32:27 +00:00
|
|
|
*
|
2016-03-01 03:15:02 +00:00
|
|
|
* @author gabrielcouto
|
|
|
|
*/
|
|
|
|
public class GMConfiguration {
|
|
|
|
private boolean allowCommandBlocks = false;
|
|
|
|
private boolean opOverride = true;
|
|
|
|
private boolean toggleValidate = true;
|
|
|
|
private Integer saveInterval = 10;
|
|
|
|
private Integer backupDuration = 24;
|
|
|
|
private String loggerLevel = "OFF";
|
|
|
|
private Map<String, Object> mirrorsMap;
|
2016-03-01 12:32:27 +00:00
|
|
|
private final GroupManager plugin;
|
2016-03-01 03:15:02 +00:00
|
|
|
private Map<String, Object> GMconfig;
|
|
|
|
|
2016-03-01 12:32:27 +00:00
|
|
|
public GMConfiguration(final GroupManager plugin) {
|
2016-03-01 03:15:02 +00:00
|
|
|
this.plugin = plugin;
|
|
|
|
/*
|
|
|
|
* Set defaults
|
|
|
|
*/
|
|
|
|
allowCommandBlocks = false;
|
|
|
|
opOverride = true;
|
|
|
|
toggleValidate = true;
|
|
|
|
saveInterval = 10;
|
|
|
|
backupDuration = 24;
|
|
|
|
loggerLevel = "OFF";
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:32:27 +00:00
|
|
|
public void adjustLoggerLevel() {
|
|
|
|
try {
|
|
|
|
GroupManager.logger.setLevel(Level.parse(loggerLevel));
|
|
|
|
return;
|
|
|
|
} catch (final Exception e) {
|
|
|
|
}
|
|
|
|
GroupManager.logger.setLevel(Level.INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Integer getBackupDuration() {
|
|
|
|
return backupDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> getMirrorsMap() {
|
|
|
|
if (!mirrorsMap.isEmpty()) {
|
|
|
|
return mirrorsMap;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Integer getSaveInterval() {
|
|
|
|
return saveInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAllowCommandBlocks() {
|
|
|
|
return allowCommandBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOpOverride() {
|
|
|
|
return opOverride;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isToggleValidate() {
|
|
|
|
return toggleValidate;
|
|
|
|
}
|
|
|
|
|
2016-03-01 03:15:02 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void load() {
|
|
|
|
if (!plugin.getDataFolder().exists()) {
|
|
|
|
plugin.getDataFolder().mkdirs();
|
|
|
|
}
|
2016-03-01 12:32:27 +00:00
|
|
|
final File configFile = new File(plugin.getDataFolder(), "config.yml");
|
2016-03-01 03:15:02 +00:00
|
|
|
if (!configFile.exists()) {
|
|
|
|
try {
|
|
|
|
Tasks.copy(plugin.getResourceAsStream("config.yml"), configFile);
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final IOException ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Error creating a new config.yml", ex);
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 12:32:27 +00:00
|
|
|
final Yaml configYAML = new Yaml(new SafeConstructor());
|
2016-03-01 03:15:02 +00:00
|
|
|
try {
|
2016-03-01 12:32:27 +00:00
|
|
|
final FileInputStream configInputStream = new FileInputStream(configFile);
|
2016-03-01 03:15:02 +00:00
|
|
|
GMconfig = (Map<String, Object>) configYAML.load(new UnicodeReader(configInputStream));
|
|
|
|
configInputStream.close();
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
throw new IllegalArgumentException("The following file couldn't pass on Parser.\n" + configFile.getPath(), ex);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Read our config settings and store them for reading later.
|
|
|
|
*/
|
|
|
|
try {
|
2016-03-01 12:32:27 +00:00
|
|
|
final Map<String, Object> config = getElement("config", getElement("settings", GMconfig));
|
2016-03-01 03:15:02 +00:00
|
|
|
try {
|
|
|
|
allowCommandBlocks = (Boolean) config.get("allow_commandblocks");
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'allow_commandblocks' node. Using default settings", ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
opOverride = (Boolean) config.get("opOverrides");
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'opOverrides' node. Using default settings", ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
toggleValidate = (Boolean) config.get("validate_toggle");
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'validate_toggle' node. Using default settings", ex);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* data node for save/backup timers.
|
|
|
|
*/
|
|
|
|
try {
|
2016-03-01 12:32:27 +00:00
|
|
|
final Map<String, Object> save = getElement("save", getElement("data", getElement("settings", GMconfig)));
|
2016-03-01 03:15:02 +00:00
|
|
|
try {
|
|
|
|
saveInterval = (Integer) save.get("minutes");
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'minutes' node. Using default setting", ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
backupDuration = (Integer) save.get("hours");
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'hours' node. Using default setting", ex);
|
|
|
|
}
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
GroupManager.logger.log(Level.SEVERE, "Missing or corrupt 'data' node. Using default settings", ex);
|
|
|
|
}
|
2016-03-01 12:32:27 +00:00
|
|
|
final Object level = ((Map<String, String>) getElement("settings", GMconfig).get("logging")).get("level");
|
2016-03-01 03:15:02 +00:00
|
|
|
if (level instanceof String)
|
|
|
|
loggerLevel = (String) level;
|
|
|
|
/*
|
|
|
|
* Store our mirrors map for parsing later.
|
|
|
|
*/
|
|
|
|
mirrorsMap = (Map<String, Object>) ((Map<String, Object>) GMconfig.get("settings")).get("mirrors");
|
|
|
|
if (mirrorsMap == null)
|
|
|
|
throw new Exception();
|
2016-03-01 12:32:27 +00:00
|
|
|
} catch (final Exception ex) {
|
2016-03-01 03:15:02 +00:00
|
|
|
/*
|
|
|
|
* Flag the error and use defaults
|
|
|
|
*/
|
|
|
|
GroupManager.logger.log(Level.SEVERE, "There are errors in your config.yml. Using default settings", ex);
|
|
|
|
mirrorsMap = new HashMap<String, Object>();
|
|
|
|
}
|
|
|
|
// Setup defaults
|
|
|
|
adjustLoggerLevel();
|
|
|
|
plugin.setValidateOnlinePlayer(isToggleValidate());
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2016-03-01 12:32:27 +00:00
|
|
|
private Map<String, Object> getElement(final String element, final Map<String, Object> map) {
|
2016-03-01 03:15:02 +00:00
|
|
|
if (!map.containsKey(element)) {
|
|
|
|
throw new IllegalArgumentException("The config.yml has no '" + element + ".\n");
|
|
|
|
}
|
|
|
|
return (Map<String, Object>) map.get(element);
|
|
|
|
}
|
|
|
|
}
|