// farmer's delight's kitchen. the stove is a heat source, like its StoveBlock. the cooking pot, like its // CookingPotBlockEntity, cooks its recipes while something hot is under it, keeps the meal, and serves it into the // container the meal needs (a bowl), or straight out when it needs none import type { BlockRef, Container, ScreenHandle, ServerContext } from "bworld/server"; export function setup(ctx: ServerContext) { register_stove(ctx); register_cooking_pot(ctx); } // stove // what lights an unlit stove, used up. the java mod's are flint and steel and fire charges, which bworld doesn't have const STOVE_FUEL = "bworld:coal"; // what puts it out: any shovel, like the java mod (a water bucket too, once there are buckets) const STOVE_EXTINGUISHER_TOOL = "shovel"; // standing on a lit stove hurts, like a magma block: 1 health every 10 ticks const STOVE_BURN_DAMAGE = 1; const STOVE_BURN_TICKS = 10; function register_stove(ctx: ServerContext) { const lit = (block: BlockRef) => ctx.world.get_state(block.x, block.y, block.z, "lit") === 1; const set_lit = (block: BlockRef, value: boolean) => ctx.world.set_state(block.x, block.y, block.z, "lit", value ? 1 : 0); ctx.components.register_block("farmersdelight:stove", { // placed lit, like the java mod. block data so it ticks, to burn whoever stands on it on_create(block) { block.data ??= {}; }, on_interact(block, _params, player) { const held = player.inventory.get(player.selected_slot); if (!held) return false; if (lit(block)) { if (ctx.items.tool(held.id) !== STOVE_EXTINGUISHER_TOOL) return false; set_lit(block, false); return true; } if (held.id !== STOVE_FUEL) return false; held.count -= 1; set_lit(block, true); return true; }, on_tick(block) { if (ctx.system.current_tick % STOVE_BURN_TICKS !== 0 || !lit(block)) return; for (const player of ctx.players.all()) { const { x, y, z } = player.position; if (Math.floor(x) === block.x && Math.floor(z) === block.z && Math.abs(y - (block.y + 1)) < 0.01) { player.damage(STOVE_BURN_DAMAGE, "hot_floor"); } } }, }); } // cooking pot // slots 0-5 are the ingredients, 6 the meal waiting to be served, 7 containers for it, 8 the served meal const INGREDIENT_SLOTS = 6; const MEAL_SLOT = 6; const CONTAINER_SLOT = 7; const OUTPUT_SLOT = 8; const POT_SIZE = 9; // ticks, for recipes that don't say const DEFAULT_COOK_TIME = 200; // what's under the pot that heats it, and the state that has to be 1 for it to, like minecraft's heat_sources tag const HEAT_SOURCES: Record = { "bworld:furnace": "lit", "farmersdelight:stove": "lit", }; // a farmersdelight:cooking recipe: the ingredients in any slots, each an item id or a list of ids any of which will do interface CookingRecipe { ingredients: (string | string[])[]; result: { id: string; count: number }; // what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked container?: string; cook_time: number; } interface PotData { container: string; // ticks, counting up to cook_time_total cook_time: number; cook_time_total: number; // what the meal in the meal slot is served in, "" for nothing meal_container: string; } const POT_BARS = [ { x: 2.2, y: 2, value: "heat", max: "heat_max", direction: "up" as const, empty_texture: "bworld:fire_empty", full_texture: "bworld:fire_full", }, { x: 4.6, y: 0.5, value: "progress", max: "progress_max", direction: "right" as const, empty_texture: "bworld:arrow_empty", full_texture: "bworld:arrow_full", }, ]; // checked when the server starts, so a broken recipe is a load error naming it function load_recipes(ctx: ServerContext): CookingRecipe[] { return ctx.recipes.list("farmersdelight:cooking").map((json, i) => { const where = `farmersdelight:cooking recipe ${i + 1} (${json.result?.id ?? "no result"})`; const item = (id: unknown) => { if (typeof id !== "string" || !ctx.items.exists(id)) throw new Error(`${where}: there's no item ${id}`); return id; }; const ingredients = json.ingredients; if (!Array.isArray(ingredients) || ingredients.length < 1 || ingredients.length > INGREDIENT_SLOTS) { throw new Error(`${where}: ingredients must be 1 to ${INGREDIENT_SLOTS} items`); } for (const ingredient of ingredients) { if (Array.isArray(ingredient)) { if (ingredient.length === 0) throw new Error(`${where}: an ingredient's list of items is empty`); ingredient.forEach(item); } else { item(ingredient); } } item(json.result?.id); if (!Number.isInteger(json.result.count) || json.result.count < 1) { throw new Error(`${where}: result.count must be a positive whole number`); } if (json.container !== undefined) item(json.container); const cook_time = json.cook_time ?? DEFAULT_COOK_TIME; if (!Number.isInteger(cook_time) || cook_time < 1) { throw new Error(`${where}: cook_time must be positive ticks`); } return { ingredients, result: json.result, container: json.container, cook_time }; }); } // the recipe whose ingredients are exactly what's in the ingredient slots, one item from each, in any order function find_recipe(recipes: CookingRecipe[], container: Container): CookingRecipe | undefined { const items: string[] = []; for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) { const item = container.get(slot); if (item) items.push(item.id); } if (items.length === 0) return undefined; return recipes.find((recipe) => recipe.ingredients.length === items.length && matches(recipe.ingredients, items)); } // pairs every ingredient with a different item, trying each way round, like a shapeless recipe function matches(ingredients: (string | string[])[], items: string[], used = new Set()): boolean { if (ingredients.length === 0) return true; const [first, ...rest] = ingredients; const fits = (id: string) => Array.isArray(first) ? first.includes(id) : first === id; for (let i = 0; i < items.length; i++) { if (used.has(i) || !fits(items[i])) continue; used.add(i); if (matches(rest, items, used)) return true; used.delete(i); } return false; } function register_cooking_pot(ctx: ServerContext) { const recipes = load_recipes(ctx); // who's looking at each pot, by position, to keep their bars moving const viewers = new Map>(); const key = (block: BlockRef) => `${block.x},${block.y},${block.z}`; const pot_data = (block: BlockRef): PotData => { const existing = block.data?.container && ctx.containers.get(block.data.container); const container = existing || ctx.containers.create(POT_SIZE); block.data = { cook_time: 0, cook_time_total: 0, meal_container: "", ...block.data, container: container.id }; return block.data; }; const is_heated = (block: BlockRef) => { const below = ctx.world.get_block(block.x, block.y - 1, block.z); if (!below || !(below in HEAT_SOURCES)) return false; const state = HEAT_SOURCES[below]; return state === undefined || ctx.world.get_state(block.x, block.y - 1, block.z, state) === 1; }; const show = (screen: ScreenHandle, data: PotData, heated: boolean) => { screen.set_property("progress", data.cook_time); screen.set_property("progress_max", data.cook_time_total); screen.set_property("heat", heated ? 1 : 0); screen.set_property("heat_max", 1); }; // there's room in the meal slot for what it makes const can_cook = (container: Container, recipe: CookingRecipe) => { const meal = container.get(MEAL_SLOT); if (!meal) return true; return meal.id === recipe.result.id && meal.count + recipe.result.count <= ctx.items.max_stack(meal.id); }; const cook = (data: PotData, container: Container, recipe: CookingRecipe) => { const meal = container.get(MEAL_SLOT); if (meal) { meal.count += recipe.result.count; } else { container.set(MEAL_SLOT, { ...recipe.result }); } data.meal_container = recipe.container ?? ""; for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) { const item = container.get(slot); if (item) item.count -= 1; } }; // the meal goes to the output, into its containers when it needs them, as much as fits const serve = (data: PotData, container: Container) => { const meal = container.get(MEAL_SLOT); if (!meal) { data.meal_container = ""; return; } const output = container.get(OUTPUT_SLOT); if (output && output.id !== meal.id) return; const room = ctx.items.max_stack(meal.id) - (output?.count ?? 0); let count = Math.min(meal.count, room); const bowls = container.get(CONTAINER_SLOT); if (data.meal_container) { count = bowls?.id === data.meal_container ? Math.min(count, bowls.count) : 0; } if (count <= 0) return; if (data.meal_container) bowls!.count -= count; if (output) { output.count += count; } else { container.set(OUTPUT_SLOT, { id: meal.id, count }); } meal.count -= count; }; ctx.components.register_block("farmersdelight:cooking_pot", { on_create(block) { pot_data(block); }, on_interact(block, _params, player) { const data = pot_data(block); const container = ctx.containers.get(data.container)!; // holding what the meal is served in serves one straight into the hand, like the java mod const meal = container.get(MEAL_SLOT); const held = player.inventory.get(player.selected_slot); if (meal && data.meal_container && held?.id === data.meal_container) { held.count -= 1; meal.count -= 1; player.give_item(meal.id, 1); return true; } const screen = ctx.ui.open_container(player, { container, layout: [ ...Array.from( { length: INGREDIENT_SLOTS }, (_, slot) => ({ slot, x: 1.2 + slot % 3, y: Math.floor(slot / 3) }), ), // the meal stays in the pot until it's served { slot: MEAL_SLOT, x: 6.4, y: 0.5, output_only: true, on_take: () => false }, { slot: CONTAINER_SLOT, x: 4.6, y: 2 }, { slot: OUTPUT_SLOT, x: 6.4, y: 2, output_only: true }, ], rows: 3, bars: POT_BARS, }); show(screen, data, is_heated(block)); const screens = viewers.get(key(block)) ?? new Set(); viewers.set(key(block), screens); screens.add(screen); screen.on_close(() => screens.delete(screen)); return true; }, on_break(block) { const container = block.data?.container && ctx.containers.get(block.data.container); if (!container) return; for (let slot = 0; slot < container.size; slot++) { const item = container.get(slot); if (item) ctx.world.drop_item(block.x, block.y, block.z, item); } ctx.containers.delete(container.id); }, on_tick(block) { const data = pot_data(block); const container = ctx.containers.get(data.container)!; const heated = is_heated(block); const recipe = heated ? find_recipe(recipes, container) : undefined; if (recipe && can_cook(container, recipe)) { data.cook_time_total = recipe.cook_time; data.cook_time += 1; if (data.cook_time >= recipe.cook_time) { data.cook_time = 0; cook(data, container, recipe); } } else { // cools off, losing progress slowly data.cook_time = Math.max(0, data.cook_time - 2); } serve(data, container); for (const screen of viewers.get(key(block)) ?? []) show(screen, data, heated); }, }); }