001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    
006    import java.util.ArrayList;
007    import java.util.Random;
008    
009    import net.minecraftforge.common.ForgeDirection;
010    
011    public class BlockStem extends BlockFlower
012    {
013        /** Defines if it is a Melon or a Pumpkin that the stem is producing. */
014        private Block fruitType;
015    
016        protected BlockStem(int par1, Block par2Block)
017        {
018            super(par1, 111);
019            this.fruitType = par2Block;
020            this.setTickRandomly(true);
021            float var3 = 0.125F;
022            this.setBlockBounds(0.5F - var3, 0.0F, 0.5F - var3, 0.5F + var3, 0.25F, 0.5F + var3);
023            this.setCreativeTab((CreativeTabs)null);
024        }
025    
026        /**
027         * Gets passed in the blockID of the block below and supposed to return true if its allowed to grow on the type of
028         * blockID passed in. Args: blockID
029         */
030        protected boolean canThisPlantGrowOnThisBlockID(int par1)
031        {
032            return par1 == Block.tilledField.blockID;
033        }
034    
035        /**
036         * Ticks the block if it's been scheduled
037         */
038        public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
039        {
040            super.updateTick(par1World, par2, par3, par4, par5Random);
041    
042            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9)
043            {
044                float var6 = this.getGrowthModifier(par1World, par2, par3, par4);
045    
046                if (par5Random.nextInt((int)(25.0F / var6) + 1) == 0)
047                {
048                    int var7 = par1World.getBlockMetadata(par2, par3, par4);
049    
050                    if (var7 < 7)
051                    {
052                        ++var7;
053                        par1World.setBlockMetadataWithNotify(par2, par3, par4, var7);
054                    }
055                    else
056                    {
057                        if (par1World.getBlockId(par2 - 1, par3, par4) == this.fruitType.blockID)
058                        {
059                            return;
060                        }
061    
062                        if (par1World.getBlockId(par2 + 1, par3, par4) == this.fruitType.blockID)
063                        {
064                            return;
065                        }
066    
067                        if (par1World.getBlockId(par2, par3, par4 - 1) == this.fruitType.blockID)
068                        {
069                            return;
070                        }
071    
072                        if (par1World.getBlockId(par2, par3, par4 + 1) == this.fruitType.blockID)
073                        {
074                            return;
075                        }
076    
077                        int var8 = par5Random.nextInt(4);
078                        int var9 = par2;
079                        int var10 = par4;
080    
081                        if (var8 == 0)
082                        {
083                            var9 = par2 - 1;
084                        }
085    
086                        if (var8 == 1)
087                        {
088                            ++var9;
089                        }
090    
091                        if (var8 == 2)
092                        {
093                            var10 = par4 - 1;
094                        }
095    
096                        if (var8 == 3)
097                        {
098                            ++var10;
099                        }
100    
101                        int var11 = par1World.getBlockId(var9, par3 - 1, var10);
102    
103                        boolean isSoil = (blocksList[var11] != null && blocksList[var11].canSustainPlant(par1World, var9, par3 - 1, var10, ForgeDirection.UP, this));
104                        if (par1World.getBlockId(var9, par3, var10) == 0 && (isSoil || var11 == Block.dirt.blockID || var11 == Block.grass.blockID))
105                        {
106                            par1World.setBlockWithNotify(var9, par3, var10, this.fruitType.blockID);
107                        }
108                    }
109                }
110            }
111        }
112    
113        public void fertilizeStem(World par1World, int par2, int par3, int par4)
114        {
115            par1World.setBlockMetadataWithNotify(par2, par3, par4, 7);
116        }
117    
118        private float getGrowthModifier(World par1World, int par2, int par3, int par4)
119        {
120            float var5 = 1.0F;
121            int var6 = par1World.getBlockId(par2, par3, par4 - 1);
122            int var7 = par1World.getBlockId(par2, par3, par4 + 1);
123            int var8 = par1World.getBlockId(par2 - 1, par3, par4);
124            int var9 = par1World.getBlockId(par2 + 1, par3, par4);
125            int var10 = par1World.getBlockId(par2 - 1, par3, par4 - 1);
126            int var11 = par1World.getBlockId(par2 + 1, par3, par4 - 1);
127            int var12 = par1World.getBlockId(par2 + 1, par3, par4 + 1);
128            int var13 = par1World.getBlockId(par2 - 1, par3, par4 + 1);
129            boolean var14 = var8 == this.blockID || var9 == this.blockID;
130            boolean var15 = var6 == this.blockID || var7 == this.blockID;
131            boolean var16 = var10 == this.blockID || var11 == this.blockID || var12 == this.blockID || var13 == this.blockID;
132    
133            for (int var17 = par2 - 1; var17 <= par2 + 1; ++var17)
134            {
135                for (int var18 = par4 - 1; var18 <= par4 + 1; ++var18)
136                {
137                    int var19 = par1World.getBlockId(var17, par3 - 1, var18);
138                    float var20 = 0.0F;
139    
140                    if (blocksList[var19] != null && blocksList[var19].canSustainPlant(par1World, var17, par3 - 1, var18, ForgeDirection.UP, this))
141                    {
142                        var20 = 1.0F;
143    
144                        if (blocksList[var19].isFertile(par1World, var17, par3 - 1, var18))
145                        {
146                            var20 = 3.0F;
147                        }
148                    }
149    
150                    if (var17 != par2 || var18 != par4)
151                    {
152                        var20 /= 4.0F;
153                    }
154    
155                    var5 += var20;
156                }
157            }
158    
159            if (var16 || var14 && var15)
160            {
161                var5 /= 2.0F;
162            }
163    
164            return var5;
165        }
166    
167        @SideOnly(Side.CLIENT)
168    
169        /**
170         * Returns the color this block should be rendered. Used by leaves.
171         */
172        public int getRenderColor(int par1)
173        {
174            int var2 = par1 * 32;
175            int var3 = 255 - par1 * 8;
176            int var4 = par1 * 4;
177            return var2 << 16 | var3 << 8 | var4;
178        }
179    
180        @SideOnly(Side.CLIENT)
181    
182        /**
183         * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
184         * when first determining what to render.
185         */
186        public int colorMultiplier(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
187        {
188            return this.getRenderColor(par1IBlockAccess.getBlockMetadata(par2, par3, par4));
189        }
190    
191        /**
192         * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
193         */
194        public int getBlockTextureFromSideAndMetadata(int par1, int par2)
195        {
196            return this.blockIndexInTexture;
197        }
198    
199        /**
200         * Sets the block's bounds for rendering it as an item
201         */
202        public void setBlockBoundsForItemRender()
203        {
204            float var1 = 0.125F;
205            this.setBlockBounds(0.5F - var1, 0.0F, 0.5F - var1, 0.5F + var1, 0.25F, 0.5F + var1);
206        }
207    
208        /**
209         * Updates the blocks bounds based on its current state. Args: world, x, y, z
210         */
211        public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
212        {
213            this.maxY = (double)((float)(par1IBlockAccess.getBlockMetadata(par2, par3, par4) * 2 + 2) / 16.0F);
214            float var5 = 0.125F;
215            this.setBlockBounds(0.5F - var5, 0.0F, 0.5F - var5, 0.5F + var5, (float)this.maxY, 0.5F + var5);
216        }
217    
218        /**
219         * The type of render function that is called for this block
220         */
221        public int getRenderType()
222        {
223            return 19;
224        }
225    
226        @SideOnly(Side.CLIENT)
227    
228        /**
229         * Returns the current state of the stem. Returns -1 if the stem is not fully grown, or a value between 0 and 3
230         * based on the direction the stem is facing.
231         */
232        public int getState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
233        {
234            int var5 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
235            return var5 < 7 ? -1 : (par1IBlockAccess.getBlockId(par2 - 1, par3, par4) == this.fruitType.blockID ? 0 : (par1IBlockAccess.getBlockId(par2 + 1, par3, par4) == this.fruitType.blockID ? 1 : (par1IBlockAccess.getBlockId(par2, par3, par4 - 1) == this.fruitType.blockID ? 2 : (par1IBlockAccess.getBlockId(par2, par3, par4 + 1) == this.fruitType.blockID ? 3 : -1))));
236        }
237    
238        /**
239         * Drops the block items with a specified chance of dropping the specified items
240         */
241        public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
242        {
243            super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, par7);
244        }
245    
246        @Override 
247        public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune)
248        {
249            ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
250    
251            for (int i = 0; i < 3; i++)
252            {
253                if (world.rand.nextInt(15) <= metadata)
254                {
255                    ret.add(new ItemStack(fruitType == pumpkin ? Item.pumpkinSeeds : Item.melonSeeds));
256                }
257            }
258    
259            return ret;
260        }
261    
262        /**
263         * Returns the ID of the items to drop on destruction.
264         */
265        public int idDropped(int par1, Random par2Random, int par3)
266        {
267            return -1;
268        }
269    
270        /**
271         * Returns the quantity of items to drop on block destruction.
272         */
273        public int quantityDropped(Random par1Random)
274        {
275            return 1;
276        }
277    
278        @SideOnly(Side.CLIENT)
279    
280        /**
281         * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
282         */
283        public int idPicked(World par1World, int par2, int par3, int par4)
284        {
285            return this.fruitType == Block.pumpkin ? Item.pumpkinSeeds.shiftedIndex : (this.fruitType == Block.melon ? Item.melonSeeds.shiftedIndex : 0);
286        }
287    }