Skip to content

Commit

Permalink
Rewrite menus in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Aug 1, 2024
1 parent 4c90666 commit 7b954d0
Show file tree
Hide file tree
Showing 19 changed files with 515 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

public final class AdornMenuScreens {
public static void register() {
MenuScreens.register(AdornMenus.INSTANCE.getDRAWER(), DrawerScreen::new);
MenuScreens.register(AdornMenus.INSTANCE.getKITCHEN_CUPBOARD(), KitchenCupboardScreen::new);
MenuScreens.register(AdornMenus.INSTANCE.getTRADING_STATION(), TradingStationScreen::new);
MenuScreens.register(AdornMenus.INSTANCE.getBREWER(), BrewerScreen::new);
MenuScreens.register(AdornMenus.DRAWER.get(), DrawerScreen::new);
MenuScreens.register(AdornMenus.KITCHEN_CUPBOARD.get(), KitchenCupboardScreen::new);
MenuScreens.register(AdornMenus.TRADING_STATION.get(), TradingStationScreen::new);
MenuScreens.register(AdornMenus.BREWER.get(), BrewerScreen::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public boolean dropStack(TradingStationScreen screen, EmiIngredient stack, int x
}

private static <T> Optional<T> single(List<T> ts) {
return ts.size() == 1 ? Optional.of(ts.get(0)) : Optional.empty();
return ts.size() == 1 ? Optional.of(ts.getFirst()) : Optional.empty();
}
}
32 changes: 32 additions & 0 deletions common/src/main/java/juuxel/adorn/menu/AdornMenus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package juuxel.adorn.menu;

import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import juuxel.adorn.platform.PlatformBridges;
import kotlin.jvm.functions.Function3;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.menu.Menu;
import net.minecraft.menu.MenuType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.resource.featuretoggle.FeatureFlags;

public final class AdornMenus {
public static final Registrar<MenuType<?>> MENUS = RegistrarFactory.get().create(RegistryKeys.SCREEN_HANDLER);
public static final Registered<MenuType<DrawerMenu>> DRAWER =
MENUS.register("drawer", () -> createType(DrawerMenu::load));
public static final Registered<MenuType<KitchenCupboardMenu>> KITCHEN_CUPBOARD =
MENUS.register("kitchen_cupboard", () -> createType(KitchenCupboardMenu::load));
public static final Registered<MenuType<TradingStationMenu>> TRADING_STATION =
MENUS.register("trading_station", () -> new MenuType<>(TradingStationMenu::new, FeatureFlags.VANILLA_FEATURES));
public static final Registered<MenuType<BrewerMenu>> BREWER =
MENUS.register("brewer", () -> new MenuType<>(BrewerMenu::new, FeatureFlags.VANILLA_FEATURES));

public static void init() {
}

private static <M extends Menu> MenuType<M> createType(Function3<Integer, PlayerInventory, PacketByteBuf, M> factory) {
return PlatformBridges.Companion.getMenus().createType(factory);
}
}
150 changes: 150 additions & 0 deletions common/src/main/java/juuxel/adorn/menu/BrewerMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package juuxel.adorn.menu;

import juuxel.adorn.block.entity.BrewerBlockEntity;
import juuxel.adorn.fluid.FluidReference;
import juuxel.adorn.fluid.FluidUnit;
import juuxel.adorn.fluid.FluidVolume;
import juuxel.adorn.item.AdornItems;
import juuxel.adorn.platform.PlatformBridges;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.menu.Menu;
import net.minecraft.menu.Slot;
import net.minecraft.menu.property.ArrayPropertyDelegate;
import net.minecraft.menu.property.PropertyDelegate;
import org.jetbrains.annotations.Nullable;

public final class BrewerMenu extends Menu {
private final Inventory container;
private final PropertyDelegate propertyDelegate;
private FluidReference fluid;
private final PlayerEntity player;
private @Nullable FluidVolume lastFluid = null;

public BrewerMenu(int syncId, PlayerInventory playerInventory, Inventory container, PropertyDelegate propertyDelegate, FluidReference fluid) {
super(AdornMenus.BREWER.get(), syncId);
this.container = container;
this.propertyDelegate = propertyDelegate;
this.fluid = fluid;
this.player = playerInventory.player;

checkSize(container, BrewerBlockEntity.CONTAINER_SIZE);
checkDataCount(propertyDelegate, 1);

addSlot(new MainSlot(container, BrewerBlockEntity.INPUT_SLOT, 80, 56));
addSlot(new Slot(container, BrewerBlockEntity.LEFT_INGREDIENT_SLOT, 50, 17));
addSlot(new Slot(container, BrewerBlockEntity.RIGHT_INGREDIENT_SLOT, 110, 17));
addSlot(new FluidContainerSlot(container, BrewerBlockEntity.FLUID_CONTAINER_SLOT, 123, 60));

// Main player inventory
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 8; x++) {
addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
}
}

// Hotbar
for (int x = 0; x <= 8; x++) {
addSlot(new Slot(playerInventory, x, 8 + x * 18, 142));
}

addProperties(propertyDelegate);
}

public BrewerMenu(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, new SimpleInventory(BrewerBlockEntity.CONTAINER_SIZE), new ArrayPropertyDelegate(1), FluidVolume.empty(FluidUnit.LITRE));
}

public int getProgress() {
return propertyDelegate.get(0);
}

public FluidReference getFluid() {
return fluid;
}

public void setFluid(FluidReference fluid) {
this.fluid = fluid;
}

@Override
public boolean canUse(PlayerEntity player) {
return container.canPlayerUse(player);
}

@Override
public ItemStack quickMove(PlayerEntity player, int index) {
var result = ItemStack.EMPTY;
var slot = slots.get(index);

if (slot.hasStack()) {
var stack = slot.getStack();
result = stack.copy();

if (index <= BrewerBlockEntity.FLUID_CONTAINER_SLOT) {
if (!insertItem(stack, BrewerBlockEntity.FLUID_CONTAINER_SLOT + 1, slots.size(), true)) {
return ItemStack.EMPTY;
}
} else {
Slot mugSlot = slots.get(BrewerBlockEntity.INPUT_SLOT);

if (!mugSlot.hasStack() && mugSlot.canInsert(stack)) {
mugSlot.setStack(stack.split(Math.min(mugSlot.getMaxItemCount(stack), stack.getCount())));
}

if (!stack.isEmpty() && !insertItem(stack, BrewerBlockEntity.LEFT_INGREDIENT_SLOT, BrewerBlockEntity.FLUID_CONTAINER_SLOT + 1, false)) {
return ItemStack.EMPTY;
}
}

if (stack.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
}

return result;
}

@Override
public void sendContentUpdates() {
super.sendContentUpdates();

var last = lastFluid;
if (last == null || !FluidReference.areFluidsAndAmountsEqual(fluid, last)) {
lastFluid = fluid.createSnapshot();
PlatformBridges.Companion.getNetwork().sendBrewerFluidSync(player, syncId, fluid);
}
}

private static final class MainSlot extends Slot {
private MainSlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
}

@Override
public int getMaxItemCount() {
return 1;
}

@Override
public boolean canInsert(ItemStack stack) {
return stack.isOf(AdornItems.MUG.get());
}
}

private static final class FluidContainerSlot extends Slot {
private FluidContainerSlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
}

@Override
public int getMaxItemCount() {
return 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package juuxel.adorn.menu;

import net.minecraft.inventory.Inventory;
import net.minecraft.menu.MenuContext;

public interface ContainerBlockMenu {
Inventory getInventory();
MenuContext getContext();
}
22 changes: 22 additions & 0 deletions common/src/main/java/juuxel/adorn/menu/DrawerMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package juuxel.adorn.menu;

import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.menu.MenuContext;
import net.minecraft.network.PacketByteBuf;

public final class DrawerMenu extends SimpleMenu {
private static final int WIDTH = 5;
private static final int HEIGHT = 3;

public DrawerMenu(int syncId, PlayerInventory playerInventory, Inventory inventory, MenuContext context) {
super(AdornMenus.DRAWER.get(), syncId, WIDTH, HEIGHT, inventory, playerInventory, context);
}

public static DrawerMenu load(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
var pos = buf.readBlockPos();
var context = MenuContext.create(playerInventory.player.getWorld(), pos);
return new DrawerMenu(syncId, playerInventory, new SimpleInventory(WIDTH * HEIGHT), context);
}
}
22 changes: 22 additions & 0 deletions common/src/main/java/juuxel/adorn/menu/KitchenCupboardMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package juuxel.adorn.menu;

import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.menu.MenuContext;
import net.minecraft.network.PacketByteBuf;

public final class KitchenCupboardMenu extends SimpleMenu {
private static final int WIDTH = 5;
private static final int HEIGHT = 3;

public KitchenCupboardMenu(int syncId, PlayerInventory playerInventory, Inventory inventory, MenuContext context) {
super(AdornMenus.KITCHEN_CUPBOARD.get(), syncId, WIDTH, HEIGHT, inventory, playerInventory, context);
}

public static KitchenCupboardMenu load(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
var pos = buf.readBlockPos();
var context = MenuContext.create(playerInventory.player.getWorld(), pos);
return new KitchenCupboardMenu(syncId, playerInventory, new SimpleInventory(WIDTH * HEIGHT), context);
}
}
92 changes: 92 additions & 0 deletions common/src/main/java/juuxel/adorn/menu/SimpleMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package juuxel.adorn.menu;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.menu.Menu;
import net.minecraft.menu.MenuContext;
import net.minecraft.menu.MenuType;
import net.minecraft.menu.Slot;

public abstract class SimpleMenu extends Menu implements ContainerBlockMenu {
private final int width;
private final int height;
private final Inventory inventory;
private final MenuContext context;

public SimpleMenu(MenuType<?> type, int syncId, int width, int height, Inventory inventory, PlayerInventory playerInventory, MenuContext context) {
super(type, syncId);
this.width = width;
this.height = height;
this.inventory = inventory;
this.context = context;

int offset = (9 - width) / 2;
checkSize(inventory, width * height);

int slot = 18;

// Container
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
addSlot(new Slot(inventory, y * width + x, 8 + (x + offset) * slot, 17 + y * slot));
}
}

// Main player inventory
for (int y = 0; y <= 2; y++) {
for (int x = 0; x <= 8; x++) {
addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * slot, 84 + y * slot));
}
}

// Hotbar
for (int x = 0; x <= 8; x++) {
addSlot(new Slot(playerInventory, x, 8 + x * slot, 142));
}
}

@Override
public Inventory getInventory() {
return inventory;
}

@Override
public MenuContext getContext() {
return context;
}

@Override
public boolean canUse(PlayerEntity player) {
return inventory.canPlayerUse(player);
}

@Override
public ItemStack quickMove(PlayerEntity player, int index) {
var result = ItemStack.EMPTY;
var slot = slots.get(index);

if (slot != null && slot.hasStack()) {
var containerSize = width * height;
var stack = slot.getStack();
result = stack.copy();

if (index < containerSize) {
if (!insertItem(stack, containerSize, slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!insertItem(stack, 0, containerSize, false)) {
return ItemStack.EMPTY;
}

if (stack.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
}

return result;
}
}
Loading

0 comments on commit 7b954d0

Please sign in to comment.