package com.elmakers.mine.bukkit.action.builtin;

import javax.annotation.Nullable;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;

public class CastAction extends BaseSpellAction {
    private @Nullable String spellKey;
    private boolean costFree;
    private boolean cooldownFree;
    private @Nullable ConfigurationSection spellParameters;
    private boolean asConsole;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);

        spellKey = parameters.getString("spell");
        spellParameters = parameters.getConfigurationSection("spell_parameters");
        costFree = parameters.getBoolean("cost_free", true);
        cooldownFree = parameters.getBoolean("cooldown_free", false);
        asConsole = parameters.getBoolean("console", false);
    }

    @Override
    public SpellResult perform(CastContext context) {
        if (spellKey == null) {
            return SpellResult.FAIL;
        }

        Mage targetMage = null;
        if (asConsole) {
            targetMage = context.getController().getConsoleMage();
        } else {
            Entity target = context.getTargetEntity();
            if (target != null) {
                targetMage = context.getController().getMage(target);
            }
        }

        if (targetMage == null) {
            return SpellResult.NO_TARGET;
        }

        Spell spell = targetMage.getSpell(spellKey);
        if (spell == null) {
            return SpellResult.FAIL;
        }

        boolean originalCostFree = targetMage.isCostFree();
        boolean originalCooldownFree = targetMage.isCooldownFree();

        targetMage.setCostFree(costFree);
        targetMage.setCooldownFree(cooldownFree);

        try {
            return spell.cast(spellParameters)
                    ? SpellResult.CAST
                    : SpellResult.FAIL;
        } finally {
            targetMage.setCostFree(originalCostFree);
            targetMage.setCooldownFree(originalCooldownFree);
        }
    }

    @Override
    public boolean requiresTarget() {
        return true;
    }

    @Override
    public boolean requiresTargetEntity() {
        return true;
    }
}

@@ -674,16 +674,46 @@
 Console;
+%0A    private boolean asTarget;
 %0A%0A    @O
@@ -1165,24 +1165,85 @@
 e%22, false);%0A
+        asTarget = parameters.getBoolean(%22as_target%22, true);%0A
     %7D%0A%0A    @
@@ -1523,16 +1523,30 @@
   %7D else
+ if (asTarget)
  %7B%0A     
@@ -1714,32 +1714,93 @@
      %7D%0A        %7D
+ else %7B%0A            targetMage = context.getMage();%0A        %7D
 %0A%0A        if (ta
@@ -2576,36 +2576,40 @@
 %0A        return 
-true
+asTarget
 ;%0A    %7D%0A%0A    @Ov
@@ -2674,20 +2674,24 @@
  return 
-true
+asTarget
 ;%0A    %7D%0A

Add as_target parameter to Cast action