package hmggirlfront.block.tile;
 
import java.util.List;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.mob.EntityIBC01;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
 
public class TileEntity_Goods extends TileEntity implements ITickable
{
	public boolean spawn = false;
	
	public int count = 0;
	
	public int id = 0;
    
	@SideOnly(Side.CLIENT)
    public double getMaxRenderDistanceSquared()
    {
        return 65536.0D;
    }
	
    @Override
	public void update() {
    	Block block = this.world.getBlockState(this.pos).getBlock();
        
        int x = this.pos.getX();
        int y= this.pos.getY();
        int z = this.pos.getZ();
        
        {
        	this.canspawn(x, y, z, 2);
        }
        if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.b_goods_ibc01_break) {
        	id = 1;
        }else if(this.world.getBlockState(this.pos).getBlock() == mod_GirlFront.b_goods_tank_break) {
        	id = 0;
        }
        
        boolean x_pos = false;
        boolean z_pos = false;
        
        if(x %5 == 0) {
        	x_pos = true;
        }
        if(z %5 == 0) {
        	z_pos = true;
        }
        
        
		if (spawn && (x_pos || z_pos) && id == 1) {
			if (!this.world.isRemote) {
				EntityIBC01 entityskeleton = new EntityIBC01(this.world);
				entityskeleton.setLocationAndAngles(x, y, z, 0, 0.0F);
				this.world.spawnEntity(entityskeleton);
				this.world.setBlockToAir(this.pos);
			}
		}
       
	}
	
    public void canspawn(double x, double y, double z, int han) {
    	if(this.world.isRemote)return;
    	Entity entity = null;
    	boolean player = false;
    	{
    		AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
        			(double)(x + han), (double)(y + han), (double)(z+ han)))
            		.grow(1);
            List llist2 = this.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
            if(llist2!=null){
                for (int lj = 0; lj < llist2.size(); lj++) {
                	
                	Entity entity1 = (Entity)llist2.get(lj);
                	if (entity1.canBeCollidedWith())
                    {
                		if (entity1 instanceof EntityPlayer && entity1 != null)
                        {
                			EntityPlayer ss = (EntityPlayer) entity1;
                			ItemStack itemstack = ss.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
                    		if(!itemstack.isEmpty() && itemstack.getItem() == Items.GOLDEN_HELMET){
                    		}else {
                    			spawn = true;
                    		}
                        }
                    }
                }
            }
    	}
    }
    
}