Guest User

Untitled

a guest
Sep 23rd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. package Futurology.Machines;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockContainer;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.client.renderer.texture.IconRegister;
  9. import net.minecraft.entity.EntityLiving;
  10. import net.minecraft.entity.item.EntityItem;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.inventory.Container;
  13. import net.minecraft.inventory.IInventory;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.util.Icon;
  18. import net.minecraft.util.MathHelper;
  19. import net.minecraft.world.World;
  20. import Futurology.FuturologyCore;
  21.  
  22. public class BlockForge extends BlockContainer
  23. {
  24.  
  25. /**
  26. * Is the random generator used by forge to drop the inventory contents in random directions.
  27. */
  28. private final Random forgeRand = new Random();
  29.  
  30. /** True if this is an active forge, false if idle */
  31. private final boolean isActive;
  32.  
  33.  
  34.  
  35.  
  36. /**
  37. * This flag is used to prevent the forge inventory to be dropped upon block removal, is used internally when the
  38. * forge block changes from idle to active and vice-versa.
  39. */
  40. private static boolean keepForgeInventory = false;
  41.  
  42. private Icon topIcon;
  43.  
  44. private Icon frontIcon;
  45.  
  46. public BlockForge(int par1, int i, Material rock, boolean par2)
  47. {
  48. super(par1, Material.rock);
  49. this.isActive = par2;
  50. }
  51.  
  52. /**
  53. * Returns the ID of the items to drop on destruction.
  54. */
  55. public int idDropped(int par1, Random par2Random, int par3)
  56. {
  57. return FuturologyCore.tungstenForge.blockID;
  58. }
  59.  
  60. /**
  61. * Called whenever the block is added into the world. Args: world, x, y, z
  62. */
  63. public void onBlockAdded(World par1World, int par2, int par3, int par4)
  64. {
  65. super.onBlockAdded(par1World, par2, par3, par4);
  66. this.setDefaultDirection(par1World, par2, par3, par4);
  67. }
  68.  
  69. /**
  70. * set a blocks direction
  71. */
  72. private void setDefaultDirection(World par1World, int par2, int par3, int par4)
  73. {
  74. if (!par1World.isRemote)
  75. {
  76. int l = par1World.getBlockId(par2, par3, par4 - 1);
  77. int i1 = par1World.getBlockId(par2, par3, par4 + 1);
  78. int j1 = par1World.getBlockId(par2 - 1, par3, par4);
  79. int k1 = par1World.getBlockId(par2 + 1, par3, par4);
  80. byte b0 = 3;
  81.  
  82. if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
  83. {
  84. b0 = 3;
  85. }
  86.  
  87. if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
  88. {
  89. b0 = 2;
  90. }
  91.  
  92. if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
  93. {
  94. b0 = 5;
  95. }
  96.  
  97. if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
  98. {
  99. b0 = 4;
  100. }
  101.  
  102. par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
  103. }
  104. }
  105.  
  106.  
  107.  
  108. /**
  109. * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
  110. */
  111. public Icon getIcon(int par1, int par2)
  112. {
  113. return par1 == 1 ? this.topIcon : (par1 == 0 ? this.topIcon : (par1 != par2 ? this.blockIcon : this.frontIcon));
  114. }
  115.  
  116.  
  117.  
  118. /**
  119. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  120. * is the only chance you get to register icons.
  121. */
  122. public void registerIcons(IconRegister par1IconRegister)
  123. {
  124. this.blockIcon = par1IconRegister.registerIcon("Futurology:Icon");
  125. this.frontIcon = par1IconRegister.registerIcon(this.isActive ? "Futurology:FrontIcon" : "Futurology:frontIconActive");
  126. this.topIcon = par1IconRegister.registerIcon("Futurology:Icon");
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /**
  134. * Called upon block activation (right click on the block.)
  135. */
  136. @Override
  137. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int i, float f, float g, float t) {
  138. TileEntity tile_entity = world.getBlockTileEntity(x, y, z);
  139.  
  140. if (tile_entity == null || player.isSneaking()) {
  141.  
  142. return false;
  143. }
  144.  
  145. player.openGui(FuturologyCore.instance, 0, world, x, y, z);
  146.  
  147. return true;
  148. }
  149.  
  150.  
  151. /**
  152. * Update which block ID the forge is using depending on whether or not it is burning
  153. */
  154. public static void updateForgeBlockState(boolean par0, World par1World, int par2, int par3, int par4)
  155. {
  156. int l = par1World.getBlockMetadata(par2, par3, par4);
  157. TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
  158. keepForgeInventory = true;
  159.  
  160. if (par0)
  161. {
  162. par1World.setBlock(par2, par3, par4, FuturologyCore.tungstenForge.blockID);
  163. }
  164. else
  165. {
  166. par1World.setBlock(par2, par3, par4, FuturologyCore.tungstenForgeActive.blockID);
  167. }
  168.  
  169. keepForgeInventory = false;
  170. par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
  171.  
  172. if (tileentity != null)
  173. {
  174. tileentity.validate();
  175. par1World.setBlockTileEntity(par2, par3, par4, tileentity);
  176. }
  177. }
  178.  
  179.  
  180.  
  181. /**
  182. * A randomly called display update to be able to add particles or other items for display
  183. */
  184. public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
  185. {
  186. if (!isActive)
  187. {
  188. int l = par1World.getBlockMetadata(par2, par3, par4);
  189. float f = (float)par2 + 0.5F;
  190. float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
  191. float f2 = (float)par4 + 0.5F;
  192. float f3 = 0.52F;
  193. float f4 = par5Random.nextFloat() * 0.6F - 0.3F;
  194.  
  195. if (l == 4)
  196. {
  197. par1World.spawnParticle("smoke", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  198. par1World.spawnParticle("flame", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  199. }
  200. else if (l == 5)
  201. {
  202. par1World.spawnParticle("smoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  203. par1World.spawnParticle("flame", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  204. }
  205. else if (l == 2)
  206. {
  207. par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
  208. par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
  209. }
  210. else if (l == 3)
  211. {
  212. par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
  213. par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
  214. }
  215. }
  216. }
  217.  
  218. /**
  219. * Returns a new instance of a block's tile entity class. Called on placing the block.
  220. */
  221. public TileEntity createNewTileEntity(World par1World)
  222. {
  223. return new TileEntityForge();
  224. }
  225.  
  226. /**
  227. * Called when the block is placed in the world.
  228. */
  229. public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
  230. {
  231. int l = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  232.  
  233. if (l == 0)
  234. {
  235. par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
  236. }
  237.  
  238. if (l == 1)
  239. {
  240. par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
  241. }
  242.  
  243. if (l == 2)
  244. {
  245. par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
  246. }
  247.  
  248. if (l == 3)
  249. {
  250. par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
  251. }
  252.  
  253. if (par6ItemStack.hasDisplayName())
  254. {
  255. ((TileEntityForge)par1World.getBlockTileEntity(par2, par3, par4)).method94129(par6ItemStack.getDisplayName());
  256. }
  257. }
  258.  
  259. /**
  260. * ejects contained items into the world, and notifies neighbours of an update, as appropriate
  261. */
  262. public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
  263. {
  264. if (!keepForgeInventory)
  265. {
  266. TileEntityForge TileEntityForge = (TileEntityForge)par1World.getBlockTileEntity(par2, par3, par4);
  267.  
  268. if (TileEntityForge != null)
  269. {
  270. for (int j1 = 0; j1 < TileEntityForge.getSizeInventory(); ++j1)
  271. {
  272. ItemStack itemstack = TileEntityForge.getStackInSlot(j1);
  273.  
  274. if (itemstack != null)
  275. {
  276. float f = this.forgeRand.nextFloat() * 0.8F + 0.1F;
  277. float f1 = this.forgeRand.nextFloat() * 0.8F + 0.1F;
  278. float f2 = this.forgeRand.nextFloat() * 0.8F + 0.1F;
  279.  
  280. while (itemstack.stackSize > 0)
  281. {
  282. int k1 = this.forgeRand.nextInt(21) + 10;
  283.  
  284. if (k1 > itemstack.stackSize)
  285. {
  286. k1 = itemstack.stackSize;
  287. }
  288.  
  289. itemstack.stackSize -= k1;
  290. EntityItem entityitem = new EntityItem(par1World, (double)((float)par2 + f), (double)((float)par3 + f1), (double)((float)par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));
  291.  
  292. if (itemstack.hasTagCompound())
  293. {
  294. entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  295. }
  296.  
  297. float f3 = 0.05F;
  298. entityitem.motionX = (double)((float)this.forgeRand.nextGaussian() * f3);
  299. entityitem.motionY = (double)((float)this.forgeRand.nextGaussian() * f3 + 0.2F);
  300. entityitem.motionZ = (double)((float)this.forgeRand.nextGaussian() * f3);
  301. par1World.spawnEntityInWorld(entityitem);
  302. }
  303. }
  304. }
  305.  
  306. par1World.func_96440_m(par2, par3, par4, par5);
  307. }
  308. }
  309.  
  310. super.breakBlock(par1World, par2, par3, par4, par5, par6);
  311. }
  312.  
  313. /**
  314. * If this returns true, then comparators facing away from this block will use the value from
  315. * getComparatorInputOverride instead of the actual redstone signal strength.
  316. */
  317. public boolean hasComparatorInputOverride()
  318. {
  319. return true;
  320. }
  321.  
  322. /**
  323. * If hasComparatorInputOverride returns true, the return value from this is used instead of the redstone signal
  324. * strength when this block inputs to a comparator.
  325. */
  326. public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5)
  327. {
  328. return Container.calcRedstoneFromInventory((IInventory)par1World.getBlockTileEntity(par2, par3, par4));
  329. }
  330.  
  331.  
  332.  
  333. /**
  334. * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
  335. */
  336. public int idPicked(World par1World, int par2, int par3, int par4)
  337. {
  338. return FuturologyCore.tungstenForge.blockID;
  339. }
  340.  
  341.  
  342.  
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment