Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.coriin.rechroma.screen;
- import net.coriin.rechroma.block.ModBlocks;
- import net.coriin.rechroma.block.entity.custom.CastingTableBlockEntity;
- import net.coriin.rechroma.screen.slot.ModResultSlot;
- import net.minecraft.network.FriendlyByteBuf;
- import net.minecraft.world.entity.player.Inventory;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.inventory.AbstractContainerMenu;
- import net.minecraft.world.inventory.ContainerLevelAccess;
- import net.minecraft.world.inventory.MenuType;
- import net.minecraft.world.inventory.Slot;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.level.Level;
- import net.minecraft.world.level.block.entity.BlockEntity;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.items.SlotItemHandler;
- import org.jetbrains.annotations.Nullable;
- public class CastingTableMenu extends AbstractContainerMenu {
- private final CastingTableBlockEntity blockEntity;
- private final Level level;
- public CastingTableMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) {
- this(pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()));
- }
- public CastingTableMenu(int pContainerId, Inventory inv, BlockEntity entity) {
- super(ModMenuTypes.CASTING_TABLE_MENU.get(), pContainerId);
- checkContainerSize(inv, 10);
- blockEntity = ((CastingTableBlockEntity) entity);
- this.level = inv.player.level;
- addPlayerInventory(inv);
- addPlayerHotbar(inv);
- this.blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> {
- this.addSlot(new SlotItemHandler(handler, 0, 62, 37));
- this.addSlot(new SlotItemHandler(handler, 1, 80, 37));
- this.addSlot(new SlotItemHandler(handler, 2, 98, 37));
- this.addSlot(new SlotItemHandler(handler, 3, 62, 55));
- this.addSlot(new SlotItemHandler(handler, 4, 80, 55));
- this.addSlot(new SlotItemHandler(handler, 5, 103, 55));
- this.addSlot(new SlotItemHandler(handler, 6, 62, 73));
- this.addSlot(new SlotItemHandler(handler, 7, 80, 73));
- this.addSlot(new SlotItemHandler(handler, 8, 98, 73));
- this.addSlot(new ModResultSlot(handler, 9, 187, 10));
- });
- }
- private static final int HOTBAR_SLOT_COUNT = 9;
- private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
- private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
- private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
- private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
- private static final int VANILLA_FIRST_SLOT_INDEX = 0;
- private static final int BE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
- // THIS YOU HAVE TO DEFINE!
- private static final int BE_INVENTORY_SLOT_COUNT = 10;
- @Override
- public ItemStack quickMoveStack(Player playerIn, int index) {
- Slot sourceSlot = slots.get(index);
- if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY;
- ItemStack sourceStack = sourceSlot.getItem();
- ItemStack copyOfSourceStack = sourceStack.copy();
- if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
- if (!moveItemStackTo(sourceStack, BE_INVENTORY_FIRST_SLOT_INDEX, BE_INVENTORY_FIRST_SLOT_INDEX
- + BE_INVENTORY_SLOT_COUNT, false)) {
- return ItemStack.EMPTY;
- }
- } else if (index < BE_INVENTORY_FIRST_SLOT_INDEX + BE_INVENTORY_SLOT_COUNT) {
- if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
- return ItemStack.EMPTY;
- }
- } else {
- System.out.println("Invalid slotIndex:" + index);
- return ItemStack.EMPTY;
- }
- if (sourceStack.getCount() == 0) {
- sourceSlot.set(ItemStack.EMPTY);
- } else {
- sourceSlot.setChanged();
- }
- sourceSlot.onTake(playerIn, sourceStack);
- return copyOfSourceStack;
- }
- @Override
- public boolean stillValid(Player pPlayer) {
- return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()),
- pPlayer, ModBlocks.CASTING_TABLE.get());
- }
- private void addPlayerInventory(Inventory playerInventory) {
- for (int i = 0; i < 3; ++i) {
- for (int l = 0; l < 9; ++l) {
- this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 86 + i * 18));
- }
- }
- }
- private void addPlayerHotbar(Inventory playerInventory) {
- for (int i = 0; i < 9; ++i) {
- this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144));
- }
- }
- }
Add Comment
Please, Sign In to add comment