Skip to content

Commit 4051045

Browse files
committed
fix: Fix crafting refill on 2x2 grids not filling correctly and voiding one item (#261)
* fix: Fix items being put into wrong slots on 2x2 grid refill #255 * fix: Do not try to refill when the grid does not support the recipe size
1 parent 922d351 commit 4051045

4 files changed

Lines changed: 29 additions & 9 deletions

File tree

common/src/main/java/net/blay09/mods/craftingtweaks/api/RecipeMapper.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
import java.util.Optional;
88

99
public interface RecipeMapper<T extends Recipe<?>> {
10-
int mapToMatrixSlot(T recipe, int ingredientIndex);
10+
/**
11+
* @deprecated Use {@link #mapToMatrixSlot(Recipe, int, int)} which supports a custom grid width instead.
12+
*/
13+
@Deprecated
14+
default int mapToMatrixSlot(T recipe, int ingredientIndex) {
15+
return mapToMatrixSlot(recipe, 3, ingredientIndex);
16+
}
17+
18+
default int mapToMatrixSlot(T recipe, int gridWidth, int ingredientIndex) {
19+
return mapToMatrixSlot(recipe, ingredientIndex);
20+
}
21+
1122
List<Optional<Ingredient>> getIngredients(T recipe);
1223
}

common/src/main/java/net/blay09/mods/craftingtweaks/api/impl/DefaultGridRefillHandler.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import net.blay09.mods.craftingtweaks.api.CraftingGrid;
55
import net.blay09.mods.craftingtweaks.api.CraftingTweaksAPI;
66
import net.blay09.mods.craftingtweaks.api.GridRefillHandler;
7-
import net.blay09.mods.craftingtweaks.crafting.CraftingContext;
87
import net.blay09.mods.craftingtweaks.crafting.ContainerIngredientProvider;
8+
import net.blay09.mods.craftingtweaks.crafting.CraftingContext;
99
import net.blay09.mods.craftingtweaks.crafting.IngredientToken;
1010
import net.minecraft.world.entity.player.Player;
1111
import net.minecraft.world.inventory.AbstractContainerMenu;
@@ -27,23 +27,32 @@ public void refillRecipe(CraftingGrid grid, Player player, AbstractContainerMenu
2727
return;
2828
}
2929

30+
// TODO Would be nice if our Grid API had info on width/height so this didn't need to be hardcoded
31+
final var gridSize = grid.getGridSize(player, menu);
32+
final var gridWidth = gridSize == 4 ? 2 : 3;
33+
final var gridHeight = gridSize == 4 ? 2 : 3;
34+
3035
final var recipe = recipeHolder.value();
3136
final var context = new CraftingContext(List.of(new ContainerIngredientProvider(player.getInventory())));
3237
final var operation = context.createOperation((RecipeHolder<Recipe<?>>) recipeHolder).prepare();
3338
if (!operation.canCraft()) {
3439
return;
3540
}
3641

42+
final var matrixMapper = CraftingTweaksAPI.getRecipeMapper(recipe.getClass());
43+
if (matrixMapper.getIngredients(recipe).size() > gridSize) {
44+
return;
45+
}
46+
3747
int operations = 0;
3848
outer:
3949
do {
4050
final var ingredientTokens = operation.getIngredientTokens();
41-
final var matrixMapper = CraftingTweaksAPI.getRecipeMapper(recipe.getClass());
4251

4352
final var matrixDiff = new HashMap<Integer, IngredientToken>();
4453
for (int i = 0; i < ingredientTokens.size(); i++) {
4554
final var ingredientToken = ingredientTokens.get(i);
46-
var matrixSlot = matrixMapper.mapToMatrixSlot(recipe, i);
55+
var matrixSlot = matrixMapper.mapToMatrixSlot(recipe, gridWidth, i);
4756
if (matrixSlot != -1) {
4857
final var itemStack = ingredientToken.peek();
4958
final var slotStack = craftMatrix.getItem(matrixSlot);

common/src/main/java/net/blay09/mods/craftingtweaks/crafting/ShapedRecipeMatrixMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
public class ShapedRecipeMatrixMapper implements RecipeMapper<ShapedRecipe> {
1111
@Override
12-
public int mapToMatrixSlot(ShapedRecipe recipe, int ingredientIndex) {
12+
public int mapToMatrixSlot(ShapedRecipe recipe, int gridWidth, int ingredientIndex) {
1313
final int recipeWidth = recipe.getWidth();
1414
final int origX = ingredientIndex % recipeWidth;
1515
final int origY = ingredientIndex / recipeWidth;
1616

17-
// Offset to center the recipe if its width is 1
18-
final int offsetX = recipeWidth == 1 ? 1 : 0;
17+
// Offset to center the recipe if its width is 1 in a 3-wide grid
18+
final int offsetX = gridWidth == 3 && recipeWidth == 1 ? 1 : 0;
1919

20-
return origY * 3 + origX + offsetX;
20+
return origY * gridWidth + origX + offsetX;
2121
}
2222

2323
@Override

common/src/main/java/net/blay09/mods/craftingtweaks/crafting/ShapelessRecipeMatrixMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class ShapelessRecipeMatrixMapper implements RecipeMapper<ShapelessRecipe> {
1212
@Override
13-
public int mapToMatrixSlot(ShapelessRecipe recipe, int ingredientIndex) {
13+
public int mapToMatrixSlot(ShapelessRecipe recipe, int gridWidth, int ingredientIndex) {
1414
return ingredientIndex;
1515
}
1616

0 commit comments

Comments
 (0)