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