Skip blocks that don't exist on schematics

This commit is contained in:
2026-11-21 09:10:43 -03:00
parent c0c182490e
commit f36176a872
6 changed files with 40 additions and 9 deletions
+1 -1
View File
@@ -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<string>;
// 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,
+7 -1
View File
@@ -509,7 +509,13 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
// cubi: entities, like dropped items, are the engine's
for (const { file, json } of mod.schematics) {
for (const { id } of json.palette) uses(file, id, "block");
// the server skips blocks that don't exist, so a schematic still loads without the mod they came from
for (const id of new Set(json.palette.map((entry) => 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)) {
+1 -1
View File
@@ -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}`);
}
+1 -1
View File
@@ -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}`);
}
+15 -4
View File
@@ -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<number>();
const palette = schematic.palette.map(({ id, s }, i) => {
if (id === AIR_ID) return { id, value: AIR };
const info = EverythingRegistry.get<BlockRegistry>("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 };
}
+15 -1
View File
@@ -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")!;