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