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.client.renderer.texture.IconRegister;
008import net.minecraft.creativetab.CreativeTabs;
009import net.minecraft.item.ItemStack;
010import net.minecraft.util.Icon;
011import net.minecraft.world.World;
012import net.minecraft.world.gen.feature.WorldGenBigTree;
013import net.minecraft.world.gen.feature.WorldGenForest;
014import net.minecraft.world.gen.feature.WorldGenHugeTrees;
015import net.minecraft.world.gen.feature.WorldGenTaiga2;
016import net.minecraft.world.gen.feature.WorldGenTrees;
017import net.minecraft.world.gen.feature.WorldGenerator;
018
019import net.minecraftforge.event.terraingen.TerrainGen;
020
021public class BlockSapling extends BlockFlower
022{
023    public static final String[] WOOD_TYPES = new String[] {"oak", "spruce", "birch", "jungle"};
024    private static final String[] field_94370_b = new String[] {"sapling", "sapling_spruce", "sapling_birch", "sapling_jungle"};
025    @SideOnly(Side.CLIENT)
026    private Icon[] field_94371_c;
027
028    protected BlockSapling(int par1)
029    {
030        super(par1);
031        float f = 0.4F;
032        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
033        this.setCreativeTab(CreativeTabs.tabDecorations);
034    }
035
036    /**
037     * Ticks the block if it's been scheduled
038     */
039    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
040    {
041        if (!par1World.isRemote)
042        {
043            super.updateTick(par1World, par2, par3, par4, par5Random);
044
045            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5Random.nextInt(7) == 0)
046            {
047                this.func_96477_c(par1World, par2, par3, par4, par5Random);
048            }
049        }
050    }
051
052    @SideOnly(Side.CLIENT)
053
054    /**
055     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
056     */
057    public Icon getBlockTextureFromSideAndMetadata(int par1, int par2)
058    {
059        par2 &= 3;
060        return this.field_94371_c[par2];
061    }
062
063    public void func_96477_c(World par1World, int par2, int par3, int par4, Random par5Random)
064    {
065        int l = par1World.getBlockMetadata(par2, par3, par4);
066
067        if ((l & 8) == 0)
068        {
069            par1World.setBlockMetadataWithNotify(par2, par3, par4, l | 8, 4);
070        }
071        else
072        {
073            this.growTree(par1World, par2, par3, par4, par5Random);
074        }
075    }
076
077    /**
078     * Attempts to grow a sapling into a tree
079     */
080    public void growTree(World par1World, int par2, int par3, int par4, Random par5Random)
081    {
082        if (!TerrainGen.saplingGrowTree(par1World, par5Random, par2, par3, par4)) return;
083
084        int l = par1World.getBlockMetadata(par2, par3, par4) & 3;
085        Object object = null;
086        int i1 = 0;
087        int j1 = 0;
088        boolean flag = false;
089
090        if (l == 1)
091        {
092            object = new WorldGenTaiga2(true);
093        }
094        else if (l == 2)
095        {
096            object = new WorldGenForest(true);
097        }
098        else if (l == 3)
099        {
100            for (i1 = 0; i1 >= -1; --i1)
101            {
102                for (j1 = 0; j1 >= -1; --j1)
103                {
104                    if (this.isSameSapling(par1World, par2 + i1, par3, par4 + j1, 3) && this.isSameSapling(par1World, par2 + i1 + 1, par3, par4 + j1, 3) && this.isSameSapling(par1World, par2 + i1, par3, par4 + j1 + 1, 3) && this.isSameSapling(par1World, par2 + i1 + 1, par3, par4 + j1 + 1, 3))
105                    {
106                        object = new WorldGenHugeTrees(true, 10 + par5Random.nextInt(20), 3, 3);
107                        flag = true;
108                        break;
109                    }
110                }
111
112                if (object != null)
113                {
114                    break;
115                }
116            }
117
118            if (object == null)
119            {
120                j1 = 0;
121                i1 = 0;
122                object = new WorldGenTrees(true, 4 + par5Random.nextInt(7), 3, 3, false);
123            }
124        }
125        else
126        {
127            object = new WorldGenTrees(true);
128
129            if (par5Random.nextInt(10) == 0)
130            {
131                object = new WorldGenBigTree(true);
132            }
133        }
134
135        if (flag)
136        {
137            par1World.setBlock(par2 + i1, par3, par4 + j1, 0, 0, 4);
138            par1World.setBlock(par2 + i1 + 1, par3, par4 + j1, 0, 0, 4);
139            par1World.setBlock(par2 + i1, par3, par4 + j1 + 1, 0, 0, 4);
140            par1World.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, 0, 0, 4);
141        }
142        else
143        {
144            par1World.setBlock(par2, par3, par4, 0, 0, 4);
145        }
146
147        if (!((WorldGenerator)object).generate(par1World, par5Random, par2 + i1, par3, par4 + j1))
148        {
149            if (flag)
150            {
151                par1World.setBlock(par2 + i1, par3, par4 + j1, this.blockID, l, 4);
152                par1World.setBlock(par2 + i1 + 1, par3, par4 + j1, this.blockID, l, 4);
153                par1World.setBlock(par2 + i1, par3, par4 + j1 + 1, this.blockID, l, 4);
154                par1World.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, this.blockID, l, 4);
155            }
156            else
157            {
158                par1World.setBlock(par2, par3, par4, this.blockID, l, 4);
159            }
160        }
161    }
162
163    /**
164     * Determines if the same sapling is present at the given location.
165     */
166    public boolean isSameSapling(World par1World, int par2, int par3, int par4, int par5)
167    {
168        return par1World.getBlockId(par2, par3, par4) == this.blockID && (par1World.getBlockMetadata(par2, par3, par4) & 3) == par5;
169    }
170
171    /**
172     * Determines the damage on the item the block drops. Used in cloth and wood.
173     */
174    public int damageDropped(int par1)
175    {
176        return par1 & 3;
177    }
178
179    @SideOnly(Side.CLIENT)
180
181    /**
182     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
183     */
184    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
185    {
186        par3List.add(new ItemStack(par1, 1, 0));
187        par3List.add(new ItemStack(par1, 1, 1));
188        par3List.add(new ItemStack(par1, 1, 2));
189        par3List.add(new ItemStack(par1, 1, 3));
190    }
191
192    @SideOnly(Side.CLIENT)
193
194    /**
195     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
196     * is the only chance you get to register icons.
197     */
198    public void registerIcons(IconRegister par1IconRegister)
199    {
200        this.field_94371_c = new Icon[field_94370_b.length];
201
202        for (int i = 0; i < this.field_94371_c.length; ++i)
203        {
204            this.field_94371_c[i] = par1IconRegister.registerIcon(field_94370_b[i]);
205        }
206    }
207}