AvidPenguin

BlockStem.java

Jul 24th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1. package avidpenguin.bettercraft.block;
  2.  
  3. import avidpenguin.bettercraft.BetterCraft;
  4. import cpw.mods.fml.relauncher.Side;
  5. import cpw.mods.fml.relauncher.SideOnly;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9.  
  10. import net.minecraft.block.Block;
  11. import net.minecraft.block.BlockFlower;
  12. import net.minecraft.client.renderer.texture.IconRegister;
  13. import net.minecraft.creativetab.CreativeTabs;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.util.Icon;
  17. import net.minecraft.util.MathHelper;
  18. import net.minecraft.world.IBlockAccess;
  19. import net.minecraft.world.World;
  20. import net.minecraftforge.common.ForgeDirection;
  21.  
  22. public class BlockStem extends BlockFlower
  23. {
  24. /** Defines if it is a Melon or a Pumpkin that the stem is producing. */
  25. private final Block fruitType;
  26. @SideOnly(Side.CLIENT)
  27. private Icon theIcon;
  28.  
  29. public BlockStem(int par1, Block par2Block)
  30. {
  31. super(par1);
  32. this.fruitType = par2Block;
  33. this.setTickRandomly(true);
  34. float f = 0.125F;
  35. this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
  36. this.setCreativeTab((CreativeTabs)null);
  37. }
  38.  
  39. /**
  40. * Gets passed in the blockID of the block below and supposed to return true if its allowed to grow on the type of
  41. * blockID passed in. Args: blockID
  42. */
  43. protected boolean canThisPlantGrowOnThisBlockID(int par1)
  44. {
  45. return par1 == Block.tilledField.blockID;
  46. }
  47.  
  48. /**
  49. * Ticks the block if it's been scheduled
  50. */
  51. public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
  52. {
  53. super.updateTick(par1World, par2, par3, par4, par5Random);
  54.  
  55. if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9)
  56. {
  57. float f = this.getGrowthModifier(par1World, par2, par3, par4);
  58.  
  59. if (par5Random.nextInt((int)(25.0F / f) + 1) == 0)
  60. {
  61. int l = par1World.getBlockMetadata(par2, par3, par4);
  62.  
  63. if (l < 7)
  64. {
  65. ++l;
  66. par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
  67. }
  68. else
  69. {
  70. if (par1World.getBlockId(par2 - 1, par3, par4) == this.fruitType.blockID)
  71. {
  72. return;
  73. }
  74.  
  75. if (par1World.getBlockId(par2 + 1, par3, par4) == this.fruitType.blockID)
  76. {
  77. return;
  78. }
  79.  
  80. if (par1World.getBlockId(par2, par3, par4 - 1) == this.fruitType.blockID)
  81. {
  82. return;
  83. }
  84.  
  85. if (par1World.getBlockId(par2, par3, par4 + 1) == this.fruitType.blockID)
  86. {
  87. return;
  88. }
  89.  
  90. int i1 = par5Random.nextInt(4);
  91. int j1 = par2;
  92. int k1 = par4;
  93.  
  94. if (i1 == 0)
  95. {
  96. j1 = par2 - 1;
  97. }
  98.  
  99. if (i1 == 1)
  100. {
  101. ++j1;
  102. }
  103.  
  104. if (i1 == 2)
  105. {
  106. k1 = par4 - 1;
  107. }
  108.  
  109. if (i1 == 3)
  110. {
  111. ++k1;
  112. }
  113.  
  114. int l1 = par1World.getBlockId(j1, par3 - 1, k1);
  115.  
  116. boolean isSoil = (blocksList[l1] != null && blocksList[l1].canSustainPlant(par1World, j1, par3 - 1, k1, ForgeDirection.UP, this));
  117. if (par1World.isAirBlock(j1, par3, k1) && (isSoil || l1 == Block.dirt.blockID || l1 == Block.grass.blockID))
  118. {
  119. par1World.setBlock(j1, par3, k1, this.fruitType.blockID);
  120. }
  121. }
  122. }
  123. }
  124. }
  125.  
  126. public void fertilizeStem(World par1World, int par2, int par3, int par4)
  127. {
  128. int l = par1World.getBlockMetadata(par2, par3, par4) + MathHelper.getRandomIntegerInRange(par1World.rand, 2, 5);
  129.  
  130. if (l > 7)
  131. {
  132. l = 7;
  133. }
  134.  
  135. par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
  136. }
  137.  
  138. private float getGrowthModifier(World par1World, int par2, int par3, int par4)
  139. {
  140. float f = 1.0F;
  141. int l = par1World.getBlockId(par2, par3, par4 - 1);
  142. int i1 = par1World.getBlockId(par2, par3, par4 + 1);
  143. int j1 = par1World.getBlockId(par2 - 1, par3, par4);
  144. int k1 = par1World.getBlockId(par2 + 1, par3, par4);
  145. int l1 = par1World.getBlockId(par2 - 1, par3, par4 - 1);
  146. int i2 = par1World.getBlockId(par2 + 1, par3, par4 - 1);
  147. int j2 = par1World.getBlockId(par2 + 1, par3, par4 + 1);
  148. int k2 = par1World.getBlockId(par2 - 1, par3, par4 + 1);
  149. boolean flag = j1 == this.blockID || k1 == this.blockID;
  150. boolean flag1 = l == this.blockID || i1 == this.blockID;
  151. boolean flag2 = l1 == this.blockID || i2 == this.blockID || j2 == this.blockID || k2 == this.blockID;
  152.  
  153. for (int l2 = par2 - 1; l2 <= par2 + 1; ++l2)
  154. {
  155. for (int i3 = par4 - 1; i3 <= par4 + 1; ++i3)
  156. {
  157. int j3 = par1World.getBlockId(l2, par3 - 1, i3);
  158. float f1 = 0.0F;
  159.  
  160. if (blocksList[j3] != null && blocksList[j3].canSustainPlant(par1World, l2, par3 - 1, i3, ForgeDirection.UP, this))
  161. {
  162. f1 = 1.0F;
  163.  
  164. if (blocksList[j3].isFertile(par1World, l2, par3 - 1, i3))
  165. {
  166. f1 = 3.0F;
  167. }
  168. }
  169.  
  170. if (l2 != par2 || i3 != par4)
  171. {
  172. f1 /= 4.0F;
  173. }
  174.  
  175. f += f1;
  176. }
  177. }
  178.  
  179. if (flag2 || flag && flag1)
  180. {
  181. f /= 2.0F;
  182. }
  183.  
  184. return f;
  185. }
  186.  
  187. @SideOnly(Side.CLIENT)
  188.  
  189. /**
  190. * Returns the color this block should be rendered. Used by leaves.
  191. */
  192. public int getRenderColor(int par1)
  193. {
  194. int j = par1 * 32;
  195. int k = 255 - par1 * 8;
  196. int l = par1 * 4;
  197. return j << 16 | k << 8 | l;
  198. }
  199.  
  200. @SideOnly(Side.CLIENT)
  201.  
  202. /**
  203. * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
  204. * when first determining what to render.
  205. */
  206. public int colorMultiplier(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
  207. {
  208. return this.getRenderColor(par1IBlockAccess.getBlockMetadata(par2, par3, par4));
  209. }
  210.  
  211. /**
  212. * Sets the block's bounds for rendering it as an item
  213. */
  214. public void setBlockBoundsForItemRender()
  215. {
  216. float f = 0.125F;
  217. this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
  218. }
  219.  
  220. /**
  221. * Updates the blocks bounds based on its current state. Args: world, x, y, z
  222. */
  223. public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
  224. {
  225. this.maxY = (double)((float)(par1IBlockAccess.getBlockMetadata(par2, par3, par4) * 2 + 2) / 16.0F);
  226. float f = 0.125F;
  227. this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, (float)this.maxY, 0.5F + f);
  228. }
  229.  
  230. /**
  231. * The type of render function that is called for this block
  232. */
  233. public int getRenderType()
  234. {
  235. return 19;
  236. }
  237.  
  238. @SideOnly(Side.CLIENT)
  239.  
  240. /**
  241. * Returns the current state of the stem. Returns -1 if the stem is not fully grown, or a value between 0 and 3
  242. * based on the direction the stem is facing.
  243. */
  244. public int getState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
  245. {
  246. int l = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
  247. return l < 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))));
  248. }
  249.  
  250. /**
  251. * Drops the block items with a specified chance of dropping the specified items
  252. */
  253. public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
  254. {
  255. super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, par7);
  256. }
  257.  
  258. @Override
  259. public ArrayList<ItemStack> getBlockDropped(World world, int x, int y, int z, int metadata, int fortune)
  260. {
  261. ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
  262.  
  263. for (int i = 0; i < 3; i++)
  264. {
  265. if (world.rand.nextInt(15) <= metadata)
  266. {
  267. ret.add(new ItemStack(BetterCraft.tomatoseeds));
  268. }
  269. }
  270.  
  271. return ret;
  272. }
  273.  
  274. /**
  275. * Returns the ID of the items to drop on destruction.
  276. */
  277. public int idDropped(int par1, Random par2Random, int par3)
  278. {
  279. return -1;
  280. }
  281.  
  282. /**
  283. * Returns the quantity of items to drop on block destruction.
  284. */
  285. public int quantityDropped(Random par1Random)
  286. {
  287. return 1;
  288. }
  289.  
  290. @SideOnly(Side.CLIENT)
  291.  
  292. /**
  293. * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
  294. */
  295. public int idPicked(World par1World, int par2, int par3, int par4)
  296. {
  297. return BetterCraft.tomatoseeds.itemID;
  298. }
  299.  
  300. @SideOnly(Side.CLIENT)
  301.  
  302. /**
  303. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  304. * is the only chance you get to register icons.
  305. */
  306. public void registerIcons(IconRegister par1IconRegister)
  307. {
  308. this.blockIcon = par1IconRegister.registerIcon(this.getTextureName() + "_disconnected");
  309. this.theIcon = par1IconRegister.registerIcon(this.getTextureName() + "_connected");
  310. }
  311.  
  312. @SideOnly(Side.CLIENT)
  313. public Icon getStemIcon()
  314. {
  315. return this.theIcon;
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment