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