Search in sources :

Example 16 with SpellKey

use of com.elmakers.mine.bukkit.api.spell.SpellKey in project MagicPlugin by elBukkit.

the class HeroesSpellSkill method use.

@Override
public SkillResult use(Hero hero, String[] strings) {
    Mage mage = controller.getMage(hero.getPlayer());
    boolean success = false;
    {
        String spellKey = spellTemplate.getKey();
        int targetLevel = SkillConfigManager.getUseSetting(hero, this, "tier", spellLevel, true);
        if (targetLevel != 1) {
            SpellKey key = new SpellKey(spellTemplate.getSpellKey().getBaseKey(), targetLevel);
            spellKey = key.getKey();
        }
        Spell spell = mage.getSpell(spellKey);
        if (spell == null) {
            if (targetLevel > 1) {
                controller.getLogger().warning("Invalid tier for spell in skills config: " + spellKey + " (tier " + spellLevel + ")");
            } else {
                controller.getLogger().warning("Invalid spell in skills config: " + spellKey);
            }
            return SkillResult.FAIL;
        }
        Set<String> parameterKeys = parameters.getKeys(false);
        ConfigurationSection spellParameters = spellTemplate.getSpellParameters();
        ConfigurationSection heroParameters = new MemoryConfiguration();
        for (String parameterKey : parameterKeys) {
            String value = parameters.getString(parameterKey);
            String magicKey = heroesToMagic(parameterKey);
            Double doubleValue = null;
            try {
                doubleValue = Double.parseDouble(value);
            } catch (NumberFormatException ignored) {
            }
            Object magicValue = spellParameters.getString(magicKey);
            if (doubleValue != null) {
                doubleValue = SkillConfigManager.getUseSetting(hero, this, parameterKey, doubleValue, true);
                Double doubleMagicValue = null;
                try {
                    if (magicValue != null) {
                        doubleMagicValue = Double.parseDouble(magicValue.toString());
                    }
                } catch (NumberFormatException ignored) {
                }
                if (doubleMagicValue != null && doubleValue.equals(doubleMagicValue))
                    continue;
            } else {
                value = SkillConfigManager.getUseSetting(hero, this, parameterKey, value);
                if (magicValue != null && value != null && value.equals(magicValue))
                    continue;
            }
            if (doubleValue != null) {
                heroParameters.set(magicKey, doubleValue);
            } else {
                heroParameters.set(magicKey, value);
            }
        }
        // Don't let Magic get in the way of using the skill
        heroParameters.set("cost_reduction", 2);
        heroParameters.set("cooldown_reduction", 2);
        success = spell.cast(heroParameters);
    }
    if (success) {
        this.broadcastExecuteText(hero);
    }
    return success ? SkillResult.NORMAL : SkillResult.FAIL;
}
Also used : Set(java.util.Set) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) Spell(com.elmakers.mine.bukkit.api.spell.Spell) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 17 with SpellKey

use of com.elmakers.mine.bukkit.api.spell.SpellKey in project MagicPlugin by elBukkit.

the class MagicController method getSpellConfig.

@Nullable
protected ConfigurationSection getSpellConfig(String key, ConfigurationSection config, boolean addInherited, Set<String> resolving) {
    // Catch circular dependencies
    if (resolvingKeys.contains(key)) {
        getLogger().log(Level.WARNING, "Circular dependency detected in spell configs: " + StringUtils.join(resolvingKeys, " -> ") + " -> " + key);
        return config;
    }
    resolvingKeys.add(key);
    if (addInherited) {
        ConfigurationSection built = spellConfigurations.get(key);
        if (built != null) {
            return built;
        }
    } else {
        ConfigurationSection built = baseSpellConfigurations.get(key);
        if (built != null) {
            return built;
        }
    }
    ConfigurationSection spellNode = config.getConfigurationSection(key);
    if (spellNode == null) {
        getLogger().warning("Spell " + key + " not known");
        return null;
    }
    spellNode = ConfigurationUtils.cloneConfiguration(spellNode);
    SpellKey spellKey = new SpellKey(key);
    String inheritFrom = spellNode.getString("inherit");
    if (inheritFrom != null && inheritFrom.equalsIgnoreCase("false")) {
        inheritFrom = null;
    }
    String upgradeInheritsFrom = null;
    if (spellKey.isVariant()) {
        if (!spellUpgradesEnabled) {
            return null;
        }
        int level = spellKey.getLevel();
        upgradeInheritsFrom = spellKey.getBaseKey();
        if (level != 2) {
            upgradeInheritsFrom += "|" + (level - 1);
        }
    }
    boolean processInherited = addInherited && inheritFrom != null;
    if (processInherited || upgradeInheritsFrom != null) {
        if (processInherited && key.equals(inheritFrom)) {
            getLogger().warning("Spell " + key + " inherits from itself");
        } else if (processInherited) {
            ConfigurationSection inheritConfig = getSpellConfig(inheritFrom, config, true, resolving);
            if (inheritConfig != null) {
                spellNode = ConfigurationUtils.addConfigurations(spellNode, inheritConfig, false);
            } else {
                getLogger().warning("Spell " + key + " inherits from unknown ancestor " + inheritFrom);
            }
        }
        if (upgradeInheritsFrom != null) {
            if (config.contains(upgradeInheritsFrom)) {
                ConfigurationSection baseInheritConfig = getSpellConfig(upgradeInheritsFrom, config, inheritFrom == null, resolving);
                spellNode = ConfigurationUtils.addConfigurations(spellNode, baseInheritConfig, inheritFrom != null);
            } else {
                getLogger().warning("Spell upgrade " + key + " inherits from unknown level " + upgradeInheritsFrom);
            }
        }
    } else {
        ConfigurationSection defaults = config.getConfigurationSection("default");
        if (defaults != null) {
            spellNode = ConfigurationUtils.addConfigurations(spellNode, defaults, false);
        }
    }
    if (addInherited) {
        spellConfigurations.put(key, spellNode);
    } else {
        baseSpellConfigurations.put(key, spellNode);
    }
    // Apply spell override last
    ConfigurationSection override = config.getConfigurationSection("override");
    if (override != null) {
        spellNode = ConfigurationUtils.addConfigurations(spellNode, override, true);
    }
    return spellNode;
}
Also used : SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Nullable(javax.annotation.Nullable)

Example 18 with SpellKey

use of com.elmakers.mine.bukkit.api.spell.SpellKey in project MagicPlugin by elBukkit.

the class CasterProperties method getSpell.

@Nullable
@Override
public Spell getSpell(String spellKey) {
    Mage mage = getMage();
    if (mage == null) {
        return null;
    }
    SpellKey key = new SpellKey(spellKey);
    spellKey = key.getBaseKey();
    Set<String> spells = getSpells();
    if (!spells.contains(spellKey))
        return null;
    Map<String, Integer> spellLevels = getSpellLevels();
    Integer level = spellLevels.get(spellKey);
    if (level != null) {
        spellKey = new SpellKey(spellKey, level).getKey();
    }
    return mage.getSpell(spellKey);
}
Also used : SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) Nullable(javax.annotation.Nullable)

Example 19 with SpellKey

use of com.elmakers.mine.bukkit.api.spell.SpellKey in project MagicPlugin by elBukkit.

the class Wand method getSpellTemplate.

@Nullable
@Override
public SpellTemplate getSpellTemplate(String spellKey) {
    SpellKey key = new SpellKey(spellKey);
    spellKey = key.getBaseKey();
    if (!spells.contains(spellKey))
        return null;
    Integer level = spellLevels.get(spellKey);
    if (level != null) {
        spellKey = new SpellKey(spellKey, level).getKey();
    }
    return controller.getSpellTemplate(spellKey);
}
Also used : SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) Nullable(javax.annotation.Nullable)

Example 20 with SpellKey

use of com.elmakers.mine.bukkit.api.spell.SpellKey in project MagicPlugin by elBukkit.

the class Wand method migrate.

@SuppressWarnings("unchecked")
protected void migrate(int version, ConfigurationSection wandConfig) {
    // were not present in the template configs.
    if (version <= 1) {
        ConfigurationSection templateConfig = controller.getWandTemplateConfiguration(wandConfig.getString("template"));
        if (templateConfig != null) {
            // This is an unfortunate special case for wands waiting to be randomized
            String randomizeIcon = templateConfig.getString("randomize_icon");
            String currentIcon = wandConfig.getString("icon");
            if (randomizeIcon != null && currentIcon != null && randomizeIcon.equals(currentIcon)) {
                wandConfig.set("icon", null);
            }
            // This was a potentially leftover property from randomized wands we can ditch
            wandConfig.set("randomize", null);
            Set<String> keys = templateConfig.getKeys(false);
            for (String key : keys) {
                Object templateData = templateConfig.get(key);
                Object wandData = wandConfig.get(key);
                if (wandData == null)
                    continue;
                String templateString = templateData.toString();
                String wandString = wandData.toString();
                if (templateData instanceof List) {
                    templateString = templateString.substring(1, templateString.length() - 1);
                    templateString = templateString.replace(", ", ",");
                    templateData = templateString;
                }
                if (wandString.equalsIgnoreCase(templateString)) {
                    wandConfig.set(key, null);
                    continue;
                }
                try {
                    double numericValue = Double.parseDouble(wandString);
                    double numericTemplate = Double.parseDouble(templateString);
                    if (numericValue == numericTemplate) {
                        wandConfig.set(key, null);
                        continue;
                    }
                } catch (NumberFormatException ignored) {
                }
                if (wandData.equals(templateData)) {
                    wandConfig.set(key, null);
                }
            }
        }
    }
    // Remove icon if matches template
    if (version <= 3) {
        ConfigurationSection templateConfig = controller.getWandTemplateConfiguration(wandConfig.getString("template"));
        String templateIcon = templateConfig == null ? null : templateConfig.getString("icon");
        if (templateIcon != null && templateIcon.equals(wandConfig.getString("icon", ""))) {
            wandConfig.set("icon", null);
        }
    }
    // Migration: remove level from spell inventory
    if (version <= 4) {
        Object spellInventoryRaw = wandConfig.get("spell_inventory");
        if (spellInventoryRaw != null) {
            Map<String, ? extends Object> spellInventory = null;
            Map<String, Integer> newSpellInventory = new HashMap<>();
            if (spellInventoryRaw instanceof Map) {
                spellInventory = (Map<String, ? extends Object>) spellInventoryRaw;
            } else if (spellInventoryRaw instanceof ConfigurationSection) {
                spellInventory = NMSUtils.getMap((ConfigurationSection) spellInventoryRaw);
            }
            if (spellInventory != null) {
                for (Map.Entry<String, ? extends Object> spellEntry : spellInventory.entrySet()) {
                    Object slot = spellEntry.getValue();
                    if (slot != null && slot instanceof Integer) {
                        SpellKey spellKey = new SpellKey(spellEntry.getKey());
                        // Prefer to use the base spell if present since that is what we'd be
                        // using on load.
                        Object testSlot = spellInventory.get(spellKey.getBaseKey());
                        if (testSlot != null) {
                            slot = testSlot;
                        }
                        newSpellInventory.put(spellKey.getBaseKey(), (Integer) slot);
                    }
                }
                wandConfig.set("spell_inventory", newSpellInventory);
            }
        }
    }
    // Migration: move attributes to item_attributes
    if (version <= 5) {
        ConfigurationSection attributes = wandConfig.getConfigurationSection("attributes");
        wandConfig.set("attributes", null);
        wandConfig.set("item_attributes", attributes);
    }
    wandConfig.set("version", WAND_VERSION);
}
Also used : HashMap(java.util.HashMap) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)30 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)9 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)9 ArrayList (java.util.ArrayList)6 ItemStack (org.bukkit.inventory.ItemStack)6 Nullable (javax.annotation.Nullable)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Mage (com.elmakers.mine.bukkit.api.magic.Mage)3 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 Map (java.util.Map)3 Set (java.util.Set)3 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)2 Inventory (org.bukkit.inventory.Inventory)2 MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)1 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 SpellData (com.elmakers.mine.bukkit.api.data.SpellData)1