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

import org.bukkit.configuration.ConfigurationSection;

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 String spellKey;
    private boolean costFree;
    private boolean cooldownFree;
    private ConfigurationSection spellParameters;

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

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

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

        Mage targetMage = context.getController().getMage(context.getTargetEntity());

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

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

        boolean success = false;
        try {
            Spell spell = targetMage.getSpell(spellKey);
            if (spell != null) {
                success = spell.cast(spellParameters);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            success = false;
        }

        targetMage.setCostFree(originalCostFree);
        targetMage.setCooldownFree(originalCooldownFree);

        return success ? SpellResult.CAST : SpellResult.FAIL;
    }

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

    @Override
    public boolean requiresTargetEntity() {
        return true;
    }
}
@@ -39,24 +39,59 @@
 n.builtin;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 import org.b
@@ -131,16 +131,49 @@
 Section;
+%0Aimport org.bukkit.entity.Entity;
 %0A%0Aimport
@@ -493,16 +493,26 @@
 private 
+@Nullable 
 String s
@@ -596,16 +596,26 @@
 private 
+@Nullable 
 Configur
@@ -788,29 +788,24 @@
 );%0A%0A        
-this.
 spellKey = p
@@ -842,21 +842,16 @@
         
-this.
 spellPar
@@ -924,21 +924,16 @@
         
-this.
 costFree
@@ -989,13 +989,8 @@
     
-this.
 cool
@@ -1138,33 +1138,16 @@
 if (
-context.getTargetEntity()
+spellKey
  == 
@@ -1189,17 +1189,12 @@
 ult.
-NO_TARGET
+FAIL
 ;%0A  
@@ -1201,24 +1201,25 @@
       %7D%0A
+%0A
         
 if (spel
@@ -1206,36 +1206,85 @@
  %7D%0A%0A        
-if (spellKey
+Entity target = context.getTargetEntity();%0A        if (target
  == null) %7B%0A
@@ -1306,36 +1306,41 @@
 urn SpellResult.
-FAIL
+NO_TARGET
 ;%0A        %7D%0A%0A   
@@ -1398,35 +1398,146 @@
 age(
-context.getTargetEntity());
+target);%0A%0A        Spell spell = targetMage.getSpell(spellKey);%0A        if (spell == null) %7B%0A            return SpellResult.FAIL;%0A        %7D
 %0A%0A  
@@ -1768,162 +1768,32 @@
     
-boolean success = false;%0A        try %7B%0A            Spell spell = targetMage.getSpell(spellKey);%0A            if (spell != null) %7B%0A                success =
+try %7B%0A            return
  spe
@@ -1816,17 +1816,16 @@
 ameters)
-;
 %0A       
@@ -1833,18 +1833,16 @@
     
-%7D%0A
         
 %7D ca
@@ -1841,108 +1841,65 @@
     
-%7D catch (Exception exception) %7B%0A            exception.printStackTrace();%0A            success = false
+? SpellResult.CAST%0A                    : SpellResult.FAIL
 ;%0A  
@@ -1897,33 +1897,42 @@
 .FAIL;%0A        %7D
-%0A
+ finally %7B
 %0A        targetM
@@ -1916,32 +1916,36 @@
 nally %7B%0A        
+    
 targetMage.setCo
@@ -1962,32 +1962,36 @@
 ginalCostFree);%0A
+    
         targetMa
@@ -2032,25 +2032,24 @@
 nFree);%0A
-%0A
         
 return s
@@ -2044,61 +2044,9 @@
     
-return success ? SpellResult.CAST : SpellResult.FAIL;
+%7D
 %0A   
@@ -2215,8 +2215,9 @@
 %0A    %7D%0A%7D
+%0A

Clean up cast action