forked from jzitnik/twodcraft
feat: Added regex support in crafting recipe
This commit is contained in:
parent
22f98e7ff5
commit
673e1c63f1
@ -8,4 +8,5 @@ public @interface CraftingRecipeRegistry {
|
||||
String[] recipe();
|
||||
String result();
|
||||
int amount();
|
||||
boolean usingRegex() default false;
|
||||
}
|
||||
|
@ -11,4 +11,5 @@ import java.util.function.Supplier;
|
||||
public class CraftingRecipe {
|
||||
private String[][] recipe;
|
||||
private Supplier<InventoryItem> itemSupplier;
|
||||
private boolean usingRegex;
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class CraftingRecipeList {
|
||||
String[][] recipeGrid = convertTo2DGrid(annotation.recipe());
|
||||
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -53,7 +53,7 @@ public class CraftingRecipeList {
|
||||
|
||||
public static Optional<CraftingRecipe> getRecipe(String[] r) {
|
||||
for (CraftingRecipe recipe : recipes) {
|
||||
if (matchesByItemSet(recipe.getRecipe(), r)) {
|
||||
if (matchesByItemSet(recipe.getRecipe(), r, recipe.isUsingRegex())) {
|
||||
return Optional.of(recipe);
|
||||
}
|
||||
}
|
||||
@ -90,7 +90,7 @@ public class CraftingRecipeList {
|
||||
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) {
|
||||
return false;
|
||||
}
|
||||
@ -104,7 +104,7 @@ public class CraftingRecipeList {
|
||||
if (array1[i][j] == null && array2[i][j] != null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -113,7 +113,7 @@ public class CraftingRecipeList {
|
||||
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];
|
||||
|
||||
if (grid.length == 4) {
|
||||
@ -136,6 +136,6 @@ public class CraftingRecipeList {
|
||||
var croppedRecipe = trimNullEdges(recipe);
|
||||
var croppedGrid = trimNullEdges(finalGrid);
|
||||
|
||||
return are2DArraysIdentical(croppedRecipe, croppedGrid);
|
||||
return are2DArraysIdentical(croppedRecipe, croppedGrid, usingRegex);
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,12 @@ import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
|
||||
|
||||
@CraftingRecipeRegistry(
|
||||
recipe = {
|
||||
"dirt", "_", "_",
|
||||
"_", "_", "_",
|
||||
"^.*_wool$", "^.*_wool$", "^.*_wool$",
|
||||
"oak_planks", "oak_planks", "oak_planks",
|
||||
"_", "_", "_"
|
||||
},
|
||||
result = "bed",
|
||||
amount = 1
|
||||
amount = 1,
|
||||
usingRegex = true
|
||||
)
|
||||
public class BedRecipe {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user