summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java
blob: a464f3f227f6a16f3d15229be61bb2666311aac4 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package lv.enes.mc.eris_alchemy.menu;

import jakarta.annotation.Nonnull;
import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
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.Slot;
import net.minecraft.world.item.ItemStack;

public class AlchemicalChestMenu extends AbstractContainerMenu {
	private static final int WIDTH = 13;
	private static final int HEIGHT = 8;

	private final Container container;

	public AlchemicalChestMenu(int syncId, Inventory playerInventory) {
		this(syncId, playerInventory, new SimpleContainer(WIDTH * HEIGHT));
	}

	public AlchemicalChestMenu(int syncId, Inventory playerInventory, Container inventory) {
		super(ErisAlchemyRegistry.Menus.ALCHEMICAL_CHEST, syncId);
		checkContainerSize(inventory, WIDTH * HEIGHT);

		this.container = inventory;
		inventory.startOpen(playerInventory.player);

		var x_off = 8;
		var y_off = 8;

		for (var y = 0; y < HEIGHT; y++) {
			for (var x = 0; x < WIDTH; x++ ) {
				addSlot(new Slot(inventory, y * WIDTH + x, x_off + x * 18, y_off + y * 18));
			}
		}

		x_off = 44;
		y_off = 155;

		for (var y = 0; y < 3; y++) {
			for (var x = 0; x < 9; x++) {
				addSlot(new Slot(playerInventory, y * 9 + x + 9, x_off + x * 18, y_off + y * 18));
			}
		}

		y_off = 213;

		for (var x = 0; x < 9; x++) {
			addSlot(new Slot(playerInventory, x, x_off + x * 18, y_off));
		}
	}

	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);
	}
}