package net.minecraft.block;

import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockBush extends Block implements net.minecraftforge.common.IPlantable
{
    private static final String __OBFID = "CL_00000208";

    protected BlockBush(Material materialIn)
    {
        super(materialIn);
        this.setTickRandomly(true);
        float f = 0.2F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 3.0F, 0.5F + f);
        this.setCreativeTab(CreativeTabs.tabDecorations);
    }

    protected BlockBush()
    {
        this(Material.plants);
    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return super.canPlaceBlockAt(worldIn, pos) && worldIn.getBlockState(pos.offsetDown()).getBlock().canSustainPlant(worldIn, pos.offsetDown(), net.minecraft.util.EnumFacing.UP, this);
    }

    /**
     * is the block grass, dirt or farmland
     */
    protected boolean canPlaceBlockOn(Block ground)
    {
        return ground == Blocks.grass || ground == Blocks.dirt || ground == Blocks.farmland;
    }

    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
        super.onNeighborBlockChange(worldIn, pos, state, neighborBlock);
        this.func_176475_e(worldIn, pos, state);
    }

    public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
    {
        this.func_176475_e(worldIn, pos, state);
    }

    protected void func_176475_e(World worldIn, BlockPos p_176475_2_, IBlockState p_176475_3_)
    {
        if (!this.canBlockStay(worldIn, p_176475_2_, p_176475_3_))
        {
            this.dropBlockAsItem(worldIn, p_176475_2_, p_176475_3_, 0);
            worldIn.setBlockState(p_176475_2_, Blocks.air.getDefaultState(), 3);
        }
    }

    public boolean canBlockStay(World worldIn, BlockPos p_180671_2_, IBlockState p_180671_3_)
    {
        return this.canPlaceBlockOn(worldIn.getBlockState(p_180671_2_.offsetDown()).getBlock());
    }

    public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
    {
        return null;
    }

    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        return false;
    }

    @SideOnly(Side.CLIENT)
    public EnumWorldBlockLayer getBlockLayer()
    {
        return EnumWorldBlockLayer.CUTOUT;
    }

    @Override
    public net.minecraftforge.common.EnumPlantType getPlantType(net.minecraft.world.IBlockAccess world, BlockPos pos)
    {
        if (this == Blocks.wheat)          return net.minecraftforge.common.EnumPlantType.Crop;
        if (this == Blocks.carrots)        return net.minecraftforge.common.EnumPlantType.Crop;
        if (this == Blocks.potatoes)       return net.minecraftforge.common.EnumPlantType.Crop;
        if (this == Blocks.melon_stem)     return net.minecraftforge.common.EnumPlantType.Crop;
        if (this == Blocks.pumpkin_stem)   return net.minecraftforge.common.EnumPlantType.Crop;
        if (this == Blocks.deadbush)       return net.minecraftforge.common.EnumPlantType.Desert;
        if (this == Blocks.waterlily)      return net.minecraftforge.common.EnumPlantType.Water;
        if (this == Blocks.red_mushroom)   return net.minecraftforge.common.EnumPlantType.Cave;
        if (this == Blocks.brown_mushroom) return net.minecraftforge.common.EnumPlantType.Cave;
        if (this == Blocks.nether_wart)    return net.minecraftforge.common.EnumPlantType.Nether;
        if (this == Blocks.sapling)        return net.minecraftforge.common.EnumPlantType.Plains;
        if (this == Blocks.tallgrass)      return net.minecraftforge.common.EnumPlantType.Plains;
        if (this == Blocks.double_plant)   return net.minecraftforge.common.EnumPlantType.Plains;
        if (this == Blocks.red_flower)     return net.minecraftforge.common.EnumPlantType.Plains;
        if (this == Blocks.yellow_flower)  return net.minecraftforge.common.EnumPlantType.Plains;
        return net.minecraftforge.common.EnumPlantType.Plains;
    }

    @Override
    public IBlockState getPlant(net.minecraft.world.IBlockAccess world, BlockPos pos)
    {
        IBlockState state = world.getBlockState(pos);
        if (state.getBlock() != this) return getDefaultState();
        return state;
    }
}