CustomContainerBase.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package me.km.inventory;
  2. import me.km.networking.ModPacketHandler;
  3. import net.minecraft.advancements.CriteriaTriggers;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.EntityPlayerMP;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.IContainerListener;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.Slot;
  11. import net.minecraft.inventory.SlotCrafting;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.network.play.server.SPacketSetSlot;
  14. import net.minecraft.network.play.server.SPacketWindowItems;
  15. import net.minecraft.network.play.server.SPacketWindowProperty;
  16. import net.minecraft.util.NonNullList;
  17. public abstract class CustomContainerBase extends Container
  18. {
  19. protected final EntityPlayer viewer;
  20. protected final boolean isServerSide;
  21. protected final InventoryBase inv;
  22. private final int numRows;
  23. protected CustomContainerBase(InventoryBase inv, EntityPlayer viewer)
  24. {
  25. // basic stuff
  26. this.viewer = viewer;
  27. this.isServerSide = viewer instanceof EntityPlayerMP;
  28. this.inv = inv;
  29. this.numRows = inv.getRows();
  30. int i = (this.numRows - 4) * 18;
  31. // inventory slots
  32. int counter = 0;
  33. for(int y = 0; y < this.numRows; y++)
  34. {
  35. for(int x = 0; x < 9; x++)
  36. {
  37. if(inv.isSlotValid(x, y))
  38. {
  39. super.addSlotToContainer(new IdSlot(inv, counter, 8 + x * 18, 18 + y * 18, x + y * 9));
  40. counter++;
  41. }
  42. }
  43. }
  44. InventoryPlayer pInv = this.viewer.inventory;
  45. // plaver inventory slots
  46. for(int y = 0; y < 3; y++)
  47. {
  48. for(int x = 0; x < 9; x++)
  49. {
  50. super.addSlotToContainer(new Slot(pInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + i));
  51. }
  52. }
  53. for(int x = 0; x < 9; x++)
  54. {
  55. super.addSlotToContainer(new Slot(pInv, x, 8 + x * 18, 161 + i));
  56. }
  57. }
  58. public InventoryBase getInventoryBase()
  59. {
  60. return inv;
  61. }
  62. public void closeSafe()
  63. {
  64. viewer.closeScreen();
  65. }
  66. public void openForPlayer()
  67. {
  68. if(isServerSide)
  69. {
  70. EntityPlayerMP p = (EntityPlayerMP) viewer;
  71. if(p.openContainer != p.inventoryContainer)
  72. {
  73. p.closeScreen();
  74. }
  75. p.getNextWindowId();
  76. p.openContainer = this;
  77. p.openContainer.windowId = p.currentWindowId;
  78. ModPacketHandler.sendCustomInventory(p, windowId, inv);
  79. p.openContainer.addListener(new IContainerListener()
  80. {
  81. @Override
  82. public void sendAllContents(Container containerToSend, NonNullList<ItemStack> itemsList)
  83. {
  84. p.connection.sendPacket(new SPacketWindowItems(containerToSend.windowId, itemsList));
  85. p.connection.sendPacket(new SPacketSetSlot(-1, -1, p.inventory.getItemStack()));
  86. }
  87. @Override
  88. public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack)
  89. {
  90. if(!(containerToSend.getSlot(slotInd) instanceof SlotCrafting))
  91. {
  92. if(containerToSend == p.inventoryContainer)
  93. {
  94. CriteriaTriggers.INVENTORY_CHANGED.trigger(p, p.inventory);
  95. }
  96. if(!p.isChangingQuantityOnly)
  97. {
  98. p.connection.sendPacket(new SPacketSetSlot(containerToSend.windowId, slotInd, stack));
  99. }
  100. }
  101. }
  102. @Override
  103. public void sendWindowProperty(Container containerIn, int varToUpdate, int newValue)
  104. {
  105. }
  106. @Override
  107. public void sendAllWindowProperties(Container containerIn, IInventory inventory)
  108. {
  109. }
  110. });
  111. }
  112. }
  113. @Override
  114. public boolean canInteractWith(EntityPlayer p)
  115. {
  116. return this.inv.isUsableByPlayer(p);
  117. }
  118. @Override
  119. public ItemStack transferStackInSlot(EntityPlayer p, int index)
  120. {
  121. ItemStack stack = ItemStack.EMPTY;
  122. Slot slot = this.inventorySlots.get(index);
  123. int size = inv.getSizeInventory();
  124. if(slot != null && slot.getHasStack())
  125. {
  126. ItemStack slotStack = slot.getStack();
  127. stack = slotStack.copy();
  128. if(index < size)
  129. {
  130. if(!this.mergeItemStack(slotStack, size, this.inventorySlots.size(), true))
  131. {
  132. return ItemStack.EMPTY;
  133. }
  134. }
  135. else if(!this.mergeItemStack(slotStack, 0, size, false))
  136. {
  137. return ItemStack.EMPTY;
  138. }
  139. if(slotStack.isEmpty())
  140. {
  141. slot.putStack(ItemStack.EMPTY);
  142. }
  143. else
  144. {
  145. slot.onSlotChanged();
  146. }
  147. }
  148. return stack;
  149. }
  150. }