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

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.Vector;

import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.spell.TargetingSpell;
import com.elmakers.mine.bukkit.utility.Target;
import com.elmakers.mine.bukkit.wand.Wand;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DisarmSpell extends TargetingSpell 
{
    private Random random = new Random();

	@Override
	public SpellResult onCast(ConfigurationSection parameters) 
	{
		Target target = getTarget();
		if (!target.hasEntity() || !(target.getEntity() instanceof LivingEntity)) {
			return SpellResult.NO_TARGET;    
		}
		LivingEntity entity = (LivingEntity)target.getEntity();
		
		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) && controller.isMage(entity)) {
            Mage targetMage = controller.getMage(entity);
			
			// Check for protected players (admins, generally...)
			// This gets overridden by superpower...
			if (!mage.isSuperPowered() && isSuperProtected(targetMage)) {
				return SpellResult.NO_TARGET;
			}
			
			if (targetMage != null && targetMage.getActiveWand() != null) {
				targetMage.getActiveWand().deactivate();
			}
		}

        Integer targetSlot = null;
        PlayerInventory targetInventory = null;
        ItemStack swapItem = null;
        if (entity instanceof Player && parameters.getBoolean("keep_in_inventory", false)) {
            Player targetPlayer = (Player)entity;
            targetInventory = targetPlayer.getInventory();
            List<Integer> validSlots = new ArrayList<Integer>();
            ItemStack[] contents = targetInventory.getContents();
            for (int i = Wand.HOTBAR_SIZE; i < contents.length; i++) {
                if (contents[i] == null || contents[i].getType() == Material.AIR) {
                    validSlots.add(i);
                }
            }

            // Randomly choose a slot if no empty one was found
            if (validSlots.size() == 0) {
                int swapSlot = random.nextInt(contents.length - Wand.HOTBAR_SIZE) + Wand.HOTBAR_SIZE;
                swapItem = targetInventory.getItem(swapSlot);
                validSlots.add(swapSlot);
            }

            int chosen = random.nextInt(validSlots.size());
            targetSlot = validSlots.get(chosen);
        }

        equipment.setItemInHand(swapItem);
        if (targetSlot != null && targetInventory != null) {
            targetInventory.setItem(targetSlot, stack);
        } else {
            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.CAST;
		
	}
}

@@ -2309,58 +2309,196 @@
     
-for (int i = Wand.HOTBAR_SIZE; i %3C contents.length
+int minSlot = parameters.getInt(%22min_slot%22, Wand.HOTBAR_SIZE);%0A            int maxSlot = parameters.getInt(%22max_slot%22, contents.length - 1);%0A%0A            for (int i = minSlot; i %3C= maxSlot
 ; i+
@@ -2814,62 +2814,36 @@
 Int(
-contents.length - Wand.HOTBAR_SIZE) + Wand.HOTBAR_SIZE
+maxSlot - minSlot) + minSlot
 ;%0A  

Add min_slot and max_slot parameters to DisarmSpell