package lv.enes.mc.eris_alchemy.menu; import jakarta.annotation.Nonnull; import net.minecraft.world.Container; 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.MenuType; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; public abstract class ChestLikeMenu extends AbstractContainerMenu { protected final Container container; public ChestLikeMenu( MenuType type, int syncId, Inventory playerInventory, Container container ) { super(type, syncId); checkContainerSize(container, getRequiredSize()); this.container = container; container.startOpen(playerInventory.player); addSlots(playerInventory); } protected abstract void addSlots(Inventory playerInventory); protected abstract int getRequiredSize(); protected void addPlayerInventorySlots(Inventory playerInventory, int xOff, int yOff) { for (var y = 0; y < 3; y++) { for (var x = 0; x < 9; x++) { addSlot(new Slot(playerInventory, y * 9 + x + 9, xOff + x * 18, yOff + y * 18)); } } } protected void addPlayerHotbarSlots(Inventory playerInventory, int xOff, int yOff) { for (var x = 0; x < 9; x++) { addSlot(new Slot(playerInventory, x, xOff + x * 18, yOff)); } } public Container getContainer() { return container; } protected int getQuickMoveStart() { return 0; } protected int getQuickMoveEnd() { return container.getContainerSize(); } @Override public void broadcastChanges() { super.broadcastChanges(); } @Override public void broadcastFullState() { super.broadcastFullState(); } @Nonnull @Override public ItemStack quickMoveStack(Player player, int fromIndex) { var newStack = ItemStack.EMPTY; var slot = slots.get(fromIndex); if (!slot.hasItem()) { return newStack; } var originalStack = slot.getItem(); newStack = originalStack.copy(); if (fromIndex < container.getContainerSize()) { // In container, else player inv if (!moveItemStackTo(originalStack, container.getContainerSize(), slots.size(), true)) { return ItemStack.EMPTY; } } else if (!moveItemStackTo(originalStack, getQuickMoveStart(), getQuickMoveEnd(), false)) { return ItemStack.EMPTY; } if (originalStack.isEmpty()) { slot.setByPlayer(ItemStack.EMPTY); } else { slot.setChanged(); } return newStack; } @Override public boolean stillValid(Player player) { return container.stillValid(player); } }