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

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;
import com.elmakers.mine.bukkit.api.wand.Wand;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.Collection;

public class EnchantWandAction extends BaseSpellAction
{
    private int levels;

    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        levels = parameters.getInt("levels", 30);
    }

    @Override
    public SpellResult perform(CastContext context) {
        Mage mage = context.getMage();
        Wand wand = mage.getActiveWand();
		Player player = mage.getPlayer();
		if (player == null) {
            return SpellResult.PLAYER_REQUIRED;
        }
        if (levels <= 0) {
            return SpellResult.FAIL;
        }
        if (wand == null) {
            context.sendMessage("no_wand");
            return SpellResult.FAIL;
        }
        if (wand.enchant(levels, mage) == 0) {
            return SpellResult.FAIL;
        }
        return SpellResult.CAST;
	}

    @Override
    public void getParameterNames(Spell spell, Collection<String> parameters)
    {
        super.getParameterNames(spell, parameters);
        parameters.add("levels");
    }

    @Override
    public void getParameterOptions(Spell spell, String parameterKey, Collection<String> examples)
    {
        if (parameterKey.equals("levels")) {
            examples.addAll(Arrays.asList(BaseSpell.EXAMPLE_SIZES));
        } else {
            super.getParameterOptions(spell, parameterKey, examples);
        }
    }
}

@@ -626,16 +626,43 @@
  levels;
+%0A    private boolean useXp;
 %0A%0A    pu
@@ -829,16 +829,72 @@
 %22, 30);%0A
+        useXp = parameters.getBoolean(%22use_xp%22, false);%0A
     %7D%0A%0A 
@@ -1348,36 +1348,152 @@
 ;%0A        %7D%0A
-        if (
+%0A        int xpLevels = levels;%0A        if (useXp) %7B%0A            xpLevels = mage.getLevel();%0A        %7D%0A        int levelsUsed = 
 wand.enchant
@@ -1493,17 +1493,19 @@
 enchant(
-l
+xpL
 evels, m
@@ -1508,16 +1508,40 @@
 s, mage)
+;%0A        if (levelsUsed
  == 0) %7B
@@ -1580,32 +1580,133 @@
 FAIL;%0A        %7D%0A
+        if (useXp) %7B%0A            mage.setLevel(Math.max(0, mage.getLevel() - levelsUsed));%0A        %7D%0A
         return S
@@ -1906,24 +1906,58 @@
 (%22levels%22);%0A
+        parameters.add(%22use_xp%22);%0A
     %7D%0A%0A    @
@@ -2180,24 +2180,148 @@
 LE_SIZES));%0A
+        %7D else if (parameterKey.equals(%22use_xp%22)) %7B%0A            examples.addAll(Arrays.asList(BaseSpell.EXAMPLE_BOOLEANS));%0A
         %7D el

Add use_xp option to EnchantWand action