Guest User

Untitled

a guest
Jul 29th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package com.mod.drakania.inventory;
  2.  
  3. import com.mod.drakania.items.ItemBackPack;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.Slot;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.nbt.NBTTagCompound;
  11.  
  12.  
  13. /**
  14. * @author SCAREX
  15. *
  16. */
  17. public class ContainerBackPack extends Container
  18. {
  19. public InventoryBackPack invBackpack;
  20. public int rows;
  21.  
  22. public ContainerBackPack(InventoryPlayer playerInv, InventoryBackPack inv) {
  23. this.invBackpack = inv;
  24. this.rows = inv.getSizeInventory() / 9;
  25. int i = (this.rows - 4) * 18;
  26. int j;
  27. int k;
  28.  
  29. // Adding slots to the backpack
  30. for (j = 0; j < this.rows; ++j) {
  31. for (k = 0; k < 9; ++k) {
  32. this.addSlotToContainer(new SlotBackPack(inv, k + j * 9, 8 + k * 18, 18 + j * 18));
  33. }
  34. }
  35.  
  36. // Adding player's slots
  37. for (j = 0; j < 3; ++j) {
  38. for (k = 0; k < 9; ++k) {
  39. this.addSlotToContainer(new Slot(playerInv, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
  40. }
  41. }
  42.  
  43. for (j = 0; j < 9; ++j) {
  44. this.addSlotToContainer(new Slot(playerInv, j, 8 + j * 18, 161 + i));
  45. }
  46. }
  47.  
  48. @Override
  49. public boolean canInteractWith(EntityPlayer player) {
  50. return true;
  51. }
  52.  
  53. public void writeToNBT(ItemStack stack) {
  54. if (!stack.hasTagCompound()) stack.setTagCompound(new NBTTagCompound());
  55. invBackpack.writeToNBT(stack.getTagCompound());
  56. }
  57.  
  58. @Override
  59. public ItemStack transferStackInSlot(EntityPlayer player, int index) {
  60. ItemStack itemstack = null;
  61. Slot slot = (Slot) this.inventorySlots.get(index);
  62.  
  63. if (slot != null && slot.getHasStack()) {
  64. ItemStack itemstack1 = slot.getStack();
  65. itemstack = itemstack1.copy();
  66.  
  67. // Prevents backpack-ception (backpack inside backpack) with
  68. // shift-click
  69. if (itemstack.getItem() instanceof ItemBackPack) return null;
  70.  
  71. if (index < this.invBackpack.getSizeInventory()) {
  72. if (!this.mergeItemStack(itemstack1, this.invBackpack.getSizeInventory(), this.inventorySlots.size(), true)) return null;
  73. } else if (!this.mergeItemStack(itemstack1, 0, this.invBackpack.getSizeInventory(), false)) { return null; }
  74.  
  75. if (itemstack1.stackSize == 0)
  76. slot.putStack((ItemStack) null);
  77. else
  78. slot.onSlotChanged();
  79. }
  80.  
  81. return itemstack;
  82. }
  83.  
  84. /**
  85. * @param buttonPressed
  86. * left click, right click, wheel click, etc.
  87. * @param flag
  88. * category (e.g.: hotbar keys)
  89. */
  90. @Override
  91. public ItemStack slotClick(int slotIndex, int buttonPressed, int flag, EntityPlayer player) {
  92. // Prevents from removing current backpack
  93. if (flag == 2 && buttonPressed == player.inventory.currentItem) return null;
  94. if (slotIndex - this.invBackpack.getSizeInventory() - 27 == player.inventory.currentItem) return null;
  95. return super.slotClick(slotIndex, buttonPressed, flag, player);
  96. }
  97.  
  98. /**
  99. * Used to save content
  100. */
  101. @Override
  102. public void onContainerClosed(EntityPlayer player) {
  103. this.writeToNBT(player.getHeldItem());
  104. super.onContainerClosed(player);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment