From 08df7f1b6b6e078afc440cd8df617fe01d90cef7 Mon Sep 17 00:00:00 2001 From: Paula Date: Sun, 25 Oct 2026 16:57:38 -0300 Subject: [PATCH] Juicer --- mods/bworld/blocks/juicer.json | 20 +++++ mods/bworld/blocks/melon.json | 2 +- mods/bworld/items/beetroot.json | 5 +- mods/bworld/items/carrot.json | 5 +- mods/bworld/items/melon.json | 5 +- mods/bworld/items/potato.json | 5 +- mods/bworld/items/pumpkin.json | 5 +- mods/bworld/items/strawberry.json | 5 +- mods/bworld/models/juicer.json | 110 +++++++++++++++++++++++++ mods/bworld/scripts/server.ts | 76 ++++++++++++++++- mods/bworld/textures/juicer_bottom.png | Bin 0 -> 304 bytes mods/bworld/textures/juicer_side.png | Bin 0 -> 217 bytes mods/bworld/textures/juicer_top.png | Bin 0 -> 344 bytes mods/farmersdelight/items/cabbage.json | 5 +- mods/farmersdelight/items/tomato.json | 5 +- tests/juice_test.ts | 77 +++++++++++++++-- 16 files changed, 306 insertions(+), 19 deletions(-) create mode 100644 mods/bworld/blocks/juicer.json create mode 100644 mods/bworld/models/juicer.json create mode 100644 mods/bworld/textures/juicer_bottom.png create mode 100644 mods/bworld/textures/juicer_side.png create mode 100644 mods/bworld/textures/juicer_top.png diff --git a/mods/bworld/blocks/juicer.json b/mods/bworld/blocks/juicer.json new file mode 100644 index 0000000..131c417 --- /dev/null +++ b/mods/bworld/blocks/juicer.json @@ -0,0 +1,20 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:juicer", + "name": "Juicer", + "textures": {}, + "model": "bworld:juicer", + "item_model": "bworld:juicer", + "render_layer": "cutout", + "mining": { + "toughness": 2, + "tool": "pickaxe" + }, + "drops": "bworld:juicer", + "interactive": true, + "components": { + "bworld:juicer": {} + } + } +} diff --git a/mods/bworld/blocks/melon.json b/mods/bworld/blocks/melon.json index f546a61..0eb6c72 100644 --- a/mods/bworld/blocks/melon.json +++ b/mods/bworld/blocks/melon.json @@ -11,6 +11,6 @@ "toughness": 1.5, "tool": "axe" }, - "loot_table": "bworld:blocks/melon" + "drops": "bworld:melon" } } diff --git a/mods/bworld/items/beetroot.json b/mods/bworld/items/beetroot.json index 94a7a58..e166923 100644 --- a/mods/bworld/items/beetroot.json +++ b/mods/bworld/items/beetroot.json @@ -8,6 +8,9 @@ "saturation_modifier": 0.6 }, "places": "bworld:beetroots", - "base_sell_value": 25 + "base_sell_value": 25, + "components": { + "bworld:juiceable": { "juice": 200, "color": "#e84c6e" } + } } } diff --git a/mods/bworld/items/carrot.json b/mods/bworld/items/carrot.json index e3c1eb8..25434e7 100644 --- a/mods/bworld/items/carrot.json +++ b/mods/bworld/items/carrot.json @@ -8,6 +8,9 @@ "saturation_modifier": 0.6 }, "places": "bworld:carrots", - "base_sell_value": 12 + "base_sell_value": 12, + "components": { + "bworld:juiceable": { "juice": 200, "color": "#c87f4b" } + } } } diff --git a/mods/bworld/items/melon.json b/mods/bworld/items/melon.json index f544160..a725c48 100644 --- a/mods/bworld/items/melon.json +++ b/mods/bworld/items/melon.json @@ -2,6 +2,9 @@ "format_version": 1, "item": { "id": "bworld:melon", - "base_sell_value": 60 + "base_sell_value": 60, + "components": { + "bworld:juiceable": { "juice": 1000, "color": "#aa3d33" } + } } } diff --git a/mods/bworld/items/potato.json b/mods/bworld/items/potato.json index a5bfd0c..06d4a11 100644 --- a/mods/bworld/items/potato.json +++ b/mods/bworld/items/potato.json @@ -8,6 +8,9 @@ "saturation_modifier": 0.3 }, "places": "bworld:potatoes", - "base_sell_value": 20 + "base_sell_value": 20, + "components": { + "bworld:juiceable": { "juice": 200, "color": "#c48a3f" } + } } } diff --git a/mods/bworld/items/pumpkin.json b/mods/bworld/items/pumpkin.json index 94f2af6..cfe87f2 100644 --- a/mods/bworld/items/pumpkin.json +++ b/mods/bworld/items/pumpkin.json @@ -2,6 +2,9 @@ "format_version": 1, "item": { "id": "bworld:pumpkin", - "base_sell_value": 80 + "base_sell_value": 80, + "components": { + "bworld:juiceable": { "juice": 1000, "color": "#ea8734" } + } } } diff --git a/mods/bworld/items/strawberry.json b/mods/bworld/items/strawberry.json index 2343414..e9c1fa1 100644 --- a/mods/bworld/items/strawberry.json +++ b/mods/bworld/items/strawberry.json @@ -7,6 +7,9 @@ "nutrition": 2, "saturation_modifier": 0.3 }, - "base_sell_value": 30 + "base_sell_value": 30, + "components": { + "bworld:juiceable": { "juice": 200, "color": "#ff2840" } + } } } diff --git a/mods/bworld/models/juicer.json b/mods/bworld/models/juicer.json new file mode 100644 index 0000000..b8ddf5b --- /dev/null +++ b/mods/bworld/models/juicer.json @@ -0,0 +1,110 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:juicer", + "textures": { + "0": "bworld:juicer_side", + "1": "bworld:juicer_bottom", + "2": "bworld:juicer_top", + "particle": "bworld:juicer_side" + }, + "elements": [ + { + "from": [0, 0, 0], + "to": [16, 2, 16], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "east": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "south": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "west": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "top": { "texture": "#1", "uv": [0, 0, 16, 16] }, + "bottom": { "texture": "#1", "uv": [0, 0, 16, 16] } + } + }, + { + "from": [0, 2, 0], + "to": [2, 14, 2], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "east": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "south": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "west": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "top": { "texture": "#0", "uv": [0, 0, 2, 2] }, + "bottom": { "texture": "#0", "uv": [0, 0, 2, 2] } + } + }, + { + "from": [14, 2, 0], + "to": [16, 14, 2], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "east": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "south": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "west": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "top": { "texture": "#0", "uv": [0, 0, 2, 2] }, + "bottom": { "texture": "#0", "uv": [0, 0, 2, 2] } + } + }, + { + "from": [14, 2, 14], + "to": [16, 14, 16], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "east": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "south": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "west": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "top": { "texture": "#0", "uv": [0, 0, 2, 2] }, + "bottom": { "texture": "#0", "uv": [0, 0, 2, 2] } + } + }, + { + "from": [0, 2, 14], + "to": [2, 14, 16], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "east": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "south": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "west": { "texture": "#0", "uv": [0, 0, 2, 12] }, + "top": { "texture": "#0", "uv": [0, 0, 2, 2] }, + "bottom": { "texture": "#0", "uv": [0, 0, 2, 2] } + } + }, + { + "from": [0, 14, 0], + "to": [16, 16, 16], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "east": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "south": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "west": { "texture": "#0", "uv": [0, 0, 16, 2] }, + "top": { "texture": "#2", "uv": [0, 0, 16, 16] }, + "bottom": { "texture": "#1", "uv": [0, 0, 16, 16] } + } + }, + { + "from": [5, 8, 5], + "to": [11, 10, 11], + "faces": { + "north": { "texture": "#0", "uv": [5, 0, 11, 2] }, + "east": { "texture": "#0", "uv": [5, 0, 11, 2] }, + "south": { "texture": "#0", "uv": [5, 0, 11, 2] }, + "west": { "texture": "#0", "uv": [5, 0, 11, 2] }, + "top": { "texture": "#2", "uv": [5, 5, 11, 11] }, + "bottom": { "texture": "#1", "uv": [5, 5, 11, 11] } + } + }, + { + "from": [7, 10, 7], + "to": [9, 14, 9], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2, 4] }, + "east": { "texture": "#0", "uv": [0, 0, 2, 4] }, + "south": { "texture": "#0", "uv": [0, 0, 2, 4] }, + "west": { "texture": "#0", "uv": [0, 0, 2, 4] }, + "top": { "texture": "#0", "uv": [0, 0, 2, 2] }, + "bottom": { "texture": "#0", "uv": [0, 0, 2, 2] } + } + } + ] + } +} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 9f40794..38ad0d4 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -2,7 +2,7 @@ // 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, Player, Season, ServerContext } from "bworld/server"; +import type { BlockRef, Container, FluidVariant, Player, Season, ServerContext } from "bworld/server"; import { register_curios } from "./curios.ts"; import { register_effects } from "./effects.ts"; import { register_fishing } from "./fishing.ts"; @@ -25,9 +25,7 @@ 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_juicer(ctx); register_curios(ctx); register_effects(ctx); register_growable_block(ctx); @@ -850,6 +848,76 @@ 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 +// juice is lost +const JUICE = "bworld:juice"; +const JUICER_PRESSES = 4; +const JUICER_CAPACITY = 1000; + +interface JuicerData { + container?: string; + // how many times it's been pressed toward the next juice + presses?: number; + tank?: { fluid: FluidVariant; amount: number }; +} + +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 + ctx.components.register_item("bworld:juiceable", {}); + const juiceable = (id: string) => ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined; + + ctx.components.register_block("bworld:juicer", { + on_interact(block, _params, player) { + const held = player.held_item; + const container = block_container(ctx, block, 1); + const data = block.data as JuicerData; + if (held && juiceable(held.id)) { + held.count = container.add(held)?.count ?? 0; + // true even when none fit, so the fruit isn't eaten instead + return true; + } + + const capacity = held && ctx.items.fluid_capacity(held.id); + if (held && capacity !== undefined) { + if ((held.data as { fluid?: unknown } | undefined)?.fluid !== undefined) return false; + const tank = data.tank; + if (!tank || tank.amount < capacity) return false; + const amount = tank.amount - capacity; + block.data = { ...data, tank: amount > 0 ? { ...tank, amount } : undefined } satisfies JuicerData; + player.exchange_held({ id: held.id, count: 1, data: { fluid: tank.fluid } }); + return true; + } + + 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 tank = data.tank; + const fits = (tank ? tank.amount : 0) + params.juice <= JUICER_CAPACITY && + (!tank || ctx.fluids.same(tank.fluid, juice)); + if (!fits) return false; + const presses = (data.presses ?? 0) + 1; + if (presses < JUICER_PRESSES) { + block.data = { ...data, presses } satisfies JuicerData; + return true; + } + item.count -= 1; + block.data = { + ...data, + presses: 0, + tank: { fluid: juice, amount: (tank?.amount ?? 0) + params.juice }, + } satisfies JuicerData; + return true; + }, + on_break(block) { + drop_contents(ctx, block); + }, + }); +} + interface GrowableBlockParams { // what grows on top of it block_id: string; diff --git a/mods/bworld/textures/juicer_bottom.png b/mods/bworld/textures/juicer_bottom.png new file mode 100644 index 0000000000000000000000000000000000000000..8c859ec6dc7e8052fae3f5df7cfeccb5fc8f6336 GIT binary patch literal 304 zcmV-00nh%4P)k7R5*>TlTB{IFc5{m+8%{Suwd^)aQ~C^0BkC;3P^S|aoRBRR2@ZEo`Yq4j zkhRcV1>M84e0E~ga!bK}eFmjl|pAxED-u z^5PV93|+bP)3jAWP7iF(*F{fjO^m(Hec%j3##+JTBWHHipFSfn@!fLu9q)f1VE46r zuD^5L$Adp(49;&}&v3xmu*)ODlR<8SFc3u_E_8}Tt4Nj7?m0qEk^5hw%?3-2f~PK_ z3mD{7sTA^7X2hTQX86T^zaIgP$0J?WAtHG1Ih{`Ac<*VNhQr~o)VA9##csD70aO)$ zzVF#=HmK@y&QVoFWI2~6e3vs%)dMsh_=c(=iU9popNX7-o)2{Zlp%zv!P^U8ec~GF z=X~+}Tvvq84ghC%@+r4qI#<{ADj`5j1EzC%w_7o{A~Ib;Rf%a>)x>mnIj~_Og0nTQ z{fMS>t5h@jFt^sev8?efbw=sECxk#u16AoRf7@&`R*z{QrUBEH_JaD?DUCc{Ph&Nu qv76KA=X%k$?fBQ$e`&Pb literal 0 HcmV?d00001 diff --git a/mods/farmersdelight/items/cabbage.json b/mods/farmersdelight/items/cabbage.json index f61cb7a..198c860 100644 --- a/mods/farmersdelight/items/cabbage.json +++ b/mods/farmersdelight/items/cabbage.json @@ -7,6 +7,9 @@ "nutrition": 2, "saturation_modifier": 0.4 }, - "base_sell_value": 35 + "base_sell_value": 35, + "components": { + "bworld:juiceable": { "juice": 500, "color": "#7ab244" } + } } } diff --git a/mods/farmersdelight/items/tomato.json b/mods/farmersdelight/items/tomato.json index 0af2493..8c0139b 100644 --- a/mods/farmersdelight/items/tomato.json +++ b/mods/farmersdelight/items/tomato.json @@ -7,6 +7,9 @@ "nutrition": 1, "saturation_modifier": 0.3 }, - "base_sell_value": 15 + "base_sell_value": 15, + "components": { + "bworld:juiceable": { "juice": 250, "color": "#c0341f" } + } } } diff --git a/tests/juice_test.ts b/tests/juice_test.ts index 4a7800e..8e114d0 100644 --- a/tests/juice_test.ts +++ b/tests/juice_test.ts @@ -1,12 +1,14 @@ // 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 +// juice: one fluid named and colored after the bworld:juiceable item in its data, and the juicer that makes it 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"; +import { copy_dir, stone_floor, test_game } from "./helpers.ts"; + +const CLOUDBERRY = "testberries:cloudberry"; // mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have async function setup() { @@ -26,18 +28,18 @@ async function setup() { write("items/cloudberry.json", { format_version: 1, item: { - id: "testberries:cloudberry", + id: CLOUDBERRY, texture: "engine:missing", components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } }, }, }); - const { game } = await test_game(dir); + const t = await test_game(dir); Deno.removeSync(dir, { recursive: true }); - return game.mods.fluid_rules.api(); + return { ...t, fluids: t.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 { fluids } = await setup(); const juiceable = EverythingRegistry.entries("items") .filter(([, item]) => item.components?.["bworld:juiceable"]); assert(juiceable.length > 0); @@ -54,3 +56,66 @@ Deno.test("juice made from any item with bworld:juiceable is named after it, in plain.color, ]); }); + +Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fills empty containers from it", async () => { + const t = await setup(); + 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]; + const api = t.game.mods.player(player); + api.give_item(CLOUDBERRY, 4); + api.give_item("bworld:glass_bottle", 2); + // selects the slot with id, or an empty one without + const hold = (id?: string) => + t.send(1, { + type: "select_slot", + slot: player.inventory.to_data().findIndex((item) => (item?.id ?? null) === (id ?? null)), + }); + const use = () => t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + const press = (times: number) => { + hold(); + move(true); + for (let i = 0; i < times; i++) use(); + move(false); + }; + // the bottles, empty ones first + const bottles = () => + player.inventory.to_data().filter((item) => item?.id === "bworld:glass_bottle") + .sort((a, b) => Number(a!.data !== undefined) - Number(b!.data !== undefined)); + t.game.set_block(1, 100, 0, "bworld:juicer"); + + hold(CLOUDBERRY); + use(); + assertEquals(player.inventory.to_data().filter((item) => item?.id === CLOUDBERRY), []); + // right clicking with an empty hand without sneaking doesn't press it + hold(); + use(); + + // 2 cloudberries and 3 presses toward the next are 200 mB, not a bottle's 250 + press(11); + hold("bworld:glass_bottle"); + use(); + assertEquals(bottles(), [{ id: "bworld:glass_bottle", count: 2 }]); + + // the 3rd makes 300 mB, a bottle and 50 mB left, which isn't another + press(1); + hold("bworld:glass_bottle"); + use(); + use(); + const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } }; + assertEquals(bottles(), [ + { id: "bworld:glass_bottle", count: 1 }, + { id: "bworld:glass_bottle", count: 1, data: { fluid: juice } }, + ]); + + // the cloudberry it didn't press yet comes out when it's broken + t.send(1, { type: "break_block", x: 1, y: 100, z: 0 }); + const dropped = t.game.item_entities().map((entity) => entity.item.to_data()); + assertEquals(dropped.sort((a, b) => a.id.localeCompare(b.id)), [ + { id: "bworld:juicer", count: 1 }, + { id: CLOUDBERRY, count: 1 }, + ]); +});