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

import org.bukkit.Color;
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.utilities.InventoryUtils;
import com.elmakers.mine.bukkit.utilities.Target;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class ForceSpell extends Spell
{
	LivingEntity targetEntity = null;
	private Color effectColor = null;
	
	final private static int DEFAULT_MAGNITUDE = 3;
	
	@Override
	public SpellResult onCast(ConfigurationNode parameters) 
	{
		if (targetEntity != null)
		{
			if (targetEntity instanceof LivingEntity)
			{
				if (!targetEntity.isValid() || targetEntity.isDead())
				{
					releaseTarget();
				}
				if (!targetEntity.getWorld().getName().equals(getLocation().getWorld().getName())) 
				{
					releaseTarget();
				}
				if (targetEntity != null && getPlayer().getLocation().distanceSquared(targetEntity.getLocation()) > getMaxRangeSquared())
				{
					releaseTarget();
				}
			}
		}
		
		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;
			}

			effectColor = mage.getEffectColor();
			if (effectColor == null) {
				effectColor = Color.fromRGB(Integer.parseInt(parameters.getString("effect_color", "FF0000"), 16));
			}

			targetEntity = (LivingEntity)target.getEntity();
			if (effectColor != null) {
				InventoryUtils.addPotionEffect(targetEntity, effectColor);
			}
			return SpellResult.COST_FREE;
		}

		double multiplier = parameters.getDouble("size", 1);

		int magnitude = parameters.getInt("entity_force", DEFAULT_MAGNITUDE);
		forceEntity(targetEntity, multiplier, magnitude);
		return SpellResult.SUCCESS;
	}

	protected void forceEntity(Entity target, double multiplier, int magnitude)
	{
		magnitude = (int)((double)magnitude * multiplier);
		Vector forceVector = mage.getLocation().getDirection();
		forceVector.normalize();
		forceVector.multiply(magnitude);
		target.setVelocity(forceVector);
	}
	
	protected void releaseTarget() {
		if (targetEntity != null && effectColor != null) {
			InventoryUtils.clearPotionEffect(targetEntity);
		}
		targetEntity = null;
	}

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

            releaseTarget();
			return true;
		}
		
		return false;
	}
}

@@ -74,16 +74,69 @@
 .Color;%0A
+import org.bukkit.Location;%0Aimport org.bukkit.World;%0A
 import o
@@ -835,24 +835,112 @@
 ntity)%0A%09%09%09%7B%0A
+%09%09%09%09Location location = getLocation();%0A%09%09%09%09World targetWorld = targetEntity.getWorld();%0A
 %09%09%09%09if (!tar
@@ -1022,39 +1022,78 @@
 %09%09%09%7D
+ 
 %0A%09%09%09%09
+else 
 if (
-!
 target
-Entity.
+World == null %7C%7C location == null %7C%7C !tar
 getWorld
 ().g
@@ -1080,34 +1080,32 @@
  %7C%7C !targetWorld
-()
 .getName().equal
@@ -1106,29 +1106,24 @@
 .equals(
-getL
+l
 ocation
-()
 .getWorl
@@ -1176,66 +1176,31 @@
 %09%09%09%7D
+ 
 %0A%09%09%09%09
-if (targetEntity != null && getPlayer().getL
+else if (l
 ocation
-()
 .dis
@@ -2716,300 +2716,19 @@
 %0A%09%09%7B
-%0A    
        
- if %0A            (%0A                    (targetEntity instanceof LivingEntity) %0A            &&      !targetEntity.isDead() %0A            &&      getPlayer().getLocation().distanceSquared(targetEntity.getLocation()) %3E getMaxRangeSquared()%0A            )%0A            %7B%0A                
+%0A%09%09%09
 cast
@@ -2758,22 +2758,8 @@
 t%22);
-%0A            %7D
 %0A%0A  

Fix Force NPE