refactor: Rewritten crafting table

This commit is contained in:
Jakub Žitník 2025-03-02 10:23:29 +01:00
parent 16ecf3bc72
commit be32774d4c
Signed by: jzitnik
GPG Key ID: C577A802A6AF4EF3
16 changed files with 240 additions and 87 deletions

View File

@ -0,0 +1,11 @@
package cz.jzitnik.game.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface CraftingRecipeRegistry {
String[] recipe();
String result();
int amount();
}

View File

@ -2,6 +2,8 @@ package cz.jzitnik.game.crafting;
import cz.jzitnik.game.entities.items.InventoryItem;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
import org.reflections.Reflections;
import java.util.*;
@ -9,97 +11,44 @@ public class CraftingRecipeList {
private static final List<CraftingRecipe> recipes = new ArrayList<>();
static {
// Oak planks
recipes.add(new CraftingRecipe(new String[][]{
{"oak_log", null, null},
{null, null, null},
{null, null, null}
}, () -> new InventoryItem(4, ItemBlockSupplier.getItem("oak_planks"))));
registerRecipes();
}
// Crafting table
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", "oak_planks", null},
{"oak_planks", "oak_planks", null},
{null, null, null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("crafting_table"))));
private static void registerRecipes() {
Reflections reflections = new Reflections("cz.jzitnik.game.crafting.recipes");
Set<Class<?>> recipeClasses = reflections.getTypesAnnotatedWith(CraftingRecipeRegistry.class);
// Stick
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", null, null},
{"oak_planks", null, null},
{null, null, null}
}, () -> new InventoryItem(4, ItemBlockSupplier.getItem("stick"))));
for (Class<?> clazz : recipeClasses) {
try {
CraftingRecipeRegistry annotation = clazz.getAnnotation(CraftingRecipeRegistry.class);
String[][] recipeGrid = convertTo2DGrid(annotation.recipe());
// Wooden pickaxe
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", "oak_planks", "oak_planks"},
{null, "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_pickaxe"))));
recipes.add(new CraftingRecipe(recipeGrid,
() -> new InventoryItem(annotation.amount(), ItemBlockSupplier.getItem(annotation.result()))));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Wooden axe
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", "oak_planks", null},
{"oak_planks", "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_axe"))));
recipes.add(new CraftingRecipe(new String[][]{
{null, "oak_planks", "oak_planks"},
{null, "stick", "oak_planks"},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_axe"))));
private static String[][] convertTo2DGrid(String[] flatGrid) {
int size = (int) Math.sqrt(flatGrid.length); // Assumes 3x3
String[][] grid = new String[size][size];
// Stone pickaxe
recipes.add(new CraftingRecipe(new String[][]{
{"cobblestone", "cobblestone", "cobblestone"},
{null, "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_axe"))));
for (int i = 0; i < size; i++) {
System.arraycopy(flatGrid, i * size, grid[i], 0, size);
}
// Stone axe
recipes.add(new CraftingRecipe(new String[][]{
{"cobblestone", "cobblestone", null},
{"cobblestone", "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_axe"))));
recipes.add(new CraftingRecipe(new String[][]{
{null, "cobblestone", "cobblestone"},
{null, "stick", "cobblestone"},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_axe"))));
// Convert "_" placeholders back to null
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (grid[i][j].equals("_")) {
grid[i][j] = null;
}
}
}
// Chest
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", "oak_planks", "oak_planks"},
{"oak_planks", null, "oak_planks"},
{"oak_planks", "oak_planks", "oak_planks"}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("chest"))));
// Wooden shovel
recipes.add(new CraftingRecipe(new String[][]{
{null, "oak_planks", null},
{null, "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("wooden_shovel"))));
// Stone shovel
recipes.add(new CraftingRecipe(new String[][]{
{null, "cobblestone", null},
{null, "stick", null},
{null, "stick", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("stone_shovel"))));
recipes.add(new CraftingRecipe(new String[][]{
{"cobblestone", "cobblestone", "cobblestone"},
{"cobblestone", null, "cobblestone"},
{"cobblestone", "cobblestone", "cobblestone"}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("furnace"))));
recipes.add(new CraftingRecipe(new String[][]{
{"oak_planks", "oak_planks", null},
{"oak_planks", "oak_planks", null},
{"oak_planks", "oak_planks", null}
}, () -> new InventoryItem(1, ItemBlockSupplier.getItem("oak_door"))));
return grid;
}
public static Optional<CraftingRecipe> getRecipe(String[] r) {
@ -115,7 +64,6 @@ public class CraftingRecipeList {
int n = array.length;
int minRow = n, maxRow = -1, minCol = n, maxCol = -1;
// Find the bounding box of non-null values
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (array[i][j] != null) {
@ -127,7 +75,6 @@ public class CraftingRecipeList {
}
}
// If all elements are null, return an empty array
if (maxRow == -1) {
return new String[0][0];
}
@ -136,7 +83,6 @@ public class CraftingRecipeList {
int newSizeCol = maxCol - minCol + 1;
String[][] trimmedArray = new String[newSizeRow][newSizeCol];
// Copy the relevant portion of the array
for (int i = 0; i < newSizeRow; i++) {
System.arraycopy(array[minRow + i], minCol, trimmedArray[i], 0, newSizeCol);
}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "oak_planks", "oak_planks",
"oak_planks", "_", "oak_planks",
"oak_planks", "oak_planks", "oak_planks"
},
result = "chest",
amount = 1
)
public class ChestRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "oak_planks", "_",
"oak_planks", "oak_planks", "_",
"_", "_", "_"
},
result = "crafting_table",
amount = 1
)
public class CraftingTableRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"cobblestone", "cobblestone", "cobblestone",
"cobblestone", "_", "cobblestone",
"cobblestone", "cobblestone", "cobblestone"
},
result = "furnace",
amount = 1
)
public class FurnaceRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "oak_planks", "_",
"oak_planks", "oak_planks", "_",
"oak_planks", "oak_planks", "_"
},
result = "oak_door",
amount = 3
)
public class OakDoorRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_log", "_", "_",
"_", "_", "_",
"_", "_", "_"
},
result = "oak_planks",
amount = 4
)
public class OakPlanksRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "_", "_",
"oak_planks", "_", "_",
"_", "_", "_"
},
result = "stick",
amount = 4
)
public class StickRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"_", "cobblestone", "cobblestone",
"_", "stick", "cobblestone",
"_", "stick", "_"
},
result = "stone_pickaxe",
amount = 1
)
public class StoneAxe2Recipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"cobblestone", "cobblestone", "_",
"cobblestone", "stick", "_",
"_", "stick", "_"
},
result = "stone_pickaxe",
amount = 1
)
public class StoneAxeRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"cobblestone", "cobblestone", "cobblestone",
"_", "stick", "_",
"_", "stick", "_"
},
result = "stone_pickaxe",
amount = 1
)
public class StonePickaxeRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"_", "cobblestone", "_",
"_", "stick", "_",
"_", "stick", "_"
},
result = "stone_shovel",
amount = 1
)
public class StoneShovelRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"_", "oak_planks", "oak_planks",
"_", "stick", "oak_planks",
"_", "stick", "_"
},
result = "wooden_axe",
amount = 1
)
public class WoodenAxe2Recipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "oak_planks", "_",
"oak_planks", "stick", "_",
"_", "stick", "_"
},
result = "oak_planks",
amount = 1
)
public class WoodenAxeRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"oak_planks", "oak_planks", "oak_planks",
"_", "stick", "_",
"_", "stick", "_"
},
result = "wooden_pickaxe",
amount = 1
)
public class WoodenPickaxeRecipe {}

View File

@ -0,0 +1,14 @@
package cz.jzitnik.game.crafting.recipes;
import cz.jzitnik.game.annotations.CraftingRecipeRegistry;
@CraftingRecipeRegistry(
recipe = {
"_", "oak_planks", "_",
"_", "stick", "_",
"_", "stick", "_"
},
result = "wooden_shovel",
amount = 1
)
public class WoodenShovelRecipe {}