package com.elmakers.mine.bukkit.plugins.magicworlds.spawn.builtin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.plugin.Plugin;

import com.elmakers.mine.bukkit.api.magic.MagicAPI;
import com.elmakers.mine.bukkit.plugins.magicworlds.MagicWorldsController;
import com.elmakers.mine.bukkit.plugins.magicworlds.spawn.SpawnRule;

public class CastRule extends SpawnRule {
	protected List<CastSpell>		spells;
    private class CastSpell
    {
    	protected final String			name;
        protected final String[] 		parameters;
        
        public CastSpell(String name, String[] parameters) {
        	this.name = name;
        	this.parameters = parameters;
        }
    }
    
    @Override
    public boolean load(String key, ConfigurationSection parameters, MagicWorldsController controller)
    {
    	if (!super.load(key, parameters, controller)) return false;
		Collection<String> spells = parameters.getStringList("spells");
		if (spells == null || spells.size() == 0) return false;
		this.spells = new ArrayList<CastSpell>();
		for (String spellName : spells) {
			String[] spellParameters = new String[0];
			if (spellName.contains(" ")) {
				String[] pieces = spellName.split(" ");
				spellName = pieces[0];
				spellParameters = new String[pieces.length - 1];
				for (int i = 1; i < pieces.length; i++) {
					spellParameters[i - 1] = pieces[i];
				}
			}
			
			this.spells.add(new CastSpell(spellName, spellParameters));
			
			controller.getLogger().info(" Casting: " + spellName + " on " + targetEntityType.name() + " at y > " + minY  
					+ " with a " + (percentChance * 100) + "% chance");
		}
		
		return true;
    }
    
    @Override
    public LivingEntity onProcess(Plugin plugin, LivingEntity entity) {
    	if (controller == null) {
    		return null;
    	}
    	
    	String[] standardParameters = {
        	"pworld", entity.getLocation().getWorld().getName(), 
    		"px", Integer.toString(entity.getLocation().getBlockX()), 
    		"py", Integer.toString(entity.getLocation().getBlockY()), 
    		"pz", Integer.toString(entity.getLocation().getBlockZ()), 
    		"target", "self"
    	};
    	
    	for (CastSpell spell : spells) {
	    	String[] fullParameters = new String[spell.parameters.length + standardParameters.length];
	    	for (int index = 0; index < standardParameters.length; index++) {
	    		fullParameters[index] = standardParameters[index];
			
	    	}
	    	for (int index = 0; index < spell.parameters.length; index++) {
	    		fullParameters[index  + standardParameters.length] = spell.parameters[index];
	    	}
	    	
	    	MagicAPI magic = controller.getMagic();
	    	if (magic != null) {
	    		magic.cast(spell.name, fullParameters);    	
	        	controller.getLogger().info(" Spawn rule casting: " + spell.name + " at " + entity.getLocation().toVector());
	    	}
    	}
    	
    	// This will always end rule processing, so make sure to put them last.
    	return entity;
    }
}

@@ -143,16 +143,53 @@
 .List;%0A%0A
+import org.bukkit.World.Environment;%0A
 import o
@@ -230,24 +230,24 @@
 ionSection;%0A
-
 import org.b
@@ -582,16 +582,47 @@
 spells;%0A
+%09protected int %09%09%09%09%09yOffset;%0A%09%0A
     priv
@@ -1075,16 +1075,71 @@
  false;%0A
+    %09%0A    %09yOffset = parameters.getInt(%22y_offset%22, 0);%0A
 %09%09Collec
@@ -2054,24 +2054,216 @@
    %09%7D%0A    %09%0A
+    %09int y = entity.getLocation().getBlockY() + yOffset;%0A    %09if (y %3E 250) y = 250;%0A    %09if (entity.getWorld().getEnvironment() == Environment.NETHER && y %3E 118) %7B%0A    %09%09y = 118;%0A    %09%7D%0A    %09%0A
     %09String%5B
@@ -2448,40 +2448,9 @@
 ing(
-entity.getLocation().getBlockY()
+y
 ), %0A

Add a y-offset for the cast rule