CustomContainer.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. {
  15. private final ModInventory inv;
  16. private final int numRows;
  17. public CustomContainer(int id, PlayerInventory pInv, ModInventory inv)
  18. {
  19. super(null, id);
  20. // basic stuff
  21. this.inv = inv;
  22. this.numRows = inv.getRows();
  23. inv.openInventory(pInv.player);
  24. int i = (this.numRows - 4) * 18;
  25. // inventory slots
  26. int counter = 0;
  27. for(int y = 0; y < this.numRows; y++)
  28. {
  29. for(int x = 0; x < 9; x++)
  30. {
  31. if(inv.isSlotValid(x, y))
  32. {
  33. addSlot(new Slot(inv, counter, 8 + x * 18, 18 + y * 18));
  34. counter++;
  35. }
  36. }
  37. }
  38. // plaver inventory slots
  39. for(int y = 0; y < 3; y++)
  40. {
  41. for(int x = 0; x < 9; x++)
  42. {
  43. addSlot(new Slot(pInv, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + i));
  44. }
  45. }
  46. for(int x = 0; x < 9; x++)
  47. {
  48. addSlot(new Slot(pInv, x, 8 + x * 18, 161 + i));
  49. }
  50. }
  51. public ModInventory getInventoryBase()
  52. {
  53. return inv;
  54. }
  55. public boolean onButtonClick(int slot, int dragType, ClickType click, PlayerEntity p)
  56. {
  57. return false;
  58. }
  59. public static void openForPlayer(ServerPlayerEntity p, ModInventory inv, String title, Script sc)
  60. {
  61. // taken from ServerPlayerEntity.openContainer
  62. if(p.isSpectator())
  63. {
  64. p.sendStatusMessage((new TranslationTextComponent("container.spectatorCantOpen")).applyTextStyle(TextFormatting.RED), true);
  65. return;
  66. }
  67. if(p.openContainer != p.container)
  68. {
  69. p.closeScreen();
  70. }
  71. p.getNextWindowId();
  72. Container container = new ServerCustomContainer(p.currentWindowId, p.inventory, inv, title, sc);
  73. ModPacketHandler.sendCustomInventory(p, p.currentWindowId, title, inv);
  74. container.addListener(p);
  75. p.openContainer = container;
  76. }
  77. @Override
  78. public boolean canInteractWith(PlayerEntity p)
  79. {
  80. return this.inv.isUsableByPlayer(p);
  81. }
  82. @Override
  83. public ItemStack transferStackInSlot(PlayerEntity p, int index)
  84. {
  85. // taken from ChestContainer, changed size
  86. ItemStack stack = ItemStack.EMPTY;
  87. Slot slot = this.inventorySlots.get(index);
  88. int size = inv.getSizeInventory();
  89. if(slot != null && slot.getHasStack())
  90. {
  91. ItemStack slotStack = slot.getStack();
  92. stack = slotStack.copy();
  93. if(index < size)
  94. {
  95. if(!this.mergeItemStack(slotStack, size, this.inventorySlots.size(), true))
  96. {
  97. return ItemStack.EMPTY;
  98. }
  99. }
  100. else if(!this.mergeItemStack(slotStack, 0, size, false))
  101. {
  102. return ItemStack.EMPTY;
  103. }
  104. if(slotStack.isEmpty())
  105. {
  106. slot.putStack(ItemStack.EMPTY);
  107. }
  108. else
  109. {
  110. slot.onSlotChanged();
  111. }
  112. }
  113. return stack;
  114. }
  115. @Override
  116. public boolean canMergeSlot(ItemStack stack, Slot slot)
  117. {
  118. return inv.getSlotStatus(slot.slotNumber) == 1;
  119. }
  120. @Override
  121. public boolean canDragIntoSlot(Slot slot)
  122. {
  123. return inv.getSlotStatus(slot.slotNumber) == 1;
  124. }
  125. @Override
  126. protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection)
  127. {
  128. boolean flag = false;
  129. int i = startIndex;
  130. if(reverseDirection)
  131. {
  132. i = endIndex - 1;
  133. }
  134. if(stack.isStackable())
  135. {
  136. while(!stack.isEmpty())
  137. {
  138. if(reverseDirection)
  139. {
  140. if(i < startIndex)
  141. {
  142. break;
  143. }
  144. }
  145. else if(i >= endIndex)
  146. {
  147. break;
  148. }
  149. if(inv.getSlotStatus(i) == 1)
  150. {
  151. Slot slot = this.inventorySlots.get(i);
  152. ItemStack itemstack = slot.getStack();
  153. if(!itemstack.isEmpty() && areItemsAndTagsEqual(stack, itemstack))
  154. {
  155. int j = itemstack.getCount() + stack.getCount();
  156. int maxSize = Math.min(slot.getSlotStackLimit(), stack.getMaxStackSize());
  157. if(j <= maxSize)
  158. {
  159. stack.setCount(0);
  160. itemstack.setCount(j);
  161. slot.onSlotChanged();
  162. flag = true;
  163. }
  164. else if(itemstack.getCount() < maxSize)
  165. {
  166. stack.shrink(maxSize - itemstack.getCount());
  167. itemstack.setCount(maxSize);
  168. slot.onSlotChanged();
  169. flag = true;
  170. }
  171. }
  172. }
  173. if(reverseDirection)
  174. {
  175. --i;
  176. }
  177. else
  178. {
  179. ++i;
  180. }
  181. }
  182. }
  183. if(!stack.isEmpty())
  184. {
  185. if(reverseDirection)
  186. {
  187. i = endIndex - 1;
  188. }
  189. else
  190. {
  191. i = startIndex;
  192. }
  193. while(true)
  194. {
  195. if(reverseDirection)
  196. {
  197. if(i < startIndex)
  198. {
  199. break;
  200. }
  201. }
  202. else if(i >= endIndex)
  203. {
  204. break;
  205. }
  206. if(inv.getSlotStatus(i) == 1)
  207. {
  208. Slot slot1 = this.inventorySlots.get(i);
  209. ItemStack itemstack1 = slot1.getStack();
  210. if(itemstack1.isEmpty() && slot1.isItemValid(stack))
  211. {
  212. if(stack.getCount() > slot1.getSlotStackLimit())
  213. {
  214. slot1.putStack(stack.split(slot1.getSlotStackLimit()));
  215. }
  216. else
  217. {
  218. slot1.putStack(stack.split(stack.getCount()));
  219. }
  220. slot1.onSlotChanged();
  221. flag = true;
  222. break;
  223. }
  224. }
  225. if(reverseDirection)
  226. {
  227. --i;
  228. }
  229. else
  230. {
  231. ++i;
  232. }
  233. }
  234. }
  235. return flag;
  236. }
  237. @Override
  238. public final ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity p)
  239. {
  240. if(slotId < 0 || slotId >= inv.getSizeInventory())
  241. {
  242. return super.slotClick(slotId, dragType, clickTypeIn, p);
  243. }
  244. switch(inv.getSlotStatus(slotId))
  245. {
  246. case 0: return ItemStack.EMPTY;
  247. case 1: return super.slotClick(slotId, dragType, clickTypeIn, p);
  248. case 2:
  249. case 3:
  250. onButtonClick(slotId, dragType, clickTypeIn, p);
  251. return ItemStack.EMPTY;
  252. }
  253. return ItemStack.EMPTY;
  254. }
  255. }