Advertisement
Guest User

TileEntityIronWorkBench

a guest
Jul 4th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.58 KB | None | 0 0
  1. package simcraft.machines.workshop;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraftforge.common.ForgeDirection;
  12. import net.minecraftforge.common.ISidedInventory;
  13.  
  14. import simcraft.core.RegBlocks;
  15. import simcraft.core.SimCraft;
  16. import simcraft.core.interf.IBenchHammer;
  17. import simcraft.core.network.NetworkClientTileEntityEventListener;
  18. import simcraft.core.network.NetworkTileEntityEventListener;
  19. import simcraft.core.recipes.RecipesAdvIronBnch;
  20. import simcraft.machines.TileEntityCommonWorkbench;
  21.  
  22.  
  23. public class TileEntityIronWorkBench extends TileEntityCommonWorkbench implements IInventory, IBenchHammer, NetworkTileEntityEventListener, NetworkClientTileEntityEventListener
  24. {
  25. public EntityPlayer player;
  26.  
  27. /** Determines if the check for adjacent Iron Work Benches has taken place. */
  28. public boolean adjacentModelChecked = false;
  29.  
  30. /** Contains the chest tile located adjacent to this one (if any) */
  31. public TileEntityIronWorkBench adjacentModelZNeg;
  32.  
  33. /** Contains the chest tile located adjacent to this one (if any) */
  34. public TileEntityIronWorkBench adjacentModelXPos;
  35.  
  36. /** Contains the chest tile located adjacent to this one (if any) */
  37. public TileEntityIronWorkBench adjacentModelXNeg;
  38.  
  39. /** Contains the chest tile located adjacent to this one (if any) */
  40. public TileEntityIronWorkBench adjacentModelZPosition;
  41.  
  42. /** The number of players currently using this chest */
  43. public int numUsingPlayers;
  44.  
  45. /** Server sync counter (once per 20 ticks) */
  46. private int ticksSinceSync;
  47.  
  48. /**
  49. * Integers for Hammer
  50. */
  51. public int HammerSlot1Time = 0;
  52. public int currentItemHammerSlot1Time = 0;
  53. public boolean CrushHammer;
  54.  
  55.  
  56.  
  57. public TileEntityIronWorkBench()
  58. {
  59. super(27);
  60. this.CrushHammer = false;
  61. }
  62.  
  63. /**
  64. * Returns the number of slots in the inventory.
  65. */
  66. public int getSizeInventory()
  67. {
  68. return 27;
  69. }
  70.  
  71. public String getInvName() {
  72. return "IronWorkBench";
  73. }
  74.  
  75. /**
  76. * Reads a tile entity from NBT.
  77. */
  78. public void readFromNBT(NBTTagCompound var1)
  79. {
  80. super.readFromNBT(var1);
  81. this.HammerSlot1Time = var1.getShort("Slot1Time");
  82. }
  83.  
  84. /**
  85. * Writes a tile entity to NBT.
  86. */
  87. public void writeToNBT(NBTTagCompound var1)
  88. {
  89. super.writeToNBT(var1);
  90. var1.setShort("Slot1Time", (short)this.HammerSlot1Time);
  91. }
  92.  
  93. /**
  94. * Do not make give this method the name canInteractWith because it clashes with Container
  95. */
  96. public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  97. {
  98. return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  99. }
  100.  
  101. public int getProgressSlot1(int var1)
  102. {
  103. return this.HammerSlot1Time * var1 / this.getHammerTime1();
  104. }
  105.  
  106. public int getHammerTime1()
  107. {
  108. int var1 = 1;
  109. if (this.inventory[10] != null)
  110. {
  111. var1 = 200;
  112. }
  113.  
  114.  
  115. return var1;
  116. }
  117.  
  118. private boolean canCrushSlot1()
  119. {
  120. if (this.inventory[10] == null)
  121. {
  122. return false;
  123. }
  124. else
  125. {
  126. ItemStack var1 = RecipesAdvIronBnch.Crushing().getCrushingResult(this.inventory[10]);
  127. return var1 == null ? false : (this.inventory[11] == null ? true : (!this.inventory[11].isItemEqual(var1) ? false : (this.inventory[11].stackSize < this.getInventoryStackLimit() && this.inventory[11].stackSize < this.inventory[11].getMaxStackSize() ? true : this.inventory[11].stackSize < var1.getMaxStackSize())));
  128. }
  129. }
  130.  
  131. public void CrushSlot1()
  132. {
  133. if (this.canCrushSlot1())
  134. {
  135. ItemStack var1 = RecipesAdvIronBnch.Crushing().getCrushingResult(this.inventory[10]);
  136.  
  137. if (this.inventory[11] == null)
  138. {
  139. this.inventory[11] = var1.copy();
  140. }
  141. else if (this.inventory[11].itemID == var1.itemID)
  142. {
  143. this.inventory[11].stackSize += var1.stackSize;
  144. }
  145. if (SimCraft.globalFunctions.isSimulating())
  146. {
  147. onNetworkEvent(1);
  148. }
  149. if (!this.worldObj.isRemote)
  150. {
  151. SimCraft.network.initiateTileEntityEvent(this, 1, true);
  152. }
  153. updateContainingBlockInfo();
  154.  
  155. if (this.inventory[10].getItem().hasContainerItem())
  156. {
  157. this.inventory[10] = new ItemStack(this.inventory[10].getItem().getContainerItem());
  158. }
  159. else
  160. {
  161. --this.inventory[10].stackSize;
  162.  
  163. }
  164.  
  165. if (this.inventory[10].stackSize <= 0)
  166. {
  167. this.inventory[10] = null;
  168.  
  169. }
  170. }
  171. }
  172.  
  173. /**
  174. * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  175. * like when you close a workbench GUI.
  176. */
  177. public ItemStack getStackInSlotOnClosing(int var1)
  178. {
  179. if (this.inventory[var1] == null)
  180. {
  181. return null;
  182. }
  183. else
  184. {
  185. ItemStack var2 = this.inventory[var1];
  186. this.inventory[var1] = null;
  187. return var2;
  188. }
  189.  
  190. }
  191.  
  192.  
  193. /**
  194. * Causes the TileEntity to reset all it's cached values for it's container block, blockID, metaData and in the case
  195. * of chests, the adjcacent chest check
  196. */
  197. public void updateContainingBlockInfo()
  198. {
  199. super.updateContainingBlockInfo();
  200. this.adjacentModelChecked = false;
  201. }
  202.  
  203.  
  204. /**
  205. * Performs the check for adjacent chests to determine if this chest is double or not.
  206. */
  207. public void checkForAdjacentModel()
  208. {
  209. if (!this.adjacentModelChecked)
  210. {
  211. this.adjacentModelChecked = true;
  212. this.adjacentModelZNeg = null;
  213. this.adjacentModelXPos = null;
  214. this.adjacentModelXNeg = null;
  215. this.adjacentModelZPosition = null;
  216.  
  217. if (this.worldObj.getBlockId(this.xCoord - 1, this.yCoord, this.zCoord) == RegBlocks.BlockIronWorkBench.blockID)
  218. {
  219. this.adjacentModelXNeg = (TileEntityIronWorkBench)this.worldObj.getBlockTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
  220. }
  221.  
  222. if (this.worldObj.getBlockId(this.xCoord + 1, this.yCoord, this.zCoord) == RegBlocks.BlockIronWorkBench.blockID)
  223. {
  224. this.adjacentModelXPos = (TileEntityIronWorkBench)this.worldObj.getBlockTileEntity(this.xCoord + 1, this.yCoord, this.zCoord);
  225. }
  226.  
  227. if (this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord - 1) == RegBlocks.BlockIronWorkBench.blockID)
  228. {
  229. this.adjacentModelZNeg = (TileEntityIronWorkBench)this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
  230. }
  231.  
  232. if (this.worldObj.getBlockId(this.xCoord, this.yCoord, this.zCoord + 1) == RegBlocks.BlockIronWorkBench.blockID)
  233. {
  234. this.adjacentModelZPosition = (TileEntityIronWorkBench)this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord + 1);
  235. }
  236.  
  237. if (this.adjacentModelZNeg != null)
  238. {
  239. this.adjacentModelZNeg.updateContainingBlockInfo();
  240. }
  241. if (this.adjacentModelZPosition != null)
  242. {
  243. this.adjacentModelZPosition.updateContainingBlockInfo();
  244. }
  245. if (this.adjacentModelXPos != null)
  246. {
  247. this.adjacentModelXPos.updateContainingBlockInfo();
  248. }
  249. if (this.adjacentModelXNeg != null)
  250. {
  251. this.adjacentModelXNeg.updateContainingBlockInfo();
  252. }
  253. }
  254. }
  255.  
  256.  
  257.  
  258. /**
  259. * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
  260. * ticks and creates a new spawn inside its implementation.
  261. */
  262. public void updateEntity()
  263. {
  264. super.updateEntity();
  265. this.checkForAdjacentModel();
  266.  
  267. boolean var1 = this.HammerSlot1Time > 0;
  268. boolean var2 = false;
  269.  
  270.  
  271. if (this.inventory[10] == null && !this.adjacentModelChecked)
  272. {
  273. this.HammerSlot1Time = 0;
  274. }
  275.  
  276.  
  277.  
  278. if (++this.ticksSinceSync % 20 * 4 == 0)
  279. {
  280. this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, RegBlocks.BlockIronWorkBench.blockID, 1, this.numUsingPlayers);
  281. }
  282.  
  283.  
  284. if (!this.worldObj.isRemote)
  285. {
  286.  
  287. if (this.canCrushSlot1())
  288. {
  289. if (this.HammerSlot1Time > this.getHammerTime1())
  290. {
  291. var2 = true;
  292. if (SimCraft.globalFunctions.isSimulating())
  293. {
  294. onNetworkEvent(10);
  295. }
  296. if (!this.worldObj.isRemote)
  297. {
  298. SimCraft.network.initiateTileEntityEvent(this, 10, true);
  299. }
  300. }
  301. }
  302. }
  303.  
  304. if (this.HammerSlot1Time > 210)
  305. {
  306. if (SimCraft.globalFunctions.isSimulating())
  307. {
  308. onNetworkEvent(11);
  309. }
  310. if (!this.worldObj.isRemote)
  311. {
  312. SimCraft.network.initiateTileEntityEvent(this, 11, true);
  313. }
  314. }
  315.  
  316. if (var2)
  317. {
  318. this.onInventoryChanged();
  319. }
  320. }
  321.  
  322. /**
  323. * Called when a client event is received with the event number and argument, see World.sendClientEvent
  324. */
  325. public boolean receiveClientEvent(int par1, int par2)
  326. {
  327. if (par1 == 1)
  328. {
  329. this.numUsingPlayers = par2;
  330. return true;
  331. }
  332. else
  333. {
  334. return super.receiveClientEvent(par1, par2);
  335. }
  336. }
  337.  
  338. public void openChest()
  339. {
  340. if (this.numUsingPlayers < 0)
  341. {
  342. this.numUsingPlayers = 0;
  343. }
  344. ++this.numUsingPlayers;
  345. this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, RegBlocks.BlockIronWorkBench.blockID, 1, this.numUsingPlayers);
  346. this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID);
  347. this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType().blockID);
  348. }
  349.  
  350. public void closeChest()
  351. {
  352. if (this.getBlockType() != null && this.getBlockType() instanceof blockWorkBench)
  353. {
  354. --this.numUsingPlayers;
  355. this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, RegBlocks.BlockIronWorkBench.blockID, 1, this.numUsingPlayers);
  356. this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID);
  357. this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType().blockID);
  358.  
  359. }
  360. }
  361.  
  362. /**
  363. * invalidates a tile entity
  364. */
  365. public void invalidate()
  366. {
  367. this.updateContainingBlockInfo();
  368. this.checkForAdjacentModel();
  369. if (this.audioSource != null)
  370. {
  371.  
  372. SimCraft.soundsManager.removeSources(this);
  373. this.audioSource = null;
  374. }
  375. super.invalidate();
  376. }
  377.  
  378.  
  379. public int getStartInventorySide(int side) {
  380. if (side == 0) return 11;
  381. if (side == 1) return 12;
  382. if (side == 2) return 13;
  383. if (side == 3) return 14;
  384. if (side == 4) return 15;
  385. return 10;
  386. }
  387.  
  388.  
  389. public int getSizeInventorySide(int side) {
  390.  
  391. return 10;
  392. }
  393.  
  394.  
  395. /**
  396. * Boolean Flag when wrench is used on TileEntity
  397. */
  398. public boolean CrushHammer() {
  399. return true;
  400. }
  401.  
  402. public void CrushHammerInt()
  403. {
  404. SimCraft.network.initiateClientTileEntityEvent(this, 12);
  405. }
  406.  
  407. public void Working()
  408. {
  409. this.HammerSlot1Time = this.HammerSlot1Time + 10;
  410. if (SimCraft.globalFunctions.isSimulating())
  411. {
  412. onNetworkEvent(0);
  413. }
  414. if (!this.worldObj.isRemote)
  415. {
  416. SimCraft.network.initiateTileEntityEvent(this, 0, true);
  417. }
  418. }
  419.  
  420. /**
  421. * Creates the SoundSource
  422. */
  423. public void onNetworkEvent(int var1)
  424. {
  425. if (this.audioSource == null && this.getStartSoundFile() != null)
  426. {
  427. this.audioSource = SimCraft.soundsManager.createSource(this, this.getStartSoundFile());
  428. }
  429.  
  430. switch (var1)
  431. {
  432. case 0:
  433. if (this.audioSource != null)
  434. {
  435. this.audioSource.stop();
  436.  
  437. if (this.getStartSoundFile() != null)
  438. {
  439. SimCraft.soundsManager.playOnce(this, this.getStartSoundFile());
  440. }
  441. }
  442.  
  443. break;
  444.  
  445. case 1:
  446. if (this.audioSource != null)
  447. {
  448. this.audioSource.stop();
  449.  
  450. if (this.getBreakSoundFile() != null)
  451. {
  452. SimCraft.soundsManager.playOnce(this, this.getBreakSoundFile());
  453. }
  454. }
  455.  
  456. break;
  457. case 10:
  458. {
  459. this.HammerSlot1Time = 0;
  460. this.CrushSlot1();
  461. break;
  462. }
  463. case 11:
  464. {
  465. this.HammerSlot1Time = 0;
  466. break;
  467. }
  468. case 12:
  469. {
  470. this.Working();
  471. break;
  472. }
  473. }
  474. }
  475.  
  476. public boolean canUpdate()
  477. {
  478. return SimCraft.globalFunctions.isSimulating();
  479. }
  480.  
  481. public void onNetworkEvent(EntityPlayer var1, int var2)
  482. {
  483. switch (var2)
  484. {
  485. case 12:
  486. {
  487. this.Working();
  488. break;
  489. }
  490.  
  491. }
  492.  
  493. }
  494.  
  495. public String getStartSoundFile()
  496. {
  497. return "machines/hammeriron.ogg";
  498. }
  499.  
  500. public String getBreakSoundFile()
  501. {
  502. return "machines/rocks.ogg";
  503. }
  504.  
  505. public boolean isStackValidForSlot(int i, ItemStack itemstack) {
  506. return false;
  507. }
  508.  
  509.  
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement