From f36176a8726b2b83a736edd98abf1d58d5cc39dd Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 21 Nov 2026 09:10:43 -0300 Subject: [PATCH] Skip blocks that don't exist on schematics --- common/mod_api/server.ts | 2 +- common/mod_check.ts | 8 +++++++- server/game/load_mods.ts | 2 +- server/game/mod_runtime.ts | 2 +- server/game/schematics.ts | 19 +++++++++++++++---- tests/schematic_test.ts | 16 +++++++++++++++- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 21c5906..a991049 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -587,7 +587,7 @@ export interface SchematicApi { // create's schematic and quill. resolves to where the file is. throws for a box of more than 1048576 blocks (2^20) export(from: Position, to: Position): Promise; // adds a schematic for place, by an id in this mod's namespace, while the server phase runs. it's checked here: - // every block, state and entity in it has to be registered + // every state and entity in it has to be registered. blocks that aren't are left out, with a warning register(id: Id, schematic: Schematic): void; // a copy of a registered schematic, a file's or one a script registered, with its id, like neoforge's // StructureTemplateManager.get. undefined for an id nothing registered. for reading what's in it before placing it, diff --git a/common/mod_check.ts b/common/mod_check.ts index 2cd67ad..54d6c76 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -509,7 +509,13 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable entry.id))) { + check_namespace(file, id); + if (!block_owners.has(id)) { + warnings.push(`${file}: block ${id} doesn't exist, it will be left out`); + } + } for (const { id } of json.entities) { check_namespace(file, id); if (!id.startsWith("cubi:") && !entity_owners.has(id)) { diff --git a/server/game/load_mods.ts b/server/game/load_mods.ts index 8ecbad0..dae1c3f 100644 --- a/server/game/load_mods.ts +++ b/server/game/load_mods.ts @@ -73,7 +73,7 @@ export async function start_game( for (const mod of mods) { for (const schematic of mod.data.schematics ?? []) { try { - game.schematics.set(schematic.id!, prepare_schematic(game, schematic)); + game.schematics.set(schematic.id!, prepare_schematic(game, schematic.id!, schematic)); } catch (e) { throw new ModLoadError(mod.listing.id, `schematic ${schematic.id}: ${(e as Error).message}`); } diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 0924d6a..fa10179 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -1064,7 +1064,7 @@ export class ModRuntime { register: (id, schematic) => { register.new_id("schematic", id, game().schematics.has(id)); try { - game().schematics.set(id, prepare_schematic(game(), schematic)); + game().schematics.set(id, prepare_schematic(game(), id, schematic)); } catch (e) { register.fail(`schematic ${id}: ${(e as Error).message}`); } diff --git a/server/game/schematics.ts b/server/game/schematics.ts index 91ad9cd..886ff33 100644 --- a/server/game/schematics.ts +++ b/server/game/schematics.ts @@ -109,8 +109,9 @@ export interface RegisteredSchematic { palette: { id: string; value: number }[]; } -// a copy of the schematic, checked once so placing it can't fail halfway. throws saying what's wrong with it -export function prepare_schematic(game: GameServer, json: Schematic): RegisteredSchematic { +// a copy of the schematic, checked once so placing it can't fail halfway. blocks that aren't registered are left out, +// with a warning. throws saying what's wrong with it +export function prepare_schematic(game: GameServer, id: string, json: Schematic): RegisteredSchematic { const problems = schematic_problems(json); if (problems.length > 0) throw new Error(problems.slice(0, 5).join(", ")); const schematic = structuredClone(json); @@ -118,10 +119,15 @@ export function prepare_schematic(game: GameServer, json: Schematic): Registered if (volume > MAX_SCHEMATIC_VOLUME) { throw new Error(`a schematic holds up to ${MAX_SCHEMATIC_VOLUME} blocks, and this one is ${volume}`); } - const palette = schematic.palette.map(({ id, s }) => { + // palette entries whose block isn't registered + const missing = new Set(); + const palette = schematic.palette.map(({ id, s }, i) => { if (id === AIR_ID) return { id, value: AIR }; const info = EverythingRegistry.get("blocks", id); - if (!info) throw new Error(`it has ${id}, which isn't a block`); + if (!info) { + missing.add(i); + return { id, value: AIR }; + } return { id, value: value_with_states(game.world.block_ids[id], info, s ?? {}) }; }); for (const { i, p } of schematic.blocks) { @@ -135,6 +141,11 @@ export function prepare_schematic(game: GameServer, json: Schematic): Registered for (const { id } of schematic.entities) { if (!has_entity_type(id)) throw new Error(`it has ${id}, which isn't an entity`); } + if (missing.size > 0) { + schematic.blocks = schematic.blocks.filter(({ i }) => !missing.has(i)); + const ids = [...missing].map((i) => palette[i].id).join(", "); + console.warn(`Schematic ${id} has blocks that don't exist, leaving them out: ${ids}`); + } return { schematic, palette }; } diff --git a/tests/schematic_test.ts b/tests/schematic_test.ts index ac1acff..e66ccd8 100644 --- a/tests/schematic_test.ts +++ b/tests/schematic_test.ts @@ -123,10 +123,16 @@ export function server(ctx: ServerContext) { 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: [{ id: "testmod:nothing" }], blocks: [{ i: 0, p: [0, 0, 0] }], entities: [] }); + 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: [] }); @@ -245,6 +251,14 @@ Deno.test("a schematic is checked when it's registered, and a placement before a 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("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")!;