Guest User

Untitled

a guest
Apr 28th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. public class BoilerMenu extends AbstractContainerMenu {
  2.  
  3. public final BoilerBlockEntity blockEntity;
  4. private final Level level;
  5. private final ContainerData data;
  6. private FluidStack waterFluidStack;
  7. private FluidStack ppFluidStack;
  8.  
  9. public BoilerMenu(int id, Inventory inv, FriendlyByteBuf data) {
  10. this(id, inv, inv.player.level.getBlockEntity(data.readBlockPos()), new SimpleContainerData(2));
  11. }
  12. public BoilerMenu(int id, Inventory inv, BlockEntity entity, ContainerData data) {
  13. super(ModMenus.BOILER.get(), id);
  14. checkContainerSize(inv, Constants.BOILER_SLOTS);
  15. blockEntity = (BoilerBlockEntity) entity;
  16. this.level = inv.player.level;
  17. this.data = data;
  18. this.waterFluidStack = blockEntity.getWaterFluid();
  19. this.ppFluidStack = blockEntity.getPPFluid();
  20.  
  21. addPlayerInventory(inv);
  22. addPlayerHotbar(inv);
  23.  
  24. this.blockEntity.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(handler -> {
  25. this.addSlot(new SlotItemHandler(handler, 0, 8,17 - 25));
  26. this.addSlot(new SlotItemHandler(handler, 1, 8, 103 - 25));
  27. this.addSlot(new SlotItemHandler(handler, 2, 152, 17 - 25));
  28. this.addSlot(new SlotItemHandler(handler, 3, 152, 103 - 25));
  29.  
  30. int o = 4;
  31. for (int y = 0; y < 3; y++) {
  32. for (int x = 0; x < 3; x++) {
  33. this.addSlot(new SlotItemHandler(handler, o, 46 + x * 18, (29 + y * 18) - 25));
  34. o++;
  35. }
  36. }
  37. });
  38.  
  39. addDataSlots(data);
  40. }
  41.  
  42. public boolean isCrafting() {
  43. return data.get(0) > 0;
  44. }
  45. public int getScaledProgress() {
  46. int progress = this.data.get(0);
  47. int maxProgress = this.data.get(1);
  48. int progressArrowSize = 24;
  49.  
  50. return maxProgress != 0 && progress != 0 ? progress * progressArrowSize / maxProgress : 0;
  51. }
  52.  
  53.  
  54. // CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons
  55. // must assign a slot number to each of the slots used by the GUI.
  56. // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar.
  57. // Each time we add a Slot to the container, it automatically increases the slotIndex, which means
  58. // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
  59. // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
  60. // 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8)
  61. private static final int HOTBAR_SLOT_COUNT = 9;
  62. private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
  63. private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
  64. private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
  65. private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
  66. private static final int VANILLA_FIRST_SLOT_INDEX = 0;
  67. private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
  68. @Override
  69. public ItemStack quickMoveStack(Player playerIn, int index) {
  70. Slot sourceSlot = slots.get(index);
  71. if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM
  72. ItemStack sourceStack = sourceSlot.getItem();
  73. ItemStack copyOfSourceStack = sourceStack.copy();
  74.  
  75. // Check if the slot clicked is one of the vanilla container slots
  76. if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
  77. // This is a vanilla container slot so merge the stack into the tile inventory
  78. if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX
  79. + Constants.BOILER_SLOTS, false)) {
  80. return ItemStack.EMPTY; // EMPTY_ITEM
  81. }
  82. } else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + Constants.BOILER_SLOTS) {
  83. // This is a TE slot so merge the stack into the players inventory
  84. if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
  85. return ItemStack.EMPTY;
  86. }
  87. } else {
  88. System.out.println("Invalid slotIndex:" + index);
  89. return ItemStack.EMPTY;
  90. }
  91. // If stack size == 0 (the entire stack was moved) set slot contents to null
  92. if (sourceStack.getCount() == 0) {
  93. sourceSlot.set(ItemStack.EMPTY);
  94. } else {
  95. sourceSlot.setChanged();
  96. }
  97. sourceSlot.onTake(playerIn, sourceStack);
  98. return copyOfSourceStack;
  99. }
  100.  
  101. @Override
  102. public boolean stillValid(Player player) {
  103. return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()),
  104. player, ModBlocks.BOILER.get());
  105. }
  106.  
  107.  
  108. private void addPlayerInventory(Inventory playerInventory) {
  109. for (int i = 0; i < 3; ++i) {
  110. for (int l = 0; l < 9; ++l) {
  111. this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, (86 + i * 18) + 23));
  112. }
  113. }
  114. }
  115. private void addPlayerHotbar(Inventory playerInventory) {
  116. for (int i = 0; i < 9; ++i) {
  117. this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144 + 23));
  118. }
  119. }
  120.  
  121. public FluidStack getWaterFluid() {
  122. return this.waterFluidStack;
  123. }
  124. public void setWaterFluid(FluidStack fluid) {
  125. this.waterFluidStack = fluid;
  126. }
  127.  
  128.  
  129. public FluidStack getPPFluid() {
  130. return this.ppFluidStack;
  131. }
  132. public void setPPFluid(FluidStack fluid) {
  133. this.ppFluidStack = fluid;
  134. }
  135. }
Add Comment
Please, Sign In to add comment