<commit_before>package com.elmakers.mine.bukkit.spell.builtin;

import com.elmakers.mine.bukkit.api.block.UndoQueue;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.elmakers.mine.bukkit.api.block.BlockBatch;
import com.elmakers.mine.bukkit.api.block.UndoList;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.block.batch.SpellBatch;
import com.elmakers.mine.bukkit.spell.TargetingSpell;
import com.elmakers.mine.bukkit.utility.Target;

public class UndoSpell extends TargetingSpell
{
	private String undoListName;
	
	@Override
	public SpellResult onCast(ConfigurationSection parameters) 
	{
		Target target = getTarget();
		Entity caster = mage.getEntity();
        int timeout = parameters.getInt("target_timeout", 0);
		if (target.hasEntity() && controller.isMage(target.getEntity()))
		{
			Mage mage = controller.getMage(target.getEntity());
            UndoQueue queue = mage.getUndoQueue();
			UndoList undoList = queue.undoRecent(timeout);
			if (undoList != null) {
				undoListName = undoList.getName();
			}
			return undoList != null ? SpellResult.CAST : SpellResult.NO_TARGET;
		}

        if (!parameters.getBoolean("target_blocks", true)) {
            return SpellResult.NO_TARGET;
        }
		
		Block targetBlock = isLookingDown() ? getLocation().getBlock() : target.getBlock();
		if (targetBlock != null)
		{
			boolean targetAll = mage.isSuperPowered();
			if (targetAll)
			{
				UndoList undid = controller.undoRecent(targetBlock, timeout);
				if (undid != null) 
				{
					Mage targetMage = undid.getOwner();
					undoListName = undid.getName();
					setTargetName(targetMage.getName());
					return SpellResult.CAST;
				}
			}
			else
			{
				setTargetName(mage.getName());
				BlockBatch batch = mage.cancelPending();
				if (batch != null) {
					undoListName = (batch instanceof SpellBatch) ? ((SpellBatch)batch).getSpell().getName() : null;
					return SpellResult.COST_FREE;
				}
				UndoList undoList = mage.undo(targetBlock);<commit_msg>Fix Undo canceling a pending batch<commit_after>package com.elmakers.mine.bukkit.spell.builtin;

import com.elmakers.mine.bukkit.api.block.UndoQueue;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.elmakers.mine.bukkit.api.block.BlockBatch;
import com.elmakers.mine.bukkit.api.block.UndoList;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.block.batch.SpellBatch;
import com.elmakers.mine.bukkit.spell.TargetingSpell;
import com.elmakers.mine.bukkit.utility.Target;

public class UndoSpell extends TargetingSpell
{
	private String undoListName;
	
	@Override
	public SpellResult onCast(ConfigurationSection parameters) 
	{
		Target target = getTarget();
		Entity caster = mage.getEntity();
        int timeout = parameters.getInt("target_timeout", 0);
		if (target.hasEntity() && controller.isMage(target.getEntity()))
		{
			Mage mage = controller.getMage(target.getEntity());

            BlockBatch batch = mage.cancelPending();
            if (batch != null) {
                undoListName = (batch instanceof SpellBatch) ? ((SpellBatch)batch).getSpell().getName() : null;
                return SpellResult.CAST;
            }

            UndoQueue queue = mage.getUndoQueue();
			UndoList undoList = queue.undoRecent(timeout);
			if (undoList != null) {
				undoListName = undoList.getName();
			}
			return undoList != null ? SpellResult.CAST : SpellResult.NO_TARGET;
		}

        if (!parameters.getBoolean("target_blocks", true)) {
            return SpellResult.NO_TARGET;
        }
		
		Block targetBlock = isLookingDown() ? getLocation().getBlock() : target.getBlock();
		if (targetBlock != null)
		{
			boolean targetAll = mage.isSuperPowered();
			if (targetAll)
			{
				UndoList undid = controller.undoRecent(targetBlock, timeout);
				if (undid != null) 
				{
					Mage targetMage = undid.getOwner();
					undoListName = undid.getName();
					setTargetName(targetMage.getName());
					return SpellResult.CAST;
				}
			}
			else
			{
				setTargetName(mage.getName());
				UndoList undoList = mage.undo(targetBlock);
{'fraction_non_alphanumeric': 0.12018140589569161, 'fraction_numerical': 0.00045351473922902497, 'mean_word_length': 3.743010752688172, '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': '258576', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1402, 'n_words': 250}