Skip to content

Commit 37567ce

Browse files
committed
feat: recipe input helpers
simplify menu creation and initial synchronization
1 parent 6384adc commit 37567ce

39 files changed

+417
-556
lines changed

src/main/java/dev/galacticraft/machinelib/api/block/BaseBlock.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
package dev.galacticraft.machinelib.api.block;
2424

2525
import dev.galacticraft.machinelib.api.block.entity.BaseBlockEntity;
26+
import dev.galacticraft.machinelib.api.menu.SynchronizedMenu;
2627
import dev.galacticraft.machinelib.impl.block.entity.BaseBlockEntityTicker;
28+
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
2729
import net.minecraft.core.BlockPos;
30+
import net.minecraft.network.chat.Component;
31+
import net.minecraft.server.level.ServerPlayer;
2832
import net.minecraft.world.InteractionResult;
33+
import net.minecraft.world.entity.player.Inventory;
2934
import net.minecraft.world.entity.player.Player;
35+
import net.minecraft.world.inventory.AbstractContainerMenu;
3036
import net.minecraft.world.level.Level;
3137
import net.minecraft.world.level.block.BaseEntityBlock;
3238
import net.minecraft.world.level.block.RenderShape;
@@ -65,8 +71,34 @@ public BaseBlock(Properties settings) {
6571
if (!level.isClientSide) {
6672
BlockEntity entity = level.getBlockEntity(pos);
6773
if (entity instanceof BaseBlockEntity be) {
68-
player.openMenu(be);
69-
return InteractionResult.CONSUME;
74+
player.openMenu(new ExtendedScreenHandlerFactory<>() {
75+
@Nullable
76+
@Override
77+
public AbstractContainerMenu createMenu(int syncId, Inventory inventory, Player player) {
78+
assert player.getInventory() == inventory;
79+
80+
SynchronizedMenu<?> menu = be.createMenu(syncId, inventory, player);
81+
if (menu != null) {
82+
menu.registerData(menu.getData());
83+
}
84+
return menu;
85+
}
86+
87+
@Override
88+
public @NotNull Component getDisplayName() {
89+
return be.getDisplayName();
90+
}
91+
92+
@Override
93+
public Object getScreenOpeningData(ServerPlayer player) {
94+
return be.getScreenOpeningData(player);
95+
}
96+
97+
@Override
98+
public boolean shouldCloseCurrentScreen() {
99+
return be.shouldCloseCurrentScreen();
100+
}
101+
});
70102
}
71103
}
72104

src/main/java/dev/galacticraft/machinelib/api/block/MachineBlock.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon
229229

230230
security.tryUpdate(player);
231231
if (security.hasAccess(player)) {
232-
player.openMenu(machine);
233-
return InteractionResult.CONSUME;
232+
return super.useWithoutItem(state, level, pos, player, hit);
234233
}
235234
}
236235
}

src/main/java/dev/galacticraft/machinelib/api/block/entity/BaseBlockEntity.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222

2323
package dev.galacticraft.machinelib.api.block.entity;
2424

25-
import dev.galacticraft.machinelib.api.menu.MachineMenu;
2625
import dev.galacticraft.machinelib.api.menu.SynchronizedMenu;
27-
import io.netty.buffer.Unpooled;
2826
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
2927
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
3028
import net.minecraft.core.BlockPos;
3129
import net.minecraft.core.HolderLookup;
3230
import net.minecraft.nbt.CompoundTag;
33-
import net.minecraft.network.RegistryFriendlyByteBuf;
3431
import net.minecraft.network.chat.Component;
3532
import net.minecraft.network.protocol.Packet;
3633
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
@@ -41,7 +38,6 @@
4138
import net.minecraft.util.profiling.ProfilerFiller;
4239
import net.minecraft.world.entity.player.Inventory;
4340
import net.minecraft.world.entity.player.Player;
44-
import net.minecraft.world.inventory.AbstractContainerMenu;
4541
import net.minecraft.world.level.ChunkPos;
4642
import net.minecraft.world.level.block.entity.BlockEntity;
4743
import net.minecraft.world.level.block.entity.BlockEntityType;
@@ -54,7 +50,7 @@
5450
*
5551
* @see SynchronizedMenu
5652
*/
57-
public abstract class BaseBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory<RegistryFriendlyByteBuf> {
53+
public abstract class BaseBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory<BlockPos> {
5854
/**
5955
* Constructs a new base block entity.
6056
*
@@ -82,23 +78,9 @@ protected BaseBlockEntity(BlockEntityType<? extends BaseBlockEntity> type, Block
8278
*/
8379
public abstract @Nullable CustomPacketPayload createUpdatePayload();
8480

85-
/**
86-
* {@return a newly created menu}
87-
*
88-
* @param syncId
89-
* @param inventory
90-
* @param player
91-
* @return
92-
*/
93-
@Nullable
94-
public abstract SynchronizedMenu<? extends BaseBlockEntity> openMenu(int syncId, Inventory inventory, Player player);
95-
9681
@Override
97-
public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
98-
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), player.server.registryAccess());
99-
buf.writeBlockPos(this.getBlockPos());
100-
((MachineMenu<?>) player.containerMenu).getData().synchronizeInitial(buf);
101-
return buf;
82+
public BlockPos getScreenOpeningData(ServerPlayer player) {
83+
return this.getBlockPos();
10284
}
10385

10486
// we override the update packet to circumvent nbt serialization
@@ -116,15 +98,16 @@ public RegistryFriendlyByteBuf getScreenOpeningData(ServerPlayer player) {
11698
return payload == null ? null : (Packet) new ClientboundCustomPayloadPacket(payload);
11799
}
118100

101+
/**
102+
* {@return a newly created menu}
103+
*
104+
* @param syncId the synchronization id of the menu
105+
* @param inventory the player's inventory
106+
* @param player the player opening the menu
107+
*/
119108
@Nullable
120109
@Override
121-
public final AbstractContainerMenu createMenu(int syncId, Inventory inventory, Player player) {
122-
SynchronizedMenu<?> menu = this.openMenu(syncId, inventory, player);
123-
if (menu != null) {
124-
menu.registerData(menu.getData());
125-
}
126-
return menu;
127-
}
110+
public abstract SynchronizedMenu<? extends BaseBlockEntity> createMenu(int syncId, Inventory inventory, Player player);
128111

129112
@Override
130113
public @NotNull Component getDisplayName() {

src/main/java/dev/galacticraft/machinelib/api/block/entity/MachineBlockEntity.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected MachineBlockEntity(BlockEntityType<? extends MachineBlockEntity> type,
102102
this.energyStorage = spec.createEnergyStorage();
103103
}
104104

105-
public static <T extends MachineBlockEntity> void registerProviders(@NotNull BlockEntityType<T> type) {
105+
public static <T extends MachineBlockEntity> void registerProviders(@NotNull BlockEntityType<? extends T> type) {
106106
EnergyStorage.SIDED.registerForBlockEntity((blockEntity, direction) -> {
107107
if (direction == null) return blockEntity.energyStorage().getExposedStorage(ResourceFlow.BOTH);
108108
return blockEntity.energyStorage().getExposedStorage(blockEntity.getIOConfig().get(Objects.requireNonNull(BlockFace.from(blockEntity.getBlockState(), direction))).getFlow());
@@ -118,8 +118,8 @@ public static <T extends MachineBlockEntity> void registerProviders(@NotNull Blo
118118
}
119119

120120
@SafeVarargs
121-
public static <T extends MachineBlockEntity> void registerProviders(BlockEntityType<T> @NotNull ... types) {
122-
for (BlockEntityType<T> type : types) {
121+
public static <T extends MachineBlockEntity> void registerProviders(BlockEntityType<? extends T> @NotNull ... types) {
122+
for (BlockEntityType<? extends T> type : types) {
123123
registerProviders(type);
124124
}
125125
}
@@ -128,7 +128,7 @@ public static <T extends MachineBlockEntity> void registerProviders(BlockEntityT
128128
protected abstract @NotNull MachineStatus tick(@NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull ProfilerFiller profiler);
129129

130130
@Override
131-
public abstract @Nullable MachineMenu<? extends MachineBlockEntity> openMenu(int syncId, Inventory inventory, Player player);
131+
public abstract @Nullable MachineMenu<? extends MachineBlockEntity> createMenu(int syncId, Inventory inventory, Player player);
132132

133133
/**
134134
* {@return the item storage of this machine}
@@ -256,10 +256,10 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
256256
tag.put(Constant.Nbt.ITEM_STORAGE, this.itemStorage.createTag());
257257
}
258258
if (this.fluidStorage.size() > 0) {
259-
tag.put(Constant.Nbt.FLUID_STORAGE, this.itemStorage.createTag());
259+
tag.put(Constant.Nbt.FLUID_STORAGE, this.fluidStorage.createTag());
260260
}
261261
if (this.energyStorage.getCapacity() > 0) {
262-
tag.put(Constant.Nbt.ENERGY_STORAGE, this.itemStorage.createTag());
262+
tag.put(Constant.Nbt.ENERGY_STORAGE, this.energyStorage.createTag());
263263
}
264264
}
265265

src/main/java/dev/galacticraft/machinelib/api/compat/vanilla/CraftingRecipeTestContainer.java

-125
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.api.compat.vanilla;
24+
25+
import dev.galacticraft.machinelib.api.storage.slot.ItemResourceSlot;
26+
import dev.galacticraft.machinelib.api.util.ItemStackUtil;
27+
import net.minecraft.world.item.ItemStack;
28+
29+
import java.util.AbstractList;
30+
31+
public class ItemList extends AbstractList<ItemStack> {
32+
private final ItemResourceSlot[] slots;
33+
34+
public ItemList(ItemResourceSlot[] slots) {
35+
this.slots = slots;
36+
}
37+
38+
@Override
39+
public ItemStack get(int index) {
40+
return ItemStackUtil.create(this.slots[index]);
41+
}
42+
43+
@Override
44+
public int size() {
45+
return this.slots.length;
46+
}
47+
}

0 commit comments

Comments
 (0)