summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/menu
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/menu')
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java58
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/menu/ChestLikeBlockMenu.java67
2 files changed, 76 insertions, 49 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java b/src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java
index a464f3f..1dc3364 100644
--- a/src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java
+++ b/src/main/java/lv/enes/mc/eris_alchemy/menu/AlchemicalChestMenu.java
@@ -1,38 +1,31 @@
1package lv.enes.mc.eris_alchemy.menu; 1package lv.enes.mc.eris_alchemy.menu;
2 2
3import jakarta.annotation.Nonnull;
4import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry; 3import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry;
5import net.minecraft.world.Container; 4import net.minecraft.world.Container;
6import net.minecraft.world.SimpleContainer; 5import net.minecraft.world.SimpleContainer;
7import net.minecraft.world.entity.player.Inventory; 6import net.minecraft.world.entity.player.Inventory;
8import net.minecraft.world.entity.player.Player;
9import net.minecraft.world.inventory.AbstractContainerMenu;
10import net.minecraft.world.inventory.Slot; 7import net.minecraft.world.inventory.Slot;
11import net.minecraft.world.item.ItemStack;
12 8
13public class AlchemicalChestMenu extends AbstractContainerMenu { 9public class AlchemicalChestMenu extends ChestLikeBlockMenu {
14 private static final int WIDTH = 13; 10 private static final int WIDTH = 13;
15 private static final int HEIGHT = 8; 11 private static final int HEIGHT = 8;
16 12
17 private final Container container;
18
19 public AlchemicalChestMenu(int syncId, Inventory playerInventory) { 13 public AlchemicalChestMenu(int syncId, Inventory playerInventory) {
20 this(syncId, playerInventory, new SimpleContainer(WIDTH * HEIGHT)); 14 this(syncId, playerInventory, new SimpleContainer(WIDTH * HEIGHT));
21 } 15 }
22 16
23 public AlchemicalChestMenu(int syncId, Inventory playerInventory, Container inventory) { 17 public AlchemicalChestMenu(int syncId, Inventory playerInventory, Container container) {
24 super(ErisAlchemyRegistry.Menus.ALCHEMICAL_CHEST, syncId); 18 super(ErisAlchemyRegistry.Menus.ALCHEMICAL_CHEST, syncId, playerInventory, container);
25 checkContainerSize(inventory, WIDTH * HEIGHT); 19 }
26
27 this.container = inventory;
28 inventory.startOpen(playerInventory.player);
29 20
21 @Override
22 protected void addSlots(Inventory playerInventory) {
30 var x_off = 8; 23 var x_off = 8;
31 var y_off = 8; 24 var y_off = 8;
32 25
33 for (var y = 0; y < HEIGHT; y++) { 26 for (var y = 0; y < HEIGHT; y++) {
34 for (var x = 0; x < WIDTH; x++ ) { 27 for (var x = 0; x < WIDTH; x++ ) {
35 addSlot(new Slot(inventory, y * WIDTH + x, x_off + x * 18, y_off + y * 18)); 28 addSlot(new Slot(container, y * WIDTH + x, x_off + x * 18, y_off + y * 18));
36 } 29 }
37 } 30 }
38 31
@@ -52,41 +45,8 @@ public class AlchemicalChestMenu extends AbstractContainerMenu {
52 } 45 }
53 } 46 }
54 47
55 public Container getContainer() {
56 return container;
57 }
58
59 @Nonnull
60 @Override
61 public ItemStack quickMoveStack(Player player, int fromIndex) {
62 var newStack = ItemStack.EMPTY;
63 var slot = slots.get(fromIndex);
64 if (!slot.hasItem()) {
65 return newStack;
66 }
67
68 var originalStack = slot.getItem();
69 newStack = originalStack.copy();
70
71 if (fromIndex < container.getContainerSize()) {
72 if (!moveItemStackTo(originalStack, container.getContainerSize(), slots.size(), true)) {
73 return ItemStack.EMPTY;
74 }
75 } else if (!moveItemStackTo(originalStack, 0, container.getContainerSize(), false)) {
76 return ItemStack.EMPTY;
77 }
78
79 if (originalStack.isEmpty()) {
80 slot.setByPlayer(ItemStack.EMPTY);
81 } else {
82 slot.setChanged();
83 }
84
85 return newStack;
86 }
87
88 @Override 48 @Override
89 public boolean stillValid(Player player) { 49 protected int getRequiredSize() {
90 return container.stillValid(player); 50 return WIDTH * HEIGHT;
91 } 51 }
92} 52}
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/menu/ChestLikeBlockMenu.java b/src/main/java/lv/enes/mc/eris_alchemy/menu/ChestLikeBlockMenu.java
new file mode 100644
index 0000000..897abe9
--- /dev/null
+++ b/src/main/java/lv/enes/mc/eris_alchemy/menu/ChestLikeBlockMenu.java
@@ -0,0 +1,67 @@
1package lv.enes.mc.eris_alchemy.menu;
2
3import jakarta.annotation.Nonnull;
4import net.minecraft.world.Container;
5import net.minecraft.world.entity.player.Inventory;
6import net.minecraft.world.entity.player.Player;
7import net.minecraft.world.inventory.AbstractContainerMenu;
8import net.minecraft.world.inventory.MenuType;
9import net.minecraft.world.item.ItemStack;
10
11public abstract class ChestLikeBlockMenu extends AbstractContainerMenu {
12 protected final Container container;
13
14 public ChestLikeBlockMenu(
15 MenuType<? extends ChestLikeBlockMenu> type,
16 int syncId,
17 Inventory playerInventory,
18 Container container
19 ) {
20 super(type, syncId);
21 checkContainerSize(container, getRequiredSize());
22 this.container = container;
23 container.startOpen(playerInventory.player);
24 addSlots(playerInventory);
25 }
26
27 protected abstract void addSlots(Inventory playerInventory);
28 protected abstract int getRequiredSize();
29
30 public Container getContainer() {
31 return container;
32 }
33
34 @Nonnull
35 @Override
36 public ItemStack quickMoveStack(Player player, int fromIndex) {
37 var newStack = ItemStack.EMPTY;
38 var slot = slots.get(fromIndex);
39 if (!slot.hasItem()) {
40 return newStack;
41 }
42
43 var originalStack = slot.getItem();
44 newStack = originalStack.copy();
45
46 if (fromIndex < container.getContainerSize()) {
47 if (!moveItemStackTo(originalStack, container.getContainerSize(), slots.size(), true)) {
48 return ItemStack.EMPTY;
49 }
50 } else if (!moveItemStackTo(originalStack, 0, container.getContainerSize(), false)) {
51 return ItemStack.EMPTY;
52 }
53
54 if (originalStack.isEmpty()) {
55 slot.setByPlayer(ItemStack.EMPTY);
56 } else {
57 slot.setChanged();
58 }
59
60 return newStack;
61 }
62
63 @Override
64 public boolean stillValid(Player player) {
65 return container.stillValid(player);
66 }
67}