// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // exporting a box of the world as a schematic file, with /exportschematic, and placing registered schematics import { assert, assertAlmostEquals, assertEquals, assertStringIncludes, assertThrows } from "@std/assert"; import { decode_schematic, encode_schematic, type Schematic } from "@/common/schematic.ts"; import { create_entity } from "@/server/game/entity/entity_types.ts"; import { new_stack } from "@/server/game/mod_runtime.ts"; import { get_state_value, set_state_value } from "@/common/utils.ts"; import type { SchematicApi } from "@/common/mod_api/server.ts"; import type { GameServer } from "@/server/game/game_server.ts"; import { copy_dir, stone_floor, test_game } from "./helpers.ts"; // deno-lint-ignore no-explicit-any type Msg = any; async function setup() { const t = await test_game("mods"); t.join(1, "alice"); stone_floor(t.game); t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); t.take(1); // runs the command and waits for the host to write the file. the schematic, or undefined and what the player was // told const export_schematic = async (args: string) => { t.send(1, { type: "chat", text: `/exportschematic ${args}` }); await new Promise((resolve) => setTimeout(resolve, 0)); const chat = t.take(1).filter((m: Msg) => m.type === "chat").map((m: Msg) => m.text as string); const path = chat.map((text) => text.match(/^Saved the schematic to (.+)$/)?.[1]).find((path) => path); const bytes = path ? t.schematics.get(path) : undefined; return { schematic: bytes && decode_schematic(bytes), chat }; }; return { ...t, export_schematic }; } // the palette entry of the block at p, from the box's lowest corner function block_at(schematic: Schematic, p: [number, number, number]) { const block = schematic.blocks.find((block) => block.p.every((v, i) => v === p[i])); assert(block, `nothing at ${p}`); return { ...schematic.palette[block.i], data: block.data }; } Deno.test("a schematic has every block in the box with its states, air too, from the lowest corner", async () => { const t = await setup(); t.game.set_block(2, 100, 1, "bworld:oak_planks"); t.game.set_block(1, 101, 1, "bworld:furnace"); const furnace_value = t.game.world.get_block_value(1, 101, 1); const facing_west = set_state_value(furnace_value, t.game.world.get_block_info(1, 101, 1)!, "facing", 3)!; t.game.set_block_state(1, 101, 1, facing_west >>> 16); // the corners in any order const { schematic } = await t.export_schematic("2 101 1 1 100 2"); assert(schematic); assertEquals(schematic.size, [2, 2, 2]); assertEquals(schematic.blocks.length, 8); assertEquals(block_at(schematic, [1, 0, 0]).id, "bworld:oak_planks"); const furnace = block_at(schematic, [0, 1, 0]); assertEquals(furnace.id, "bworld:furnace"); assertEquals(furnace.s?.facing, 3); assertEquals(block_at(schematic, [0, 0, 1]).id, "cubi:air"); // blocks with the same states share their entry assertEquals(new Set(schematic.palette.map((entry) => JSON.stringify(entry))).size, schematic.palette.length); }); Deno.test("a chest's items are written into its data in place of its container's id", async () => { const t = await setup(); t.game.set_block(1, 100, 1, "bworld:chest"); const id = (t.game.world.get_tile(1, 100, 1)?.mod_data as { container: string }).container; t.game.containers.get(id)!.set_item(2, new_stack({ id: "bworld:coal", count: 5 })); const { schematic } = await t.export_schematic("1 100 1 1 100 1"); const items = (block_at(schematic!, [0, 0, 0]).data as { container: unknown[] }).container; assertEquals(items[2], { id: "bworld:coal", count: 5 }); assertEquals(items[0], null); // exporting doesn't change the chest assertEquals(t.game.containers.get(id)!.get_item(2)?.amount, 5); }); Deno.test("a schematic has the entities whose feet are in the box, without their uuids", async () => { const t = await setup(); const spawn = (x: number, z: number) => { const chicken = create_entity(t.game.entities, "bworld:chicken")!; chicken.set_position(x, 100, z); t.game.entities.add_entity(chicken); }; spawn(1.5, 1.25); spawn(5.5, 1.5); const { schematic } = await t.export_schematic("1 100 1 2 101 2"); assertEquals(schematic!.entities.length, 1); const [chicken] = schematic!.entities; assertEquals(chicken.id, "bworld:chicken"); assertEquals(chicken.p, [0.5, 0, 0.25]); assertEquals(chicken.data.uuid, undefined); }); Deno.test("a box with too many blocks isn't exported", async () => { const t = await setup(); const { schematic, chat } = await t.export_schematic("0 0 0 1000 200 1000"); assertEquals(schematic, undefined); assert(chat.some((text) => text.startsWith("Couldn't export it")), chat.join("\n")); assertEquals(t.schematics.size, 0); }); // a mod that registers a schematic, and leaves its ctx.schematics where the tests can call it const PLACE_SCRIPT = ` import type { ServerContext } from "cubi/server"; export function server(ctx: ServerContext) { ctx.schematics.register("testmod:hut", { size: [2, 2, 1], palette: [ { id: "bworld:oak_planks" }, { id: "bworld:furnace", s: { facing: 1 } }, { id: "bworld:chest" }, { id: "cubi:air" }, ], blocks: [ { i: 0, p: [0, 0, 0] }, { i: 2, p: [1, 0, 0] }, { i: 1, p: [0, 1, 0], data: { progress: 7 } }, { i: 3, p: [1, 1, 0] }, ], entities: [{ p: [1.5, 1, 0.5], id: "bworld:chicken", data: { rotation: [-Math.PI / 2, 0] } }], }); try { ctx.schematics.register("testmod:broken", { size: [1, 1, 1], palette: [], blocks: [], entities: [{ p: [0.5, 0, 0.5], id: "testmod:nothing", data: {} }] }); } catch (e) { ctx.storage.set("broken", (e as Error).message); } ctx.schematics.register("testmod:partial", { size: [1, 2, 1], palette: [{ id: "testmod:nothing" }, { id: "bworld:oak_planks" }], blocks: [{ i: 0, p: [0, 0, 0] }, { i: 1, p: [0, 1, 0] }], entities: [], }); // the file's is registered before scripts run try { ctx.schematics.register("testmod:post", { size: [1, 1, 1], palette: [], blocks: [], entities: [] }); } catch (e) { ctx.storage.set("post", (e as Error).message); } (globalThis as Record).placing = ctx.schematics; } `; async function place_setup() { const dir = Deno.makeTempDirSync({ prefix: "bworld_schematics_" }); copy_dir("mods/bworld", `${dir}/bworld`); Deno.mkdirSync(`${dir}/testmod/scripts`, { recursive: true }); Deno.writeTextFileSync( `${dir}/testmod/manifest.json`, JSON.stringify({ format_version: 1, id: "testmod", name: "Test", version: "1.0.0", dependencies: [{ id: "bworld", version: "*" }], main: "scripts/main.ts", }), ); Deno.writeTextFileSync(`${dir}/testmod/scripts/main.ts`, PLACE_SCRIPT); Deno.mkdirSync(`${dir}/testmod/schematics`); Deno.writeFileSync( `${dir}/testmod/schematics/post.cbsm`, encode_schematic({ size: [1, 2, 1], palette: [{ id: "bworld:oak_log" }], blocks: [{ i: 0, p: [0, 0, 0] }, { i: 0, p: [0, 1, 0] }], entities: [], }), ); const t = await test_game(dir); Deno.removeSync(dir, { recursive: true }); stone_floor(t.game); const schematics = (globalThis as Record).placing as SchematicApi; const chickens = () => t.game.entities.all().filter((entity) => entity.type === "bworld:chicken"); return { ...t, schematics, chickens }; } function facing(t: { game: GameServer }, x: number, y: number, z: number) { return get_state_value(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!, "facing"); } Deno.test("placing a schematic puts its blocks, their data and its entities where it says", async () => { const t = await place_setup(); t.game.set_block(11, 101, 20, "bworld:stone"); t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 } }); assertEquals(t.game.world.get_block_id(10, 100, 20), "bworld:oak_planks"); assertEquals(t.game.world.get_block_id(10, 101, 20), "bworld:furnace"); assertEquals(facing(t, 10, 101, 20), 1); // air replaces what was there assertEquals(t.game.world.get_block_id(11, 101, 20), "cubi:air"); // a block with data gets it as it is, one without gets its on_create assertEquals(t.game.world.get_tile(10, 101, 20)?.mod_data, { progress: 7 }); const chest = t.game.world.get_tile(11, 100, 20)?.mod_data as { container: string }; assert(t.game.containers.has(chest.container)); const [chicken] = t.chickens(); assertEquals([chicken.x, chicken.y, chicken.z], [11.5, 101, 20.5]); assertEquals(chicken.yaw, -Math.PI / 2); // placed twice, it's a different chicken t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 30 } }); assertEquals(new Set(t.chickens().map((chicken) => chicken.id)).size, 2); }); Deno.test("a placed schematic turns clockwise around its pivot, with its facing states and entities", async () => { const t = await place_setup(); t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 }, rotation: 90 }); // east of the pivot turns to south of it assertEquals(t.game.world.get_block_id(10, 100, 20), "bworld:oak_planks"); assertEquals(t.game.world.get_block_id(10, 100, 21), "bworld:chest"); // facing east turns to south assertEquals(facing(t, 10, 101, 20), 2); const [chicken] = t.chickens(); assertEquals([chicken.x, chicken.z], [10.5, 21.5]); assertAlmostEquals(Math.abs(chicken.yaw), Math.PI); }); Deno.test("a mirrored schematic flips across its pivot before it turns", async () => { const t = await place_setup(); t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 }, position_pivot: { x: 1, y: 0, z: 0 }, mirror: "x", }); assertEquals(t.game.world.get_block_id(12, 100, 20), "bworld:oak_planks"); assertEquals(t.game.world.get_block_id(11, 100, 20), "bworld:chest"); // east flips to west assertEquals(facing(t, 12, 101, 20), 3); const [chicken] = t.chickens(); assertEquals([chicken.x, chicken.z], [11.5, 20.5]); assertEquals(chicken.yaw, Math.PI / 2); }); Deno.test("a schematic is checked when it's registered, and a placement before anything changes", async () => { const t = await place_setup(); assertStringIncludes(String(t.game.mods.storage.testmod?.broken), "testmod:nothing"); assertThrows( () => t.schematics.place("testmod:broken", { position: { x: 0, y: 100, z: 0 } }), Error, "no schematic", ); assertThrows( () => t.schematics.place("testmod:hut", { position: { x: 0, y: 100, z: 0 }, rotation: 45 as 90 }), Error, "rotation", ); assertEquals(t.game.world.get_block_id(0, 100, 0), "cubi:air"); }); Deno.test("blocks that aren't registered are left out of a schematic, and the rest is placed", async () => { const t = await place_setup(); t.game.set_block(5, 100, 5, "bworld:stone"); t.schematics.place("testmod:partial", { position: { x: 5, y: 100, z: 5 } }); assertEquals(t.game.world.get_block_id(5, 100, 5), "bworld:stone"); assertEquals(t.game.world.get_block_id(5, 101, 5), "bworld:oak_planks"); }); Deno.test("/placeschematic places a registered schematic with its lowest corner at x y z", async () => { const t = await place_setup(); t.join(1, "alice"); t.take(1); t.send(1, { type: "chat", text: "/placeschematic testmod:post 3 100 4" }); assertEquals(t.game.world.get_block_id(3, 100, 4), "bworld:oak_log"); assertEquals(t.game.world.get_block_id(3, 101, 4), "bworld:oak_log"); t.take(1); t.send(1, { type: "chat", text: "/placeschematic testmod:nothing 3 100 4" }); const chat = t.take(1).filter((m: Msg) => m.type === "chat").map((m: Msg) => m.text as string); assert(chat.some((text) => text.includes("no schematic testmod:nothing")), chat.join("\n")); }); Deno.test("get gives a copy of a registered schematic with its id, a file's or a script's", async () => { const t = await place_setup(); const hut = t.schematics.get("testmod:hut")!; assertEquals(hut.id, "testmod:hut"); assertEquals(hut.size, [2, 2, 1]); assertEquals(hut.palette[hut.blocks.find((block) => block.p.join() === "0,1,0")!.i].id, "bworld:furnace"); assertEquals(t.schematics.get("testmod:post")?.id, "testmod:post"); assertEquals(t.schematics.get("testmod:nothing"), undefined); // changing the copy doesn't change what's placed hut.palette[0].id = "bworld:stone"; t.schematics.place("testmod:hut", { position: { x: 10, y: 100, z: 20 } }); assertEquals(t.game.world.get_block_id(10, 100, 20), "bworld:oak_planks"); }); Deno.test("ids gives every registered schematic's id, the files' first, without the ones that failed", async () => { const t = await place_setup(); const ids = t.schematics.ids().filter((id) => id.startsWith("testmod:")); assertEquals(ids, ["testmod:post", "testmod:hut", "testmod:partial"]); }); Deno.test("a mod's schematics/.cbsm files are registered as : before its scripts run", async () => { const t = await place_setup(); assertStringIncludes(String(t.game.mods.storage.testmod?.post), "registered twice"); t.schematics.place("testmod:post", { position: { x: 3, y: 100, z: 4 } }); assertEquals(t.game.world.get_block_id(3, 100, 4), "bworld:oak_log"); assertEquals(t.game.world.get_block_id(3, 101, 4), "bworld:oak_log"); });