Advertisement
Guest User

TileEntity

a guest
Apr 20th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.70 KB | None | 0 0
  1. package net.minecraft.render.tileentity;
  2.  
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockIstriumChest;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.inventory.ContainerChest;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.InventoryLargeChest;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.nbt.NBTTagList;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.AxisAlignedBB;
  16.  
  17. public class TileEntityIstriumChest extends TileEntity implements IInventory
  18. {
  19. private ItemStack[] field_145985_p;
  20. public boolean field_145984_a;
  21. public TileEntityIstriumChest field_145992_i;
  22. public TileEntityIstriumChest field_145990_j;
  23. public TileEntityIstriumChest field_145991_k;
  24. public TileEntityIstriumChest field_145988_l;
  25. public float field_145989_m;
  26. public float field_145986_n;
  27. public int field_145987_o;
  28. private int field_145983_q;
  29. private int field_145982_r;
  30. private String field_145981_s;
  31.  
  32. public TileEntityIstriumChest()
  33. {
  34. field_145985_p = new ItemStack[54];
  35. this.field_145982_r = -1;
  36. }
  37.  
  38. public TileEntityIstriumChest(int par1)
  39. {
  40. field_145985_p = new ItemStack[54];
  41.  
  42. this.field_145982_r = par1;
  43. }
  44.  
  45. /**
  46. * Returns the number of slots in the inventory.
  47. */
  48. public int getSizeInventory()
  49. {
  50. return 54;
  51. }
  52.  
  53. /**
  54. * Returns the stack in slot i
  55. */
  56. public ItemStack getStackInSlot(int par1)
  57. {
  58. return this.field_145985_p[par1];
  59. }
  60.  
  61. /**
  62. * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  63. * new stack.
  64. */
  65. public ItemStack decrStackSize(int par1, int par2)
  66. {
  67. if (this.field_145985_p[par1] != null)
  68. {
  69. ItemStack var3;
  70.  
  71. if (this.field_145985_p[par1].stackSize <= par2)
  72. {
  73. var3 = this.field_145985_p[par1];
  74. this.field_145985_p[par1] = null;
  75. this.onInventoryChanged();
  76. return var3;
  77. }
  78. else
  79. {
  80. var3 = this.field_145985_p[par1].splitStack(par2);
  81.  
  82. if (this.field_145985_p[par1].stackSize == 0)
  83. {
  84. this.field_145985_p[par1] = null;
  85. }
  86.  
  87. this.onInventoryChanged();
  88. return var3;
  89. }
  90. }
  91. else
  92. {
  93. return null;
  94. }
  95. }
  96.  
  97. /**
  98. * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  99. * like when you close a workbench GUI.
  100. */
  101. public ItemStack getStackInSlotOnClosing(int par1)
  102. {
  103. if (this.field_145985_p[par1] != null)
  104. {
  105. ItemStack var2 = this.field_145985_p[par1];
  106. this.field_145985_p[par1] = null;
  107. return var2;
  108. }
  109. else
  110. {
  111. return null;
  112. }
  113. }
  114.  
  115. /**
  116. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  117. */
  118. public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  119. {
  120. this.field_145985_p[par1] = par2ItemStack;
  121.  
  122. if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
  123. {
  124. par2ItemStack.stackSize = this.getInventoryStackLimit();
  125. }
  126.  
  127. this.onInventoryChanged();
  128. }
  129.  
  130. /**
  131. * Returns the name of the inventory
  132. */
  133. public String getInventoryName()
  134. {
  135. return this.isInventoryNameLocalized() ? this.field_145981_s : "container.chest";
  136. }
  137.  
  138. /**
  139. * Returns if the inventory name is localized
  140. */
  141. public boolean isInventoryNameLocalized()
  142. {
  143. return this.field_145981_s != null && this.field_145981_s.length() > 0;
  144. }
  145.  
  146. public void func_145976_a(String p_145976_1_)
  147. {
  148. this.field_145981_s = p_145976_1_;
  149. }
  150.  
  151. public void readFromNBT(NBTTagCompound p_145839_1_)
  152. {
  153. super.readFromNBT(p_145839_1_);
  154. NBTTagList var2 = p_145839_1_.getTagList("Items", 10);
  155. this.field_145985_p = new ItemStack[this.getSizeInventory()];
  156.  
  157. if (p_145839_1_.func_150297_b("CustomName", 8))
  158. {
  159. this.field_145981_s = p_145839_1_.getString("CustomName");
  160. }
  161.  
  162. for (int var3 = 0; var3 < var2.tagCount(); ++var3)
  163. {
  164. NBTTagCompound var4 = var2.getCompoundTagAt(var3);
  165. int var5 = var4.getByte("Slot") & 255;
  166.  
  167. if (var5 >= 0 && var5 < this.field_145985_p.length)
  168. {
  169. this.field_145985_p[var5] = ItemStack.loadItemStackFromNBT(var4);
  170. }
  171. }
  172. }
  173.  
  174. public void writeToNBT(NBTTagCompound p_145841_1_)
  175. {
  176. super.writeToNBT(p_145841_1_);
  177. NBTTagList var2 = new NBTTagList();
  178.  
  179. for (int var3 = 0; var3 < this.field_145985_p.length; ++var3)
  180. {
  181. if (this.field_145985_p[var3] != null)
  182. {
  183. NBTTagCompound var4 = new NBTTagCompound();
  184. var4.setByte("Slot", (byte)var3);
  185. this.field_145985_p[var3].writeToNBT(var4);
  186. var2.appendTag(var4);
  187. }
  188. }
  189.  
  190. p_145841_1_.setTag("Items", var2);
  191.  
  192. if (this.isInventoryNameLocalized())
  193. {
  194. p_145841_1_.setString("CustomName", this.field_145981_s);
  195. }
  196. }
  197.  
  198. /**
  199. * Returns the maximum stack size for a inventory slot.
  200. */
  201. public int getInventoryStackLimit()
  202. {
  203. return 64;
  204. }
  205.  
  206. /**
  207. * Do not make give this method the name canInteractWith because it clashes with Container
  208. */
  209. public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  210. {
  211. return this.worldObj.getTileEntity(this.field_145851_c, this.field_145848_d, this.field_145849_e) != this ? false : par1EntityPlayer.getDistanceSq((double)this.field_145851_c + 0.5D, (double)this.field_145848_d + 0.5D, (double)this.field_145849_e + 0.5D) <= 64.0D;
  212. }
  213.  
  214. public void updateContainingBlockInfo()
  215. {
  216. super.updateContainingBlockInfo();
  217. this.field_145984_a = false;
  218. }
  219.  
  220. private void func_145978_a(TileEntityIstriumChest p_145978_1_, int p_145978_2_)
  221. {
  222. if (p_145978_1_.isInvalid())
  223. {
  224. this.field_145984_a = false;
  225. }
  226. else if (this.field_145984_a)
  227. {
  228. switch (p_145978_2_)
  229. {
  230. case 0:
  231. if (this.field_145988_l != p_145978_1_)
  232. {
  233. this.field_145984_a = false;
  234. }
  235.  
  236. break;
  237.  
  238. case 1:
  239. if (this.field_145991_k != p_145978_1_)
  240. {
  241. this.field_145984_a = false;
  242. }
  243.  
  244. break;
  245.  
  246. case 2:
  247. if (this.field_145992_i != p_145978_1_)
  248. {
  249. this.field_145984_a = false;
  250. }
  251.  
  252. break;
  253.  
  254. case 3:
  255. if (this.field_145990_j != p_145978_1_)
  256. {
  257. this.field_145984_a = false;
  258. }
  259. }
  260. }
  261. }
  262.  
  263.  
  264.  
  265. private boolean func_145977_a(int p_145977_1_, int p_145977_2_, int p_145977_3_)
  266. {
  267. Block var4 = this.worldObj.getBlock(p_145977_1_, p_145977_2_, p_145977_3_);
  268. return var4 instanceof BlockIstriumChest && ((BlockIstriumChest)var4).field_149956_a == this.func_145980_j();
  269. }
  270.  
  271. public void updateEntity()
  272. {
  273. super.updateEntity();
  274. ++this.field_145983_q;
  275. float var1;
  276.  
  277. if (!this.worldObj.isClient && this.field_145987_o != 0 && (this.field_145983_q + this.field_145851_c + this.field_145848_d + this.field_145849_e) % 200 == 0)
  278. {
  279. this.field_145987_o = 0;
  280. var1 = 5.0F;
  281. List var2 = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox((double)((float)this.field_145851_c - var1), (double)((float)this.field_145848_d - var1), (double)((float)this.field_145849_e - var1), (double)((float)(this.field_145851_c + 1) + var1), (double)((float)(this.field_145848_d + 1) + var1), (double)((float)(this.field_145849_e + 1) + var1)));
  282. Iterator var3 = var2.iterator();
  283.  
  284. while (var3.hasNext())
  285. {
  286. EntityPlayer var4 = (EntityPlayer)var3.next();
  287.  
  288. if (var4.openContainer instanceof ContainerChest)
  289. {
  290. IInventory var5 = ((ContainerChest)var4.openContainer).getLowerChestInventory();
  291.  
  292. if (var5 == this || var5 instanceof InventoryLargeChest && ((InventoryLargeChest)var5).isPartOfLargeChest(this))
  293. {
  294. ++this.field_145987_o;
  295. }
  296. }
  297. }
  298. }
  299.  
  300. this.field_145986_n = this.field_145989_m;
  301. var1 = 0.1F;
  302. double var11;
  303.  
  304. if (this.field_145987_o > 0 && this.field_145989_m == 0.0F && this.field_145992_i == null && this.field_145991_k == null)
  305. {
  306. double var8 = (double)this.field_145851_c + 0.5D;
  307. var11 = (double)this.field_145849_e + 0.5D;
  308.  
  309. if (this.field_145988_l != null)
  310. {
  311. var11 += 0.5D;
  312. }
  313.  
  314. if (this.field_145990_j != null)
  315. {
  316. var8 += 0.5D;
  317. }
  318.  
  319. this.worldObj.playSoundEffect(var8, (double)this.field_145848_d + 0.5D, var11, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
  320. }
  321.  
  322. if (this.field_145987_o == 0 && this.field_145989_m > 0.0F || this.field_145987_o > 0 && this.field_145989_m < 1.0F)
  323. {
  324. float var9 = this.field_145989_m;
  325.  
  326. if (this.field_145987_o > 0)
  327. {
  328. this.field_145989_m += var1;
  329. }
  330. else
  331. {
  332. this.field_145989_m -= var1;
  333. }
  334.  
  335. if (this.field_145989_m > 1.0F)
  336. {
  337. this.field_145989_m = 1.0F;
  338. }
  339.  
  340. float var10 = 0.5F;
  341.  
  342. if (this.field_145989_m < var10 && var9 >= var10 && this.field_145992_i == null && this.field_145991_k == null)
  343. {
  344. var11 = (double)this.field_145851_c + 0.5D;
  345. double var6 = (double)this.field_145849_e + 0.5D;
  346.  
  347. if (this.field_145988_l != null)
  348. {
  349. var6 += 0.5D;
  350. }
  351.  
  352. if (this.field_145990_j != null)
  353. {
  354. var11 += 0.5D;
  355. }
  356.  
  357. this.worldObj.playSoundEffect(var11, (double)this.field_145848_d + 0.5D, var6, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
  358. }
  359.  
  360. if (this.field_145989_m < 0.0F)
  361. {
  362. this.field_145989_m = 0.0F;
  363. }
  364. }
  365. }
  366.  
  367. public boolean receiveClientEvent(int p_145842_1_, int p_145842_2_)
  368. {
  369. if (p_145842_1_ == 1)
  370. {
  371. this.field_145987_o = p_145842_2_;
  372. return true;
  373. }
  374. else
  375. {
  376. return super.receiveClientEvent(p_145842_1_, p_145842_2_);
  377. }
  378. }
  379.  
  380. public void openInventory()
  381. {
  382. if (this.field_145987_o < 0)
  383. {
  384. this.field_145987_o = 0;
  385. }
  386.  
  387. ++this.field_145987_o;
  388. this.worldObj.func_147452_c(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.getBlockType(), 1, this.field_145987_o);
  389. this.worldObj.notifyBlocksOfNeighborChange(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.getBlockType());
  390. this.worldObj.notifyBlocksOfNeighborChange(this.field_145851_c, this.field_145848_d - 1, this.field_145849_e, this.getBlockType());
  391. }
  392.  
  393. public void closeInventory()
  394. {
  395. if (this.getBlockType() instanceof BlockIstriumChest)
  396. {
  397. --this.field_145987_o;
  398. this.worldObj.func_147452_c(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.getBlockType(), 1, this.field_145987_o);
  399. this.worldObj.notifyBlocksOfNeighborChange(this.field_145851_c, this.field_145848_d, this.field_145849_e, this.getBlockType());
  400. this.worldObj.notifyBlocksOfNeighborChange(this.field_145851_c, this.field_145848_d - 1, this.field_145849_e, this.getBlockType());
  401. }
  402. }
  403.  
  404. /**
  405. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  406. */
  407. public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack)
  408. {
  409. return true;
  410. }
  411.  
  412. /**
  413. * invalidates a tile entity
  414. */
  415. public void invalidate()
  416. {
  417. super.invalidate();
  418. this.updateContainingBlockInfo();
  419. }
  420.  
  421. public int func_145980_j()
  422. {
  423. if (this.field_145982_r == -1)
  424. {
  425. if (this.worldObj == null || !(this.getBlockType() instanceof BlockIstriumChest))
  426. {
  427. return 0;
  428. }
  429.  
  430. this.field_145982_r = ((BlockIstriumChest)this.getBlockType()).field_149956_a;
  431. }
  432.  
  433. return this.field_145982_r;
  434. }
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement