Search in sources :

Example 1 with AddSpellEvent

use of com.elmakers.mine.bukkit.api.event.AddSpellEvent in project MagicPlugin by elBukkit.

the class CasterProperties method addSpell.

@Override
public boolean addSpell(String spellKey) {
    BaseMagicConfigurable storage = getStorage("spells");
    if (storage != this && storage != null) {
        return storage.addSpell(spellKey);
    }
    SpellTemplate template = controller.getSpellTemplate(spellKey);
    if (template == null) {
        controller.getLogger().warning("Tried to add unknown spell: " + spellKey);
        return false;
    }
    // Convert to spell if aliased
    spellKey = template.getKey();
    Collection<String> spells = getBaseSpells();
    SpellKey key = new SpellKey(spellKey);
    SpellTemplate currentSpell = getSpellTemplate(spellKey);
    boolean modified = spells.add(key.getBaseKey());
    if (modified) {
        setProperty("spells", new ArrayList<>(spells));
    }
    boolean levelModified = false;
    if (key.getLevel() > 1) {
        levelModified = upgradeSpellLevel(key.getBaseKey(), key.getLevel());
    }
    if (!modified && !levelModified) {
        return false;
    }
    // Special handling for spells to remove
    Collection<SpellKey> spellsToRemove = template.getSpellsToRemove();
    for (SpellKey removeKey : spellsToRemove) {
        removeSpell(removeKey.getBaseKey());
    }
    Mage mage = getMage();
    if (mage != null) {
        if (currentSpell != null) {
            String levelDescription = template.getLevelDescription();
            if (levelDescription == null || levelDescription.isEmpty()) {
                levelDescription = template.getName();
            }
            sendLevelMessage("spell_upgraded", currentSpell.getName(), levelDescription);
            String upgradeDescription = template.getUpgradeDescription().replace("$name", currentSpell.getName());
            if (!upgradeDescription.isEmpty()) {
                mage.sendMessage(controller.getMessages().get("spell.upgrade_description_prefix") + upgradeDescription);
            }
            SpellUpgradeEvent upgradeEvent = new SpellUpgradeEvent(mage, getWand(), currentSpell, template);
            Bukkit.getPluginManager().callEvent(upgradeEvent);
        } else {
            // This is a little hacky, but it is here to fix duplicate spell messages from the spellshop.
            if (mage.getActiveGUI() == null)
                sendAddMessage("spell_added", template.getName());
            AddSpellEvent addEvent = new AddSpellEvent(mage, getWand(), template);
            Bukkit.getPluginManager().callEvent(addEvent);
        }
    }
    return true;
}
Also used : AddSpellEvent(com.elmakers.mine.bukkit.api.event.AddSpellEvent) SpellUpgradeEvent(com.elmakers.mine.bukkit.api.event.SpellUpgradeEvent) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Aggregations

AddSpellEvent (com.elmakers.mine.bukkit.api.event.AddSpellEvent)1 SpellUpgradeEvent (com.elmakers.mine.bukkit.api.event.SpellUpgradeEvent)1 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)1