Advertisement
ButterAleks

InventoryPlayer

Apr 8th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.90 KB | None | 0 0
  1. package net.minecraft.entity.player;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import javax.annotation.Nullable;
  7. import net.minecraft.block.state.IBlockState;
  8. import net.minecraft.client.util.RecipeItemHelper;
  9. import net.minecraft.crash.CrashReport;
  10. import net.minecraft.crash.CrashReportCategory;
  11. import net.minecraft.crash.ICrashReportDetail;
  12. import net.minecraft.inventory.IInventory;
  13. import net.minecraft.inventory.ItemStackHelper;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.item.ItemArmor;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.nbt.NBTTagList;
  19. import net.minecraft.nbt.NBTUtil;
  20. import net.minecraft.network.play.server.SPacketSetSlot;
  21. import net.minecraft.util.NonNullList;
  22. import net.minecraft.util.ReportedException;
  23. import net.minecraft.util.text.ITextComponent;
  24. import net.minecraft.util.text.TextComponentString;
  25. import net.minecraft.util.text.TextComponentTranslation;
  26. import net.minecraft.world.World;
  27. import net.minecraftforge.fml.relauncher.Side;
  28. import net.minecraftforge.fml.relauncher.SideOnly;
  29.  
  30. public class InventoryPlayer implements IInventory
  31. {
  32. /** An array of 36 item stacks indicating the main player inventory (including the visible bar). */
  33. public final NonNullList<ItemStack> mainInventory = NonNullList.<ItemStack>withSize(36, ItemStack.EMPTY);
  34. /** An array of 4 item stacks containing the currently worn armor pieces. */
  35. public final NonNullList<ItemStack> armorInventory = NonNullList.<ItemStack>withSize(4, ItemStack.EMPTY);
  36. public final NonNullList<ItemStack> offHandInventory = NonNullList.<ItemStack>withSize(1, ItemStack.EMPTY);
  37. private final List<NonNullList<ItemStack>> allInventories;
  38. /** The index of the currently held item (0-8). */
  39. public int currentItem;
  40. /** The player whose inventory this is. */
  41. public EntityPlayer player;
  42. /** The stack currently held by the mouse cursor */
  43. private ItemStack itemStack;
  44. private int timesChanged;
  45.  
  46. public InventoryPlayer(EntityPlayer playerIn)
  47. {
  48. this.allInventories = Arrays.<NonNullList<ItemStack>>asList(this.mainInventory, this.armorInventory, this.offHandInventory);
  49. this.itemStack = ItemStack.EMPTY;
  50. this.player = playerIn;
  51. }
  52.  
  53. /**
  54. * Returns the item stack currently held by the player.
  55. */
  56. public ItemStack getCurrentItem()
  57. {
  58. return isHotbar(this.currentItem) ? (ItemStack)this.mainInventory.get(this.currentItem) : ItemStack.EMPTY;
  59. }
  60.  
  61. /**
  62. * Get the size of the player hotbar inventory
  63. */
  64. public static int getHotbarSize()
  65. {
  66. return 9;
  67. }
  68.  
  69. private boolean canMergeStacks(ItemStack stack1, ItemStack stack2)
  70. {
  71. return !stack1.isEmpty() && this.stackEqualExact(stack1, stack2) && stack1.isStackable() && stack1.getCount() < stack1.getMaxStackSize() && stack1.getCount() < this.getInventoryStackLimit();
  72. }
  73.  
  74. /**
  75. * Checks item, NBT, and meta if the item is not damageable
  76. */
  77. private boolean stackEqualExact(ItemStack stack1, ItemStack stack2)
  78. {
  79. return stack1.getItem() == stack2.getItem() && (!stack1.getHasSubtypes() || stack1.getMetadata() == stack2.getMetadata()) && ItemStack.areItemStackTagsEqual(stack1, stack2);
  80. }
  81.  
  82. /**
  83. * Returns the first item stack that is empty.
  84. */
  85. public int getFirstEmptyStack()
  86. {
  87. for (int i = 0; i < this.mainInventory.size(); ++i)
  88. {
  89. if (((ItemStack)this.mainInventory.get(i)).isEmpty())
  90. {
  91. return i;
  92. }
  93. }
  94.  
  95. return -1;
  96. }
  97.  
  98. @SideOnly(Side.CLIENT)
  99. public void setPickedItemStack(ItemStack stack)
  100. {
  101. int i = this.getSlotFor(stack);
  102.  
  103. if (isHotbar(i))
  104. {
  105. this.currentItem = i;
  106. }
  107. else
  108. {
  109. if (i == -1)
  110. {
  111. this.currentItem = this.getBestHotbarSlot();
  112.  
  113. if (!((ItemStack)this.mainInventory.get(this.currentItem)).isEmpty())
  114. {
  115. int j = this.getFirstEmptyStack();
  116.  
  117. if (j != -1)
  118. {
  119. this.mainInventory.set(j, this.mainInventory.get(this.currentItem));
  120. }
  121. }
  122.  
  123. this.mainInventory.set(this.currentItem, stack);
  124. }
  125. else
  126. {
  127. this.pickItem(i);
  128. }
  129. }
  130. }
  131.  
  132. public void pickItem(int index)
  133. {
  134. this.currentItem = this.getBestHotbarSlot();
  135. ItemStack itemstack = this.mainInventory.get(this.currentItem);
  136. this.mainInventory.set(this.currentItem, this.mainInventory.get(index));
  137. this.mainInventory.set(index, itemstack);
  138. }
  139.  
  140. public static boolean isHotbar(int index)
  141. {
  142. return index >= 0 && index < 9;
  143. }
  144.  
  145. /**
  146. * Finds the stack or an equivalent one in the main inventory
  147. */
  148. @SideOnly(Side.CLIENT)
  149. public int getSlotFor(ItemStack stack)
  150. {
  151. for (int i = 0; i < this.mainInventory.size(); ++i)
  152. {
  153. if (!((ItemStack)this.mainInventory.get(i)).isEmpty() && this.stackEqualExact(stack, this.mainInventory.get(i)))
  154. {
  155. return i;
  156. }
  157. }
  158.  
  159. return -1;
  160. }
  161.  
  162. public int findSlotMatchingUnusedItem(ItemStack p_194014_1_)
  163. {
  164. for (int i = 0; i < this.mainInventory.size(); ++i)
  165. {
  166. ItemStack itemstack = this.mainInventory.get(i);
  167.  
  168. if (!((ItemStack)this.mainInventory.get(i)).isEmpty() && this.stackEqualExact(p_194014_1_, this.mainInventory.get(i)) && !((ItemStack)this.mainInventory.get(i)).isItemDamaged() && !itemstack.isItemEnchanted() && !itemstack.hasDisplayName())
  169. {
  170. return i;
  171. }
  172. }
  173.  
  174. return -1;
  175. }
  176.  
  177. public int getBestHotbarSlot()
  178. {
  179. for (int i = 0; i < 9; ++i)
  180. {
  181. int j = (this.currentItem + i) % 9;
  182.  
  183. if (((ItemStack)this.mainInventory.get(j)).isEmpty())
  184. {
  185. return j;
  186. }
  187. }
  188.  
  189. for (int k = 0; k < 9; ++k)
  190. {
  191. int l = (this.currentItem + k) % 9;
  192.  
  193. if (!((ItemStack)this.mainInventory.get(l)).isItemEnchanted())
  194. {
  195. return l;
  196. }
  197. }
  198.  
  199. return this.currentItem;
  200. }
  201.  
  202. /**
  203. * Switch the current item to the next one or the previous one
  204. */
  205. @SideOnly(Side.CLIENT)
  206. public void changeCurrentItem(int direction)
  207. {
  208. if (direction > 0)
  209. {
  210. direction = 1;
  211. }
  212.  
  213. if (direction < 0)
  214. {
  215. direction = -1;
  216. }
  217.  
  218. for (this.currentItem -= direction; this.currentItem < 0; this.currentItem += 9)
  219. {
  220. ;
  221. }
  222.  
  223. while (this.currentItem >= 9)
  224. {
  225. this.currentItem -= 9;
  226. }
  227. }
  228.  
  229. /**
  230. * Removes matching items from the inventory.
  231. * @param itemIn The item to match, null ignores.
  232. * @param metadataIn The metadata to match, -1 ignores.
  233. * @param removeCount The number of items to remove. If less than 1, removes all matching items.
  234. * @param itemNBT The NBT data to match, null ignores.
  235. * @return The number of items removed from the inventory.
  236. */
  237. public int clearMatchingItems(@Nullable Item itemIn, int metadataIn, int removeCount, @Nullable NBTTagCompound itemNBT)
  238. {
  239. int i = 0;
  240.  
  241. for (int j = 0; j < this.getSizeInventory(); ++j)
  242. {
  243. ItemStack itemstack = this.getStackInSlot(j);
  244.  
  245. if (!itemstack.isEmpty() && (itemIn == null || itemstack.getItem() == itemIn) && (metadataIn <= -1 || itemstack.getMetadata() == metadataIn) && (itemNBT == null || NBTUtil.areNBTEquals(itemNBT, itemstack.getTagCompound(), true)))
  246. {
  247. int k = removeCount <= 0 ? itemstack.getCount() : Math.min(removeCount - i, itemstack.getCount());
  248. i += k;
  249.  
  250. if (removeCount != 0)
  251. {
  252. itemstack.shrink(k);
  253.  
  254. if (itemstack.isEmpty())
  255. {
  256. this.setInventorySlotContents(j, ItemStack.EMPTY);
  257. }
  258.  
  259. if (removeCount > 0 && i >= removeCount)
  260. {
  261. return i;
  262. }
  263. }
  264. }
  265. }
  266.  
  267. if (!this.itemStack.isEmpty())
  268. {
  269. if (itemIn != null && this.itemStack.getItem() != itemIn)
  270. {
  271. return i;
  272. }
  273.  
  274. if (metadataIn > -1 && this.itemStack.getMetadata() != metadataIn)
  275. {
  276. return i;
  277. }
  278.  
  279. if (itemNBT != null && !NBTUtil.areNBTEquals(itemNBT, this.itemStack.getTagCompound(), true))
  280. {
  281. return i;
  282. }
  283.  
  284. int l = removeCount <= 0 ? this.itemStack.getCount() : Math.min(removeCount - i, this.itemStack.getCount());
  285. i += l;
  286.  
  287. if (removeCount != 0)
  288. {
  289. this.itemStack.shrink(l);
  290.  
  291. if (this.itemStack.isEmpty())
  292. {
  293. this.itemStack = ItemStack.EMPTY;
  294. }
  295.  
  296. if (removeCount > 0 && i >= removeCount)
  297. {
  298. return i;
  299. }
  300. }
  301. }
  302.  
  303. return i;
  304. }
  305.  
  306. /**
  307. * This function stores as many items of an ItemStack as possible in a matching slot and returns the quantity of
  308. * left over items.
  309. */
  310. private int storePartialItemStack(ItemStack itemStackIn)
  311. {
  312. int i = this.storeItemStack(itemStackIn);
  313.  
  314. if (i == -1)
  315. {
  316. i = this.getFirstEmptyStack();
  317. }
  318.  
  319. return i == -1 ? itemStackIn.getCount() : this.addResource(i, itemStackIn);
  320. }
  321.  
  322. private int addResource(int p_191973_1_, ItemStack p_191973_2_)
  323. {
  324. Item item = p_191973_2_.getItem();
  325. int i = p_191973_2_.getCount();
  326. ItemStack itemstack = this.getStackInSlot(p_191973_1_);
  327.  
  328. if (itemstack.isEmpty())
  329. {
  330. itemstack = p_191973_2_.copy(); // Forge: Replace Item clone above to preserve item capabilities when picking the item up.
  331. itemstack.setCount(0);
  332.  
  333. if (p_191973_2_.hasTagCompound())
  334. {
  335. itemstack.setTagCompound(p_191973_2_.getTagCompound().copy());
  336. }
  337.  
  338. this.setInventorySlotContents(p_191973_1_, itemstack);
  339. }
  340.  
  341. int j = i;
  342.  
  343. if (i > itemstack.getMaxStackSize() - itemstack.getCount())
  344. {
  345. j = itemstack.getMaxStackSize() - itemstack.getCount();
  346. }
  347.  
  348. if (j > this.getInventoryStackLimit() - itemstack.getCount())
  349. {
  350. j = this.getInventoryStackLimit() - itemstack.getCount();
  351. }
  352.  
  353. if (j == 0)
  354. {
  355. return i;
  356. }
  357. else
  358. {
  359. i = i - j;
  360. itemstack.grow(j);
  361. itemstack.setAnimationsToGo(5);
  362. return i;
  363. }
  364. }
  365.  
  366. /**
  367. * stores an itemstack in the users inventory
  368. */
  369. public int storeItemStack(ItemStack itemStackIn)
  370. {
  371. if (this.canMergeStacks(this.getStackInSlot(this.currentItem), itemStackIn))
  372. {
  373. return this.currentItem;
  374. }
  375. else if (this.canMergeStacks(this.getStackInSlot(40), itemStackIn))
  376. {
  377. return 40;
  378. }
  379. else
  380. {
  381. for (int i = 0; i < this.mainInventory.size(); ++i)
  382. {
  383. if (this.canMergeStacks(this.mainInventory.get(i), itemStackIn))
  384. {
  385. return i;
  386. }
  387. }
  388.  
  389. return -1;
  390. }
  391. }
  392.  
  393. /**
  394. * Decrement the number of animations remaining. Only called on client side. This is used to handle the animation of
  395. * receiving a block.
  396. */
  397. public void decrementAnimations()
  398. {
  399. for (NonNullList<ItemStack> nonnulllist : this.allInventories)
  400. {
  401. for (int i = 0; i < nonnulllist.size(); ++i)
  402. {
  403. if (!((ItemStack)nonnulllist.get(i)).isEmpty())
  404. {
  405. ((ItemStack)nonnulllist.get(i)).updateAnimation(this.player.world, this.player, i, this.currentItem == i);
  406. }
  407. }
  408. }
  409. for (ItemStack is : armorInventory) // FORGE: Tick armor on animation ticks
  410. {
  411. if (!is.isEmpty())
  412. {
  413. is.getItem().onArmorTick(player.world, player, is);
  414. }
  415. }
  416. }
  417.  
  418. /**
  419. * Adds the item stack to the inventory, returns false if it is impossible.
  420. */
  421. public boolean addItemStackToInventory(ItemStack itemStackIn)
  422. {
  423. return this.add(-1, itemStackIn);
  424. }
  425.  
  426. public boolean add(int p_191971_1_, final ItemStack p_191971_2_)
  427. {
  428. if (p_191971_2_.isEmpty())
  429. {
  430. return false;
  431. }
  432. else
  433. {
  434. try
  435. {
  436. if (p_191971_2_.isItemDamaged())
  437. {
  438. if (p_191971_1_ == -1)
  439. {
  440. p_191971_1_ = this.getFirstEmptyStack();
  441. }
  442.  
  443. if (p_191971_1_ >= 0)
  444. {
  445. this.mainInventory.set(p_191971_1_, p_191971_2_.copy());
  446. ((ItemStack)this.mainInventory.get(p_191971_1_)).setAnimationsToGo(5);
  447. p_191971_2_.setCount(0);
  448. return true;
  449. }
  450. else if (this.player.capabilities.isCreativeMode)
  451. {
  452. p_191971_2_.setCount(0);
  453. return true;
  454. }
  455. else
  456. {
  457. return false;
  458. }
  459. }
  460. else
  461. {
  462. int i;
  463.  
  464. while (true)
  465. {
  466. i = p_191971_2_.getCount();
  467.  
  468. if (p_191971_1_ == -1)
  469. {
  470. p_191971_2_.setCount(this.storePartialItemStack(p_191971_2_));
  471. }
  472. else
  473. {
  474. p_191971_2_.setCount(this.addResource(p_191971_1_, p_191971_2_));
  475. }
  476.  
  477. if (p_191971_2_.isEmpty() || p_191971_2_.getCount() >= i)
  478. {
  479. break;
  480. }
  481. }
  482.  
  483. if (p_191971_2_.getCount() == i && this.player.capabilities.isCreativeMode)
  484. {
  485. p_191971_2_.setCount(0);
  486. return true;
  487. }
  488. else
  489. {
  490. return p_191971_2_.getCount() < i;
  491. }
  492. }
  493. }
  494. catch (Throwable throwable)
  495. {
  496. CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Adding item to inventory");
  497. CrashReportCategory crashreportcategory = crashreport.makeCategory("Item being added");
  498. crashreportcategory.addCrashSection("Item ID", Integer.valueOf(Item.getIdFromItem(p_191971_2_.getItem())));
  499. crashreportcategory.addCrashSection("Item data", Integer.valueOf(p_191971_2_.getMetadata()));
  500. crashreportcategory.addDetail("Registry Name", () -> String.valueOf(p_191971_2_.getItem().getRegistryName()));
  501. crashreportcategory.addDetail("Item Class", () -> p_191971_2_.getItem().getClass().getName());
  502. crashreportcategory.addDetail("Item name", new ICrashReportDetail<String>()
  503. {
  504. public String call() throws Exception
  505. {
  506. return p_191971_2_.getDisplayName();
  507. }
  508. });
  509. throw new ReportedException(crashreport);
  510. }
  511. }
  512. }
  513.  
  514. public void placeItemBackInInventory(World p_191975_1_, ItemStack p_191975_2_)
  515. {
  516. if (!p_191975_1_.isRemote)
  517. {
  518. while (!p_191975_2_.isEmpty())
  519. {
  520. int i = this.storeItemStack(p_191975_2_);
  521.  
  522. if (i == -1)
  523. {
  524. i = this.getFirstEmptyStack();
  525. }
  526.  
  527. if (i == -1)
  528. {
  529. this.player.dropItem(p_191975_2_, false);
  530. break;
  531. }
  532.  
  533. int j = p_191975_2_.getMaxStackSize() - this.getStackInSlot(i).getCount();
  534.  
  535. if (this.add(i, p_191975_2_.splitStack(j)))
  536. {
  537. ((EntityPlayerMP)this.player).connection.sendPacket(new SPacketSetSlot(-2, i, this.getStackInSlot(i)));
  538. }
  539. }
  540. }
  541. }
  542.  
  543. /**
  544. * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
  545. */
  546. public ItemStack decrStackSize(int index, int count)
  547. {
  548. List<ItemStack> list = null;
  549.  
  550. for (NonNullList<ItemStack> nonnulllist : this.allInventories)
  551. {
  552. if (index < nonnulllist.size())
  553. {
  554. list = nonnulllist;
  555. break;
  556. }
  557.  
  558. index -= nonnulllist.size();
  559. }
  560.  
  561. return list != null && !((ItemStack)list.get(index)).isEmpty() ? ItemStackHelper.getAndSplit(list, index, count) : ItemStack.EMPTY;
  562. }
  563.  
  564. public void deleteStack(ItemStack stack)
  565. {
  566. for (NonNullList<ItemStack> nonnulllist : this.allInventories)
  567. {
  568. for (int i = 0; i < nonnulllist.size(); ++i)
  569. {
  570. if (nonnulllist.get(i) == stack)
  571. {
  572. nonnulllist.set(i, ItemStack.EMPTY);
  573. break;
  574. }
  575. }
  576. }
  577. }
  578.  
  579. /**
  580. * Removes a stack from the given slot and returns it.
  581. */
  582. public ItemStack removeStackFromSlot(int index)
  583. {
  584. NonNullList<ItemStack> nonnulllist = null;
  585.  
  586. for (NonNullList<ItemStack> nonnulllist1 : this.allInventories)
  587. {
  588. if (index < nonnulllist1.size())
  589. {
  590. nonnulllist = nonnulllist1;
  591. break;
  592. }
  593.  
  594. index -= nonnulllist1.size();
  595. }
  596.  
  597. if (nonnulllist != null && !((ItemStack)nonnulllist.get(index)).isEmpty())
  598. {
  599. ItemStack itemstack = nonnulllist.get(index);
  600. nonnulllist.set(index, ItemStack.EMPTY);
  601. return itemstack;
  602. }
  603. else
  604. {
  605. return ItemStack.EMPTY;
  606. }
  607. }
  608.  
  609. /**
  610. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  611. */
  612. public void setInventorySlotContents(int index, ItemStack stack)
  613. {
  614. NonNullList<ItemStack> nonnulllist = null;
  615.  
  616. for (NonNullList<ItemStack> nonnulllist1 : this.allInventories)
  617. {
  618. if (index < nonnulllist1.size())
  619. {
  620. nonnulllist = nonnulllist1;
  621. break;
  622. }
  623.  
  624. index -= nonnulllist1.size();
  625. }
  626.  
  627. if (nonnulllist != null)
  628. {
  629. nonnulllist.set(index, stack);
  630. }
  631. }
  632.  
  633. public float getDestroySpeed(IBlockState state)
  634. {
  635. float f = 1.0F;
  636.  
  637. if (!((ItemStack)this.mainInventory.get(this.currentItem)).isEmpty())
  638. {
  639. f *= ((ItemStack)this.mainInventory.get(this.currentItem)).getDestroySpeed(state);
  640. }
  641.  
  642. return f;
  643. }
  644.  
  645. /**
  646. * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80
  647. * for crafting).
  648. */
  649. public NBTTagList writeToNBT(NBTTagList nbtTagListIn)
  650. {
  651. for (int i = 0; i < this.mainInventory.size(); ++i)
  652. {
  653. if (!((ItemStack)this.mainInventory.get(i)).isEmpty())
  654. {
  655. NBTTagCompound nbttagcompound = new NBTTagCompound();
  656. nbttagcompound.setByte("Slot", (byte)i);
  657. ((ItemStack)this.mainInventory.get(i)).writeToNBT(nbttagcompound);
  658. nbtTagListIn.appendTag(nbttagcompound);
  659. }
  660. }
  661.  
  662. for (int j = 0; j < this.armorInventory.size(); ++j)
  663. {
  664. if (!((ItemStack)this.armorInventory.get(j)).isEmpty())
  665. {
  666. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  667. nbttagcompound1.setByte("Slot", (byte)(j + 100));
  668. ((ItemStack)this.armorInventory.get(j)).writeToNBT(nbttagcompound1);
  669. nbtTagListIn.appendTag(nbttagcompound1);
  670. }
  671. }
  672.  
  673. for (int k = 0; k < this.offHandInventory.size(); ++k)
  674. {
  675. if (!((ItemStack)this.offHandInventory.get(k)).isEmpty())
  676. {
  677. NBTTagCompound nbttagcompound2 = new NBTTagCompound();
  678. nbttagcompound2.setByte("Slot", (byte)(k + 150));
  679. ((ItemStack)this.offHandInventory.get(k)).writeToNBT(nbttagcompound2);
  680. nbtTagListIn.appendTag(nbttagcompound2);
  681. }
  682. }
  683.  
  684. return nbtTagListIn;
  685. }
  686.  
  687. /**
  688. * Reads from the given tag list and fills the slots in the inventory with the correct items.
  689. */
  690. public void readFromNBT(NBTTagList nbtTagListIn)
  691. {
  692. this.mainInventory.clear();
  693. this.armorInventory.clear();
  694. this.offHandInventory.clear();
  695.  
  696. for (int i = 0; i < nbtTagListIn.tagCount(); ++i)
  697. {
  698. NBTTagCompound nbttagcompound = nbtTagListIn.getCompoundTagAt(i);
  699. int j = nbttagcompound.getByte("Slot") & 255;
  700. ItemStack itemstack = new ItemStack(nbttagcompound);
  701.  
  702. if (!itemstack.isEmpty())
  703. {
  704. if (j >= 0 && j < this.mainInventory.size())
  705. {
  706. this.mainInventory.set(j, itemstack);
  707. }
  708. else if (j >= 100 && j < this.armorInventory.size() + 100)
  709. {
  710. this.armorInventory.set(j - 100, itemstack);
  711. }
  712. else if (j >= 150 && j < this.offHandInventory.size() + 150)
  713. {
  714. this.offHandInventory.set(j - 150, itemstack);
  715. }
  716. }
  717. }
  718. }
  719.  
  720. /**
  721. * Returns the number of slots in the inventory.
  722. */
  723. public int getSizeInventory()
  724. {
  725. return this.mainInventory.size() + this.armorInventory.size() + this.offHandInventory.size();
  726. }
  727.  
  728. public boolean isEmpty()
  729. {
  730. for (ItemStack itemstack : this.mainInventory)
  731. {
  732. if (!itemstack.isEmpty())
  733. {
  734. return false;
  735. }
  736. }
  737.  
  738. for (ItemStack itemstack1 : this.armorInventory)
  739. {
  740. if (!itemstack1.isEmpty())
  741. {
  742. return false;
  743. }
  744. }
  745.  
  746. for (ItemStack itemstack2 : this.offHandInventory)
  747. {
  748. if (!itemstack2.isEmpty())
  749. {
  750. return false;
  751. }
  752. }
  753.  
  754. return true;
  755. }
  756.  
  757. /**
  758. * Returns the stack in the given slot.
  759. */
  760. public ItemStack getStackInSlot(int index)
  761. {
  762. List<ItemStack> list = null;
  763.  
  764. for (NonNullList<ItemStack> nonnulllist : this.allInventories)
  765. {
  766. if (index < nonnulllist.size())
  767. {
  768. list = nonnulllist;
  769. break;
  770. }
  771.  
  772. index -= nonnulllist.size();
  773. }
  774.  
  775. return list == null ? ItemStack.EMPTY : (ItemStack)list.get(index);
  776. }
  777.  
  778. /**
  779. * Get the name of this object. For players this returns their username
  780. */
  781. public String getName()
  782. {
  783. return "container.inventory";
  784. }
  785.  
  786. /**
  787. * Returns true if this thing is named
  788. */
  789. public boolean hasCustomName()
  790. {
  791. return false;
  792. }
  793.  
  794. /**
  795. * Get the formatted ChatComponent that will be used for the sender's username in chat
  796. */
  797. public ITextComponent getDisplayName()
  798. {
  799. return (ITextComponent)(this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0]));
  800. }
  801.  
  802. /**
  803. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  804. */
  805. public int getInventoryStackLimit()
  806. {
  807. return 64;
  808. }
  809.  
  810. public boolean canHarvestBlock(IBlockState state)
  811. {
  812. if (state.getMaterial().isToolNotRequired())
  813. {
  814. return true;
  815. }
  816. else
  817. {
  818. ItemStack itemstack = this.getStackInSlot(this.currentItem);
  819. return !itemstack.isEmpty() ? itemstack.canHarvestBlock(state) : false;
  820. }
  821. }
  822.  
  823. /**
  824. * returns a player armor item (as itemstack) contained in specified armor slot.
  825. */
  826. @SideOnly(Side.CLIENT)
  827. public ItemStack armorItemInSlot(int slotIn)
  828. {
  829. return this.armorInventory.get(slotIn);
  830. }
  831.  
  832. /**
  833. * Damages armor in each slot by the specified amount.
  834. */
  835. public void damageArmor(float damage)
  836. {
  837. damage = damage / 4.0F;
  838.  
  839. if (damage < 1.0F)
  840. {
  841. damage = 1.0F;
  842. }
  843.  
  844. for (int i = 0; i < this.armorInventory.size(); ++i)
  845. {
  846. ItemStack itemstack = this.armorInventory.get(i);
  847.  
  848. if (itemstack.getItem() instanceof ItemArmor)
  849. {
  850. itemstack.damageItem((int)damage, this.player);
  851. }
  852. }
  853. }
  854.  
  855. /**
  856. * Drop all armor and main inventory items.
  857. */
  858. public void dropAllItems()
  859. {
  860. for (List<ItemStack> list : this.allInventories)
  861. {
  862. for (int i = 0; i < list.size(); ++i)
  863. {
  864. ItemStack itemstack = list.get(i);
  865.  
  866. if (!itemstack.isEmpty())
  867. {
  868. this.player.dropItem(itemstack, true, false);
  869. list.set(i, ItemStack.EMPTY);
  870. }
  871. }
  872. }
  873. }
  874.  
  875. /**
  876. * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
  877. * hasn't changed and skip it.
  878. */
  879. public void markDirty()
  880. {
  881. ++this.timesChanged;
  882. }
  883.  
  884. @SideOnly(Side.CLIENT)
  885. public int getTimesChanged()
  886. {
  887. return this.timesChanged;
  888. }
  889.  
  890. /**
  891. * Set the stack helds by mouse, used in GUI/Container
  892. */
  893. public void setItemStack(ItemStack itemStackIn)
  894. {
  895. this.itemStack = itemStackIn;
  896. }
  897.  
  898. /**
  899. * Stack helds by mouse, used in GUI and Containers
  900. */
  901. public ItemStack getItemStack()
  902. {
  903. return this.itemStack;
  904. }
  905.  
  906. /**
  907. * Don't rename this method to canInteractWith due to conflicts with Container
  908. */
  909. public boolean isUsableByPlayer(EntityPlayer player)
  910. {
  911. if (this.player.isDead)
  912. {
  913. return false;
  914. }
  915. else
  916. {
  917. return player.getDistanceSq(this.player) <= 64.0D;
  918. }
  919. }
  920.  
  921. /**
  922. * Returns true if the specified ItemStack exists in the inventory.
  923. */
  924. public boolean hasItemStack(ItemStack itemStackIn)
  925. {
  926. label23:
  927.  
  928. for (List<ItemStack> list : this.allInventories)
  929. {
  930. Iterator iterator = list.iterator();
  931.  
  932. while (true)
  933. {
  934. if (!iterator.hasNext())
  935. {
  936. continue label23;
  937. }
  938.  
  939. ItemStack itemstack = (ItemStack)iterator.next();
  940.  
  941. if (!itemstack.isEmpty() && itemstack.isItemEqual(itemStackIn))
  942. {
  943. break;
  944. }
  945. }
  946.  
  947. return true;
  948. }
  949.  
  950. return false;
  951. }
  952.  
  953. public void openInventory(EntityPlayer player)
  954. {
  955. }
  956.  
  957. public void closeInventory(EntityPlayer player)
  958. {
  959. }
  960.  
  961. /**
  962. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. For
  963. * guis use Slot.isItemValid
  964. */
  965. public boolean isItemValidForSlot(int index, ItemStack stack)
  966. {
  967. return true;
  968. }
  969.  
  970. /**
  971. * Copy the ItemStack contents from another InventoryPlayer instance
  972. */
  973. public void copyInventory(InventoryPlayer playerInventory)
  974. {
  975. for (int i = 0; i < this.getSizeInventory(); ++i)
  976. {
  977. this.setInventorySlotContents(i, playerInventory.getStackInSlot(i));
  978. }
  979.  
  980. this.currentItem = playerInventory.currentItem;
  981. }
  982.  
  983. public int getField(int id)
  984. {
  985. return 0;
  986. }
  987.  
  988. public void setField(int id, int value)
  989. {
  990. }
  991.  
  992. public int getFieldCount()
  993. {
  994. return 0;
  995. }
  996.  
  997. public void clear()
  998. {
  999. for (List<ItemStack> list : this.allInventories)
  1000. {
  1001. list.clear();
  1002. }
  1003. }
  1004.  
  1005. public void fillStackedContents(RecipeItemHelper helper, boolean p_194016_2_)
  1006. {
  1007. for (ItemStack itemstack : this.mainInventory)
  1008. {
  1009. helper.accountStack(itemstack);
  1010. }
  1011.  
  1012. if (p_194016_2_)
  1013. {
  1014. helper.accountStack(this.offHandInventory.get(0));
  1015. }
  1016. }
  1017. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement