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

import org.bukkit.block.Block;

import com.elmakers.mine.bukkit.plugins.magic.Spell;
import com.elmakers.mine.bukkit.plugins.magic.SpellResult;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class UndoSpell extends Spell
{
	@Override
	public SpellResult onCast(ConfigurationNode parameters) 
	{
		if (parameters.containsKey("player"))
		{
			String undoPlayer = parameters.getString("player");
			boolean undone = controller.undo(undoPlayer);
			if (undone)
			{
				sendMessage("You revert " + undoPlayer + "'s construction");
			}
			else
			{
				sendMessage("There is nothing to undo for " + undoPlayer);
			}
			return undone ? SpellResult.SUCCESS : SpellResult.FAILURE;
		}

		if (parameters.containsKey("type"))
		{
			String typeString = (String)parameters.getString("type");
			if (typeString.equals("commit"))
			{
				if (controller.commit(getPlayer().getName())) {
					sendMessage("Undo queue cleared");
					return SpellResult.SUCCESS;
				} else {
					castMessage("Nothing to commit");
					return SpellResult.FAILURE;
				}
			}
			boolean targetAll = typeString.equals("target_all");
			if (typeString.equals("target") || targetAll)
			{
				// targetThrough(Material.GLASS);
				Block target = getTargetBlock();
				if (target != null)
				{
					boolean undone = false;
					if (targetAll)
					{
						undone = controller.undoAny(getPlayer(), target);
					}
					else
					{
						undone = controller.undo(getPlayer().getName(), target);
						if (undone) {
							sendMessage("You revert your construction");
						}
					}

					if (undone)
					{
						return SpellResult.SUCCESS;
					}
				}
				return SpellResult.NO_TARGET;
			}
		}

		/*
		 * No target, or target isn't yours- just undo last
		 */
		boolean undone = controller.undo(getPlayer().getName());
		if (undone)
		{
			castMessage("You revert your construction");
		}
		else
		{
			castMessage("Nothing to undo");
		}
		return undone ? SpellResult.SUCCESS : SpellResult.FAILURE;	
	}
}

@@ -1126,16 +1126,282 @@
 %09%7D%0A%09%09%09%7D%0A
+%09%09%09else if (typeString.equals(%22commitall%22))%0A%09%09%09%7B%0A%09%09%09%09if (controller.commitAll()) %7B%0A%09%09%09%09%09sendMessage(%22All undo queues cleared%22);%0A%09%09%09%09%09return SpellResult.SUCCESS;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09castMessage(%22Nothing in any undo queues%22);%0A%09%09%09%09%09return SpellResult.FAILURE;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A
 %09%09%09boole

Add "commitall" option to undo spell.