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