import { Container, ItemStack } from "$/common/inventory.ts"; import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; import type { GridRecipe } from "$/common/mod_data.ts"; // recipes come from mods, see RecipeBook in common/mod_loader.ts function get_crafting_grid(crafting: Container): (string | undefined)[] { const grid: (string | undefined)[] = []; for (let i = 0; i < 9; i++) { grid.push(crafting.get_item(i)?.type_id); } return grid; } function matches_recipe(grid: (string | undefined)[], recipe: GridRecipe): boolean { for (let y = 0; y <= 3 - recipe.height; y++) { for (let x = 0; x <= 3 - recipe.width; x++) { let match = true; for (let gy = 0; gy < 3; gy++) { for (let gx = 0; gx < 3; gx++) { const grid_index = gy * 3 + gx; if (gx >= x && gx < x + recipe.width && gy >= y && gy < y + recipe.height) { const recipe_index = (gy - y) * recipe.width + (gx - x); if (grid[grid_index] !== recipe.pattern[recipe_index]) { match = false; break; } } else if (grid[grid_index] !== undefined) { match = false; break; } } if (!match) break; } if (match) return true; } } return false; } // puts what the grid makes in the result slot export function update_crafting_result(crafting: Container, recipes: GridRecipe[]) { const grid = get_crafting_grid(crafting); const recipe = recipes.find((recipe) => matches_recipe(grid, recipe)); crafting.set_item(CRAFTING_RESULT_SLOT, recipe ? new ItemStack(recipe.result.id, recipe.result.count) : undefined); } export function consume_recipe_items(crafting: Container) { for (let i = 0; i < 9; i++) { const slot = crafting.get_slot(i); if (slot.has_item()) { slot.amount = slot.amount! - 1; } } }