/*******************************************************************************
 * Copyright (C) 2017 Jay Avery
 * 
 * This file is part of Geomastery. Geomastery is free software: distributed
 * under the GNU Affero General Public License (<http://www.gnu.org/licenses/>).
 ******************************************************************************/
package jayavery.geomastery.blocks;

import java.util.List;
import com.google.common.collect.Lists;
import jayavery.geomastery.utilities.BlockMaterial;
import jayavery.geomastery.utilities.BlockWeight;
import jayavery.geomastery.utilities.ToolType;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityAmbientCreature;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

/** Flat breakable roof block. */
public class BlockFlatroof extends BlockBuilding {

    public BlockFlatroof(String name, float hardness, ToolType harvestTool) {
        
        super(BlockMaterial.WOOD_HANDHARVESTABLE, name,
                CreativeTabs.BUILDING_BLOCKS, hardness, harvestTool);
    }
    
    @Override
    public BlockWeight getWeight() {
        
        return BlockWeight.NONE;
    }
    
    @Override
    public boolean shouldConnect(IBlockAccess world, IBlockState state,
            BlockPos pos, EnumFacing direcion) {
        
        return false;
    }
    
    @Override
    public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos,
            IBlockState state, int fortune) {
        
        return Lists.newArrayList(new ItemStack(Item.getItemFromBlock(this)));
    }

    @Override
    public boolean isValid(World world, BlockPos pos) {
                        
        if (BlockWeight.getWeight(world.getBlockState(pos.down()).getBlock())
                .canSupport(this.getWeight())) {
            
            return true;
        }
        
        for (EnumFacing facing : EnumFacing.HORIZONTALS) {
            
            int distance = 1;
            
            while (distance <= 2) {
                
                BlockPos offset = pos.offset(facing, distance);
                
                if (this.isValidSupport(world, offset)) {
                    
                    return true;
                }
                
                if (!(world.getBlockState(offset).getBlock()
                        instanceof BlockFlatroof)) {
                    
                    break;
                }
                
                distance++;
            }
        }
        
        return false;
    }

    /** Breaks the block if an entity tries to walk on it. */
    @Override
    public void onEntityCollidedWithBlock(World world, BlockPos pos,
            IBlockState state, Entity entity) {
        
        if (entity instanceof EntityLivingBase &&
                !(entity instanceof EntityAmbientCreature)) {
            
            EntityLivingBase living = (EntityLivingBase) entity;
            BlockPos feetPos = new BlockPos(living.posX,
                    living.getEntityBoundingBox().minY + 0.5D, living.posZ);

            if (feetPos.equals(pos) && !BlockWeight.getWeight(world
                    .getBlockState(pos.down()).getBlock())
                    .canSupport(this.getWeight())) {
            
                world.destroyBlock(pos, true);
            }
        }
    }
    
    /** @return Whether the given position is a valid supported roof block. */
    protected boolean isValidSupport(World world, BlockPos pos) {
        
        boolean isRoof = world.getBlockState(pos).getBlock()
                instanceof BlockFlatroof;
        boolean isSupported = BlockWeight.getWeight(world.getBlockState(pos
                .down()).getBlock()).canSupport(this.getWeight());
        return isRoof && isSupported;
    }
    
    @Override
    public AxisAlignedBB getBoundingBox(IBlockState state,
            IBlockAccess world, BlockPos pos) {
        
        return TWO;
    }
    
    @Override
    public AxisAlignedBB getCollisionBoundingBox(IBlockState state,
            IBlockAccess world, BlockPos pos) {
        
        return NULL_AABB;
    }
    
    @Override
    public IBlockState getActualState(IBlockState state,
            IBlockAccess world, BlockPos pos) {
        
        return state;
    }
    
    @Override
    public BlockStateContainer createBlockState() {
        
        return new BlockStateContainer(this);
    }
}

@@ -409,16 +409,41 @@
 l.List;%0A
+import java.util.Random;%0A
 import c
@@ -1018,16 +1018,61 @@
 Facing;%0A
+import net.minecraft.util.EnumParticleTypes;%0A
 import n
@@ -1224,16 +1224,114 @@
 d.World;
+%0Aimport net.minecraftforge.fml.relauncher.Side;%0Aimport net.minecraftforge.fml.relauncher.SideOnly;
 %0A%0A/** Fl
@@ -4345,32 +4345,731 @@
 ted;%0A    %7D%0A    %0A
+    /** Make drips when raining (as vanilla leaves). */%0A    @SideOnly(Side.CLIENT)%0A    @Override%0A    public void randomDisplayTick(IBlockState stateIn, World world,%0A            BlockPos pos, Random rand) %7B%0A        %0A        if (world.isRainingAt(pos.up()) && !world.getBlockState(pos.down())%0A                .isSideSolid(world, pos.down(), EnumFacing.UP) &&%0A                rand.nextInt(15) == 1) %7B%0A            %0A            double d0 = pos.getX() + rand.nextFloat();%0A            double d1 = pos.getY() - 0.05D;%0A            double d2 = pos.getZ() + rand.nextFloat();%0A            world.spawnParticle(EnumParticleTypes.DRIP_WATER, d0, d1, d2,%0A                    0.0D, 0.0D, 0.0D);%0A        %7D%0A    %7D%0A    %0A
     @Override%0A  

Make flat roofs drip in rain