|
| 1 | +package net.kaupenjoe.tutorialmod.screen.custom; |
| 2 | + |
| 3 | +import net.kaupenjoe.tutorialmod.block.ModBlocks; |
| 4 | +import net.kaupenjoe.tutorialmod.block.entity.PedestalBlockEntity; |
| 5 | +import net.kaupenjoe.tutorialmod.screen.ModMenuTypes; |
| 6 | +import net.minecraft.network.FriendlyByteBuf; |
| 7 | +import net.minecraft.world.entity.player.Inventory; |
| 8 | +import net.minecraft.world.entity.player.Player; |
| 9 | +import net.minecraft.world.inventory.AbstractContainerMenu; |
| 10 | +import net.minecraft.world.inventory.ContainerLevelAccess; |
| 11 | +import net.minecraft.world.inventory.Slot; |
| 12 | +import net.minecraft.world.item.ItemStack; |
| 13 | +import net.minecraft.world.level.Level; |
| 14 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 15 | +import net.neoforged.neoforge.items.SlotItemHandler; |
| 16 | + |
| 17 | +public class PedestalMenu extends AbstractContainerMenu { |
| 18 | + public final PedestalBlockEntity blockEntity; |
| 19 | + private final Level level; |
| 20 | + |
| 21 | + public PedestalMenu(int containerId, Inventory inv, FriendlyByteBuf extraData) { |
| 22 | + this(containerId, inv, inv.player.level().getBlockEntity(extraData.readBlockPos())); |
| 23 | + } |
| 24 | + |
| 25 | + public PedestalMenu(int containerId, Inventory inv, BlockEntity blockEntity) { |
| 26 | + super(ModMenuTypes.PEDESTAL_MENU.get(), containerId); |
| 27 | + this.blockEntity = ((PedestalBlockEntity) blockEntity); |
| 28 | + this.level = inv.player.level(); |
| 29 | + |
| 30 | + addPlayerInventory(inv); |
| 31 | + addPlayerHotbar(inv); |
| 32 | + |
| 33 | + this.addSlot(new SlotItemHandler(this.blockEntity.inventory, 0, 80, 35)); |
| 34 | + } |
| 35 | + |
| 36 | + // CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons |
| 37 | + // must assign a slot number to each of the slots used by the GUI. |
| 38 | + // For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar. |
| 39 | + // Each time we add a Slot to the container, it automatically increases the slotIndex, which means |
| 40 | + // 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8) |
| 41 | + // 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35) |
| 42 | + // 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8) |
| 43 | + private static final int HOTBAR_SLOT_COUNT = 9; |
| 44 | + private static final int PLAYER_INVENTORY_ROW_COUNT = 3; |
| 45 | + private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9; |
| 46 | + private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT; |
| 47 | + private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT; |
| 48 | + private static final int VANILLA_FIRST_SLOT_INDEX = 0; |
| 49 | + private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT; |
| 50 | + |
| 51 | + // THIS YOU HAVE TO DEFINE! |
| 52 | + private static final int TE_INVENTORY_SLOT_COUNT = 1; // must be the number of slots you have! |
| 53 | + @Override |
| 54 | + public ItemStack quickMoveStack(Player playerIn, int pIndex) { |
| 55 | + Slot sourceSlot = slots.get(pIndex); |
| 56 | + if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM |
| 57 | + ItemStack sourceStack = sourceSlot.getItem(); |
| 58 | + ItemStack copyOfSourceStack = sourceStack.copy(); |
| 59 | + |
| 60 | + // Check if the slot clicked is one of the vanilla container slots |
| 61 | + if (pIndex < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) { |
| 62 | + // This is a vanilla container slot so merge the stack into the tile inventory |
| 63 | + if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX |
| 64 | + + TE_INVENTORY_SLOT_COUNT, false)) { |
| 65 | + return ItemStack.EMPTY; // EMPTY_ITEM |
| 66 | + } |
| 67 | + } else if (pIndex < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) { |
| 68 | + // This is a TE slot so merge the stack into the players inventory |
| 69 | + if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) { |
| 70 | + return ItemStack.EMPTY; |
| 71 | + } |
| 72 | + } else { |
| 73 | + System.out.println("Invalid slotIndex:" + pIndex); |
| 74 | + return ItemStack.EMPTY; |
| 75 | + } |
| 76 | + // If stack size == 0 (the entire stack was moved) set slot contents to null |
| 77 | + if (sourceStack.getCount() == 0) { |
| 78 | + sourceSlot.set(ItemStack.EMPTY); |
| 79 | + } else { |
| 80 | + sourceSlot.setChanged(); |
| 81 | + } |
| 82 | + sourceSlot.onTake(playerIn, sourceStack); |
| 83 | + return copyOfSourceStack; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean stillValid(Player player) { |
| 88 | + return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), |
| 89 | + player, ModBlocks.PEDESTAL.get()); |
| 90 | + } |
| 91 | + |
| 92 | + private void addPlayerInventory(Inventory playerInventory) { |
| 93 | + for (int i = 0; i < 3; ++i) { |
| 94 | + for (int l = 0; l < 9; ++l) { |
| 95 | + this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 84 + i * 18)); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void addPlayerHotbar(Inventory playerInventory) { |
| 101 | + for (int i = 0; i < 9; ++i) { |
| 102 | + this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142)); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments