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

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;

import com.elmakers.mine.bukkit.blocks.SimulateBatch;
import com.elmakers.mine.bukkit.plugins.magic.BlockSpell;
import com.elmakers.mine.bukkit.plugins.magic.SpellResult;
import com.elmakers.mine.bukkit.utilities.Target;
import com.elmakers.mine.bukkit.utilities.borrowed.ConfigurationNode;

public class SimulateSpell extends BlockSpell {
	
	private static final int DEFAULT_RADIUS = 32;

	@Override
	public SpellResult onCast(ConfigurationNode parameters) {
		Target t = getTarget();
		if (t == null) {
			return SpellResult.NO_TARGET;
		}
		
		Block target = t.getBlock();
		if (target == null) {
			return SpellResult.NO_TARGET;
		}
		
		if (!hasBuildPermission(target)) {
			return SpellResult.INSUFFICIENT_PERMISSION;
		}
		
		int radius = parameters.getInt("radius", DEFAULT_RADIUS);
		radius = parameters.getInt("r", radius);
		
		Material birthMaterial = target.getType();
		birthMaterial = parameters.getMaterial("material", birthMaterial);
		Material deathMaterial = parameters.getMaterial("death_material", Material.AIR);

		SimulateBatch batch = new SimulateBatch(this, target.getLocation(), radius, birthMaterial, deathMaterial);
		
		boolean includeCommands = parameters.getBoolean("commands", true);
		if (includeCommands) {
			if (mage.getCommandSender() instanceof BlockCommandSender) {
				BlockCommandSender commandBlock = (BlockCommandSender)mage.getCommandSender();
				batch.setCommandBlock(commandBlock.getBlock());
			} else if (target.getType() == Material.COMMAND) {
				batch.setCommandBlock(target);
			}
		}
		
		mage.addPendingBlockBatch(batch);
		
		return SpellResult.CAST;
	}
}

@@ -49,16 +49,42 @@
 pells;%0A%0A
+import org.bukkit.Bukkit;%0A
 import o
@@ -1218,16 +1218,22 @@
 IR);%0A%0A%09%09
+final 
 Simulate
@@ -1734,41 +1734,400 @@
 %09%0A%09%09
-mage.addPendingBlockBatch(batch);
+// delay is in ms, gets converted.%0A%09%09int delay = parameters.getInt(%22delay%22, 0);%0A%09%09// 1000 ms in a second, 20 ticks in a second - 1000 / 20 = 50.%0A%09%09delay /= 50;%0A%09%09if (delay %3E 0) %7B%0A%09%09%09Bukkit.getScheduler().runTaskLater(controller.getPlugin(), new Runnable() %7B%0A%09%09%09%09public void run() %7B%0A%09%09%09%09%09mage.addPendingBlockBatch(batch);%0A%09%09%09%09%7D%0A%09%09%09%7D, delay);%0A%09%09%7D else %7B%0A%09%09%09mage.addPendingBlockBatch(batch);%0A%09%09%7D
 %0A%09%09%0A

Add built-in delay to Simulate spell to fix issue with multiple gliders.