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