summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/menu/ChestLikeBlockMenu.java
blob: 897abe961f1a97f7c5cb8a550d12096d57aca6c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.item.ItemStack;

public abstract class ChestLikeBlockMenu extends AbstractContainerMenu {
	protected final Container container;

	public ChestLikeBlockMenu(
			MenuType<? extends ChestLikeBlockMenu> 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();

	public Container getContainer() {
		return container;
	}

	@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()) {
			if (!moveItemStackTo(originalStack, container.getContainerSize(), slots.size(), true)) {
				return ItemStack.EMPTY;
			}
		} else if (!moveItemStackTo(originalStack, 0, container.getContainerSize(), 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);
	}
}