Search in sources :

Example 1 with ManaAbilityOption

use of com.archyx.aureliumskills.mana.ManaAbilityOption in project AureliumSkills by Archy-X.

the class AbilityManager method loadOptions.

public void loadOptions() {
    File file = new File(plugin.getDataFolder(), "abilities_config.yml");
    if (!file.exists()) {
        plugin.saveResource("abilities_config.yml", false);
    }
    FileConfiguration config = updateFile(file, YamlConfiguration.loadConfiguration(file));
    migrateOptions(file, config);
    // Loads ability options
    int amountLoaded = 0;
    int amountDisabled = 0;
    long startTime = System.currentTimeMillis();
    ConfigurationSection abilities = config.getConfigurationSection("abilities");
    if (abilities != null) {
        for (Skill skill : plugin.getSkillRegistry().getSkills()) {
            String skillName = skill.name().toLowerCase(Locale.ENGLISH);
            ConfigurationSection skillAbilities = abilities.getConfigurationSection(skillName);
            if (skillAbilities != null) {
                for (String abilityName : skillAbilities.getKeys(false)) {
                    // Check if ability is valid
                    boolean hasKey = false;
                    for (Ability ability : Ability.values()) {
                        if (abilityName.toUpperCase().equals(ability.name())) {
                            hasKey = true;
                            break;
                        }
                    }
                    if (hasKey) {
                        String path = "abilities." + skillName + "." + abilityName + ".";
                        Ability ability = Ability.valueOf(abilityName.toUpperCase());
                        boolean enabled = config.getBoolean(path + "enabled", true);
                        if (!enabled) {
                            amountDisabled++;
                        }
                        double baseValue = config.getDouble(path + "base", ability.getDefaultBaseValue());
                        double valuePerLevel = config.getDouble(path + "per_level", ability.getDefaultValuePerLevel());
                        // Get default unlock value
                        int defUnlock = 2;
                        for (int i = 0; i < skill.getAbilities().size(); i++) {
                            if (skill.getAbilities().get(i).get() == ability) {
                                defUnlock += i;
                                break;
                            }
                        }
                        int unlock = config.getInt(path + "unlock", defUnlock);
                        int levelUp = config.getInt(path + "level_up", 5);
                        int maxLevel = config.getInt(path + "max_level", 0);
                        // Load options
                        Set<String> optionKeys = getOptionKeys(ability);
                        Map<String, OptionValue> options = null;
                        if (optionKeys != null) {
                            options = new HashMap<>();
                            for (String key : optionKeys) {
                                Object value = config.get(path + key);
                                if (value != null) {
                                    options.put(key, new OptionValue(value));
                                }
                            }
                        }
                        AbilityOption option;
                        // Checks if ability has 2 values
                        if (ability.hasTwoValues()) {
                            double baseValue2 = config.getDouble(path + "base_2", ability.getDefaultBaseValue2());
                            double valuePerLevel2 = config.getDouble(path + "per_level_2", ability.getDefaultValuePerLevel2());
                            if (options != null) {
                                option = new AbilityOption(enabled, baseValue, valuePerLevel, baseValue2, valuePerLevel2, unlock, levelUp, maxLevel, options);
                            } else {
                                option = new AbilityOption(enabled, baseValue, valuePerLevel, baseValue2, valuePerLevel2, unlock, levelUp, maxLevel);
                            }
                        } else {
                            if (options != null) {
                                option = new AbilityOption(enabled, baseValue, valuePerLevel, unlock, levelUp, maxLevel, options);
                            } else {
                                option = new AbilityOption(enabled, baseValue, valuePerLevel, unlock, levelUp, maxLevel);
                            }
                        }
                        abilityOptions.put(ability, option);
                        amountLoaded++;
                    }
                }
            }
        }
    }
    ConfigurationSection manaAbilities = config.getConfigurationSection("mana_abilities");
    if (manaAbilities != null) {
        for (String manaAbilityName : manaAbilities.getKeys(false)) {
            boolean hasKey = false;
            for (MAbility manaAbility : MAbility.values()) {
                if (manaAbilityName.toUpperCase().equals(manaAbility.name())) {
                    hasKey = true;
                    break;
                }
            }
            if (hasKey) {
                MAbility mAbility = MAbility.valueOf(manaAbilityName.toUpperCase());
                String path = "mana_abilities." + manaAbilityName + ".";
                boolean enabled = config.getBoolean(path + "enabled", true);
                if (!enabled) {
                    amountDisabled++;
                }
                double baseValue = config.getDouble(path + "base_value", mAbility.getDefaultBaseValue());
                double valuePerLevel = config.getDouble(path + "value_per_level", mAbility.getDefaultValuePerLevel());
                double cooldown = config.getDouble(path + "cooldown", mAbility.getDefaultBaseCooldown());
                double cooldownPerLevel = config.getDouble(path + "cooldown_per_level", mAbility.getDefaultCooldownPerLevel());
                double manaCost = config.getDouble(path + "mana_cost", mAbility.getDefaultBaseManaCost());
                double manaCostPerLevel = config.getDouble(path + "mana_cost_per_level", mAbility.getDefaultManaCostPerLevel());
                int unlock = config.getInt(path + "unlock", 7);
                int levelUp = config.getInt(path + "level_up", 7);
                int maxLevel = config.getInt(path + "max_level", 0);
                // Load options
                Set<String> optionKeys = plugin.getManaAbilityManager().getOptionKeys(mAbility);
                Map<String, OptionValue> options = null;
                if (optionKeys != null) {
                    options = new HashMap<>();
                    for (String key : optionKeys) {
                        Object value = config.get(path + key);
                        if (value != null) {
                            options.put(key, new OptionValue(value));
                        }
                    }
                }
                ManaAbilityOption option;
                if (options != null) {
                    option = new ManaAbilityOption(enabled, baseValue, valuePerLevel, cooldown, cooldownPerLevel, manaCost, manaCostPerLevel, unlock, levelUp, maxLevel, options);
                } else {
                    option = new ManaAbilityOption(enabled, baseValue, valuePerLevel, cooldown, cooldownPerLevel, manaCost, manaCostPerLevel, unlock, levelUp, maxLevel);
                }
                manaAbilityOptions.put(mAbility, option);
                amountLoaded++;
            }
        }
    }
    long endTime = System.currentTimeMillis();
    long timeElapsed = endTime - startTime;
    if (amountDisabled > 0) {
        plugin.getLogger().info("Disabled " + amountDisabled + " Abilities");
    }
    plugin.getLogger().info("Loaded " + amountLoaded + " Ability Options in " + timeElapsed + "ms");
}
Also used : MAbility(com.archyx.aureliumskills.mana.MAbility) ManaAbilityOption(com.archyx.aureliumskills.mana.ManaAbilityOption) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) Skill(com.archyx.aureliumskills.skills.Skill) ManaAbilityOption(com.archyx.aureliumskills.mana.ManaAbilityOption) OptionValue(com.archyx.aureliumskills.configuration.OptionValue) MAbility(com.archyx.aureliumskills.mana.MAbility) File(java.io.File) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

OptionValue (com.archyx.aureliumskills.configuration.OptionValue)1 MAbility (com.archyx.aureliumskills.mana.MAbility)1 ManaAbilityOption (com.archyx.aureliumskills.mana.ManaAbilityOption)1 Skill (com.archyx.aureliumskills.skills.Skill)1 File (java.io.File)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1