Juicer recipes register

This commit is contained in:
2026-10-30 22:35:44 -03:00
parent 43521b3675
commit 4717e45a9a
9 changed files with 171 additions and 42 deletions
+3 -2
View File
@@ -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, `{ "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. "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 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 (`bworld:glass_bottle`, 250 mB) and buckets (`bworld:bucket`, 1000 mB) hold it. The base game makes a
component. [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 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 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
+23
View File
@@ -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 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. 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 ## Choices
Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and
+69
View File
@@ -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));
}
+14 -14
View File
@@ -2,13 +2,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // 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 // 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_curios } from "./curios.ts";
import { register_effects } from "./effects.ts"; import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts"; import { register_fishing } from "./fishing.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { register_verdigris } from "./verdigris/server.ts"; import { register_verdigris } from "./verdigris/server.ts";
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.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) { export function setup(ctx: ServerContext) {
register_hoeable(ctx); 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 // the juicer has no screen. right clicking it with something a juicing recipe takes puts that in, one kind at a time,
// pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into its juice, while that fits in the // and pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into the recipe's juice, while
// tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the // 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 // 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) // "stack", "tank" and "presses" client data, which its visual draws (see scripts/client.ts)
const JUICE = "bworld:juice";
interface JuicerData { interface JuicerData {
container?: string; container?: string;
// how many times it's been pressed toward the next juice // how many times it's been pressed toward the next juice
@@ -863,9 +862,10 @@ interface JuicerData {
} }
function register_juicer(ctx: ServerContext) { 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<JuiceableParams>("bworld:juiceable", {}); ctx.components.register_item<JuiceableParams>("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 show = (block: BlockRef, container: Container) => {
const item = container.get(0); const item = container.get(0);
const data = item?.data !== undefined && { data: item.data }; const data = item?.data !== undefined && { data: item.data };
@@ -877,7 +877,7 @@ function register_juicer(ctx: ServerContext) {
const held = player.held_item; const held = player.held_item;
const container = block_container(ctx, block, 1); const container = block_container(ctx, block, 1);
const data = block.data as JuicerData; 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; held.count = container.add(held)?.count ?? 0;
show(block, container); show(block, container);
// true even when none fit, so the fruit isn't eaten instead // 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; if (held || !player.is_sneaking()) return false;
const item = container.get(0); const item = container.get(0);
const params = item && juiceable(item.id); const recipe = item && juicing_for(recipes, item.id);
if (!item || !params) return false; if (!item || !recipe) return false;
const juice: FluidVariant = { id: JUICE, data: { item: item.id } }; const { fluid: juice, amount } = recipe.juice;
const tank = data.tank; 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)); (!tank || ctx.fluids.same(tank.fluid, juice));
if (!fits) return false; if (!fits) return false;
const presses = (data.presses ?? 0) + 1; const presses = (data.presses ?? 0) + 1;
@@ -914,7 +914,7 @@ function register_juicer(ctx: ServerContext) {
} }
item.count -= 1; item.count -= 1;
show(block, container); 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.data = { ...data, presses: 0, tank: filled } satisfies JuicerData;
block.set_client_data("tank", filled); block.set_client_data("tank", filled);
block.set_client_data("presses", undefined); block.set_client_data("presses", undefined);
+4 -1
View File
@@ -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 // 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 // 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 type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared";
import { register_juicing } from "./juicing.ts";
import { register_verdigris_rules } from "./verdigris/shared.ts"; import { register_verdigris_rules } from "./verdigris/shared.ts";
export function setup(ctx: SharedContext) { export function setup(ctx: SharedContext) {
@@ -13,6 +15,7 @@ export function setup(ctx: SharedContext) {
register_door(ctx); register_door(ctx);
register_verdigris_rules(ctx); register_verdigris_rules(ctx);
register_juice(ctx); register_juice(ctx);
register_juicing(ctx);
} }
// the sides a fence connects on, and the face of the block there it connects to // the sides a fence connects on, and the face of the block there it connects to
+4 -15
View File
@@ -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, type: JUICING,
title: "Juicing", title: "Juicing",
width: 116, width: 116,
@@ -169,7 +169,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
catalysts: [JUICER], catalysts: [JUICER],
draw(recipe, layout) { draw(recipe, layout) {
const [{ fluid, amount }] = fluid_outputs(recipe); 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.texture("bworld:arrow_full", 31, 1, ARROW, ARROW);
layout.fluid(fluid, amount, 60, 0); layout.fluid(fluid, amount, 60, 0);
layout.text(`${amount} mB`, 82, 5); 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 // the base game's juicer's recipes, which press an item into a fluid, its "fluid_results". bworld registers one for
// 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 // every item with bworld:juiceable, see mods/bworld/scripts/juicing.ts
// its data like the juicer makes it
const JUICING = "bworld:juicing"; const JUICING = "bworld:juicing";
const JUICER = "bworld:juicer"; 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 // 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 // when and where it bites. their params are under "fish", which reading a recipe's inputs skips
const FISHING = "bworld:fishing"; const FISHING = "bworld:fishing";
@@ -9,7 +9,6 @@ import type { ClientContext, Graphics, Screen } from "bworld/client";
import { import {
crabpot_recipes, crabpot_recipes,
fishing_recipes, fishing_recipes,
juicing_recipes,
make_categories, make_categories,
type RecipeCategory, type RecipeCategory,
type RecipeLayout, type RecipeLayout,
@@ -56,7 +55,6 @@ export class RecipeIndex {
...this.ctx.recipes.list(), ...this.ctx.recipes.list(),
...fishing_recipes(this.ctx), ...fishing_recipes(this.ctx),
...crabpot_recipes(this.ctx), ...crabpot_recipes(this.ctx),
...juicing_recipes(this.ctx),
]; ];
this.#categories = make_categories(this.ctx, this.#recipes); this.#categories = make_categories(this.ctx, this.#recipes);
} }
+51 -6
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // 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 // 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 { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import type { FluidJson } from "$/common/mod_data.ts"; import type { FluidJson } from "$/common/mod_data.ts";
import type { ClientContext, Instances, Visual, VisualBlock } from "$/common/mod_api/client.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"; import { copy_dir, stone_floor, test_game } from "./helpers.ts";
const CLOUDBERRY = "testberries:cloudberry"; 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 // mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have, a pebble
async function setup() { // that isn't juiceable, and recipes files by name
async function setup(recipes: Record<string, unknown> = {}) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" }); const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" });
copy_dir("mods/bworld", `${dir}/bworld`); copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => { const write = (path: string, json: unknown) => {
@@ -41,9 +43,14 @@ async function setup() {
components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } }, components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } },
}, },
}); });
const t = await test_game(dir); write("items/pebble.json", { format_version: 1, item: { id: PEBBLE, texture: "engine:missing" } });
Deno.removeSync(dir, { recursive: true }); for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe });
return { ...t, fluids: t.game.mods.fluid_rules.api() }; 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 () => { 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 // 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 // server's rules, the same as a client's
async function drawn_juicer() { async function drawn_juicer() {
+3 -2
View File
@@ -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 () => { Deno.test("each juiceable item has a juicing recipe that presses it into its own juice", async () => {
await test_game("mods"); // registered by the base game's shared script, see mods/bworld/scripts/juicing.ts
const index = new RecipeIndex(client_ctx()); 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 // 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")!; const juicing = index.find(item("bworld:strawberry"), "uses").find((found) => found.category.title === "Juicing")!;