    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        /**
    144         * Generate a seed ItemStack for this crop.
    145         */
    146        protected int getSeedItem()
    147        {
    148            return Item.seeds.shiftedIndex;
    149        }
    150
    151        /**
    152         * Generate a crop produce ItemStack for this crop.
    153         */
    154        protected int getCropItem()
    155        {
    156            return Item.wheat.shiftedIndex;
    157        }
    158
    159        /**
    160         * Drops the block items with a specified chance of dropping the specified items
    161         */
    162        public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
    163        {
    164            super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, 0);
    165        }
    166
    167        @Override
    168        public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune)
    169        {
    170            ArrayList<ItemStack> ret = super.getBlockDropped(world, x, y, z, metadata, fortune);
    171
    172            if (metadata >= 7)
    173            {
    174                for (int n = 0; n < 3 + fortune; n++)
    175                {
    176                    if (world.rand.nextInt(15) <= metadata)
    177                    {
    178                        ret.add(new ItemStack(this.getSeedItem(), 1, 0));
    179                    }
    180                }
    181            }
    182
    183            return ret;
    184        }
    185
    186        /**
    187         * Returns the ID of the items to drop on destruction.
    188         */
    189        public int idDropped(int par1, Random par2Random, int par3)
    190        {
    191            return par1 == 7 ? this.getCropItem() : this.getSeedItem();
    192        }
    193
    194        /**
    195         * Returns the quantity of items to drop on block destruction.
    196         */
    197        public int quantityDropped(Random par1Random)
    198        {
    199            return 1;
    200        }
    201
    202        @SideOnly(Side.CLIENT)
    203
    204        /**
    205         * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
    206         */
    207        public int idPicked(World par1World, int par2, int par3, int par4)
    208        {
    209            return this.getSeedItem();
    210        }
    211    }