Guest User

TileEntityCommonWorkbench

a guest
Jul 4th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. package simcraft.machines;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.item.ItemStack;
  5. import net.minecraft.nbt.NBTTagCompound;
  6. import net.minecraft.nbt.NBTTagList;
  7. import net.minecraft.tileentity.TileEntity;
  8. import simcraft.core.sound.SoundSource;
  9.  
  10.  
  11. public class TileEntityCommonWorkbench extends TileEntity
  12. {
  13. public ItemStack[] inventory;
  14. private String Localizedname;
  15.  
  16. /**
  17. * Sound Sources Used at Most by Machines.
  18. */
  19.  
  20. public SoundSource audioSource;
  21. public SoundSource audioSource1;
  22. public SoundSource audioSource2;
  23.  
  24.  
  25. public TileEntityCommonWorkbench(int var1)
  26. {
  27. this.inventory = new ItemStack[var1];
  28. }
  29.  
  30. /**
  31. * Returns the number of slots in the inventory.
  32. */
  33. public int getSizeInventory()
  34. {
  35. return this.inventory.length;
  36. }
  37.  
  38. /**
  39. * Returns the stack in slot i
  40. */
  41. public ItemStack getStackInSlot(int var1)
  42. {
  43. return this.inventory[var1];
  44. }
  45.  
  46. /**
  47. * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
  48. * stack.
  49. */
  50. public ItemStack decrStackSize(int var1, int var2)
  51. {
  52. if (this.inventory[var1] != null)
  53. {
  54. ItemStack var3;
  55.  
  56. if (this.inventory[var1].stackSize <= var2)
  57. {
  58. var3 = this.inventory[var1];
  59. this.inventory[var1] = null;
  60. this.onInventoryChanged();
  61. return var3;
  62. }
  63. else
  64. {
  65. var3 = this.inventory[var1].splitStack(var2);
  66.  
  67. if (this.inventory[var1].stackSize == 0)
  68. {
  69. this.inventory[var1] = null;
  70. }
  71. this.onInventoryChanged();
  72. return var3;
  73. }
  74. }
  75. else
  76. {
  77. return null;
  78. }
  79. }
  80.  
  81. /**
  82. * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  83. * like when you close a workbench GUI.
  84. */
  85. public ItemStack getStackInSlotOnClosing(int var1)
  86. {
  87. if (this.inventory[var1] == null)
  88. {
  89. return null;
  90. }
  91. else
  92. {
  93. ItemStack var2 = this.inventory[var1];
  94. this.inventory[var1] = null;
  95. return var2;
  96. }
  97.  
  98. }
  99.  
  100.  
  101. /**
  102. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  103. */
  104. public void setInventorySlotContents(int var1, ItemStack var2)
  105. {
  106. this.inventory[var1] = var2;
  107.  
  108. if (var2 != null && var2.stackSize > this.getInventoryStackLimit())
  109. {
  110. var2.stackSize = this.getInventoryStackLimit();
  111. }
  112. this.onInventoryChanged();
  113. }
  114.  
  115. /**
  116. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
  117. * this more of a set than a get?*
  118. */
  119. public int getInventoryStackLimit()
  120. {
  121. return 64;
  122. }
  123.  
  124. /**
  125. * Reads a tile entity from NBT.
  126. */
  127. public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  128. {
  129. super.readFromNBT(par1NBTTagCompound);
  130. NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
  131. this.inventory = new ItemStack[this.getSizeInventory()];
  132.  
  133. if (par1NBTTagCompound.hasKey("CustomName"))
  134. {
  135. this.Localizedname = par1NBTTagCompound.getString("CustomName");
  136. }
  137.  
  138. for (int var3 = 0; var3 < nbttaglist.tagCount(); ++var3)
  139. {
  140. NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(var3);
  141. byte var5 = nbttagcompound1.getByte("Slot");
  142.  
  143. if (var5 >= 0 && var5 < this.inventory.length)
  144. {
  145. this.inventory[var5] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  146. }
  147. }
  148. }
  149.  
  150. /**
  151. * Writes a tile entity to NBT.
  152. */
  153. public void writeToNBT(NBTTagCompound par1NBTTagCompound)
  154. {
  155. super.writeToNBT(par1NBTTagCompound);
  156. NBTTagList nbttaglist = new NBTTagList();
  157.  
  158. for (int var3 = 0; var3 < this.inventory.length; ++var3)
  159. {
  160. if (this.inventory[var3] != null)
  161. {
  162. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  163. nbttagcompound1.setByte("Slot", (byte)var3);
  164. this.inventory[var3].writeToNBT(nbttagcompound1);
  165. nbttaglist.appendTag(nbttagcompound1);
  166. }
  167. }
  168.  
  169. par1NBTTagCompound.setTag("Items", nbttaglist);
  170.  
  171. if (this.isInvNameLocalized())
  172. {
  173. par1NBTTagCompound.setString("CustomName", this.Localizedname);
  174. }
  175. }
  176.  
  177. public boolean isInvNameLocalized()
  178. {
  179. return this.Localizedname != null && this.Localizedname.length() > 0;
  180. }
  181.  
  182. /**
  183. * Do not make give this method the name canInteractWith because it clashes with Container
  184. */
  185. public boolean isUseableByPlayer(EntityPlayer var1)
  186. {
  187. return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : var1.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  188. }
  189.  
  190. public void openChest() {}
  191.  
  192. public void closeChest() {}
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment