Guest User

blockIronWorkBench

a guest
Jul 4th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.54 KB | None | 0 0
  1. package simcraft.machines.workshop;
  2.  
  3. import static net.minecraftforge.common.ForgeDirection.DOWN;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Random;
  9.  
  10. import cpw.mods.fml.relauncher.Side;
  11. import cpw.mods.fml.relauncher.SideOnly;
  12.  
  13. import net.minecraft.block.Block;
  14. import net.minecraft.block.BlockContainer;
  15. import net.minecraft.block.material.Material;
  16. import net.minecraft.client.renderer.texture.IconRegister;
  17. import net.minecraft.creativetab.CreativeTabs;
  18. import net.minecraft.entity.EntityLiving;
  19. import net.minecraft.entity.item.EntityItem;
  20. import net.minecraft.entity.player.EntityPlayer;
  21. import net.minecraft.inventory.IInventory;
  22. import net.minecraft.inventory.InventoryLargeChest;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.tileentity.TileEntity;
  26. import net.minecraft.tileentity.TileEntityChest;
  27. import net.minecraft.util.Icon;
  28. import net.minecraft.util.MathHelper;
  29. import net.minecraft.world.World;
  30.  
  31. import simcraft.core.RegBlocks;
  32. import simcraft.core.SimCraft;
  33.  
  34. public class blockIronWorkBench extends BlockContainer
  35. {
  36. @SideOnly(Side.CLIENT)
  37. private static Icon icon;
  38. private Class BlockIronWorkBenchClass;
  39. public Random random;
  40.  
  41.  
  42. public blockIronWorkBench(int var1, Class var2)
  43. {
  44. super(var1, Material.iron);
  45. this.BlockIronWorkBenchClass = var2;
  46. this.random = new Random();
  47. this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
  48. this.setHardness(2.0F);
  49. this.setResistance(5F);
  50. this.setUnlocalizedName("Iron Work Bench");
  51. this.setCreativeTab(RegBlocks.tabWorkShop);
  52. }
  53.  
  54. @SideOnly(Side.CLIENT)
  55. public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List) //Adds the metadata blocks to the creative inventory
  56. {
  57. for (int var4 = 0; var4 < 1; ++var4)
  58. {
  59. par3List.add(new ItemStack(par1, 1, var4));
  60. }
  61. }
  62.  
  63. public boolean isOpaqueCube()
  64. {
  65. return false;
  66. }
  67.  
  68. public boolean renderAsNormalBlock()
  69. {
  70. return false;
  71. }
  72.  
  73. public int getRenderType()
  74. {
  75. return SimCraft.BlockIronWorkBenchID;
  76. }
  77.  
  78. public int quantityDropped(Random random)
  79. {
  80. return 1;
  81. }
  82.  
  83. /**
  84. * Returns the TileEntity used by this block.
  85. */
  86. public TileEntity createNewTileEntity(World var1)
  87. {
  88. try
  89. {
  90. return (TileEntity)this.BlockIronWorkBenchClass.newInstance();
  91. }
  92. catch (Exception var2)
  93. {
  94. throw new RuntimeException(var2);
  95. }
  96. }
  97.  
  98. /**
  99. * Returns the ID of the items to drop on destruction.
  100. */
  101. public int idDropped(int var1, Random var2, int var3)
  102. {
  103. return RegBlocks.BlockIronWorkBench.blockID;
  104. }
  105.  
  106. /**
  107. * Called when the block is placed in the world.
  108. */
  109. public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
  110. {
  111. int var6 = par1World.getBlockId(par2, par3, par4 - 1);
  112. int var7 = par1World.getBlockId(par2, par3, par4 + 1);
  113. int var8 = par1World.getBlockId(par2 - 1, par3, par4);
  114. int var9 = par1World.getBlockId(par2 + 1, par3, par4);
  115. byte var10 = 0;
  116. int var11 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  117.  
  118. if (var11 == 0)
  119. {
  120. var10 = 2;
  121. }
  122.  
  123. if (var11 == 1)
  124. {
  125. var10 = 5;
  126. }
  127.  
  128. if (var11 == 2)
  129. {
  130. var10 = 3;
  131. }
  132.  
  133. if (var11 == 3)
  134. {
  135. var10 = 4;
  136. }
  137.  
  138. if (var6 != this.blockID && var7 != this.blockID && var8 != this.blockID && var9 != this.blockID)
  139. {
  140. par1World.setBlockMetadataWithNotify(par2, par3, par4, var10, 3);
  141. }
  142. else
  143. {
  144. if ((var6 == this.blockID || var7 == this.blockID) && (var10 == 4 || var10 == 5))
  145. {
  146. if (var6 == this.blockID)
  147. {
  148. par1World.setBlockMetadataWithNotify(par2, par3, par4 - 1, var10, 3);
  149. }
  150. else
  151. {
  152. par1World.setBlockMetadataWithNotify(par2, par3, par4 + 1, var10, 3);
  153. }
  154.  
  155. par1World.setBlockMetadataWithNotify(par2, par3, par4, var10, 3);
  156. }
  157.  
  158. if ((var8 == this.blockID || var9 == this.blockID) && (var10 == 2 || var10 == 3))
  159. {
  160. if (var8 == this.blockID)
  161. {
  162. par1World.setBlockMetadataWithNotify(par2 - 1, par3, par4, var10, 3);
  163. }
  164. else
  165. {
  166. par1World.setBlockMetadataWithNotify(par2 + 1, par3, par4, var10, 3);
  167. }
  168.  
  169. par1World.setBlockMetadataWithNotify(par2, par3, par4, var10, 3);
  170. }
  171. }
  172. }
  173.  
  174. /**
  175. * Called whenever the block is added into the world. Args: world, x, y, z
  176. */
  177. public void onBlockAdded(World par1World, int par2, int par3, int par4)
  178. {
  179. super.onBlockAdded(par1World, par2, par3, par4);
  180. this.unifyAdjacentModel(par1World, par2, par3, par4);
  181. int var5 = par1World.getBlockId(par2, par3, par4 - 1);
  182. int var6 = par1World.getBlockId(par2, par3, par4 + 1);
  183. int var7 = par1World.getBlockId(par2 - 1, par3, par4);
  184. int var8 = par1World.getBlockId(par2 + 1, par3, par4);
  185.  
  186. if (var5 == this.blockID)
  187. {
  188. this.unifyAdjacentModel(par1World, par2, par3, par4 - 1);
  189. }
  190.  
  191. if (var6 == this.blockID)
  192. {
  193. this.unifyAdjacentModel(par1World, par2, par3, par4 + 1);
  194. }
  195.  
  196. if (var7 == this.blockID)
  197. {
  198. this.unifyAdjacentModel(par1World, par2 - 1, par3, par4);
  199. }
  200.  
  201. if (var8 == this.blockID)
  202. {
  203. this.unifyAdjacentModel(par1World, par2 + 1, par3, par4);
  204. }
  205. }
  206.  
  207.  
  208. /**
  209. * Turns the adjacent chests to a double chest.
  210. */
  211. public void unifyAdjacentModel(World par1World, int par2, int par3, int par4)
  212. {
  213. if (!par1World.isRemote)
  214. {
  215. int var5 = par1World.getBlockId(par2, par3, par4 - 1);
  216. int var6 = par1World.getBlockId(par2, par3, par4 + 1);
  217. int var7 = par1World.getBlockId(par2 - 1, par3, par4);
  218. int var8 = par1World.getBlockId(par2 + 1, par3, par4);
  219. boolean var9 = true;
  220. int var10;
  221. int var11;
  222. boolean var12;
  223. byte var13;
  224. int var14;
  225.  
  226. if (var5 != this.blockID && var6 != this.blockID)
  227. {
  228. if (var7 != this.blockID && var8 != this.blockID)
  229. {
  230. var13 = 3;
  231.  
  232. if (Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var6])
  233. {
  234. var13 = 3;
  235. }
  236.  
  237. if (Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var5])
  238. {
  239. var13 = 2;
  240. }
  241.  
  242. if (Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var8])
  243. {
  244. var13 = 5;
  245. }
  246.  
  247. if (Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var7])
  248. {
  249. var13 = 4;
  250. }
  251. }
  252. else
  253. {
  254. var10 = par1World.getBlockId(var7 == this.blockID ? par2 - 1 : par2 + 1, par3, par4 - 1);
  255. var11 = par1World.getBlockId(var7 == this.blockID ? par2 - 1 : par2 + 1, par3, par4 + 1);
  256. var13 = 3;
  257. var12 = true;
  258.  
  259. if (var7 == this.blockID)
  260. {
  261. var14 = par1World.getBlockMetadata(par2 - 1, par3, par4);
  262. }
  263. else
  264. {
  265. var14 = par1World.getBlockMetadata(par2 + 1, par3, par4);
  266. }
  267.  
  268. if (var14 == 2)
  269. {
  270. var13 = 2;
  271. }
  272.  
  273. if ((Block.opaqueCubeLookup[var5] || Block.opaqueCubeLookup[var10]) && !Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var11])
  274. {
  275. var13 = 3;
  276. }
  277.  
  278. if ((Block.opaqueCubeLookup[var6] || Block.opaqueCubeLookup[var11]) && !Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var10])
  279. {
  280. var13 = 2;
  281. }
  282. }
  283. }
  284. else
  285. {
  286. var10 = par1World.getBlockId(par2 - 1, par3, var5 == this.blockID ? par4 - 1 : par4 + 1);
  287. var11 = par1World.getBlockId(par2 + 1, par3, var5 == this.blockID ? par4 - 1 : par4 + 1);
  288. var13 = 5;
  289. var12 = true;
  290.  
  291. if (var5 == this.blockID)
  292. {
  293. var14 = par1World.getBlockMetadata(par2, par3, par4 - 1);
  294. }
  295. else
  296. {
  297. var14 = par1World.getBlockMetadata(par2, par3, par4 + 1);
  298. }
  299.  
  300. if (var14 == 4)
  301. {
  302. var13 = 4;
  303. }
  304.  
  305. if ((Block.opaqueCubeLookup[var7] || Block.opaqueCubeLookup[var10]) && !Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var11])
  306. {
  307. var13 = 5;
  308. }
  309.  
  310. if ((Block.opaqueCubeLookup[var8] || Block.opaqueCubeLookup[var11]) && !Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var10])
  311. {
  312. var13 = 4;
  313. }
  314. }
  315.  
  316. par1World.setBlockMetadataWithNotify(par2, par3, par4, var13, 3);
  317. }
  318. }
  319.  
  320. /**
  321. * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
  322. */
  323. public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
  324. {
  325. int var5 = 0;
  326.  
  327. if (par1World.getBlockId(par2 - 1, par3, par4) == this.blockID)
  328. {
  329. ++var5;
  330. }
  331.  
  332. if (par1World.getBlockId(par2 + 1, par3, par4) == this.blockID)
  333. {
  334. ++var5;
  335. }
  336.  
  337. if (par1World.getBlockId(par2, par3, par4 - 1) == this.blockID)
  338. {
  339. ++var5;
  340. }
  341.  
  342. if (par1World.getBlockId(par2, par3, par4 + 1) == this.blockID)
  343. {
  344. ++var5;
  345. }
  346.  
  347. return var5 > 1 ? false : (this.isThereANeighborIronBench(par1World, par2 - 1, par3, par4) ? false : (this.isThereANeighborIronBench(par1World, par2 + 1, par3, par4) ? false : (this.isThereANeighborIronBench(par1World, par2, par3, par4 - 1) ? false : !this.isThereANeighborIronBench(par1World, par2, par3, par4 + 1))));
  348. }
  349.  
  350.  
  351. /**
  352. * Checks the neighbor blocks to see if there is a chest there. Args: world, x, y, z
  353. */
  354. private boolean isThereANeighborIronBench(World par1World, int par2, int par3, int par4)
  355. {
  356. return par1World.getBlockId(par2, par3, par4) != this.blockID ? false : (par1World.getBlockId(par2 - 1, par3, par4) == this.blockID ? true : (par1World.getBlockId(par2 + 1, par3, par4) == this.blockID ? true : (par1World.getBlockId(par2, par3, par4 - 1) == this.blockID ? true : par1World.getBlockId(par2, par3, par4 + 1) == this.blockID)));
  357. }
  358.  
  359. /**
  360. * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  361. * their own) Args: x, y, z, neighbor blockID
  362. */
  363. public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
  364. {
  365. super.onNeighborBlockChange(par1World, par2, par3, par4, par5);
  366. TileEntityIronWorkBench var6 = (TileEntityIronWorkBench)par1World.getBlockTileEntity(par2, par3, par4);
  367.  
  368. if (var6 != null)
  369. {
  370. var6.updateContainingBlockInfo();
  371. }
  372. }
  373.  
  374. /**
  375. * Called whenever the block is removed.
  376. */
  377. public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
  378. {
  379. TileEntityIronWorkBench var5 = (TileEntityIronWorkBench)par1World.getBlockTileEntity(par2, par3, par4);
  380.  
  381. if (var5 != null)
  382. {
  383. for (int var6 = 0; var6 < var5.getSizeInventory(); ++var6)
  384. {
  385. ItemStack var7 = var5.getStackInSlot(var6);
  386.  
  387. if (var7 != null)
  388. {
  389. float var8 = this.random.nextFloat() * 0.8F + 0.1F;
  390. float var9 = this.random.nextFloat() * 0.8F + 0.1F;
  391. EntityItem var12;
  392.  
  393. for (float var10 = this.random.nextFloat() * 0.8F + 0.1F; var7.stackSize > 0; par1World.spawnEntityInWorld(var12))
  394. {
  395. int var11 = this.random.nextInt(21) + 10;
  396.  
  397. if (var11 > var7.stackSize)
  398. {
  399. var11 = var7.stackSize;
  400. }
  401.  
  402. var7.stackSize -= var11;
  403. var12 = new EntityItem(par1World, (double)((float)par2 + var8), (double)((float)par3 + var9), (double)((float)par4 + var10), new ItemStack(var7.itemID, var11, var7.getItemDamage()));
  404. float var13 = 0.05F;
  405. var12.motionX = (double)((float)this.random.nextGaussian() * var13);
  406. var12.motionY = (double)((float)this.random.nextGaussian() * var13 + 0.2F);
  407. var12.motionZ = (double)((float)this.random.nextGaussian() * var13);
  408.  
  409. if (var7.hasTagCompound())
  410. {
  411. var12.getEntityItem().setTagCompound((NBTTagCompound)var7.getTagCompound().copy());
  412. }
  413. }
  414. }
  415. }
  416. }
  417.  
  418. super.breakBlock(par1World, par2, par3, par4, par5, par6);
  419. }
  420.  
  421. /**
  422. * Called upon block activation (left or right click on the block.). The three integers represent x,y,z of the
  423. * block.
  424. */
  425.  
  426. public static boolean isHoldingTop(EntityPlayer player)
  427. {
  428. return player.inventory.getCurrentItem() != null &&
  429. player.inventory.getCurrentItem().itemID == RegBlocks.BlockIronWorkBenchAddon.blockID;
  430. }
  431.  
  432. public static boolean isHoldingTop1(EntityPlayer player)
  433. {
  434. return player.inventory.getCurrentItem() != null &&
  435. player.inventory.getCurrentItem().itemID == RegBlocks.BlockIronPressAddon.blockID;
  436. }
  437.  
  438.  
  439. /**
  440. * Called upon block activation (left or right click on the block.). The three integers represent x,y,z of the
  441. * block.
  442. */
  443.  
  444. public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
  445. {
  446.  
  447. if (SimCraft.globalFunctions.isSimulating() && isHoldingTop(par5EntityPlayer))
  448. {
  449. return false;
  450. }
  451.  
  452. if (SimCraft.globalFunctions.isSimulating() && isHoldingTop1(par5EntityPlayer))
  453. {
  454. return false;
  455. }
  456.  
  457.  
  458. if (SimCraft.globalFunctions.isSimulating())
  459. {
  460. if (par5EntityPlayer.isSneaking())
  461. {
  462. return false;
  463. }
  464. else
  465. {
  466.  
  467. Object var6 = (TileEntityIronWorkBench)par1World.getBlockTileEntity(par2, par3, par4);
  468.  
  469. par5EntityPlayer.openGui(SimCraft.instance, 3, par1World, par2, par3, par4);
  470. return true;
  471. }
  472. }
  473. return true;
  474. }
  475.  
  476. @SideOnly(Side.CLIENT)
  477. public Icon getIcon(int par1, int par2)
  478. {
  479. return this.icon;
  480. }
  481.  
  482. @SideOnly(Side.CLIENT)
  483. /**
  484. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  485. * is the only chance you get to register icons.
  486. */
  487. public void registerIcons(IconRegister par1IconRegister)
  488. {
  489. this.icon = par1IconRegister.registerIcon(SimCraft.modid + ":" + "IronWorkBench_Single");
  490. }
  491.  
  492.  
  493. }
Advertisement
Add Comment
Please, Sign In to add comment