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

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import com.elmakers.mine.bukkit.effects.EffectTrail;
import com.elmakers.mine.bukkit.effects.ParticleType;
import com.elmakers.mine.bukkit.plugins.magic.Mage;
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.plugins.magic.wand.Wand;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class DisarmSpell extends Spell 
{
	private final static int 		maxEffectRange = 16;
	private final static int 		effectSpeed = 1;
	private final static float 		particleData = 2f;
	private final static int 		effectPeriod = 2;
	private final static int 		particleCount = 6;
    
	@Override
	public SpellResult onCast(ConfigurationNode parameters) 
	{
		int effectRange = Math.min(getMaxRange(), maxEffectRange / effectSpeed);
		Location effectLocation = getPlayer().getEyeLocation();
		Vector effectDirection = effectLocation.getDirection();
		EffectTrail effectTrail = new EffectTrail(controller.getPlugin(), effectLocation, effectDirection, effectRange);
		effectTrail.setParticleType(ParticleType.MOB_SPELL);
		effectTrail.setParticleCount(particleCount);
		effectTrail.setEffectData(particleData);
		effectTrail.setParticleOffset(0.2f, 0.2f, 0.2f);
		effectTrail.setSpeed(effectSpeed);
		effectTrail.setPeriod(effectPeriod);
		effectTrail.start();
		
		Target target = getTarget();
		Entity targetEntity = target.getEntity();
		if (targetEntity == null || !(targetEntity instanceof LivingEntity))
		{
			return SpellResult.NO_TARGET;    
		}
		LivingEntity entity = (LivingEntity)targetEntity;
		EntityEquipment equipment = entity.getEquipment();
		ItemStack stack = equipment.getItemInHand();
		
		if (stack == null || stack.getType() == Material.AIR)
		{
			return SpellResult.NO_TARGET;
		}
		
		// Special case for wands
		if (Wand.isWand(stack) && entity instanceof Player) {
			Player targetPlayer = (Player)entity;
			Mage targetMage = controller.getMage(targetPlayer);
			if (targetMage != null && targetMage.getActiveWand() != null) {
				targetMage.getActiveWand().deactivate();
			}
		}
		
		equipment.setItemInHand(null);
		Location location = entity.getLocation();
		location.setY(location.getY() + 1);
		Item item = entity.getWorld().dropItemNaturally(location, stack);
		Vector velocity = item.getVelocity();
		velocity.setY(velocity.getY() * 5);
		item.setVelocity(velocity);
		
		return SpellResult.SUCCESS;
		
	}
}

@@ -1766,19 +1766,190 @@
 tart();%0A
+%0A
 %09%09
-%0A
+LivingEntity entity = null;%0A%09%09String targetType = (String)parameters.getString(%22target%22, %22other%22);%0A%09%09if (targetType.equals(%22self%22)) %7B%0A%09%09%09entity = getPlayer();%0A%09%09%7D else %7B%0A%09
 %09%09Target
@@ -1971,16 +1971,17 @@
 rget();%0A
+%09
 %09%09Entity
@@ -2016,16 +2016,17 @@
 tity();%0A
+%09
 %09%09if (ta
@@ -2086,26 +2086,28 @@
 gEntity))%0A%09%09
+%09
 %7B%0A
+%09
 %09%09%09return Sp
@@ -2137,25 +2137,14 @@
  %0A%09%09
+%09
 %7D%0A%09%09
-LivingEntity 
+%09
 enti
@@ -2176,16 +2176,23 @@
 Entity;%0A
+%09%09%7D%0A%09%09%0A
 %09%09Entity

Add "self" parameter to disarm spell.