CustomContainer.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package me.km.inventory;
  2. import me.hammerle.snuviscript.code.Script;
  3. import me.km.networking.ModPacketHandler;
  4. import net.minecraft.entity.player.PlayerEntity;
  5. import net.minecraft.entity.player.ServerPlayerEntity;
  6. import net.minecraft.entity.player.PlayerInventory;
  7. import net.minecraft.inventory.container.ClickType;
  8. import net.minecraft.inventory.container.Container;
  9. import net.minecraft.inventory.container.Slot;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.util.text.TextFormatting;
  12. import net.minecraft.util.text.TranslationTextComponent;
  13. public class CustomContainer extends Container {
  14. private final ModInventory inv;
  15. private final int numRows;
  16. public CustomContainer(int id, PlayerInventory pInv, ModInventory inv) {
  17. super(null, id);
  18. // basic stuff
  19. this.inv = inv;
  20. this.numRows = inv.getRows();
  21. inv.openInventory(pInv.player);
  22. int i = (this.numRows - 4) * 18;
  23. // inventory slots
  24. int counter = 0;
  25. for(int y = 0; y < this.numRows; y++) {
  26. for(int x = 0; x < 9; x++) {
  27. if(inv.isSlotValid(x, y)) {
  28. addSlot(new Slot(inv, counter, 8 + x * 18, 18 + y * 18));
  29. counter++;
  30. }
  31. }
  32. }
  33. // plaver inventory slots
  34. for(int y = 0; y < 3; y++) {
  35. for(int x = 0; x < 9; x++) {
  36. addSlot(new Slot(pInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + i));
  37. }
  38. }
  39. for(int x = 0; x < 9; x++) {
  40. addSlot(new Slot(pInv, x, 8 + x * 18, 161 + i));
  41. }
  42. }
  43. public ModInventory getInventoryBase() {
  44. return inv;
  45. }
  46. public boolean onButtonClick(int slot, int dragType, ClickType click, PlayerEntity p) {
  47. return false;
  48. }
  49. public static void openForPlayer(ServerPlayerEntity p, ModInventory inv, String title, Script sc) {
  50. // taken from ServerPlayerEntity.openContainer
  51. if(p.isSpectator()) {
  52. p.sendStatusMessage((new TranslationTextComponent("container.spectatorCantOpen")).mergeStyle(TextFormatting.RED), true);
  53. return;
  54. }
  55. if(p.openContainer != p.container) {
  56. p.closeScreen();
  57. }
  58. p.getNextWindowId();
  59. Container container = new ServerCustomContainer(p.currentWindowId, p.inventory, inv, title, sc);
  60. ModPacketHandler.sendCustomInventory(p, p.currentWindowId, title, inv);
  61. container.addListener(p);
  62. p.openContainer = container;
  63. }
  64. @Override
  65. public boolean canInteractWith(PlayerEntity p) {
  66. return this.inv.isUsableByPlayer(p);
  67. }
  68. @Override
  69. public ItemStack transferStackInSlot(PlayerEntity p, int index) {
  70. // taken from ChestContainer, changed size
  71. ItemStack stack = ItemStack.EMPTY;
  72. Slot slot = this.inventorySlots.get(index);
  73. int size = inv.getSizeInventory();
  74. if(slot != null && slot.getHasStack()) {
  75. ItemStack slotStack = slot.getStack();
  76. stack = slotStack.copy();
  77. if(index < size) {
  78. if(!this.mergeItemStack(slotStack, size, this.inventorySlots.size(), true)) {
  79. return ItemStack.EMPTY;
  80. }
  81. } else if(!this.mergeItemStack(slotStack, 0, size, false)) {
  82. return ItemStack.EMPTY;
  83. }
  84. if(slotStack.isEmpty()) {
  85. slot.putStack(ItemStack.EMPTY);
  86. } else {
  87. slot.onSlotChanged();
  88. }
  89. }
  90. return stack;
  91. }
  92. @Override
  93. public boolean canMergeSlot(ItemStack stack, Slot slot) {
  94. return inv.getSlotStatus(slot.slotNumber) == 1;
  95. }
  96. @Override
  97. public boolean canDragIntoSlot(Slot slot) {
  98. return inv.getSlotStatus(slot.slotNumber) == 1;
  99. }
  100. @Override
  101. protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection) {
  102. boolean flag = false;
  103. int i = startIndex;
  104. if(reverseDirection) {
  105. i = endIndex - 1;
  106. }
  107. if(stack.isStackable()) {
  108. while(!stack.isEmpty()) {
  109. if(reverseDirection) {
  110. if(i < startIndex) {
  111. break;
  112. }
  113. } else if(i >= endIndex) {
  114. break;
  115. }
  116. if(inv.getSlotStatus(i) == 1) {
  117. Slot slot = this.inventorySlots.get(i);
  118. ItemStack itemstack = slot.getStack();
  119. if(!itemstack.isEmpty() && areItemsAndTagsEqual(stack, itemstack)) {
  120. int j = itemstack.getCount() + stack.getCount();
  121. int maxSize = Math.min(slot.getSlotStackLimit(), stack.getMaxStackSize());
  122. if(j <= maxSize) {
  123. stack.setCount(0);
  124. itemstack.setCount(j);
  125. slot.onSlotChanged();
  126. flag = true;
  127. } else if(itemstack.getCount() < maxSize) {
  128. stack.shrink(maxSize - itemstack.getCount());
  129. itemstack.setCount(maxSize);
  130. slot.onSlotChanged();
  131. flag = true;
  132. }
  133. }
  134. }
  135. if(reverseDirection) {
  136. --i;
  137. } else {
  138. ++i;
  139. }
  140. }
  141. }
  142. if(!stack.isEmpty()) {
  143. if(reverseDirection) {
  144. i = endIndex - 1;
  145. } else {
  146. i = startIndex;
  147. }
  148. while(true) {
  149. if(reverseDirection) {
  150. if(i < startIndex) {
  151. break;
  152. }
  153. } else if(i >= endIndex) {
  154. break;
  155. }
  156. if(inv.getSlotStatus(i) == 1) {
  157. Slot slot1 = this.inventorySlots.get(i);
  158. ItemStack itemstack1 = slot1.getStack();
  159. if(itemstack1.isEmpty() && slot1.isItemValid(stack)) {
  160. if(stack.getCount() > slot1.getSlotStackLimit()) {
  161. slot1.putStack(stack.split(slot1.getSlotStackLimit()));
  162. } else {
  163. slot1.putStack(stack.split(stack.getCount()));
  164. }
  165. slot1.onSlotChanged();
  166. flag = true;
  167. break;
  168. }
  169. }
  170. if(reverseDirection) {
  171. --i;
  172. } else {
  173. ++i;
  174. }
  175. }
  176. }
  177. return flag;
  178. }
  179. @Override
  180. public final ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity p) {
  181. if(slotId < 0 || slotId >= inv.getSizeInventory()) {
  182. return super.slotClick(slotId, dragType, clickTypeIn, p);
  183. }
  184. switch(inv.getSlotStatus(slotId)) {
  185. case 0:
  186. return ItemStack.EMPTY;
  187. case 1:
  188. return super.slotClick(slotId, dragType, clickTypeIn, p);
  189. case 2:
  190. case 3:
  191. onButtonClick(slotId, dragType, clickTypeIn, p);
  192. return ItemStack.EMPTY;
  193. }
  194. return ItemStack.EMPTY;
  195. }
  196. }