<commit_before>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.magic.MageController;
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.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

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

public class StashWandAction extends BaseSpellAction
{
    private ItemStack stashedItem;
    private Mage targetMage;
    private int slotNumber;
    private boolean returnOnFinish = true;

	private class StashWandUndoAction implements Runnable
	{
		public StashWandUndoAction() {
		}

		@Override
		public void run() {
		    returnItem();
		}
	}

	private void returnItem() {
        if (targetMage == null || stashedItem == null) return;
        Player player = targetMage.getPlayer();
        if (player == null) return;

        ItemStack existing = player.getInventory().getItem(slotNumber);
        if (existing == null || existing.getType() == Material.AIR) {
            player.getInventory().setItem(slotNumber, stashedItem);
        } else {
            targetMage.giveItem(stashedItem);
        }
        targetMage.checkWand();
        stashedItem = null;
        targetMage = null;
    }

	@Override
	public SpellResult perform(CastContext context)
	{
        Entity entity = context.getTargetEntity();
        if (entity == null) {
            if (!context.getTargetsCaster()) return SpellResult.NO_TARGET;
            entity = context.getEntity();
        }
        if (entity == null || !(entity instanceof Player))
        {
            return SpellResult.NO_TARGET;
        }

        Player player = (Player)entity;
        MageController controller = context.getController();
        Mage mage = controller.getMage(player);

        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (itemInHand == null || itemInHand.getType() == Material.AIR)
        {
            return SpellResult.FAIL;
        }

        Wand activeWand = mage.getActiveWand();

        // Check for trying to stash an item in the offhand slot
        // Not handling this for now.
        if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        }

        if (activeWand != null) {
            activeWand.deactivate();
        }

        slotNumber = player.getInventory().getHeldItemSlot();
        stashedItem = player.getInventory().getItemInMainHand();

        player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));

        targetMage = mage;
        context.registerForUndo(new StashWandUndoAction());
		return SpellResult.CAST;
	}
<commit_msg>Fix StashWand when using offhand casts<commit_after>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.magic.MageController;
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 com.elmakers.mine.bukkit.utility.InventoryUtils;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

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

public class StashWandAction extends BaseSpellAction
{
    private ItemStack stashedItem;
    private Mage targetMage;
    private int slotNumber;
    private boolean isOffhand = false;
    private boolean returnOnFinish = true;

	private class StashWandUndoAction implements Runnable
	{
		public StashWandUndoAction() {
		}

		@Override
		public void run() {
		    returnItem();
		}
	}

	private void returnItem() {
        if (targetMage == null || stashedItem == null) return;
        Player player = targetMage.getPlayer();
        if (player == null) return;

        boolean gave = false;
        if (isOffhand) {
            ItemStack existing = player.getInventory().getItemInOffHand();
            if (InventoryUtils.isEmpty(existing)) {
                player.getInventory().setItemInOffHand(stashedItem);
                gave = true;
            }
        } else {
            ItemStack existing = player.getInventory().getItem(slotNumber);
            if (InventoryUtils.isEmpty(existing)) {
                player.getInventory().setItem(slotNumber, stashedItem);
            }
        }
        if (!gave) {
            targetMage.giveItem(stashedItem);
        }
        targetMage.checkWand();
        stashedItem = null;
        targetMage = null;
    }

	@Override
	public SpellResult perform(CastContext context)
	{
        Entity entity = context.getTargetEntity();
        if (entity == null) {
            if (!context.getTargetsCaster()) return SpellResult.NO_TARGET;
            entity = context.getEntity();
        }
        if (entity == null || !(entity instanceof Player))
        {
            return SpellResult.NO_TARGET;
        }

        Player player = (Player)entity;
        MageController controller = context.getController();
        Mage mage = controller.getMage(player);

        Wand activeWand = mage.getActiveWand();
        Wand offhandWand = mage.getOffhandWand();

        // Check for trying to stash an item in the offhand slot
        ItemStack activeItem = null;
        if (offhandWand == context.getWand()) {
            isOffhand = true;
            activeWand = offhandWand;
            activeItem = player.getInventory().getItemInOffHand();
        } else if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        } else {
            isOffhand = false;
            activeItem = player.getInventory().getItemInMainHand();
        }

        if (InventoryUtils.isEmpty(activeItem))
        {
            return SpellResult.FAIL;
        }

        if (activeWand != null) {
            activeWand.deactivate();
        }

        slotNumber = player.getInventory().getHeldItemSlot();

        if (isOffhand) {
            stashedItem = player.getInventory().getItemInOffHand();
            player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
        } else {
            stashedItem = player.getInventory().getItemInMainHand();
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
        }

        targetMage = mage;
        context.registerForUndo(new StashWandUndoAction());
		return SpellResult.CAST;
	}

{'fraction_non_alphanumeric': 0.10361067503924647, 'fraction_numerical': 0.0, 'mean_word_length': 2.6923076923076925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6275489', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2056, 'n_words': 398}