diff --git a/docs/modding/items/food.md b/docs/modding/items/food.md index 9fa929c..3bdcb20 100644 --- a/docs/modding/items/food.md +++ b/docs/modding/items/food.md @@ -23,6 +23,28 @@ An item with a `food` property can be eaten. Players eat by right clicking while Food that places a block, like potatoes with `places`, is planted when clicked somewhere it can go and eaten otherwise. +## Juice + +Items that can be juiced, like fruit, have the base game's `bworld:juiceable` component: + +```json +"components": { + "bworld:juiceable": { "juice": 100, "color": "#c42d39" } +} +``` + +| Param | What it does | +| ------- | ------------------------------------------------- | +| `juice` | How much juice one makes, in mB. A bottle is 250. | +| `color` | The juice's color, like `"#c42d39"`. | + +All juice is one [fluid](../resources/fluids.md), `bworld:juice`, and the item it's made from is in its data: +`{ "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. + ## Hunger and Health Hunger and health work like Minecraft's: diff --git a/mods/bworld/fluids/juice.json b/mods/bworld/fluids/juice.json new file mode 100644 index 0000000..b6ef70d --- /dev/null +++ b/mods/bworld/fluids/juice.json @@ -0,0 +1,8 @@ +{ + "format_version": 1, + "fluid": { + "id": "bworld:juice", + "name": "Juice", + "color": "#e8a33d" + } +} diff --git a/mods/bworld/items/bucket.json b/mods/bworld/items/bucket.json index fa3092f..6c28448 100644 --- a/mods/bworld/items/bucket.json +++ b/mods/bworld/items/bucket.json @@ -4,7 +4,8 @@ "id": "bworld:bucket", "texture": "bworld:bucket", "targets_fluids": true, - "max_stack": 16, - "base_sell_value": 180 + "base_sell_value": 180, + "fluid_container": { "capacity": 1000, "full_max_stack": 1 }, + "layers": [{ "texture": "bworld:bucket_fluid", "tint": "fluid" }] } } diff --git a/mods/bworld/items/glass_bottle.json b/mods/bworld/items/glass_bottle.json new file mode 100644 index 0000000..08d76e9 --- /dev/null +++ b/mods/bworld/items/glass_bottle.json @@ -0,0 +1,9 @@ +{ + "format_version": 1, + "item": { + "id": "bworld:glass_bottle", + "texture": "bworld:glass_bottle", + "fluid_container": { "capacity": 250 }, + "layers": [{ "texture": "bworld:bottle_fluid", "tint": "fluid" }] + } +} diff --git a/mods/bworld/recipes/crafting_glass_bottle.json b/mods/bworld/recipes/crafting_glass_bottle.json new file mode 100644 index 0000000..cdf6363 --- /dev/null +++ b/mods/bworld/recipes/crafting_glass_bottle.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "G G", + " G " + ], + "key": { + "G": "bworld:glass" + }, + "result": { + "id": "bworld:glass_bottle", + "count": 3 + } + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index b9d51d4..9f40794 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -8,6 +8,7 @@ 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 } from "./shared.ts"; export function setup(ctx: ServerContext) { register_hoeable(ctx); @@ -24,6 +25,9 @@ export function setup(ctx: ServerContext) { register_door(ctx); register_trapdoor(ctx); register_buckets(ctx); + // what juice is made from, read by whatever makes it, like create's press. the juice's name and color are in + // shared.ts + ctx.components.register_item("bworld:juiceable", {}); register_curios(ctx); register_effects(ctx); register_growable_block(ctx); @@ -793,8 +797,8 @@ function register_farming(ctx: ServerContext) { }); } -// buckets, like minecraft's: right clicking water with a bucket picks it up into a water bucket, which places it back -// and is a bucket again +// buckets, like minecraft's: right clicking water with an empty bucket picks it up into a water bucket, which places it +// back and is a bucket again. a bucket is also a fluid container, so it can be full of juice, which it can't place // right clicking it with a bucket takes it away, giving the filled bucket interface BucketableParams { @@ -812,7 +816,9 @@ function register_buckets(ctx: ServerContext) { ctx.components.register_block("bworld:bucketable", { on_interact(block, params, player) { const bucket = player.held_item; - if (bucket?.id !== BUCKET) return false; + if (bucket?.id !== BUCKET || (bucket.data as { fluid?: unknown } | undefined)?.fluid !== undefined) { + return false; + } const { x, y, z } = block; if (ctx.world.get_block(x, y, z) === block.id) { // only a fluid's source fills it, not where it's flowing @@ -822,10 +828,7 @@ function register_buckets(ctx: ServerContext) { // the fluid in a waterlogged block, like a fence in water, which stays ctx.world.set_state(x, y, z, "waterlogged", 0); } - bucket.count -= 1; - // the last one becomes the filled bucket in the hand, from a stack it goes in the inventory - if (bucket.count <= 0) player.inventory.set(player.selected_slot, { id: params.into, count: 1 }); - else player.give_item(params.into); + player.exchange_held({ id: params.into, count: 1 }); return true; }, }); diff --git a/mods/bworld/scripts/shared.ts b/mods/bworld/scripts/shared.ts index 51ad48e..f3bd590 100644 --- a/mods/bworld/scripts/shared.ts +++ b/mods/bworld/scripts/shared.ts @@ -3,7 +3,7 @@ // 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 +// 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 import type { SharedBlock, SharedContext } from "bworld/shared"; import { register_verdigris_rules } from "./verdigris/shared.ts"; @@ -12,6 +12,7 @@ export function setup(ctx: SharedContext) { register_trapdoor(ctx); register_door(ctx); register_verdigris_rules(ctx); + register_juice(ctx); } // the sides a fence connects on, and the face of the block there it connects to @@ -136,3 +137,28 @@ function register_door(ctx: SharedContext) { }, }); } + +// a juiceable item's component, like a strawberry's: juice is the mB one of it makes +export interface JuiceableParams { + juice: number; + color: string; +} + +// juice is one fluid for every fruit, like create's potion fluid: which fruit sits in its data, { item }. it's named +// after that item, "Strawberry Juice", in the color its bworld:juiceable says +function register_juice(ctx: SharedContext) { + const item_of = (data: unknown) => { + const item = (data as { item?: unknown } | undefined)?.item; + return typeof item === "string" && ctx.items.exists(item) ? item : undefined; + }; + ctx.fluids.register("bworld:juice", { + name(data) { + const item = item_of(data); + return item && `${ctx.items.name(item)} Juice`; + }, + color(data) { + const item = item_of(data); + return item && (ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined)?.color; + }, + }); +} diff --git a/tests/base_game_test.ts b/tests/base_game_test.ts index 16564f3..68623c8 100644 --- a/tests/base_game_test.ts +++ b/tests/base_game_test.ts @@ -361,6 +361,24 @@ Deno.test("a bucket picks up a water source but not flowing water, and a water b assertEquals(t.game.world.get_block_value(-2, 100, 0) >>> 16, 0); }); +Deno.test("only an empty bucket picks up water, and from a stack the water bucket goes in the inventory", async () => { + const t = await setup(); + const player = t.game.players()[0]; + const slots = () => player.inventory.to_data().flatMap((item) => item ? [[item.id, item.count]] : []); + t.game.set_block(1, 100, 0, "bworld:water"); + + t.game.give(player, "bworld:bucket", 1, { fluid: { id: "bworld:juice" } }); + t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:water"); + assertEquals(slots(), [["bworld:bucket", 1]]); + + player.inventory.set_item(0, undefined); + t.game.give(player, "bworld:bucket", 3); + t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:air"); + assertEquals(slots(), [["bworld:bucket", 2], ["bworld:water_bucket", 1]]); +}); + Deno.test("a bucket picks up a lava source too, and a lava bucket puts one back", async () => { const t = await setup([["bworld:bucket", 1]]); const held = () => t.game.players()[0].held_item?.type_id; diff --git a/tests/juice_test.ts b/tests/juice_test.ts new file mode 100644 index 0000000..4a7800e --- /dev/null +++ b/tests/juice_test.ts @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// juice: one fluid named and colored after the bworld:juiceable item in its data +import { assert, assertEquals } from "@std/assert"; +import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; +import type { FluidJson } from "$/common/mod_data.ts"; +import { display_name } from "$/common/utils.ts"; +import { copy_dir, test_game } from "./helpers.ts"; + +// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have +async function setup() { + const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const write = (path: string, json: unknown) => { + Deno.mkdirSync(`${dir}/testberries/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); + Deno.writeTextFileSync(`${dir}/testberries/${path}`, JSON.stringify(json)); + }; + write("manifest.json", { + format_version: 1, + id: "testberries", + name: "Test Berries", + version: "1.0.0", + dependencies: [{ id: "bworld", version: "*" }], + }); + write("items/cloudberry.json", { + format_version: 1, + item: { + id: "testberries:cloudberry", + texture: "engine:missing", + components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } }, + }, + }); + const { game } = await test_game(dir); + Deno.removeSync(dir, { recursive: true }); + return game.mods.fluid_rules.api(); +} + +Deno.test("juice made from any item with bworld:juiceable is named after it, in the component's color", async () => { + const fluids = await setup(); + const juiceable = EverythingRegistry.entries("items") + .filter(([, item]) => item.components?.["bworld:juiceable"]); + assert(juiceable.length > 0); + for (const [id, item] of juiceable) { + const juice = { id: "bworld:juice", data: { item: id } }; + assertEquals(fluids.name(juice), `${display_name(id)} Juice`); + assertEquals(fluids.color(juice), (item.components!["bworld:juiceable"] as { color: string }).color); + } + + // juice that isn't from an item is plain juice + const plain = EverythingRegistry.get("fluids", "bworld:juice")!; + assertEquals([fluids.name({ id: "bworld:juice" }), fluids.color({ id: "bworld:juice" })], [ + plain.name, + plain.color, + ]); +});