package com.elmakers.mine.bukkit.plugins.magic.spells;

import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.util.Vector;

import com.elmakers.mine.bukkit.plugins.magic.Spell;
import com.elmakers.mine.bukkit.plugins.magic.SpellResult;
import com.elmakers.mine.bukkit.plugins.magic.Target;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class ForceSpell extends Spell
{
	int magnitude = 3;
	LivingEntity targetEntity = null;
	
	@Override
	public SpellResult onCast(ConfigurationNode parameters) 
	{
		if (targetEntity != null)
		{
			if (targetEntity instanceof LivingEntity)
			{
				if (!targetEntity.isValid() || targetEntity.isDead())
				{
					targetEntity = null;
				}
				if (targetEntity != null && player.getLocation().distanceSquared(targetEntity.getLocation()) > getMaxRangeSquared())
				{
					targetEntity = null;
				}
			}
		}
		
		if (targetEntity == null) {
			targetEntity(LivingEntity.class);
			Target target = getTarget();

			if (target == null || !target.hasTarget() || !target.isEntity() || !(target.getEntity() instanceof LivingEntity))
			{
				targetEntity = null;
				return SpellResult.NO_TARGET;
			}
			
			targetEntity = (LivingEntity)target.getEntity();
			return SpellResult.COST_FREE;
		}

		double multiplier = parameters.getDouble("size", 1);
		forceEntity(targetEntity, multiplier);
		return SpellResult.SUCCESS;
	}

	protected void forceEntity(Entity target, double multiplier)
	{
		magnitude = (int)((double)magnitude * multiplier);
		Vector forceVector = getAimVector();
		forceVector.normalize();
		forceVector.multiply(magnitude);
		target.setVelocity(forceVector);
	}

	@Override
	public boolean onCancel()
	{
		if (targetEntity != null)
		{
            if 
            (
                    (targetEntity instanceof LivingEntity) 
            &&      !targetEntity.isDead() 
            &&      player.getLocation().distanceSquared(targetEntity.getLocation()) > getMaxRangeSquared()
            )
            {
                castMessage("Released target");
            }

            targetEntity = null;
			return true;
		}
		
		return false;
	}

	@Override
	public void onLoad(ConfigurationNode properties)  
	{
		magnitude = properties.getInt("entity_force", magnitude);
	}
}

@@ -319,16 +319,74 @@
 Target;%0A
+import com.elmakers.mine.bukkit.utilities.InventoryUtils;%0A
 import c
@@ -447,16 +447,16 @@
 onNode;%0A
-
 %0Apublic 
@@ -543,16 +543,38 @@
 = null;%0A
+%09int effectColor = 0;%0A
 %09%0A%09@Over
@@ -790,35 +790,31 @@
 %09%7B%0A%09%09%09%09%09
-targetEntity = null
+releaseTarget()
 ;%0A%09%09%09%09%7D%0A
@@ -945,35 +945,31 @@
 %09%7B%0A%09%09%09%09%09
-targetEntity = null
+releaseTarget()
 ;%0A%09%09%09%09%7D%0A
@@ -1322,16 +1322,111 @@
 tity();%0A
+%09%09%09if (effectColor != 0) %7B%0A%09%09%09%09InventoryUtils.addPotionEffect(targetEntity, effectColor);%0A%09%09%09%7D%0A
 %09%09%09retur
@@ -1837,24 +1837,192 @@
 eVector);%0A%09%7D
+%0A%09%0A%09protected void releaseTarget() %7B%0A%09%09if (targetEntity != null && effectColor != 0) %7B%0A%09%09%09InventoryUtils.addPotionEffect(targetEntity, 0);%0A%09%09%7D%0A%09%09targetEntity = null;%0A%09%7D
 %0A%0A%09@Override
@@ -2429,35 +2429,31 @@
         
-targetEntity = null
+releaseTarget()
 ;%0A%09%09%09ret
@@ -2551,24 +2551,24 @@
 rties)  %0A%09%7B%0A
-
 %09%09magnitude 
@@ -2615,13 +2615,99 @@
 itude);%0A
+%09%09effectColor = Integer.parseInt(properties.getString(%22effect_color%22, %22FF0000%22), 16);%0A
 %09%7D%0A%7D%0A

Add a selection highlight to force.