|
| 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 | +} |
0 commit comments