feat: Added regex support in crafting recipe

This commit is contained in:
Jakub Žitník 2025-03-02 15:45:21 +01:00
parent 22f98e7ff5
commit 673e1c63f1
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
4 changed files with 12 additions and 9 deletions

View File

@ -8,4 +8,5 @@ public @interface CraftingRecipeRegistry {
String[] recipe(); String[] recipe();
String result(); String result();
int amount(); int amount();
boolean usingRegex() default false;
} }

View File

@ -11,4 +11,5 @@ import java.util.function.Supplier;
public class CraftingRecipe { public class CraftingRecipe {
private String[][] recipe; private String[][] recipe;
private Supplier<InventoryItem> itemSupplier; private Supplier<InventoryItem> itemSupplier;
private boolean usingRegex;
} }

View File

@ -24,7 +24,7 @@ public class CraftingRecipeList {
String[][] recipeGrid = convertTo2DGrid(annotation.recipe()); String[][] recipeGrid = convertTo2DGrid(annotation.recipe());
recipes.add(new CraftingRecipe(recipeGrid, recipes.add(new CraftingRecipe(recipeGrid,
() -> new InventoryItem(annotation.amount(), ItemBlockSupplier.getItem(annotation.result())))); () -> new InventoryItem(annotation.amount(), ItemBlockSupplier.getItem(annotation.result())), annotation.usingRegex()));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -53,7 +53,7 @@ public class CraftingRecipeList {
public static Optional<CraftingRecipe> getRecipe(String[] r) { public static Optional<CraftingRecipe> getRecipe(String[] r) {
for (CraftingRecipe recipe : recipes) { for (CraftingRecipe recipe : recipes) {
if (matchesByItemSet(recipe.getRecipe(), r)) { if (matchesByItemSet(recipe.getRecipe(), r, recipe.isUsingRegex())) {
return Optional.of(recipe); return Optional.of(recipe);
} }
} }
@ -90,7 +90,7 @@ public class CraftingRecipeList {
return trimmedArray; return trimmedArray;
} }
public static boolean are2DArraysIdentical(String[][] array1, String[][] array2) { public static boolean are2DArraysIdentical(String[][] array1, String[][] array2, boolean usingRegex) {
if (array1.length != array2.length) { if (array1.length != array2.length) {
return false; return false;
} }
@ -104,7 +104,7 @@ public class CraftingRecipeList {
if (array1[i][j] == null && array2[i][j] != null) { if (array1[i][j] == null && array2[i][j] != null) {
return false; return false;
} }
if (array1[i][j] != null && !array1[i][j].equals(array2[i][j])) { if (array1[i][j] != null && (usingRegex ? !array2[i][j].matches(array1[i][j]) : !array1[i][j].equals(array2[i][j])) ) {
return false; return false;
} }
} }
@ -113,7 +113,7 @@ public class CraftingRecipeList {
return true; return true;
} }
private static boolean matchesByItemSet(String[][] recipe, String[] grid) { private static boolean matchesByItemSet(String[][] recipe, String[] grid, boolean usingRegex) {
String[][] finalGrid = new String[3][3]; String[][] finalGrid = new String[3][3];
if (grid.length == 4) { if (grid.length == 4) {
@ -136,6 +136,6 @@ public class CraftingRecipeList {
var croppedRecipe = trimNullEdges(recipe); var croppedRecipe = trimNullEdges(recipe);
var croppedGrid = trimNullEdges(finalGrid); var croppedGrid = trimNullEdges(finalGrid);
return are2DArraysIdentical(croppedRecipe, croppedGrid); return are2DArraysIdentical(croppedRecipe, croppedGrid, usingRegex);
} }
} }

View File

@ -4,11 +4,12 @@ import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry( @CraftingRecipeRegistry(
recipe = { recipe = {
"dirt", "_", "_", "^.*_wool$", "^.*_wool$", "^.*_wool$",
"_", "_", "_", "oak_planks", "oak_planks", "oak_planks",
"_", "_", "_" "_", "_", "_"
}, },
result = "bed", result = "bed",
amount = 1 amount = 1,
usingRegex = true
) )
public class BedRecipe {} public class BedRecipe {}