// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // items that lie around as an entity of their own instead of a cubi:item, holding the stack, and right clicking // entities, which mods' entity_interact handles import { assert, assertEquals } from "@std/assert"; import { ItemStack } from "@/common/inventory.ts"; import { ObjectEntity } from "@/server/game/entity/mod_entity.ts"; import { load_and_check } from "@/tools/check_mods.ts"; import { copy_dir, stone_floor, test_game } from "./helpers.ts"; // logs every entity_interact, cancelling it while the player sneaks, and every use of the wrench const SERVER_SCRIPT = ` import type { ServerContext } from "cubi/server"; export function server(ctx: ServerContext) { const log = (key: string, value: unknown) => { const all = ctx.storage.get>("log") ?? {}; (all[key] ??= []).push(value); ctx.storage.set("log", all); }; ctx.events.before.entity_interact.subscribe((event) => { log("interact", [event.entity.type, event.entity.get_item()?.data, event.item?.id]); if (event.player.is_sneaking()) event.cancel = true; }); ctx.components.register_item("testmod:wrench", { on_use() { log("wrench", true); }, }); } `; function test_mods(crate_entity = "testmod:crate", crate_behavior = "object") { const dir = Deno.makeTempDirSync({ prefix: "bworld_objects_" }); copy_dir("mods/bworld", `${dir}/bworld`); const mod = `${dir}/testmod`; for (const folder of ["entities", "items", "scripts"]) Deno.mkdirSync(`${mod}/${folder}`, { recursive: true }); const write = (path: string, json: unknown) => Deno.writeTextFileSync(`${mod}/${path}`, JSON.stringify(json)); write("manifest.json", { format_version: 1, id: "testmod", name: "Test", version: "1.0.0", dependencies: [{ id: "bworld", version: "*" }], main: "scripts/main.ts", }); write("entities/crate.json", { format_version: 1, entity: { id: "testmod:crate", behavior: crate_behavior, texture: "bworld:stone", width: 0.5, height: 0.5 }, }); write("items/crate.json", { format_version: 1, item: { id: "testmod:crate", texture: "bworld:stone", entity: crate_entity, max_stack: 1 }, }); write("items/wrench.json", { format_version: 1, item: { id: "testmod:wrench", texture: "bworld:stone", components: { "testmod:wrench": {} } }, }); Deno.writeTextFileSync(`${mod}/scripts/main.ts`, SERVER_SCRIPT); return dir; } // alice on a stone floor at y 100, looking north at a crate a block in front of her holding data async function setup() { const dir = test_mods(); const t = await test_game(dir); t.join(1, "alice"); stone_floor(t.game); t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); const alice = t.game.players()[0]; const log = () => (t.game.mods.storage.testmod?.log ?? {}) as Record; return { ...t, dir, alice, log }; } const crates = (game: Awaited>["game"]) => game.entities.all().filter((entity) => entity instanceof ObjectEntity); Deno.test("an item with an entity drops as that entity holding the stack, which lands and is saved with it", async () => { const { game, send, take, alice, dir } = await setup(); send(1, { type: "chat", text: "/give testmod:crate" }); alice.inventory.get_item(0)!.data = { value: 42 }; take(1); send(1, { type: "drop_item", menu: 0, slot: 0, all: false }); assertEquals(game.item_entities(), [], "it isn't a cubi:item"); const [crate] = crates(game); assertEquals(crate.type, "testmod:crate"); assertEquals(crate.item?.data, { value: 42 }); const added = take(1).find((m) => m.type === "add_entity"); assertEquals(added?.entity.data.item, { id: "testmod:crate", count: 1, data: { value: 42 } }); for (let i = 0; i < 60; i++) game.tick(); assert(crate.on_ground, "it fell onto the floor"); assertEquals(crate.y, 100); assertEquals(crate.yaw, alice.yaw, "it lies the way it was thrown"); // walking into it doesn't pick it up send(1, { type: "move", x: crate.x, y: 100, z: crate.z, yaw: 0, pitch: 0 }); for (let i = 0; i < 5; i++) game.tick(); assert(!crate.removed); assertEquals(alice.inventory.get_item(0), undefined); const reloaded = await test_game(dir, game.save()); const [loaded] = crates(reloaded.game); assertEquals(loaded.item?.to_data(), { id: "testmod:crate", count: 1, data: { value: 42 } }); Deno.removeSync(dir, { recursive: true }); }); Deno.test("right clicking an entity in reach fires entity_interact, then uses the held item unless it's cancelled", async () => { const { game, send, log, dir } = await setup(); const crate = game.spawn_item(0.5, 100, 1.5, new ItemStack("testmod:crate")); send(1, { type: "chat", text: "/give testmod:wrench" }); send(1, { type: "interact_entity", id: crate.id }); assertEquals(log()["interact"], [["testmod:crate", undefined, "testmod:wrench"]]); assertEquals(log()["wrench"], [true], "nothing cancelled it"); send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0, sneaking: true }); send(1, { type: "interact_entity", id: crate.id }); assertEquals(log()["interact"].length, 2); assertEquals(log()["wrench"], [true], "cancelling it stops the wrench being used"); // out of reach only the wrench is used, like a click on nothing send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); crate.set_position(0.5, 100, -20); send(1, { type: "interact_entity", id: crate.id }); assertEquals(log()["interact"].length, 2); assertEquals(log()["wrench"], [true, true]); Deno.removeSync(dir, { recursive: true }); }); Deno.test("check-mods reports an item's entity that doesn't exist or isn't an object", async () => { const errors = async (dir: string) => { const report = (await load_and_check(dir)).find((mod) => mod.id === "testmod")!.report; Deno.removeSync(dir, { recursive: true }); return report.errors.join("\n"); }; const missing = await errors(test_mods("testmod:nothing")); assert(missing.includes("entity testmod:nothing doesn't exist"), missing); const animal = await errors(test_mods("testmod:crate", "animal")); assert(animal.includes(`entity testmod:crate has to be an "object"`), animal); assertEquals(await errors(test_mods()), ""); });