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.IBlockAccess; 012 import net.minecraft.world.World; 013 014 import net.minecraftforge.common.ForgeDirection; 015 016 public class BlockStem extends BlockFlower 017 { 018 /** Defines if it is a Melon or a Pumpkin that the stem is producing. */ 019 private Block fruitType; 020 021 protected BlockStem(int par1, Block par2Block) 022 { 023 super(par1, 111); 024 this.fruitType = par2Block; 025 this.setTickRandomly(true); 026 float var3 = 0.125F; 027 this.setBlockBounds(0.5F - var3, 0.0F, 0.5F - var3, 0.5F + var3, 0.25F, 0.5F + var3); 028 this.setCreativeTab((CreativeTabs)null); 029 } 030 031 /** 032 * Gets passed in the blockID of the block below and supposed to return true if its allowed to grow on the type of 033 * blockID passed in. Args: blockID 034 */ 035 protected boolean canThisPlantGrowOnThisBlockID(int par1) 036 { 037 return par1 == Block.tilledField.blockID; 038 } 039 040 /** 041 * Ticks the block if it's been scheduled 042 */ 043 public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random) 044 { 045 super.updateTick(par1World, par2, par3, par4, par5Random); 046 047 if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9) 048 { 049 float var6 = this.getGrowthModifier(par1World, par2, par3, par4); 050 051 if (par5Random.nextInt((int)(25.0F / var6) + 1) == 0) 052 { 053 int var7 = par1World.getBlockMetadata(par2, par3, par4); 054 055 if (var7 < 7) 056 { 057 ++var7; 058 par1World.setBlockMetadataWithNotify(par2, par3, par4, var7); 059 } 060 else 061 { 062 if (par1World.getBlockId(par2 - 1, par3, par4) == this.fruitType.blockID) 063 { 064 return; 065 } 066 067 if (par1World.getBlockId(par2 + 1, par3, par4) == this.fruitType.blockID) 068 { 069 return; 070 } 071 072 if (par1World.getBlockId(par2, par3, par4 - 1) == this.fruitType.blockID) 073 { 074 return; 075 } 076 077 if (par1World.getBlockId(par2, par3, par4 + 1) == this.fruitType.blockID) 078 { 079 return; 080 } 081 082 int var8 = par5Random.nextInt(4); 083 int var9 = par2; 084 int var10 = par4; 085 086 if (var8 == 0) 087 { 088 var9 = par2 - 1; 089 } 090 091 if (var8 == 1) 092 { 093 ++var9; 094 } 095 096 if (var8 == 2) 097 { 098 var10 = par4 - 1; 099 } 100 101 if (var8 == 3) 102 { 103 ++var10; 104 } 105 106 int var11 = par1World.getBlockId(var9, par3 - 1, var10); 107 108 boolean isSoil = (blocksList[var11] != null && blocksList[var11].canSustainPlant(par1World, var9, par3 - 1, var10, ForgeDirection.UP, this)); 109 if (par1World.getBlockId(var9, par3, var10) == 0 && (isSoil || var11 == Block.dirt.blockID || var11 == Block.grass.blockID)) 110 { 111 par1World.setBlockWithNotify(var9, par3, var10, this.fruitType.blockID); 112 } 113 } 114 } 115 } 116 } 117 118 public void fertilizeStem(World par1World, int par2, int par3, int par4) 119 { 120 par1World.setBlockMetadataWithNotify(par2, par3, par4, 7); 121 } 122 123 private float getGrowthModifier(World par1World, int par2, int par3, int par4) 124 { 125 float var5 = 1.0F; 126 int var6 = par1World.getBlockId(par2, par3, par4 - 1); 127 int var7 = par1World.getBlockId(par2, par3, par4 + 1); 128 int var8 = par1World.getBlockId(par2 - 1, par3, par4); 129 int var9 = par1World.getBlockId(par2 + 1, par3, par4); 130 int var10 = par1World.getBlockId(par2 - 1, par3, par4 - 1); 131 int var11 = par1World.getBlockId(par2 + 1, par3, par4 - 1); 132 int var12 = par1World.getBlockId(par2 + 1, par3, par4 + 1); 133 int var13 = par1World.getBlockId(par2 - 1, par3, par4 + 1); 134 boolean var14 = var8 == this.blockID || var9 == this.blockID; 135 boolean var15 = var6 == this.blockID || var7 == this.blockID; 136 boolean var16 = var10 == this.blockID || var11 == this.blockID || var12 == this.blockID || var13 == this.blockID; 137 138 for (int var17 = par2 - 1; var17 <= par2 + 1; ++var17) 139 { 140 for (int var18 = par4 - 1; var18 <= par4 + 1; ++var18) 141 { 142 int var19 = par1World.getBlockId(var17, par3 - 1, var18); 143 float var20 = 0.0F; 144 145 if (blocksList[var19] != null && blocksList[var19].canSustainPlant(par1World, var17, par3 - 1, var18, ForgeDirection.UP, this)) 146 { 147 var20 = 1.0F; 148 149 if (blocksList[var19].isFertile(par1World, var17, par3 - 1, var18)) 150 { 151 var20 = 3.0F; 152 } 153 } 154 155 if (var17 != par2 || var18 != par4) 156 { 157 var20 /= 4.0F; 158 } 159 160 var5 += var20; 161 } 162 } 163 164 if (var16 || var14 && var15) 165 { 166 var5 /= 2.0F; 167 } 168 169 return var5; 170 } 171 172 @SideOnly(Side.CLIENT) 173 174 /** 175 * Returns the color this block should be rendered. Used by leaves. 176 */ 177 public int getRenderColor(int par1) 178 { 179 int var2 = par1 * 32; 180 int var3 = 255 - par1 * 8; 181 int var4 = par1 * 4; 182 return var2 << 16 | var3 << 8 | var4; 183 } 184 185 @SideOnly(Side.CLIENT) 186 187 /** 188 * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called 189 * when first determining what to render. 190 */ 191 public int colorMultiplier(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 192 { 193 return this.getRenderColor(par1IBlockAccess.getBlockMetadata(par2, par3, par4)); 194 } 195 196 /** 197 * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata 198 */ 199 public int getBlockTextureFromSideAndMetadata(int par1, int par2) 200 { 201 return this.blockIndexInTexture; 202 } 203 204 /** 205 * Sets the block's bounds for rendering it as an item 206 */ 207 public void setBlockBoundsForItemRender() 208 { 209 float var1 = 0.125F; 210 this.setBlockBounds(0.5F - var1, 0.0F, 0.5F - var1, 0.5F + var1, 0.25F, 0.5F + var1); 211 } 212 213 /** 214 * Updates the blocks bounds based on its current state. Args: world, x, y, z 215 */ 216 public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 217 { 218 this.maxY = (double)((float)(par1IBlockAccess.getBlockMetadata(par2, par3, par4) * 2 + 2) / 16.0F); 219 float var5 = 0.125F; 220 this.setBlockBounds(0.5F - var5, 0.0F, 0.5F - var5, 0.5F + var5, (float)this.maxY, 0.5F + var5); 221 } 222 223 /** 224 * The type of render function that is called for this block 225 */ 226 public int getRenderType() 227 { 228 return 19; 229 } 230 231 @SideOnly(Side.CLIENT) 232 233 /** 234 * Returns the current state of the stem. Returns -1 if the stem is not fully grown, or a value between 0 and 3 235 * based on the direction the stem is facing. 236 */ 237 public int getState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) 238 { 239 int var5 = par1IBlockAccess.getBlockMetadata(par2, par3, par4); 240 return var5 < 7 ? -1 : (par1IBlockAccess.getBlockId(par2 - 1, par3, par4) == this.fruitType.blockID ? 0 : (par1IBlockAccess.getBlockId(par2 + 1, par3, par4) == this.fruitType.blockID ? 1 : (par1IBlockAccess.getBlockId(par2, par3, par4 - 1) == this.fruitType.blockID ? 2 : (par1IBlockAccess.getBlockId(par2, par3, par4 + 1) == this.fruitType.blockID ? 3 : -1)))); 241 } 242 243 /** 244 * Drops the block items with a specified chance of dropping the specified items 245 */ 246 public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7) 247 { 248 super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, par7); 249 } 250 251 @Override 252 public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune) 253 { 254 ArrayList<ItemStack> ret = new ArrayList<ItemStack>(); 255 256 for (int i = 0; i < 3; i++) 257 { 258 if (world.rand.nextInt(15) <= metadata) 259 { 260 ret.add(new ItemStack(fruitType == pumpkin ? Item.pumpkinSeeds : Item.melonSeeds)); 261 } 262 } 263 264 return ret; 265 } 266 267 /** 268 * Returns the ID of the items to drop on destruction. 269 */ 270 public int idDropped(int par1, Random par2Random, int par3) 271 { 272 return -1; 273 } 274 275 /** 276 * Returns the quantity of items to drop on block destruction. 277 */ 278 public int quantityDropped(Random par1Random) 279 { 280 return 1; 281 } 282 283 @SideOnly(Side.CLIENT) 284 285 /** 286 * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative) 287 */ 288 public int idPicked(World par1World, int par2, int par3, int par4) 289 { 290 return this.fruitType == Block.pumpkin ? Item.pumpkinSeeds.itemID : (this.fruitType == Block.melon ? Item.melonSeeds.itemID : 0); 291 } 292 }