From 4717e45a9ac7c8c89891669f830b0eb54df7fed8 Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 30 Oct 2026 22:35:44 -0300 Subject: [PATCH] Juicer recipes register --- docs/modding/items/food.md | 5 +- docs/modding/resources/recipes.md | 23 +++++++ mods/bworld/scripts/juicing.ts | 69 ++++++++++++++++++++ mods/bworld/scripts/server.ts | 28 ++++---- mods/bworld/scripts/shared.ts | 5 +- mods/recipe_viewer/scripts/categories.ts | 19 ++---- mods/recipe_viewer/scripts/recipes_screen.ts | 2 - tests/juice_test.ts | 57 ++++++++++++++-- tests/recipe_viewer_test.ts | 5 +- 9 files changed, 171 insertions(+), 42 deletions(-) create mode 100644 mods/bworld/scripts/juicing.ts diff --git a/docs/modding/items/food.md b/docs/modding/items/food.md index c10c051..4c1f088 100644 --- a/docs/modding/items/food.md +++ b/docs/modding/items/food.md @@ -42,8 +42,9 @@ All juice is one [fluid](../resources/fluids.md), `bworld:juice`, and the item i `{ "id": "bworld:juice", "data": { "item": "bworld:strawberry" } }`. The base game's rules name it after that item, "Strawberry Juice", in the color its component says, so juice from another mod's item needs nothing but the component. Juices from different items are different fluids, so they never share a bottle or a tank. Glass bottles -(`bworld:glass_bottle`, 250 mB) and buckets (`bworld:bucket`, 1000 mB) hold it, and whatever makes juice reads the -component. +(`bworld:glass_bottle`, 250 mB) and buckets (`bworld:bucket`, 1000 mB) hold it. The base game makes a +[juicing recipe](../resources/recipes.md#juicing-bworldjuicing) for every item with the component, which is what the +juicer goes by. Juice sells for 1.2 times the items it took to make, rounded up, so it's always worth juicing something. A bottle of juice from an item worth 30 that makes 200 mB took 1.25 of them, and sells for 45. Juice from an item without a diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 996dc52..a628089 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -85,6 +85,29 @@ recipes when the server starts rather than the engine: Every recipe that takes the item in the stonecutter's input is listed, in load order, and the player picks one. Each cut uses up one of the input. +## Juicing: `bworld:juicing` + +For the juicer, which presses an item into a fluid. The base game registers one from its +[shared script](#recipes-from-scripts) for every item with [`bworld:juiceable`](../items/food.md#juice), making the +juice named after it, so most mods never write one. A file can add one for anything else, or make something other than +juice. It's checked when the server starts, like stonecutting: + +```json +{ + "format_version": 1, + "recipe": { + "type": "bworld:juicing", + "input": "copper_tools:maple_sap_bucket", + "fluid_results": [ + { "fluid": { "id": "copper_tools:syrup", "data": { "tree": "bworld:maple" } }, "amount": 250 } + ] + } +} +``` + +`input` is an item, a `#tag` or a list of them, and `fluid_results` is one fluid, with an amount in mB, that one of it +makes. The juicer uses the first recipe that takes the item. + ## Choices Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and diff --git a/mods/bworld/scripts/juicing.ts b/mods/bworld/scripts/juicing.ts new file mode 100644 index 0000000..be25fd5 --- /dev/null +++ b/mods/bworld/scripts/juicing.ts @@ -0,0 +1,69 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// juicing recipes: what the juicer presses an item into. the shared script registers one for every item with +// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts. +// whatever makes juice reads them, like the juicer and create's press over a basin +import type { FluidVariant, SharedContext, TagApi } from "bworld/shared"; +import type { JuiceableParams } from "./shared.ts"; + +export const JUICING = "bworld:juicing"; + +export interface JuicingRecipe { + // the items it presses + input: string[]; + // what one of them makes + juice: { fluid: FluidVariant; amount: number }; +} + +// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says +export function register_juicing(ctx: SharedContext) { + for (const id of ctx.items.ids()) { + const params = ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined; + if (!params) continue; + const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice }; + ctx.recipes.register(JUICING, { input: id, fluid_results: [juice] }); + } +} + +// what reading them needs, which server scripts have +interface RecipeReader { + // deno-lint-ignore no-explicit-any + recipes: { list(type: string): any[] }; + tags: TagApi; + items: { exists(id: string): boolean }; + fluids: { exists(id: string): boolean }; +} + +// every juicing recipe, in load order. a broken one throws, which stops the server from starting +export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] { + return ctx.recipes.list(JUICING).map((json, i) => { + const where = `${JUICING} recipe ${i + 1}`; + const choice: unknown = json.input; + const entries = typeof choice === "string" ? [choice] : Array.isArray(choice) ? choice : []; + const exists = (entry: unknown) => + typeof entry === "string" && + (entry.startsWith("#") ? ctx.tags.exists("items", entry.slice(1)) : ctx.items.exists(entry)); + if (entries.length === 0 || !entries.every(exists)) { + throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`); + } + const results: unknown = json.fluid_results; + const [juice] = Array.isArray(results) ? results : []; + const fluid = juice?.fluid?.id; + if ( + !Array.isArray(results) || results.length !== 1 || typeof fluid !== "string" || !ctx.fluids.exists(fluid) || + !Number.isInteger(juice.amount) || juice.amount < 1 + ) { + throw new Error(`${where}: fluid_results must be one { fluid, amount } of a fluid that exists, in mB`); + } + return { + input: ctx.tags.expand(choice as string | string[]), + juice: { fluid: juice.fluid, amount: juice.amount }, + }; + }); +} + +// the first recipe that presses id, undefined when none does +export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined { + return recipes.find((recipe) => recipe.input.includes(id)); +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 91a4a96..817aa69 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -2,13 +2,14 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // the base game's block and item behavior, written against the same api as any other mod -import type { BlockRef, Container, FluidVariant, Player, Season, ServerContext } from "bworld/server"; +import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/server"; import { register_curios } from "./curios.ts"; import { register_effects } from "./effects.ts"; import { register_fishing } from "./fishing.ts"; import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; import { register_verdigris } from "./verdigris/server.ts"; import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts"; +import { juicing_for, juicing_recipes } from "./juicing.ts"; export function setup(ctx: ServerContext) { register_hoeable(ctx); @@ -848,13 +849,11 @@ function register_buckets(ctx: ServerContext) { }); } -// the juicer has no screen. right clicking it with something juiceable puts that in, one kind at a time, and -// pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into its juice, while that fits in the -// tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the +// the juicer has no screen. right clicking it with something a juicing recipe takes puts that in, one kind at a time, +// and pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into the recipe's juice, while +// that fits in the tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the // juice is lost. clients see what's in it, its juice and how many times it's been pressed toward the next as its // "stack", "tank" and "presses" client data, which its visual draws (see scripts/client.ts) -const JUICE = "bworld:juice"; - interface JuicerData { container?: string; // how many times it's been pressed toward the next juice @@ -863,9 +862,10 @@ interface JuicerData { } function register_juicer(ctx: ServerContext) { - // what juice is made from, read by whatever makes it, like the juicer. the juice's name and color are in shared.ts + // what juice is made from and how it looks, read by the juicing recipes and the juice's rules in shared.ts ctx.components.register_item("bworld:juiceable", {}); - const juiceable = (id: string) => ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined; + // checked now, so a broken recipe stops the server rather than the juicer later + const recipes = juicing_recipes(ctx); const show = (block: BlockRef, container: Container) => { const item = container.get(0); const data = item?.data !== undefined && { data: item.data }; @@ -877,7 +877,7 @@ function register_juicer(ctx: ServerContext) { const held = player.held_item; const container = block_container(ctx, block, 1); const data = block.data as JuicerData; - if (held && juiceable(held.id)) { + if (held && juicing_for(recipes, held.id)) { held.count = container.add(held)?.count ?? 0; show(block, container); // true even when none fit, so the fruit isn't eaten instead @@ -899,11 +899,11 @@ function register_juicer(ctx: ServerContext) { if (held || !player.is_sneaking()) return false; const item = container.get(0); - const params = item && juiceable(item.id); - if (!item || !params) return false; - const juice: FluidVariant = { id: JUICE, data: { item: item.id } }; + const recipe = item && juicing_for(recipes, item.id); + if (!item || !recipe) return false; + const { fluid: juice, amount } = recipe.juice; const tank = data.tank; - const fits = (tank ? tank.amount : 0) + params.juice <= JUICER_CAPACITY && + const fits = (tank ? tank.amount : 0) + amount <= JUICER_CAPACITY && (!tank || ctx.fluids.same(tank.fluid, juice)); if (!fits) return false; const presses = (data.presses ?? 0) + 1; @@ -914,7 +914,7 @@ function register_juicer(ctx: ServerContext) { } item.count -= 1; show(block, container); - const filled = { fluid: juice, amount: (tank?.amount ?? 0) + params.juice }; + const filled = { fluid: juice, amount: (tank?.amount ?? 0) + amount }; block.data = { ...data, presses: 0, tank: filled } satisfies JuicerData; block.set_client_data("tank", filled); block.set_client_data("presses", undefined); diff --git a/mods/bworld/scripts/shared.ts b/mods/bworld/scripts/shared.ts index 1cb9057..ec3b643 100644 --- a/mods/bworld/scripts/shared.ts +++ b/mods/bworld/scripts/shared.ts @@ -3,8 +3,10 @@ // the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the // server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor -// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from +// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from, and +// the recipes that make it import type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared"; +import { register_juicing } from "./juicing.ts"; import { register_verdigris_rules } from "./verdigris/shared.ts"; export function setup(ctx: SharedContext) { @@ -13,6 +15,7 @@ export function setup(ctx: SharedContext) { register_door(ctx); register_verdigris_rules(ctx); register_juice(ctx); + register_juicing(ctx); } // the sides a fence connects on, and the face of the block there it connects to diff --git a/mods/recipe_viewer/scripts/categories.ts b/mods/recipe_viewer/scripts/categories.ts index 591c05f..0c39cc1 100644 --- a/mods/recipe_viewer/scripts/categories.ts +++ b/mods/recipe_viewer/scripts/categories.ts @@ -161,7 +161,7 @@ function base_categories(tags: TagApi): RecipeCategory[] { }, }, { - // what the juicer presses an item into, see juicing_recipes + // what the juicer presses an item into, see JUICING type: JUICING, title: "Juicing", width: 116, @@ -169,7 +169,7 @@ function base_categories(tags: TagApi): RecipeCategory[] { catalysts: [JUICER], draw(recipe, layout) { const [{ fluid, amount }] = fluid_outputs(recipe); - layout.slot({ ids: [recipe.input], count: 1 }, 0, 0); + layout.slot({ ids: tags.expand(recipe.input), count: 1 }, 0, 0); layout.texture("bworld:arrow_full", 31, 1, ARROW, ARROW); layout.fluid(fluid, amount, 60, 0); layout.text(`${amount} mB`, 82, 5); @@ -178,22 +178,11 @@ function base_categories(tags: TagApi): RecipeCategory[] { ]; } -// the base game's juicer, from the items with bworld:juiceable: a recipe of its own type for each, that presses one -// of it into its juice. the item is an input like any recipe's, the juice is its fluid result, with the item it's from as -// its data like the juicer makes it +// the base game's juicer's recipes, which press an item into a fluid, its "fluid_results". bworld registers one for +// every item with bworld:juiceable, see mods/bworld/scripts/juicing.ts const JUICING = "bworld:juicing"; const JUICER = "bworld:juicer"; -export function juicing_recipes(ctx: ClientContext): Recipe[] { - if (!ctx.fluids.exists("bworld:juice")) return []; - return ctx.items.ids().flatMap((id) => { - const params = ctx.items.components(id)["bworld:juiceable"] as { juice?: number } | undefined; - if (!params?.juice) return []; - const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice }; - return [{ type: JUICING, input: id, fluid_results: [juice] }]; - }); -} - // the base game's fish, the items with bworld:fish, as recipes of their own type that make them, so clicking one shows // when and where it bites. their params are under "fish", which reading a recipe's inputs skips const FISHING = "bworld:fishing"; diff --git a/mods/recipe_viewer/scripts/recipes_screen.ts b/mods/recipe_viewer/scripts/recipes_screen.ts index f960555..f3f0d3f 100644 --- a/mods/recipe_viewer/scripts/recipes_screen.ts +++ b/mods/recipe_viewer/scripts/recipes_screen.ts @@ -9,7 +9,6 @@ import type { ClientContext, Graphics, Screen } from "bworld/client"; import { crabpot_recipes, fishing_recipes, - juicing_recipes, make_categories, type RecipeCategory, type RecipeLayout, @@ -56,7 +55,6 @@ export class RecipeIndex { ...this.ctx.recipes.list(), ...fishing_recipes(this.ctx), ...crabpot_recipes(this.ctx), - ...juicing_recipes(this.ctx), ]; this.#categories = make_categories(this.ctx, this.#recipes); } diff --git a/tests/juice_test.ts b/tests/juice_test.ts index e458e98..fec84ae 100644 --- a/tests/juice_test.ts +++ b/tests/juice_test.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // juice: one fluid named, colored and valued after the bworld:juiceable item in its data, and the juicer that makes it -import { assert, assertEquals } from "@std/assert"; +import { assert, assertEquals, assertRejects } from "@std/assert"; import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import type { FluidJson } from "$/common/mod_data.ts"; import type { ClientContext, Instances, Visual, VisualBlock } from "$/common/mod_api/client.ts"; @@ -17,9 +17,11 @@ import { JUICE_VALUE, type JuiceableParams } from "$/mods/bworld/scripts/shared. import { copy_dir, stone_floor, test_game } from "./helpers.ts"; const CLOUDBERRY = "testberries:cloudberry"; +const PEBBLE = "testberries:pebble"; -// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have -async function setup() { +// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have, a pebble +// that isn't juiceable, and recipes files by name +async function setup(recipes: Record = {}) { const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" }); copy_dir("mods/bworld", `${dir}/bworld`); const write = (path: string, json: unknown) => { @@ -41,9 +43,14 @@ async function setup() { components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } }, }, }); - const t = await test_game(dir); - Deno.removeSync(dir, { recursive: true }); - return { ...t, fluids: t.game.mods.fluid_rules.api() }; + write("items/pebble.json", { format_version: 1, item: { id: PEBBLE, texture: "engine:missing" } }); + for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe }); + try { + const t = await test_game(dir); + return { ...t, fluids: t.game.mods.fluid_rules.api() }; + } finally { + Deno.removeSync(dir, { recursive: true }); + } } Deno.test("juice made from any item with bworld:juiceable is named after it, in the component's color", async () => { @@ -156,6 +163,44 @@ Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fil ]); }); +Deno.test("the juicer juices by bworld:juicing recipes: one for each juiceable item, and any a mod adds", async () => { + const pebble_juice = { fluid: { id: "bworld:juice", data: { item: PEBBLE } }, amount: 500 }; + const t = await setup({ + "pebble_juice.json": { type: "bworld:juicing", input: PEBBLE, fluid_results: [pebble_juice] }, + }); + const juicing = t.game.recipes.by_type.get("bworld:juicing")!; + const cloudberry = juicing.find((recipe) => recipe.input === CLOUDBERRY)!; + assertEquals(cloudberry.fluid_results, [{ + fluid: { id: "bworld:juice", data: { item: CLOUDBERRY } }, + amount: 100, + }]); + + // the pebble isn't juiceable, but its recipe juices it + t.join(1, "alice"); + stone_floor(t.game); + const move = (sneaking: boolean) => + t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0, ...(sneaking && { sneaking }) }); + move(false); + const player = t.game.players()[0]; + t.game.mods.player(player).give_item(PEBBLE, 1); + t.game.set_block(1, 100, 0, "bworld:juicer"); + const use = () => t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + use(); + t.send(1, { type: "select_slot", slot: 1 }); + move(true); + for (let i = 0; i < 4; i++) use(); + assertEquals(t.game.world.get_tile(1, 100, 0)?.client_data?.tank, pebble_juice); +}); + +Deno.test("a broken juicing recipe stops the server from starting", async () => { + const broken = { + type: "bworld:juicing", + input: PEBBLE, + fluid_results: [{ fluid: { id: "nope:nothing" }, amount: 1 }], + }; + await assertRejects(() => setup({ "broken.json": broken }), Error, "bworld:juicing recipe"); +}); + // a juicer at 1 100 1 drawn by bworld's client script, and a way to set a field of its client data. its fluids have the // server's rules, the same as a client's async function drawn_juicer() { diff --git a/tests/recipe_viewer_test.ts b/tests/recipe_viewer_test.ts index dfd9814..f911638 100644 --- a/tests/recipe_viewer_test.ts +++ b/tests/recipe_viewer_test.ts @@ -175,8 +175,9 @@ Deno.test("a crab pot's recipes are what each bait catches in fresh and salt wat }); Deno.test("each juiceable item has a juicing recipe that presses it into its own juice", async () => { - await test_game("mods"); - const index = new RecipeIndex(client_ctx()); + // registered by the base game's shared script, see mods/bworld/scripts/juicing.ts + const { game } = await test_game("mods"); + const index = new RecipeIndex(client_ctx(game.recipes.all)); // a strawberry's uses include the juicer pressing it into strawberry juice, shown in a fluid slot const juicing = index.find(item("bworld:strawberry"), "uses").find((found) => found.category.title === "Juicing")!;