Skip to content

Commit 7b954d0

Browse files
committed
Rewrite menus in Java
1 parent 4c90666 commit 7b954d0

19 files changed

+515
-447
lines changed

common/src/main/java/juuxel/adorn/client/gui/screen/AdornMenuScreens.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
public final class AdornMenuScreens {
77
public static void register() {
8-
MenuScreens.register(AdornMenus.INSTANCE.getDRAWER(), DrawerScreen::new);
9-
MenuScreens.register(AdornMenus.INSTANCE.getKITCHEN_CUPBOARD(), KitchenCupboardScreen::new);
10-
MenuScreens.register(AdornMenus.INSTANCE.getTRADING_STATION(), TradingStationScreen::new);
11-
MenuScreens.register(AdornMenus.INSTANCE.getBREWER(), BrewerScreen::new);
8+
MenuScreens.register(AdornMenus.DRAWER.get(), DrawerScreen::new);
9+
MenuScreens.register(AdornMenus.KITCHEN_CUPBOARD.get(), KitchenCupboardScreen::new);
10+
MenuScreens.register(AdornMenus.TRADING_STATION.get(), TradingStationScreen::new);
11+
MenuScreens.register(AdornMenus.BREWER.get(), BrewerScreen::new);
1212
}
1313
}

common/src/main/java/juuxel/adorn/compat/emi/TradingStationDragDropHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public boolean dropStack(TradingStationScreen screen, EmiIngredient stack, int x
3030
}
3131

3232
private static <T> Optional<T> single(List<T> ts) {
33-
return ts.size() == 1 ? Optional.of(ts.get(0)) : Optional.empty();
33+
return ts.size() == 1 ? Optional.of(ts.getFirst()) : Optional.empty();
3434
}
3535
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package juuxel.adorn.menu;
2+
3+
import juuxel.adorn.lib.registry.Registered;
4+
import juuxel.adorn.lib.registry.Registrar;
5+
import juuxel.adorn.lib.registry.RegistrarFactory;
6+
import juuxel.adorn.platform.PlatformBridges;
7+
import kotlin.jvm.functions.Function3;
8+
import net.minecraft.entity.player.PlayerInventory;
9+
import net.minecraft.menu.Menu;
10+
import net.minecraft.menu.MenuType;
11+
import net.minecraft.network.PacketByteBuf;
12+
import net.minecraft.registry.RegistryKeys;
13+
import net.minecraft.resource.featuretoggle.FeatureFlags;
14+
15+
public final class AdornMenus {
16+
public static final Registrar<MenuType<?>> MENUS = RegistrarFactory.get().create(RegistryKeys.SCREEN_HANDLER);
17+
public static final Registered<MenuType<DrawerMenu>> DRAWER =
18+
MENUS.register("drawer", () -> createType(DrawerMenu::load));
19+
public static final Registered<MenuType<KitchenCupboardMenu>> KITCHEN_CUPBOARD =
20+
MENUS.register("kitchen_cupboard", () -> createType(KitchenCupboardMenu::load));
21+
public static final Registered<MenuType<TradingStationMenu>> TRADING_STATION =
22+
MENUS.register("trading_station", () -> new MenuType<>(TradingStationMenu::new, FeatureFlags.VANILLA_FEATURES));
23+
public static final Registered<MenuType<BrewerMenu>> BREWER =
24+
MENUS.register("brewer", () -> new MenuType<>(BrewerMenu::new, FeatureFlags.VANILLA_FEATURES));
25+
26+
public static void init() {
27+
}
28+
29+
private static <M extends Menu> MenuType<M> createType(Function3<Integer, PlayerInventory, PacketByteBuf, M> factory) {
30+
return PlatformBridges.Companion.getMenus().createType(factory);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package juuxel.adorn.menu;
2+
3+
import juuxel.adorn.block.entity.BrewerBlockEntity;
4+
import juuxel.adorn.fluid.FluidReference;
5+
import juuxel.adorn.fluid.FluidUnit;
6+
import juuxel.adorn.fluid.FluidVolume;
7+
import juuxel.adorn.item.AdornItems;
8+
import juuxel.adorn.platform.PlatformBridges;
9+
import net.minecraft.entity.player.PlayerEntity;
10+
import net.minecraft.entity.player.PlayerInventory;
11+
import net.minecraft.inventory.Inventory;
12+
import net.minecraft.inventory.SimpleInventory;
13+
import net.minecraft.item.ItemStack;
14+
import net.minecraft.menu.Menu;
15+
import net.minecraft.menu.Slot;
16+
import net.minecraft.menu.property.ArrayPropertyDelegate;
17+
import net.minecraft.menu.property.PropertyDelegate;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
public final class BrewerMenu extends Menu {
21+
private final Inventory container;
22+
private final PropertyDelegate propertyDelegate;
23+
private FluidReference fluid;
24+
private final PlayerEntity player;
25+
private @Nullable FluidVolume lastFluid = null;
26+
27+
public BrewerMenu(int syncId, PlayerInventory playerInventory, Inventory container, PropertyDelegate propertyDelegate, FluidReference fluid) {
28+
super(AdornMenus.BREWER.get(), syncId);
29+
this.container = container;
30+
this.propertyDelegate = propertyDelegate;
31+
this.fluid = fluid;
32+
this.player = playerInventory.player;
33+
34+
checkSize(container, BrewerBlockEntity.CONTAINER_SIZE);
35+
checkDataCount(propertyDelegate, 1);
36+
37+
addSlot(new MainSlot(container, BrewerBlockEntity.INPUT_SLOT, 80, 56));
38+
addSlot(new Slot(container, BrewerBlockEntity.LEFT_INGREDIENT_SLOT, 50, 17));
39+
addSlot(new Slot(container, BrewerBlockEntity.RIGHT_INGREDIENT_SLOT, 110, 17));
40+
addSlot(new FluidContainerSlot(container, BrewerBlockEntity.FLUID_CONTAINER_SLOT, 123, 60));
41+
42+
// Main player inventory
43+
for (int y = 0; y <= 2; y++) {
44+
for (int x = 0; x <= 8; x++) {
45+
addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
46+
}
47+
}
48+
49+
// Hotbar
50+
for (int x = 0; x <= 8; x++) {
51+
addSlot(new Slot(playerInventory, x, 8 + x * 18, 142));
52+
}
53+
54+
addProperties(propertyDelegate);
55+
}
56+
57+
public BrewerMenu(int syncId, PlayerInventory playerInventory) {
58+
this(syncId, playerInventory, new SimpleInventory(BrewerBlockEntity.CONTAINER_SIZE), new ArrayPropertyDelegate(1), FluidVolume.empty(FluidUnit.LITRE));
59+
}
60+
61+
public int getProgress() {
62+
return propertyDelegate.get(0);
63+
}
64+
65+
public FluidReference getFluid() {
66+
return fluid;
67+
}
68+
69+
public void setFluid(FluidReference fluid) {
70+
this.fluid = fluid;
71+
}
72+
73+
@Override
74+
public boolean canUse(PlayerEntity player) {
75+
return container.canPlayerUse(player);
76+
}
77+
78+
@Override
79+
public ItemStack quickMove(PlayerEntity player, int index) {
80+
var result = ItemStack.EMPTY;
81+
var slot = slots.get(index);
82+
83+
if (slot.hasStack()) {
84+
var stack = slot.getStack();
85+
result = stack.copy();
86+
87+
if (index <= BrewerBlockEntity.FLUID_CONTAINER_SLOT) {
88+
if (!insertItem(stack, BrewerBlockEntity.FLUID_CONTAINER_SLOT + 1, slots.size(), true)) {
89+
return ItemStack.EMPTY;
90+
}
91+
} else {
92+
Slot mugSlot = slots.get(BrewerBlockEntity.INPUT_SLOT);
93+
94+
if (!mugSlot.hasStack() && mugSlot.canInsert(stack)) {
95+
mugSlot.setStack(stack.split(Math.min(mugSlot.getMaxItemCount(stack), stack.getCount())));
96+
}
97+
98+
if (!stack.isEmpty() && !insertItem(stack, BrewerBlockEntity.LEFT_INGREDIENT_SLOT, BrewerBlockEntity.FLUID_CONTAINER_SLOT + 1, false)) {
99+
return ItemStack.EMPTY;
100+
}
101+
}
102+
103+
if (stack.isEmpty()) {
104+
slot.setStack(ItemStack.EMPTY);
105+
} else {
106+
slot.markDirty();
107+
}
108+
}
109+
110+
return result;
111+
}
112+
113+
@Override
114+
public void sendContentUpdates() {
115+
super.sendContentUpdates();
116+
117+
var last = lastFluid;
118+
if (last == null || !FluidReference.areFluidsAndAmountsEqual(fluid, last)) {
119+
lastFluid = fluid.createSnapshot();
120+
PlatformBridges.Companion.getNetwork().sendBrewerFluidSync(player, syncId, fluid);
121+
}
122+
}
123+
124+
private static final class MainSlot extends Slot {
125+
private MainSlot(Inventory inventory, int index, int x, int y) {
126+
super(inventory, index, x, y);
127+
}
128+
129+
@Override
130+
public int getMaxItemCount() {
131+
return 1;
132+
}
133+
134+
@Override
135+
public boolean canInsert(ItemStack stack) {
136+
return stack.isOf(AdornItems.MUG.get());
137+
}
138+
}
139+
140+
private static final class FluidContainerSlot extends Slot {
141+
private FluidContainerSlot(Inventory inventory, int index, int x, int y) {
142+
super(inventory, index, x, y);
143+
}
144+
145+
@Override
146+
public int getMaxItemCount() {
147+
return 1;
148+
}
149+
}
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package juuxel.adorn.menu;
2+
3+
import net.minecraft.inventory.Inventory;
4+
import net.minecraft.menu.MenuContext;
5+
6+
public interface ContainerBlockMenu {
7+
Inventory getInventory();
8+
MenuContext getContext();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package juuxel.adorn.menu;
2+
3+
import net.minecraft.entity.player.PlayerInventory;
4+
import net.minecraft.inventory.Inventory;
5+
import net.minecraft.inventory.SimpleInventory;
6+
import net.minecraft.menu.MenuContext;
7+
import net.minecraft.network.PacketByteBuf;
8+
9+
public final class DrawerMenu extends SimpleMenu {
10+
private static final int WIDTH = 5;
11+
private static final int HEIGHT = 3;
12+
13+
public DrawerMenu(int syncId, PlayerInventory playerInventory, Inventory inventory, MenuContext context) {
14+
super(AdornMenus.DRAWER.get(), syncId, WIDTH, HEIGHT, inventory, playerInventory, context);
15+
}
16+
17+
public static DrawerMenu load(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
18+
var pos = buf.readBlockPos();
19+
var context = MenuContext.create(playerInventory.player.getWorld(), pos);
20+
return new DrawerMenu(syncId, playerInventory, new SimpleInventory(WIDTH * HEIGHT), context);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package juuxel.adorn.menu;
2+
3+
import net.minecraft.entity.player.PlayerInventory;
4+
import net.minecraft.inventory.Inventory;
5+
import net.minecraft.inventory.SimpleInventory;
6+
import net.minecraft.menu.MenuContext;
7+
import net.minecraft.network.PacketByteBuf;
8+
9+
public final class KitchenCupboardMenu extends SimpleMenu {
10+
private static final int WIDTH = 5;
11+
private static final int HEIGHT = 3;
12+
13+
public KitchenCupboardMenu(int syncId, PlayerInventory playerInventory, Inventory inventory, MenuContext context) {
14+
super(AdornMenus.KITCHEN_CUPBOARD.get(), syncId, WIDTH, HEIGHT, inventory, playerInventory, context);
15+
}
16+
17+
public static KitchenCupboardMenu load(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
18+
var pos = buf.readBlockPos();
19+
var context = MenuContext.create(playerInventory.player.getWorld(), pos);
20+
return new KitchenCupboardMenu(syncId, playerInventory, new SimpleInventory(WIDTH * HEIGHT), context);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package juuxel.adorn.menu;
2+
3+
import net.minecraft.entity.player.PlayerEntity;
4+
import net.minecraft.entity.player.PlayerInventory;
5+
import net.minecraft.inventory.Inventory;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.menu.Menu;
8+
import net.minecraft.menu.MenuContext;
9+
import net.minecraft.menu.MenuType;
10+
import net.minecraft.menu.Slot;
11+
12+
public abstract class SimpleMenu extends Menu implements ContainerBlockMenu {
13+
private final int width;
14+
private final int height;
15+
private final Inventory inventory;
16+
private final MenuContext context;
17+
18+
public SimpleMenu(MenuType<?> type, int syncId, int width, int height, Inventory inventory, PlayerInventory playerInventory, MenuContext context) {
19+
super(type, syncId);
20+
this.width = width;
21+
this.height = height;
22+
this.inventory = inventory;
23+
this.context = context;
24+
25+
int offset = (9 - width) / 2;
26+
checkSize(inventory, width * height);
27+
28+
int slot = 18;
29+
30+
// Container
31+
for (int y = 0; y < height; y++) {
32+
for (int x = 0; x < width; x++) {
33+
addSlot(new Slot(inventory, y * width + x, 8 + (x + offset) * slot, 17 + y * slot));
34+
}
35+
}
36+
37+
// Main player inventory
38+
for (int y = 0; y <= 2; y++) {
39+
for (int x = 0; x <= 8; x++) {
40+
addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * slot, 84 + y * slot));
41+
}
42+
}
43+
44+
// Hotbar
45+
for (int x = 0; x <= 8; x++) {
46+
addSlot(new Slot(playerInventory, x, 8 + x * slot, 142));
47+
}
48+
}
49+
50+
@Override
51+
public Inventory getInventory() {
52+
return inventory;
53+
}
54+
55+
@Override
56+
public MenuContext getContext() {
57+
return context;
58+
}
59+
60+
@Override
61+
public boolean canUse(PlayerEntity player) {
62+
return inventory.canPlayerUse(player);
63+
}
64+
65+
@Override
66+
public ItemStack quickMove(PlayerEntity player, int index) {
67+
var result = ItemStack.EMPTY;
68+
var slot = slots.get(index);
69+
70+
if (slot != null && slot.hasStack()) {
71+
var containerSize = width * height;
72+
var stack = slot.getStack();
73+
result = stack.copy();
74+
75+
if (index < containerSize) {
76+
if (!insertItem(stack, containerSize, slots.size(), true)) {
77+
return ItemStack.EMPTY;
78+
}
79+
} else if (!insertItem(stack, 0, containerSize, false)) {
80+
return ItemStack.EMPTY;
81+
}
82+
83+
if (stack.isEmpty()) {
84+
slot.setStack(ItemStack.EMPTY);
85+
} else {
86+
slot.markDirty();
87+
}
88+
}
89+
90+
return result;
91+
}
92+
}

0 commit comments

Comments
 (0)