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    public class BlockCrops extends BlockFlower
010    {
011        protected BlockCrops(int par1, int par2)
012        {
013            super(par1, par2);
014            this.blockIndexInTexture = par2;
015            this.setTickRandomly(true);
016            float var3 = 0.5F;
017            this.setBlockBounds(0.5F - var3, 0.0F, 0.5F - var3, 0.5F + var3, 0.25F, 0.5F + var3);
018            this.setCreativeTab((CreativeTabs)null);
019        }
020    
021        /**
022         * Gets passed in the blockID of the block below and supposed to return true if its allowed to grow on the type of
023         * blockID passed in. Args: blockID
024         */
025        protected boolean canThisPlantGrowOnThisBlockID(int par1)
026        {
027            return par1 == Block.tilledField.blockID;
028        }
029    
030        /**
031         * Ticks the block if it's been scheduled
032         */
033        public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
034        {
035            super.updateTick(par1World, par2, par3, par4, par5Random);
036    
037            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9)
038            {
039                int var6 = par1World.getBlockMetadata(par2, par3, par4);
040    
041                if (var6 < 7)
042                {
043                    float var7 = this.getGrowthRate(par1World, par2, par3, par4);
044    
045                    if (par5Random.nextInt((int)(25.0F / var7) + 1) == 0)
046                    {
047                        ++var6;
048                        par1World.setBlockMetadataWithNotify(par2, par3, par4, var6);
049                    }
050                }
051            }
052        }
053    
054        /**
055         * Apply bonemeal to the crops.
056         */
057        public void fertilize(World par1World, int par2, int par3, int par4)
058        {
059            par1World.setBlockMetadataWithNotify(par2, par3, par4, 7);
060        }
061    
062        /**
063         * Gets the growth rate for the crop. Setup to encourage rows by halving growth rate if there is diagonals, crops on
064         * different sides that aren't opposing, and by adding growth for every crop next to this one (and for crop below
065         * this one). Args: x, y, z
066         */
067        private float getGrowthRate(World par1World, int par2, int par3, int par4)
068        {
069            float var5 = 1.0F;
070            int var6 = par1World.getBlockId(par2, par3, par4 - 1);
071            int var7 = par1World.getBlockId(par2, par3, par4 + 1);
072            int var8 = par1World.getBlockId(par2 - 1, par3, par4);
073            int var9 = par1World.getBlockId(par2 + 1, par3, par4);
074            int var10 = par1World.getBlockId(par2 - 1, par3, par4 - 1);
075            int var11 = par1World.getBlockId(par2 + 1, par3, par4 - 1);
076            int var12 = par1World.getBlockId(par2 + 1, par3, par4 + 1);
077            int var13 = par1World.getBlockId(par2 - 1, par3, par4 + 1);
078            boolean var14 = var8 == this.blockID || var9 == this.blockID;
079            boolean var15 = var6 == this.blockID || var7 == this.blockID;
080            boolean var16 = var10 == this.blockID || var11 == this.blockID || var12 == this.blockID || var13 == this.blockID;
081    
082            for (int var17 = par2 - 1; var17 <= par2 + 1; ++var17)
083            {
084                for (int var18 = par4 - 1; var18 <= par4 + 1; ++var18)
085                {
086                    int var19 = par1World.getBlockId(var17, par3 - 1, var18);
087                    float var20 = 0.0F;
088    
089                    if (var19 == Block.tilledField.blockID)
090                    {
091                        var20 = 1.0F;
092    
093                        if (par1World.getBlockMetadata(var17, par3 - 1, var18) > 0)
094                        {
095                            var20 = 3.0F;
096                        }
097                    }
098    
099                    if (var17 != par2 || var18 != par4)
100                    {
101                        var20 /= 4.0F;
102                    }
103    
104                    var5 += var20;
105                }
106            }
107    
108            if (var16 || var14 && var15)
109            {
110                var5 /= 2.0F;
111            }
112    
113            return var5;
114        }
115    
116        /**
117         * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
118         */
119        public int getBlockTextureFromSideAndMetadata(int par1, int par2)
120        {
121            if (par2 < 0)
122            {
123                par2 = 7;
124            }
125    
126            return this.blockIndexInTexture + par2;
127        }
128    
129        /**
130         * The type of render function that is called for this block
131         */
132        public int getRenderType()
133        {
134            return 6;
135        }
136    
137        /**
138         * Drops the block items with a specified chance of dropping the specified items
139         */
140        public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
141        {
142            super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, 0);
143        }
144            
145        @Override 
146        public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune)
147        {
148            ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
149            if (metadata == 7)
150            {
151                ret.add(new ItemStack(Item.wheat));
152            }
153    
154            for (int n = 0; n < 3 + fortune; n++)
155            {
156    
157                if (world.rand.nextInt(15) <= metadata)
158                {
159                    ret.add(new ItemStack(Item.seeds));
160                }
161            }
162    
163            return ret;
164        }
165    
166        /**
167         * Returns the ID of the items to drop on destruction.
168         */
169        public int idDropped(int par1, Random par2Random, int par3)
170        {
171            return par1 == 7 ? Item.wheat.shiftedIndex : -1;
172        }
173    
174        /**
175         * Returns the quantity of items to drop on block destruction.
176         */
177        public int quantityDropped(Random par1Random)
178        {
179            return 1;
180        }
181    
182        @SideOnly(Side.CLIENT)
183    
184        /**
185         * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
186         */
187        public int idPicked(World par1World, int par2, int par3, int par4)
188        {
189            return Item.seeds.shiftedIndex;
190        }
191    }