272 lines
10 KiB
TypeScript
272 lines
10 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// 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. the cutting board, like its
|
|
// CuttingBoardBlockEntity, holds a stack of anything and cuts it one at a time with the right tool
|
|
|
|
import type { BlockRef, Container, ItemStack, ServerContext } from "bworld/server";
|
|
import { COOKING, type CookingRecipe, CUTTING, type CuttingRecipe, INGREDIENT_SLOTS } from "./cooking_recipes.ts";
|
|
|
|
// 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;
|
|
|
|
export 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("bworld: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 MEAL_SLOT = 6;
|
|
const CONTAINER_SLOT = 7;
|
|
const OUTPUT_SLOT = 8;
|
|
const POT_SIZE = 9;
|
|
// 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<string, string | undefined> = {
|
|
"bworld:furnace": "lit",
|
|
"bworld:stove": "lit",
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
// the recipe whose ingredients are exactly what's in the ingredient slots, one item from each, in any order
|
|
function find_recipe(ctx: ServerContext, 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 ctx.recipes.find<CookingRecipe>(COOKING, items);
|
|
}
|
|
|
|
export function register_cooking_pot(ctx: ServerContext) {
|
|
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;
|
|
};
|
|
|
|
// like the java mod's CookingPotMenu, in gui pixels: the ingredients in 2 rows of 3, the meal, and the containers
|
|
// and served meal under it. the screen is in scripts/client.ts
|
|
const menu_type = ctx.menus.register<BlockRef>("bworld:cooking_pot", (menu, block) => {
|
|
const container = ctx.containers.get(pot_data(block).container)!;
|
|
menu.title = "Cooking Pot";
|
|
for (let slot = 0; slot < INGREDIENT_SLOTS; slot++) {
|
|
menu.add_slot(container, slot, { x: 29 + (slot % 3) * 18, y: 16 + Math.floor(slot / 3) * 18 });
|
|
}
|
|
// the meal stays in the pot until it's served
|
|
menu.add_slot(container, MEAL_SLOT, { x: 122, y: 25, output_only: true, on_take: () => false });
|
|
menu.add_slot(container, CONTAINER_SLOT, { x: 90, y: 52 });
|
|
menu.add_slot(container, OUTPUT_SLOT, { x: 122, y: 52, output_only: true });
|
|
menu.add_player_inventory(7, 84);
|
|
menu.add_data("progress", () => (block.data as PotData | undefined)?.cook_time ?? 0);
|
|
menu.add_data("progress_max", () => (block.data as PotData | undefined)?.cook_time_total ?? 0);
|
|
menu.add_data("heated", () => is_heated(block) ? 1 : 0);
|
|
menu.still_valid_near(block);
|
|
});
|
|
|
|
// 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("bworld: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;
|
|
}
|
|
|
|
menu_type.open(player, block);
|
|
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(ctx, 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);
|
|
},
|
|
});
|
|
}
|
|
|
|
// cutting board
|
|
|
|
// like the java mod's CuttingBoardBlock and CuttingBoardBlockEntity. what's on the board is the item it shows
|
|
// (set_block_item), so the client draws it, like its CuttingBoardRenderer
|
|
export function register_cutting_board(ctx: ServerContext) {
|
|
const on_board = (block: BlockRef) => ctx.world.get_block_item(block.x, block.y, block.z);
|
|
const set_on_board = (block: BlockRef, item: ItemStack | undefined) =>
|
|
ctx.world.set_block_item(block.x, block.y, block.z, item);
|
|
|
|
ctx.components.register_block("bworld:cutting_board", {
|
|
on_interact(block, _params, player) {
|
|
const held = player.inventory.get(player.selected_slot);
|
|
const item = on_board(block);
|
|
|
|
// an empty hand takes everything back
|
|
if (!held) {
|
|
if (!item) return false;
|
|
set_on_board(block, undefined);
|
|
player.give_item(item.id, item.count, item.data);
|
|
return true;
|
|
}
|
|
|
|
// anything goes on, as much of it as fits, like canAddItem and addItem. a knife on an empty board too
|
|
const room = !item
|
|
? ctx.items.max_stack(held.id)
|
|
: item.id === held.id && JSON.stringify(item.data) === JSON.stringify(held.data)
|
|
? ctx.items.max_stack(item.id) - item.count
|
|
: 0;
|
|
if (room > 0) {
|
|
const count = Math.min(room, held.count);
|
|
set_on_board(block, { id: held.id, count: (item?.count ?? 0) + count, data: held.data });
|
|
held.count -= count;
|
|
return true;
|
|
}
|
|
|
|
// anything else is a tool for cutting one of what's on it, like processStoredItemUsingTool
|
|
const recipe = item && ctx.recipes.find<CuttingRecipe>(CUTTING, item.id);
|
|
if (item && recipe && ctx.items.tool(held.id) === recipe.tool) {
|
|
for (const result of recipe.result) ctx.world.drop_item(block.x, block.y, block.z, { ...result });
|
|
set_on_board(block, item.count > 1 ? { ...item, count: item.count - 1 } : undefined);
|
|
}
|
|
// like the java mod, the click is used up either way, nothing gets placed or eaten
|
|
return true;
|
|
},
|
|
on_break(block) {
|
|
const item = on_board(block);
|
|
if (item) ctx.world.drop_item(block.x, block.y, block.z, item);
|
|
},
|
|
});
|
|
}
|