Skip to content

Commit 0d49c5c

Browse files
committed
fix: don't cache recipe inputs
* remember to initialize data types on the client * query output slots when checking for space
1 parent b562093 commit 0d49c5c

File tree

5 files changed

+19
-69
lines changed

5 files changed

+19
-69
lines changed

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

+1-39
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import net.minecraft.world.item.crafting.RecipeType;
3535
import net.minecraft.world.level.block.entity.BlockEntityType;
3636
import net.minecraft.world.level.block.state.BlockState;
37-
import org.jetbrains.annotations.Contract;
3837
import org.jetbrains.annotations.NotNull;
3938

4039
/**
@@ -44,11 +43,6 @@
4443
* @param <R> The type of recipe the machine uses.
4544
*/
4645
public abstract class BasicRecipeMachineBlockEntity<I extends RecipeInput, R extends Recipe<I>> extends RecipeMachineBlockEntity<I, R> {
47-
/**
48-
* An inventory for use in finding vanilla recipes for this machine.
49-
*/
50-
protected final @NotNull I craftingInv;
51-
5246
protected final SlottedStorageAccess<Item, ItemResourceSlot> inputSlots;
5347
protected final SlottedStorageAccess<Item, ItemResourceSlot> outputSlots;
5448

@@ -101,52 +95,20 @@ protected BasicRecipeMachineBlockEntity(BlockEntityType<? extends BasicRecipeMac
10195

10296
this.inputSlots = this.itemStorage().subStorage(inputSlots, inputSlotsLen);
10397
this.outputSlots = this.itemStorage().subStorage(outputSlots, outputSlotsLen);
104-
105-
this.craftingInv = this.createCraftingInv();
106-
}
107-
108-
protected abstract I createCraftingInv();
109-
110-
/**
111-
* Creates an inventory for use in finding vanilla recipes for this machine.
112-
* NOTE: This inventory can assume that it is never modified - do not modify it!
113-
*
114-
* @return The crafting inventory of the machine.
115-
*/
116-
@Override
117-
@Contract(pure = true)
118-
protected @NotNull I craftingInv() {
119-
return this.craftingInv;
12098
}
12199

122-
/**
123-
* Inserts the active recipe's output into the machine's inventory.
124-
*
125-
* @param recipe The recipe to output.
126-
*/
127100
@Override
128101
protected void outputStacks(@NotNull RecipeHolder<R> recipe) {
129102
ItemStack assembled = recipe.value().assemble(this.craftingInv(), this.level.registryAccess());
130103
this.outputSlots.insertMatching(assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
131104
}
132105

133-
/**
134-
* Checks if the machine can output stacks for the given recipe.
135-
*
136-
* @param recipe The recipe to check.
137-
* @return {@code true} if the machine can output stacks for the recipe, {@code false} otherwise.
138-
*/
139106
@Override
140107
protected boolean canOutputStacks(@NotNull RecipeHolder<R> recipe) {
141108
ItemStack assembled = recipe.value().assemble(this.craftingInv(), this.level.registryAccess());
142-
return this.inputSlots.canInsert(assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
109+
return this.outputSlots.canInsert(assembled.getItem(), assembled.getComponentsPatch(), assembled.getCount());
143110
}
144111

145-
/**
146-
* Extracts the recipe's input from the machine's inventory.
147-
*
148-
* @param recipe The recipe to extract.
149-
*/
150112
@Override
151113
protected void extractCraftingMaterials(@NotNull RecipeHolder<R> recipe) {
152114
for (ItemResourceSlot slot : this.inputSlots) {

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

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ protected RecipeMachineBlockEntity(@NotNull BlockEntityType<? extends RecipeMach
101101

102102
/**
103103
* An inventory for use in finding vanilla recipes for this machine.
104-
* NOTE: This inventory can assume that it is never modified - do not modify it!
105104
*
106105
* @return The crafting inventory of the machine.
107106
*/

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

+8-27
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,23 @@
3737

3838
public class RecipeHelper {
3939
public static @NotNull CraftingInput craftingInput(int width, int height, SlottedStorageAccess<Item, ItemResourceSlot> storage) {
40-
ItemResourceSlot[] slots = new ItemResourceSlot[width * height];
41-
ItemResourceSlot[] storageSlots = storage.getSlots();
42-
int j = 0;
40+
List<ItemStack> items = new ArrayList<>(width * height);
4341
for (int i = 0; i < storage.size(); i++) {
44-
if (storageSlots[i].transferMode().isInput()) {
45-
slots[j++] = storageSlots[i];
42+
ItemResourceSlot slot = storage.slot(i);
43+
if (slot.transferMode().isInput()) {
44+
items.add(ItemStackUtil.create(slot));
4645
}
4746
}
48-
assert j == width * height;
4947

50-
return craftingInput(width, height, slots);
48+
return CraftingInput.of(width, height, items);
5149
}
5250

5351
public static @NotNull CraftingInput craftingInput(int offset, int width, int height, SlottedStorageAccess<Item, ItemResourceSlot> storage) {
54-
ItemResourceSlot[] slots = new ItemResourceSlot[width * height];
52+
List<ItemStack> items = new ArrayList<>(width * height);
5553
for (int i = 0; i < width * height; i++) {
56-
slots[i] = storage.getSlots()[offset + i];
54+
items.add(ItemStackUtil.create(storage.slot(offset + i)));
5755
}
58-
return craftingInput(width, height, slots);
56+
return CraftingInput.of(width, height, items);
5957
}
6058

6159
public static @NotNull CraftingInput craftingInput(int width, int height, ItemResourceSlot... slots) {
@@ -87,24 +85,7 @@ public class RecipeHelper {
8785
return new MachineRecipeInput(slots);
8886
}
8987

90-
public static @NotNull RecipeInput input(ItemResourceSlot slot) {
91-
return new SingleSlotInput(slot);
92-
}
93-
9488
public static @NotNull SingleRecipeInput single(ItemResourceSlot slot) {
9589
return new SingleRecipeInput(ItemStackUtil.create(slot));
9690
}
97-
98-
private record SingleSlotInput(ItemResourceSlot slot) implements RecipeInput {
99-
@Override
100-
public @NotNull ItemStack getItem(int i) {
101-
if (i == 0) return ItemStackUtil.create(slot);
102-
throw new IndexOutOfBoundsException("Index: " + i);
103-
}
104-
105-
@Override
106-
public int size() {
107-
return 1;
108-
}
109-
}
11091
}

src/main/java/dev/galacticraft/machinelib/api/menu/SynchronizedMenuType.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ protected SynchronizedMenuType(Factory<BE, Menu> factory) {
6060

6161
@Contract(value = "_ -> new", pure = true)
6262
public static <BE extends BaseBlockEntity, Menu extends SynchronizedMenu<BE>> @NotNull MenuType<Menu> create(ExtendedScreenHandlerType.ExtendedFactory<Menu, BlockPos> factory) {
63-
return new ExtendedScreenHandlerType<>(factory, BlockPos.STREAM_CODEC);
63+
return new ExtendedScreenHandlerType<>((syncId, inventory, data) -> {
64+
Menu menu = factory.create(syncId, inventory, data);
65+
menu.registerData(menu.getData());
66+
return menu;
67+
}, BlockPos.STREAM_CODEC);
6468
}
6569

6670
@Override

src/main/java/dev/galacticraft/machinelib/impl/network/s2c/MenuSyncPayload.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public void apply(ClientPlayNetworking.Context context) {
4848
if (player.containerMenu instanceof MachineMenu<?> menu && syncId == menu.containerId) {
4949
menu.getData().handle(this.buf);
5050
} else {
51-
MachineLib.LOGGER.warn("Received menu sync packet for invalid menu ID: {} (active: {})", syncId, player.containerMenu.containerId);
51+
if (player.containerMenu.containerId != 0) {
52+
MachineLib.LOGGER.warn("Received menu sync packet for invalid menu ID: {} (active: {})", syncId, player.containerMenu.containerId);
53+
} else {
54+
MachineLib.LOGGER.debug("Received menu sync packet for '{}' with no menu open", syncId);
55+
}
5256
}
5357
}
5458
}

0 commit comments

Comments
 (0)