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

import java.util.Random;

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

import com.elmakers.mine.bukkit.plugins.magicworlds.MagicWorldsController;

public abstract class SpawnRule implements Comparable<SpawnRule> {
	protected String					key;
	protected EntityType 				targetEntityType;
	protected float        				percentChance;
	protected int						minY;
	protected int						maxY;
    protected MagicWorldsController	controller;
    
    protected static final Random rand = new Random();

    public abstract LivingEntity onProcess(Plugin plugin, LivingEntity entity);

    public SpawnRule()
    {
    }
    
    public boolean load(String key, ConfigurationSection parameters, MagicWorldsController controller)
    {
    	this.key = key;
    	this.controller = controller;
    	String entityTypeName = parameters.getString("target_type");
    	this.targetEntityType = parseEntityType(entityTypeName);
		if (targetEntityType == null) {
			this.controller.getLogger().warning(" Invalid entity type: " + entityTypeName);
			return false;
		}
		this.minY = parameters.getInt("min_y", 0);
		this.maxY = parameters.getInt("max_y", 255);
		this.percentChance = (float)parameters.getDouble("probability", 1.0);
		
		return true;
    }
	
	@SuppressWarnings("deprecation")
	public static EntityType parseEntityType(String typeString) 
	{
		if (typeString.equalsIgnoreCase("horse")) 
		{
			return EntityType.HORSE;
		}
		
		return EntityType.fromName(typeString);
	}

    public void setPercentChance(float percentChance)
    {
        this.percentChance = percentChance;
    }
    
    public float getPercentChance()
    {
    	return percentChance;
    }
    
    public EntityType getTargetType() 
    {
    	return targetEntityType;
    }
    
    public String getKey()
    {
    	return key;
    }
    
    public LivingEntity process(Plugin plugin, LivingEntity entity) 
    {
    	if (targetEntityType != entity.getType()) return null;
        if (percentChance < rand.nextFloat()) return null;
        int y = entity.getLocation().getBlockY();
        if (y < minY || y > maxY) return null;
           	
    	return onProcess(plugin, entity);
    }
    
	@Override
	public int compareTo(SpawnRule other) 
	{
		return this.key.compareTo(other.key);
	}
}

@@ -80,16 +80,75 @@
 andom;%0A%0A
+import org.bukkit.Location;%0Aimport org.bukkit.block.Block;%0A
 import o
@@ -189,24 +189,24 @@
 ionSection;%0A
-
 import org.b
@@ -612,16 +612,53 @@
 %09%09maxY;%0A
+%09protected boolean%09%09%09%09%09allowIndoors;%0A
     prot
@@ -1310,16 +1310,84 @@
 se;%0A%09%09%7D%0A
+%09%09this.allowIndoors = parameters.getBoolean(%22allow_indoors%22, true);%0A
 %09%09this.m
@@ -2345,96 +2345,472 @@
     
-int y = entity.getLocation().getBlockY();%0A        if (y %3C minY %7C%7C y %3E maxY) return null;
+Location entityLocation = entity.getLocation();%0A        int y = entityLocation.getBlockY();%0A        if (y %3C minY %7C%7C y %3E maxY) return null;%0A        if (!this.allowIndoors) %7B%0A        %09// Bump it up two to miss things like tall grass%0A        %09Block entityBlock = entityLocation.getBlock().getRelative(0, 2, 0);%0A        %09if (entityBlock.getWorld().getHighestBlockAt(entityBlock.getLocation()).getY() %3E entityBlock.getY()) %7B%0A        %09%09return null;%0A        %09%7D %0A        %7D
 %0A   

Add an "allow_indoors" flag to spawn rules