Search in sources :

Example 51 with ConfigurationSection

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();
    }
}
Also used : SpellData(com.elmakers.mine.bukkit.api.data.SpellData) YamlDataFile(com.elmakers.mine.bukkit.data.YamlDataFile) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 52 with ConfigurationSection

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);
}
Also used : Configuration(org.bukkit.configuration.Configuration) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 53 with ConfigurationSection

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) YamlDataFile(com.elmakers.mine.bukkit.data.YamlDataFile) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 54 with ConfigurationSection

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");
}
Also used : LostWand(com.elmakers.mine.bukkit.wand.LostWand) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 55 with ConfigurationSection

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();
    }
}
Also used : LostWand(com.elmakers.mine.bukkit.wand.LostWand) YamlDataFile(com.elmakers.mine.bukkit.data.YamlDataFile) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

ConfigurationSection (org.bukkit.configuration.ConfigurationSection)263 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)29 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)27 ItemStack (org.bukkit.inventory.ItemStack)26 File (java.io.File)22 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)22 IOException (java.io.IOException)21 Map (java.util.Map)18 Mage (com.elmakers.mine.bukkit.api.magic.Mage)17 Material (org.bukkit.Material)16 Nullable (javax.annotation.Nullable)14 Location (org.bukkit.Location)13 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)11 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)10 Player (org.bukkit.entity.Player)10 EntityType (org.bukkit.entity.EntityType)9 List (java.util.List)8 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)7 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)7