    001package net.minecraft.block;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.List;
    006import java.util.Random;
    007import net.minecraft.creativetab.CreativeTabs;
    008import net.minecraft.item.ItemStack;
    009import net.minecraft.world.World;
    010import net.minecraft.world.gen.feature.WorldGenBigTree;
    011import net.minecraft.world.gen.feature.WorldGenForest;
    012import net.minecraft.world.gen.feature.WorldGenHugeTrees;
    013import net.minecraft.world.gen.feature.WorldGenTaiga2;
    014import net.minecraft.world.gen.feature.WorldGenTrees;
    015import net.minecraft.world.gen.feature.WorldGenerator;
    016
    017import net.minecraftforge.event.terraingen.TerrainGen;
    018
    019public class BlockSapling extends BlockFlower
    020{
    021    public static final String[] WOOD_TYPES = new String[] {"oak", "spruce", "birch", "jungle"};
    022
    023    protected BlockSapling(int par1, int par2)
    024    {
    025        super(par1, par2);
    026        float var3 = 0.4F;
    027        this.setBlockBounds(0.5F - var3, 0.0F, 0.5F - var3, 0.5F + var3, var3 * 2.0F, 0.5F + var3);
    028        this.setCreativeTab(CreativeTabs.tabDecorations);
    029    }
    030
    031    /**
    032     * Ticks the block if it's been scheduled
    033     */
    034    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
    035    {
    036        if (!par1World.isRemote)
    037        {
    038            super.updateTick(par1World, par2, par3, par4, par5Random);
    039
    040            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5Random.nextInt(7) == 0)
    041            {
    042                int var6 = par1World.getBlockMetadata(par2, par3, par4);
    043
    044                if ((var6 & 8) == 0)
    045                {
    046                    par1World.setBlockMetadataWithNotify(par2, par3, par4, var6 | 8);
    047                }
    048                else
    049                {
    050                    this.growTree(par1World, par2, par3, par4, par5Random);
    051                }
    052            }
    053        }
    054    }
    055
    056    /**
    057     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
    058     */
    059    public int getBlockTextureFromSideAndMetadata(int par1, int par2)
    060    {
    061        par2 &= 3;
    062        return par2 == 1 ? 63 : (par2 == 2 ? 79 : (par2 == 3 ? 30 : super.getBlockTextureFromSideAndMetadata(par1, par2)));
    063    }
    064
    065    /**
    066     * Attempts to grow a sapling into a tree
    067     */
    068    public void growTree(World par1World, int par2, int par3, int par4, Random par5Random)
    069    {
    070        if (!TerrainGen.saplingGrowTree(par1World, par5Random, par2, par3, par4)) return;
    071
    072        int var6 = par1World.getBlockMetadata(par2, par3, par4) & 3;
    073        Object var7 = null;
    074        int var8 = 0;
    075        int var9 = 0;
    076        boolean var10 = false;
    077
    078        if (var6 == 1)
    079        {
    080            var7 = new WorldGenTaiga2(true);
    081        }
    082        else if (var6 == 2)
    083        {
    084            var7 = new WorldGenForest(true);
    085        }
    086        else if (var6 == 3)
    087        {
    088            for (var8 = 0; var8 >= -1; --var8)
    089            {
    090                for (var9 = 0; var9 >= -1; --var9)
    091                {
    092                    if (this.isSameSapling(par1World, par2 + var8, par3, par4 + var9, 3) && this.isSameSapling(par1World, par2 + var8 + 1, par3, par4 + var9, 3) && this.isSameSapling(par1World, par2 + var8, par3, par4 + var9 + 1, 3) && this.isSameSapling(par1World, par2 + var8 + 1, par3, par4 + var9 + 1, 3))
    093                    {
    094                        var7 = new WorldGenHugeTrees(true, 10 + par5Random.nextInt(20), 3, 3);
    095                        var10 = true;
    096                        break;
    097                    }
    098                }
    099
    100                if (var7 != null)
    101                {
    102                    break;
    103                }
    104            }
    105
    106            if (var7 == null)
    107            {
    108                var9 = 0;
    109                var8 = 0;
    110                var7 = new WorldGenTrees(true, 4 + par5Random.nextInt(7), 3, 3, false);
    111            }
    112        }
    113        else
    114        {
    115            var7 = new WorldGenTrees(true);
    116
    117            if (par5Random.nextInt(10) == 0)
    118            {
    119                var7 = new WorldGenBigTree(true);
    120            }
    121        }
    122
    123        if (var10)
    124        {
    125            par1World.setBlock(par2 + var8, par3, par4 + var9, 0);
    126            par1World.setBlock(par2 + var8 + 1, par3, par4 + var9, 0);
    127            par1World.setBlock(par2 + var8, par3, par4 + var9 + 1, 0);
    128            par1World.setBlock(par2 + var8 + 1, par3, par4 + var9 + 1, 0);
    129        }
    130        else
    131        {
    132            par1World.setBlock(par2, par3, par4, 0);
    133        }
    134
    135        if (!((WorldGenerator)var7).generate(par1World, par5Random, par2 + var8, par3, par4 + var9))
    136        {
    137            if (var10)
    138            {
    139                par1World.setBlockAndMetadata(par2 + var8, par3, par4 + var9, this.blockID, var6);
    140                par1World.setBlockAndMetadata(par2 + var8 + 1, par3, par4 + var9, this.blockID, var6);
    141                par1World.setBlockAndMetadata(par2 + var8, par3, par4 + var9 + 1, this.blockID, var6);
    142                par1World.setBlockAndMetadata(par2 + var8 + 1, par3, par4 + var9 + 1, this.blockID, var6);
    143            }
    144            else
    145            {
    146                par1World.setBlockAndMetadata(par2, par3, par4, this.blockID, var6);
    147            }
    148        }
    149    }
    150
    151    /**
    152     * Determines if the same sapling is present at the given location.
    153     */
    154    public boolean isSameSapling(World par1World, int par2, int par3, int par4, int par5)
    155    {
    156        return par1World.getBlockId(par2, par3, par4) == this.blockID && (par1World.getBlockMetadata(par2, par3, par4) & 3) == par5;
    157    }
    158
    159    /**
    160     * Determines the damage on the item the block drops. Used in cloth and wood.
    161     */
    162    public int damageDropped(int par1)
    163    {
    164        return par1 & 3;
    165    }
    166
    167    @SideOnly(Side.CLIENT)
    168
    169    /**
    170     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
    171     */
    172    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    173    {
    174        par3List.add(new ItemStack(par1, 1, 0));
    175        par3List.add(new ItemStack(par1, 1, 1));
    176        par3List.add(new ItemStack(par1, 1, 2));
    177        par3List.add(new ItemStack(par1, 1, 3));
    178    }
    179}