CustomContainer.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.startOpen(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,
  50. Script sc) {
  51. // taken from ServerPlayerEntity.openContainer
  52. if(p.isSpectator()) {
  53. p.displayClientMessage((new TranslationTextComponent("container.spectatorCantOpen"))
  54. .withStyle(TextFormatting.RED), true);
  55. return;
  56. }
  57. if(p.containerMenu != p.inventoryMenu) {
  58. p.closeContainer();
  59. }
  60. p.nextContainerCounter();
  61. Container container =
  62. new ServerCustomContainer(p.containerCounter, p.inventory, inv, title, sc);
  63. ModPacketHandler.sendCustomInventory(p, p.containerCounter, title, inv);
  64. container.addSlotListener(p);
  65. p.containerMenu = container;
  66. }
  67. @Override
  68. public boolean stillValid(PlayerEntity p) {
  69. return this.inv.stillValid(p);
  70. }
  71. @Override
  72. public ItemStack quickMoveStack(PlayerEntity p, int index) {
  73. // taken from ChestContainer, changed size
  74. ItemStack stack = ItemStack.EMPTY;
  75. Slot slot = this.slots.get(index);
  76. int size = inv.getContainerSize();
  77. if(slot != null && slot.hasItem()) {
  78. ItemStack slotStack = slot.getItem();
  79. stack = slotStack.copy();
  80. if(index < size) {
  81. if(!this.moveItemStackTo(slotStack, size, this.slots.size(), true)) {
  82. return ItemStack.EMPTY;
  83. }
  84. } else if(!this.moveItemStackTo(slotStack, 0, size, false)) {
  85. return ItemStack.EMPTY;
  86. }
  87. if(slotStack.isEmpty()) {
  88. slot.set(ItemStack.EMPTY);
  89. } else {
  90. slot.setChanged();
  91. }
  92. }
  93. return stack;
  94. }
  95. @Override
  96. public boolean canTakeItemForPickAll(ItemStack stack, Slot slot) {
  97. return inv.getSlotStatus(slot.index) == 1;
  98. }
  99. @Override
  100. public boolean canDragTo(Slot slot) {
  101. return inv.getSlotStatus(slot.index) == 1;
  102. }
  103. @Override
  104. protected boolean moveItemStackTo(ItemStack stack, int startIndex, int endIndex,
  105. boolean reverseDirection) {
  106. boolean flag = false;
  107. int i = startIndex;
  108. if(reverseDirection) {
  109. i = endIndex - 1;
  110. }
  111. if(stack.isStackable()) {
  112. while(!stack.isEmpty()) {
  113. if(reverseDirection) {
  114. if(i < startIndex) {
  115. break;
  116. }
  117. } else if(i >= endIndex) {
  118. break;
  119. }
  120. if(inv.getSlotStatus(i) == 1) {
  121. Slot slot = this.slots.get(i);
  122. ItemStack itemstack = slot.getItem();
  123. if(!itemstack.isEmpty() && consideredTheSameItem(stack, itemstack)) {
  124. int j = itemstack.getCount() + stack.getCount();
  125. int maxSize = Math.min(slot.getMaxStackSize(), stack.getMaxStackSize());
  126. if(j <= maxSize) {
  127. stack.setCount(0);
  128. itemstack.setCount(j);
  129. slot.setChanged();
  130. flag = true;
  131. } else if(itemstack.getCount() < maxSize) {
  132. stack.shrink(maxSize - itemstack.getCount());
  133. itemstack.setCount(maxSize);
  134. slot.setChanged();
  135. flag = true;
  136. }
  137. }
  138. }
  139. if(reverseDirection) {
  140. --i;
  141. } else {
  142. ++i;
  143. }
  144. }
  145. }
  146. if(!stack.isEmpty()) {
  147. if(reverseDirection) {
  148. i = endIndex - 1;
  149. } else {
  150. i = startIndex;
  151. }
  152. while(true) {
  153. if(reverseDirection) {
  154. if(i < startIndex) {
  155. break;
  156. }
  157. } else if(i >= endIndex) {
  158. break;
  159. }
  160. if(inv.getSlotStatus(i) == 1) {
  161. Slot slot1 = this.slots.get(i);
  162. ItemStack itemstack1 = slot1.getItem();
  163. if(itemstack1.isEmpty() && slot1.mayPlace(stack)) {
  164. if(stack.getCount() > slot1.getMaxStackSize()) {
  165. slot1.set(stack.split(slot1.getMaxStackSize()));
  166. } else {
  167. slot1.set(stack.split(stack.getCount()));
  168. }
  169. slot1.setChanged();
  170. flag = true;
  171. break;
  172. }
  173. }
  174. if(reverseDirection) {
  175. --i;
  176. } else {
  177. ++i;
  178. }
  179. }
  180. }
  181. return flag;
  182. }
  183. @Override
  184. public final ItemStack clicked(int slotId, int dragType, ClickType clickTypeIn,
  185. PlayerEntity p) {
  186. if(slotId < 0 || slotId >= inv.getContainerSize()) {
  187. return super.clicked(slotId, dragType, clickTypeIn, p);
  188. }
  189. switch(inv.getSlotStatus(slotId)) {
  190. case 0:
  191. return ItemStack.EMPTY;
  192. case 1:
  193. return super.clicked(slotId, dragType, clickTypeIn, p);
  194. case 2:
  195. case 3:
  196. onButtonClick(slotId, dragType, clickTypeIn, p);
  197. return ItemStack.EMPTY;
  198. }
  199. return ItemStack.EMPTY;
  200. }
  201. }