388 lines
15 KiB
TypeScript
388 lines
15 KiB
TypeScript
// 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";
|
|
|
|
export function setup(ctx: ServerContext) {
|
|
register_stove(ctx);
|
|
register_cooking_pot(ctx);
|
|
register_cutting_board(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<string, string | undefined> = {
|
|
"bworld:furnace": "lit",
|
|
"farmersdelight:stove": "lit",
|
|
};
|
|
|
|
// a farmersdelight:cooking recipe: the ingredients in any slots. in the json each is an item id or #tag, or a list of
|
|
// those any of which will do. here each is every item that will do
|
|
interface CookingRecipe {
|
|
ingredients: 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;
|
|
}
|
|
|
|
// an ingredient: an item, or a #tag of items any of which will do
|
|
function item_or_tag(ctx: ServerContext, where: string, id: unknown) {
|
|
if (typeof id === "string" && id.startsWith("#")) {
|
|
if (!ctx.tags.exists("items", id)) throw new Error(`${where}: there's no item tag ${id}`);
|
|
} else if (typeof id !== "string" || !ctx.items.exists(id)) {
|
|
throw new Error(`${where}: there's no item ${id}`);
|
|
}
|
|
}
|
|
|
|
// 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((id) => item_or_tag(ctx, where, id));
|
|
} else {
|
|
item_or_tag(ctx, where, 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: ingredients.map((ingredient: string | string[]) => ctx.tags.expand(ingredient)),
|
|
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[][], items: string[], used = new Set<number>()): boolean {
|
|
if (ingredients.length === 0) return true;
|
|
const [first, ...rest] = ingredients;
|
|
const fits = (id: string) => first.includes(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);
|
|
|
|
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>("farmersdelight: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("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;
|
|
}
|
|
|
|
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(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);
|
|
},
|
|
});
|
|
}
|
|
|
|
// cutting board
|
|
|
|
// a farmersdelight:cutting recipe, like the java mod's: one ingredient (an item, or a list any of which will do), the
|
|
// tool type that cuts it, and everything one of it is cut into
|
|
interface CuttingRecipe {
|
|
ingredient: string[];
|
|
tool: string;
|
|
result: { id: string; count: number }[];
|
|
}
|
|
|
|
// checked when the server starts, like the cooking pot's
|
|
function load_cutting_recipes(ctx: ServerContext): CuttingRecipe[] {
|
|
return ctx.recipes.list("farmersdelight:cutting").map((json, i) => {
|
|
const where = `farmersdelight:cutting recipe ${i + 1} (${json.ingredients?.[0] ?? "no ingredient"})`;
|
|
const item = (id: unknown) => {
|
|
if (typeof id !== "string" || !ctx.items.exists(id)) throw new Error(`${where}: there's no item ${id}`);
|
|
return id;
|
|
};
|
|
if (!Array.isArray(json.ingredients) || json.ingredients.length !== 1) {
|
|
throw new Error(`${where}: ingredients must be one item, or one list of items any of which will do`);
|
|
}
|
|
const choice = [json.ingredients[0]].flat();
|
|
if (choice.length === 0) throw new Error(`${where}: the ingredient's list of items is empty`);
|
|
choice.forEach((id) => item_or_tag(ctx, where, id));
|
|
const ingredient = ctx.tags.expand(choice);
|
|
if (typeof json.tool !== "string") throw new Error(`${where}: tool must be a tool type, like "knife"`);
|
|
if (!Array.isArray(json.result) || json.result.length === 0) {
|
|
throw new Error(`${where}: result must be a list of { id, count }`);
|
|
}
|
|
for (const result of json.result) {
|
|
item(result?.id);
|
|
if (!Number.isInteger(result.count) || result.count < 1) {
|
|
throw new Error(`${where}: every result's count must be a positive whole number`);
|
|
}
|
|
}
|
|
return { ingredient, tool: json.tool, result: json.result };
|
|
});
|
|
}
|
|
|
|
// 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
|
|
function register_cutting_board(ctx: ServerContext) {
|
|
const recipes = load_cutting_recipes(ctx);
|
|
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("farmersdelight: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 && recipes.find((recipe) => recipe.ingredient.includes(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);
|
|
},
|
|
});
|
|
}
|