use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class MagicController method saveSpellData.
protected void saveSpellData(Collection<YamlDataFile> stores) {
String lastKey = "";
try {
YamlDataFile spellsDataFile = createDataFile(SPELLS_DATA_FILE);
for (SpellData data : templateDataMap.values()) {
lastKey = data.getKey().getBaseKey();
ConfigurationSection spellNode = spellsDataFile.createSection(lastKey);
if (spellNode == null) {
getLogger().warning("Error saving spell data for " + lastKey);
continue;
}
spellNode.set("cast_count", data.getCastCount());
spellNode.set("last_cast", data.getLastCast());
}
stores.add(spellsDataFile);
} catch (Throwable ex) {
getLogger().warning("Error saving spell data for " + lastKey);
ex.printStackTrace();
}
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class ParameterizedConfigurationSection method createSection.
/**
* Borrowed from Bukkit's MemorySection
*/
@Override
public ConfigurationSection createSection(String path) {
Validate.notEmpty(path, "Cannot create section at empty path");
Configuration root = getRoot();
if (root == null) {
throw new IllegalStateException("Cannot create section without a root");
}
final char separator = root.options().pathSeparator();
// i1 is the leading (higher) index
// i2 is the trailing (lower) index
int i1 = -1;
int i2;
ConfigurationSection section = this;
while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
String node = path.substring(i2, i1);
ConfigurationSection subSection = section.getConfigurationSection(node);
if (subSection == null) {
section = section.createSection(node);
} else {
section = subSection;
}
}
String key = path.substring(i2);
if (section == this) {
ConfigurationSection result = new ParameterizedConfigurationSection(this, key);
map.put(key, result);
return result;
}
return section.createSection(key);
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class MagicController method loadConfigFile.
protected ConfigurationSection loadConfigFile(String fileName, boolean loadDefaults, boolean disableDefaults, boolean filesReplace, ConfigurationSection mainConfiguration) throws IOException, InvalidConfigurationException {
String configFileName = fileName + ".yml";
File configFile = new File(configFolder, configFileName);
if (!configFile.exists()) {
getLogger().info("Saving template " + configFileName + ", edit to customize configuration.");
plugin.saveResource(configFileName, false);
}
boolean usingExample = exampleDefaults != null && exampleDefaults.length() > 0;
String examplesFileName = usingExample ? "examples/" + exampleDefaults + "/" + fileName + ".yml" : null;
String defaultsFileName = "defaults/" + fileName + ".defaults.yml";
File savedDefaults = new File(configFolder, defaultsFileName);
if (saveDefaultConfigs) {
plugin.saveResource(defaultsFileName, true);
} else if (savedDefaults.exists()) {
getLogger().info("Deleting defaults file: " + defaultsFileName + ", these have been removed to avoid confusion");
savedDefaults.delete();
}
getLogger().info("Loading " + configFile.getName());
ConfigurationSection overrides = CompatibilityUtils.loadConfiguration(configFile);
ConfigurationSection config = new MemoryConfiguration();
if (loadDefaults) {
getLogger().info(" Based on defaults " + defaultsFileName);
ConfigurationSection defaultConfig = CompatibilityUtils.loadConfiguration(plugin.getResource(defaultsFileName));
if (disableDefaults) {
Set<String> keys = defaultConfig.getKeys(false);
for (String key : keys) {
defaultConfig.getConfigurationSection(key).set("enabled", false);
}
enableAll(overrides);
}
config = ConfigurationUtils.addConfigurations(config, defaultConfig);
}
if (mainConfiguration != null) {
config = ConfigurationUtils.addConfigurations(config, mainConfiguration);
}
if (usingExample) {
InputStream input = plugin.getResource(examplesFileName);
if (input != null) {
ConfigurationSection exampleConfig = CompatibilityUtils.loadConfiguration(input);
if (disableDefaults) {
enableAll(exampleConfig);
}
config = ConfigurationUtils.addConfigurations(config, exampleConfig);
getLogger().info(" Using " + examplesFileName);
}
}
if (addExamples != null && addExamples.size() > 0) {
for (String example : addExamples) {
examplesFileName = "examples/" + example + "/" + fileName + ".yml";
InputStream input = plugin.getResource(examplesFileName);
if (input != null) {
ConfigurationSection exampleConfig = CompatibilityUtils.loadConfiguration(input);
if (disableDefaults) {
enableAll(exampleConfig);
}
config = ConfigurationUtils.addConfigurations(config, exampleConfig, false);
getLogger().info(" Added " + examplesFileName);
}
}
}
// Apply overrides after loading defaults and examples
config = ConfigurationUtils.addConfigurations(config, overrides);
// Apply file overrides last
File configSubFolder = new File(configFolder, fileName);
config = loadConfigFolder(config, configSubFolder, filesReplace, disableDefaults);
return config;
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class MagicController method loadLostWands.
protected void loadLostWands() {
try {
ConfigurationSection lostWandConfiguration = loadDataFile(LOST_WANDS_FILE);
if (lostWandConfiguration != null) {
Set<String> wandIds = lostWandConfiguration.getKeys(false);
for (String wandId : wandIds) {
if (wandId == null || wandId.length() == 0)
continue;
LostWand lostWand = new LostWand(wandId, lostWandConfiguration.getConfigurationSection(wandId));
if (!lostWand.isValid()) {
getLogger().info("Skipped invalid entry in lostwands.yml file, entry will be deleted. The wand is really lost now!");
continue;
}
addLostWand(lostWand);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
getLogger().info("Loaded " + lostWands.size() + " lost wands");
}
use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.
the class MagicController method saveLostWands.
protected void saveLostWands(Collection<YamlDataFile> stores) {
String lastKey = "";
try {
YamlDataFile lostWandsConfiguration = createDataFile(LOST_WANDS_FILE);
for (Entry<String, LostWand> wandEntry : lostWands.entrySet()) {
lastKey = wandEntry.getKey();
if (lastKey == null || lastKey.length() == 0)
continue;
ConfigurationSection wandNode = lostWandsConfiguration.createSection(lastKey);
if (wandNode == null) {
getLogger().warning("Error saving lost wand data for " + lastKey);
continue;
}
if (!wandEntry.getValue().isValid()) {
getLogger().warning("Invalid lost and data for " + lastKey);
continue;
}
wandEntry.getValue().save(wandNode);
}
stores.add(lostWandsConfiguration);
} catch (Throwable ex) {
getLogger().warning("Error saving lost wand data for " + lastKey);
ex.printStackTrace();
}
}
Aggregations