diff --git a/PLAN.md b/PLAN.md index 796d6a5..a5e8613 100644 --- a/PLAN.md +++ b/PLAN.md @@ -65,7 +65,7 @@ Each task becomes one GitHub issue under milestone M0, in this order. **Exit criteria:** the vectors pass, the round-trip suite is green, CI is green, and `convert` turns a real `.schem` into a `.nbt` that mcbuilder's importer reads back. -**Where that stands (2026-11-08):** the vectors pass, the round-trip suite is green, and CI is green on GitHub. No `.schem` saved by a real tool exists yet (see Open items), and no automated test feeds `convert`'s output to mcbuilder's importer. +**Where that stands (2026-11-16):** the vectors pass, the round-trip suite is green, CI is green on GitHub, `fixtures/real/` now holds real-tool `.schem` files (`worldedit/fantasyhouse2.schem`, three `shulkr/*.schem`, and three `flat-v3/smp_lobby_*.schem`), and `packages/mcbuilder/test/importer.test.ts` runs `scripts/convert.ts` on them and feeds the converted `.nbt` files into mcbuilder's importer, rebuilding identical blocks. ## Golden vectors @@ -273,7 +273,7 @@ The code differs from the v1 spec in the ways below. Luki decided all fourteen o - [x] Create the repository `lzif/voxwright` so the scaffold can start (created **private**; it is **public** now, checked 2026-11-11) - [x] Choose `AGPL-3.0-only` or `AGPL-3.0-or-later` for the SPDX headers, before M0 (`AGPL-3.0-only`) -- [ ] Before M2, send fixtures saved by real tools: two or three `.schem` from WorldEdit (`.nbt`, `.litematic` and `.mcstructure` of one build are in `fixtures/real/`, saved by bloxelizer.com, not Litematica or Bedrock). M2 was built without them, so this is late +- [x] Before M2, send fixtures saved by real tools: two or three `.schem` from WorldEdit (`.nbt`, `.litematic` and `.mcstructure` of `Rafeals_house` in `fixtures/real/rafeals-house/`, saved by bloxelizer.com, plus `worldedit/fantasyhouse2.schem`, three `shulkr/*.schem`, three `flat-v3/smp_lobby_*.schem`, and `legacy/storageroom-medieval.schematic` added 2026-11-16) - [ ] Check the domain and trademark (npm is settled, decided 2026-11-14: only the command line is published, unscoped as `voxmc`; the `@voxwright` npm scope holds only the old `@voxwright/mcbuild` and is not used otherwise) - [ ] Pick the open source Bedrock to Java block-mapping table and confirm its license is AGPL-compatible (needed by M2; the code uses hand-written rules meanwhile) - [x] Decide each entry under "Differences from the v1 spec" (all 14 decided 2026-11-07; see that section) diff --git a/README.md b/README.md index 1250201..c4fed51 100644 --- a/README.md +++ b/README.md @@ -320,9 +320,11 @@ Built: the renderer (see [vectors/README.md](vectors/README.md)). - `core`: NBT reader and writer (big and little endian, gzip), block strings, rotation, sectioned volume, and codecs for `.nbt`, `.schem`, `.litematic` and - `.mcstructure`, all tested. A real build saved in three of those formats - decodes to the same blocks in each, once the Java default properties that - Bedrock leaves out (such as `waterlogged=false`) are set aside. + `.mcstructure`, all tested (including real-tool `.schem` builds from WorldEdit + and Shulkr, plus flat-v2-layout files tagged `Version: 3`). A real build + saved in three of those formats decodes to the same blocks in each, once the + Java default properties that Bedrock leaves out (such as `waterlogged=false`) + are set aside. - `convert` script between all four formats. - The Agent Skill in `skills/voxwright/`: `SKILL.md`, the references, and the skill CLI (`build`, `inspect`, `preview` / `view`) as a committed zero-dependency bundle, with the web app opening a build from a @@ -341,10 +343,10 @@ Not done yet: trapdoors, beds, pillars, fences, panes, walls, torches, lanterns, leaves and blocks with `facing`), not every block in the game. The summary lists each dropped property. - Block-entity data (chest contents, sign text) and entities cannot be written from a recipe. -- `.schem` files saved by a real tool, and a run on a real phone (the touch editor - has only been driven by emulated touch so far; `docs/phone-test-checklist.md` - lists what to try). Luki checked an edited export in Litematica and smooth - navigation on a real GPU by hand on 2026-11-11; no GPU numbers were recorded. +- A run on a real phone (the touch editor has only been driven by emulated touch so + far; `docs/phone-test-checklist.md` lists what to try). Luki checked an edited + export in Litematica and smooth navigation on a real GPU by hand on 2026-11-11; + no GPU numbers were recorded. - The 0.1.0 release: the 0.1.0 version bump and tag (`voxmc` is on npm at 0.0.3). - Deferred, not in 0.1.0: the parts catalog (the spec names it, nothing defines it, so it needs a definition first) and the fast and trusted preview tiers. diff --git a/fixtures/README.md b/fixtures/README.md index 1b6794a..1f5f2b1 100644 --- a/fixtures/README.md +++ b/fixtures/README.md @@ -14,5 +14,23 @@ Schematic files shared by all tests. `packages/core/test/litematic.test.ts` and `mcstructure.test.ts` check the other two against the `.nbt`. -Still needed: files saved by real tools (two or three `.schem` from -WorldEdit). Put them in a directory per tool and add tests that read them. +- `real/worldedit/` holds `fantasyhouse2.schem`, a Sponge v3 schematic saved by + WorldEdit 7.4.6 (`enginehub:fabric`, DataVersion 5023; 22,953 blocks, 342 + block states, 93 block entities, 41 entities, non-zero `Offset`). + +- `real/shulkr/` holds three Sponge v3 builds exported by Shulkr (DataVersion + 5023): `castelo-medieval.schem` (4,895 blocks, 6 block entities), + `raifox-medivial-house.schem` (1,595 blocks, 138 block entities) and + `simple-survival-mansion.schem` (6,218 blocks, 77 block entities). + +- `real/flat-v3/` holds three real-world `.schem` files (`smp_lobby_big.schem`, + `smp_lobby_center_dense.schem`, `smp_lobby_medieval.schem`, DataVersion 3465) + that declare `Version: 3` while writing Sponge v2's flat layout (top-level + `Palette` and `BlockData` without `Schematic`/`Blocks`). `readSchem` accepts + this layout with a warning. + +- `real/legacy/` holds `storageroom-medieval.schematic` (pre-1.13 MCEdit + `Materials: "Alpha"` format), used to verify legacy `.schematic` rejection. + +`packages/core/test/real-fixtures.test.ts`, `convert.test.ts` and +`packages/mcbuilder/test/importer.test.ts` read and convert these fixtures. diff --git a/fixtures/real/flat-v3/smp_lobby_big.schem b/fixtures/real/flat-v3/smp_lobby_big.schem new file mode 100644 index 0000000..95771ff Binary files /dev/null and b/fixtures/real/flat-v3/smp_lobby_big.schem differ diff --git a/fixtures/real/flat-v3/smp_lobby_center_dense.schem b/fixtures/real/flat-v3/smp_lobby_center_dense.schem new file mode 100644 index 0000000..0787d3c Binary files /dev/null and b/fixtures/real/flat-v3/smp_lobby_center_dense.schem differ diff --git a/fixtures/real/flat-v3/smp_lobby_medieval.schem b/fixtures/real/flat-v3/smp_lobby_medieval.schem new file mode 100644 index 0000000..6330872 Binary files /dev/null and b/fixtures/real/flat-v3/smp_lobby_medieval.schem differ diff --git a/fixtures/real/legacy/storageroom-medieval.schematic b/fixtures/real/legacy/storageroom-medieval.schematic new file mode 100644 index 0000000..be536fa Binary files /dev/null and b/fixtures/real/legacy/storageroom-medieval.schematic differ diff --git a/fixtures/real/shulkr/castelo-medieval.schem b/fixtures/real/shulkr/castelo-medieval.schem new file mode 100644 index 0000000..4a21443 Binary files /dev/null and b/fixtures/real/shulkr/castelo-medieval.schem differ diff --git a/fixtures/real/shulkr/raifox-medivial-house.schem b/fixtures/real/shulkr/raifox-medivial-house.schem new file mode 100644 index 0000000..db1a9df Binary files /dev/null and b/fixtures/real/shulkr/raifox-medivial-house.schem differ diff --git a/fixtures/real/shulkr/simple-survival-mansion.schem b/fixtures/real/shulkr/simple-survival-mansion.schem new file mode 100644 index 0000000..a5222c9 Binary files /dev/null and b/fixtures/real/shulkr/simple-survival-mansion.schem differ diff --git a/fixtures/real/worldedit/fantasyhouse2.schem b/fixtures/real/worldedit/fantasyhouse2.schem new file mode 100644 index 0000000..2834e52 Binary files /dev/null and b/fixtures/real/worldedit/fantasyhouse2.schem differ diff --git a/packages/core/src/codecs/schem.ts b/packages/core/src/codecs/schem.ts index 9007165..a1dba85 100644 --- a/packages/core/src/codecs/schem.ts +++ b/packages/core/src/codecs/schem.ts @@ -228,9 +228,18 @@ export async function readSchem( if (version !== 2 && version !== 3) { throw new CodecError(`unsupported Sponge schematic version ${version} (expected 2 or 3)`); } - if ((version === 3) !== isV3) { + // Some real-world exporters write Sponge v2's flat layout (top-level `Palette` and `BlockData`, + // no nested `Schematic`/`Blocks`) while stamping `Version: 3`. Accept that layout with a warning. + const flatV2WithV3Tag = + version === 3 && + !isV3 && + child(schem, "Blocks") === undefined && + child(schem, "Palette")?.type === "compound" && + child(schem, "BlockData")?.type === "byteArray"; + if ((version === 3) !== isV3 && !flatV2WithV3Tag) { throw new CodecError(`Sponge schematic version ${version} has the wrong layout`); } + const layoutVersion: SchemVersion = flatV2WithV3Tag ? 2 : version; const width = dimension(schem, "Width"); const height = dimension(schem, "Height"); @@ -240,7 +249,7 @@ export async function readSchem( let paletteTag: NbtTag | undefined; let dataTag: NbtTag | undefined; let blockEntities: NbtTag | undefined; - if (version === 3) { + if (layoutVersion === 3) { const blocks = asCompound(child(schem, "Blocks"), "Blocks"); paletteTag = child(blocks, "Palette"); dataTag = child(blocks, "Data"); @@ -301,17 +310,23 @@ export async function readSchem( } const lost = new Tally(); - readBlockEntities(volume, compoundList(blockEntities), version, lost); - readEntities(volume, compoundList(child(schem, "Entities")), version, lost); + readBlockEntities(volume, compoundList(blockEntities), layoutVersion, lost); + readEntities(volume, compoundList(child(schem, "Entities")), layoutVersion, lost); const warnings = [...lost.lines("their position holds no block, or is missing or malformed")]; warnings.push(...dropped.lines()); + if (flatV2WithV3Tag) { + warnings.push("Version is 3, but layout is Sponge v2 (read using v2 layout)"); + } const metadata = child(schem, "Metadata"); if (metadata?.type === "compound" && metadata.value.size > 0) { warnings.push(`Metadata not carried over (${[...metadata.value.keys()].join(", ")})`); } // v3 keeps biomes in a "Biomes" compound; v2 in "BiomeData" with a "BiomePalette". - if (child(schem, version === 3 ? "Biomes" : "BiomeData") !== undefined) { + if ( + child(schem, layoutVersion === 3 ? "Biomes" : "BiomeData") !== undefined || + (flatV2WithV3Tag && child(schem, "Biomes") !== undefined) + ) { warnings.push("biomes not carried over (a volume holds no biomes)"); } diff --git a/packages/core/test/convert.test.ts b/packages/core/test/convert.test.ts index 1d2e69b..8aa8f9f 100644 --- a/packages/core/test/convert.test.ts +++ b/packages/core/test/convert.test.ts @@ -86,6 +86,41 @@ describe("scripts/convert.ts", () => { expect(final.volume.size).toBe(762); }); + it("turns a real WorldEdit .schem into a .nbt with identical blocks and entities", async () => { + const src = join(root, "fixtures/real/worldedit/fantasyhouse2.schem"); + const out = join(dir, "fantasyhouse2.nbt"); + const log = run(src, out); + expect(log).toContain("22953 blocks, 342 palette entries, DataVersion 5023"); + const original = await readSchem(new Uint8Array(readFileSync(src))); + const converted = await readStructureNbt(new Uint8Array(readFileSync(out))); + expect(converted.volume.size).toBe(original.volume.size); + expect(converted.volume.blockEntityCount).toBe(93); + expect(converted.volume.entities).toHaveLength(41); + expect(converted.meta.dataVersion).toBe(5023); + let mismatches = 0; + original.volume.eachCell((x, y, z, idx) => { + if (converted.volume.get(x, y, z) !== original.volume.palette[idx]) mismatches++; + }); + expect(mismatches).toBe(0); + }); + + it("converts a flat-v3 .schem into .nbt and prints the layout warning on stderr", async () => { + const src = join(root, "fixtures/real/flat-v3/smp_lobby_big.schem"); + const out = join(dir, "smp_lobby_big.nbt"); + const res = spawnSync(process.execPath, [script, src, out], { encoding: "utf8" }); + expect(res.status).toBe(0); + expect(res.stdout).toContain("18295 blocks, 41 palette entries, DataVersion 3465"); + expect(res.stderr).toContain("Version is 3, but layout is Sponge v2"); + const original = await readSchem(new Uint8Array(readFileSync(src))); + const converted = await readStructureNbt(new Uint8Array(readFileSync(out))); + expect(converted.volume.size).toBe(original.volume.size); + let mismatches = 0; + original.volume.eachCell((x, y, z, idx) => { + if (converted.volume.get(x, y, z) !== original.volume.palette[idx]) mismatches++; + }); + expect(mismatches).toBe(0); + }); + it("keeps the .schem Offset from .schem to .schem", async () => { const v = new Volume(); v.set(0, 0, 0, "minecraft:stone"); @@ -135,12 +170,14 @@ describe("scripts/convert.ts", () => { it("tells the user to re-save a legacy .schematic as .schem", () => { const legacy = join(dir, "old.schematic"); writeFileSync(legacy, new Uint8Array(0)); - const bad = spawnSync(process.execPath, [script, legacy, join(dir, "x.nbt")], { - encoding: "utf8", - }); - expect(bad.status).toBe(1); - expect(bad.stderr).toContain("legacy .schematic"); - expect(bad.stderr).toContain("re-save it as .schem"); + for (const src of [legacy, join(root, "fixtures/real/legacy/storageroom-medieval.schematic")]) { + const bad = spawnSync(process.execPath, [script, src, join(dir, "x.nbt")], { + encoding: "utf8", + }); + expect(bad.status).toBe(1); + expect(bad.stderr).toContain("legacy .schematic"); + expect(bad.stderr).toContain("re-save it as .schem"); + } }); it("fails clearly on bad input", () => { diff --git a/packages/core/test/real-fixtures.test.ts b/packages/core/test/real-fixtures.test.ts index f1e68bd..477019a 100644 --- a/packages/core/test/real-fixtures.test.ts +++ b/packages/core/test/real-fixtures.test.ts @@ -1,8 +1,27 @@ // SPDX-License-Identifier: AGPL-3.0-only import { describe, expect, it } from "vitest"; -import { AIR, readStructureNbt } from "../src/index.ts"; +import { + AIR, + LEGACY_SCHEMATIC_MESSAGE, + readLitematic, + readSchem, + readStructureNbt, + type Volume, + writeLitematic, + writeSchem, + writeStructureNbt, +} from "../src/index.ts"; import { loadFixture } from "./helpers.ts"; +function expectSameCells(actual: Volume, expected: Volume): void { + expect(actual.size).toBe(expected.size); + let mismatches = 0; + actual.eachCell((x, y, z, idx) => { + if (expected.get(x, y, z) !== actual.palette[idx]) mismatches++; + }); + expect(mismatches).toBe(0); +} + // Rafeals_house was saved by a real tool (bloxelizer.com) in three formats. // The .nbt uses DataVersion 5023, whose palette entries are spelled // `id` and `properties`. @@ -26,3 +45,208 @@ describe("Rafeals_house.nbt (saved by a real tool)", () => { expect(volume.palette).not.toContain(AIR); }); }); + +describe("WorldEdit fantasyhouse2.schem (Sponge v3)", () => { + const file = () => loadFixture("real/worldedit/fantasyhouse2.schem"); + + it("reads blocks, 2-byte palette varints, block entities, entities and offset", async () => { + const { volume, meta, warnings } = await readSchem(file()); + expect(meta).toEqual({ + version: 3, + dataVersion: 5023, + offset: [-18, -13, -32], + width: 44, + height: 39, + length: 34, + droppedBlockEntities: 0, + droppedEntities: 0, + }); + expect(volume.size).toBe(22953); + expect(volume.countNonAir()).toBe(22953); + expect(volume.palette).toHaveLength(342); + expect(volume.blockEntityCount).toBe(93); + expect(volume.entities).toHaveLength(41); + expect(volume.bounds()).toEqual([ + [0, 0, 0], + [43, 35, 33], + ]); + expect(warnings).toEqual(["Metadata not carried over (Date, WorldEdit)"]); + + const entityIds = new Set(volume.entities.map((e) => e.nbt.value.get("id")?.value)); + expect(entityIds).toEqual( + new Set([ + "minecraft:painting", + "minecraft:cushion", + "minecraft:spruce_boat", + "minecraft:cod", + "minecraft:armor_stand", + "minecraft:item_frame", + ]), + ); + }); + + it("keeps air across the full declared volume when requested", async () => { + const { volume } = await readSchem(file(), { keepAir: true }); + expect(volume.size).toBe(44 * 39 * 34); + expect(volume.countNonAir()).toBe(22953); + }); + + it("round-trips through .schem (v2/v3), .nbt and .litematic without losing blocks or entities", async () => { + const { volume, meta } = await readSchem(file()); + const dv = meta.dataVersion as number; + + for (const version of [2, 3] as const) { + const written = await writeSchem(volume, { dataVersion: dv, version, offset: meta.offset }); + expect(written.warnings).toEqual([]); + const back = await readSchem(written.bytes); + expect(back.meta).toMatchObject({ version, dataVersion: dv, offset: meta.offset }); + expectSameCells(back.volume, volume); + expect(back.volume.blockEntityCount).toBe(93); + expect(back.volume.entities).toHaveLength(41); + } + + const nbt = await writeStructureNbt(volume, { dataVersion: dv }); + expect(nbt.warnings).toEqual([]); + const nbtBack = await readStructureNbt(nbt.bytes); + expectSameCells(nbtBack.volume, volume); + expect(nbtBack.volume.blockEntityCount).toBe(93); + expect(nbtBack.volume.entities).toHaveLength(41); + + const lit = await writeLitematic(volume, { dataVersion: dv }); + expect(lit.warnings).toEqual([]); + const litBack = await readLitematic(lit.bytes); + expectSameCells(litBack.volume, volume); + expect(litBack.volume.blockEntityCount).toBe(93); + expect(litBack.volume.entities).toHaveLength(41); + }); +}); + +describe("Shulkr Sponge v3 .schem fixtures", () => { + const cases = [ + { + file: "real/shulkr/castelo-medieval.schem", + width: 29, + height: 39, + length: 27, + blocks: 4895, + states: 143, + blockEntities: 6, + }, + { + file: "real/shulkr/raifox-medivial-house.schem", + width: 14, + height: 20, + length: 15, + blocks: 1595, + states: 146, + blockEntities: 138, + }, + { + file: "real/shulkr/simple-survival-mansion.schem", + width: 25, + height: 26, + length: 47, + blocks: 6218, + states: 277, + blockEntities: 77, + }, + ]; + + it.each(cases)( + "reads $file ($blocks blocks, $blockEntities block entities)", + async ({ file, width, height, length, blocks, states, blockEntities }) => { + const { volume, meta, warnings } = await readSchem(loadFixture(file)); + expect(meta).toEqual({ + version: 3, + dataVersion: 5023, + offset: [0, 0, 0], + width, + height, + length, + droppedBlockEntities: 0, + droppedEntities: 0, + }); + expect(volume.size).toBe(blocks); + expect(volume.countNonAir()).toBe(blocks); + expect(volume.palette).toHaveLength(states); + expect(volume.blockEntityCount).toBe(blockEntities); + expect(volume.entities).toHaveLength(0); + expect(warnings).toEqual([ + "Metadata not carried over (Name, Author, Description, Date, Software)", + ]); + }, + ); +}); + +describe("Flat Sponge v2 layout stamped Version 3 (.schem)", () => { + const cases = [ + { + file: "real/flat-v3/smp_lobby_big.schem", + width: 81, + height: 24, + length: 81, + blocks: 18295, + states: 41, + max: [80, 13, 80] as [number, number, number], + }, + { + file: "real/flat-v3/smp_lobby_center_dense.schem", + width: 101, + height: 38, + length: 101, + blocks: 33882, + states: 88, + max: [100, 25, 100] as [number, number, number], + }, + { + file: "real/flat-v3/smp_lobby_medieval.schem", + width: 101, + height: 32, + length: 101, + blocks: 31699, + states: 40, + max: [100, 24, 100] as [number, number, number], + }, + ]; + + it.each(cases)( + "reads $file ($blocks blocks) and warns about the layout mismatch", + async ({ file, width, height, length, blocks, states, max }) => { + const { volume, meta, warnings } = await readSchem(loadFixture(file)); + expect(meta).toEqual({ + version: 3, + dataVersion: 3465, + offset: [0, 0, 0], + width, + height, + length, + droppedBlockEntities: 0, + droppedEntities: 0, + }); + expect(volume.size).toBe(blocks); + expect(volume.countNonAir()).toBe(blocks); + expect(volume.palette).toHaveLength(states); + expect(volume.bounds()).toEqual([[0, 0, 0], max]); + expect(warnings).toEqual(["Version is 3, but layout is Sponge v2 (read using v2 layout)"]); + }, + ); + + it("re-writes a flat-v3 build into standard Sponge v3 cleanly", async () => { + const orig = await readSchem(loadFixture("real/flat-v3/smp_lobby_big.schem")); + const written = await writeSchem(orig.volume, { + dataVersion: orig.meta.dataVersion as number, + }); + expect(written.warnings).toEqual([]); + const back = await readSchem(written.bytes); + expect(back.warnings).toEqual([]); + expectSameCells(back.volume, orig.volume); + }); +}); + +describe("Legacy pre-1.13 .schematic fixture", () => { + it("refuses storageroom-medieval.schematic with guidance to re-save as .schem", async () => { + await expect( + readSchem(loadFixture("real/legacy/storageroom-medieval.schematic")), + ).rejects.toThrow(LEGACY_SCHEMATIC_MESSAGE); + }); +}); diff --git a/packages/core/test/schem.test.ts b/packages/core/test/schem.test.ts index 40faa55..ccbaff5 100644 --- a/packages/core/test/schem.test.ts +++ b/packages/core/test/schem.test.ts @@ -15,7 +15,7 @@ import { writeSchem, writeStructureNbt, } from "../src/index.ts"; -import { loadFixture, tagToJson } from "./helpers.ts"; +import { CHEST, chestEntity, cow, loadFixture, tagToJson } from "./helpers.ts"; interface Expected { files: Record< @@ -127,6 +127,23 @@ describe(".schem round trips", () => { expect((await readSchem(bytes)).volume.get(0, 0, 0)).toBe("minecraft:chest[facing=north]"); }); + it("reads a flat Sponge v2 layout stamped Version 3 with a warning", async () => { + const v = new Volume(); + v.set(0, 0, 0, "minecraft:stone"); + v.set(1, 0, 0, CHEST); + v.setBlockEntity(1, 0, 0, chestEntity()); + v.addEntity(cow(0.5, 1, 0.5)); + const { root } = buildSchem(v, { dataVersion: 3465, version: 2 }); + root.value.set("Version", int(3)); + const bytes = writeNbt({ name: "", tag: root }); + const read = await readSchem(bytes); + expect(read.meta.version).toBe(3); + expect(read.warnings).toContain("Version is 3, but layout is Sponge v2 (read using v2 layout)"); + expect(cellsOf(read.volume)).toEqual(cellsOf(v)); + expect(read.volume.blockEntityCount).toBe(1); + expect(read.volume.entities).toHaveLength(1); + }); + it("refuses an empty volume", () => { expect(() => buildSchem(new Volume(), { dataVersion: 1 })).toThrow(CodecError); }); @@ -154,6 +171,8 @@ describe(".schem error handling", () => { tag: compound([["Schematic", compound([["Version", int(2)]])]]), }); await expect(readSchem(lying)).rejects.toThrow(/wrong layout/); + const flatWithoutBody = writeNbt({ name: "", tag: compound([["Version", int(3)]]) }); + await expect(readSchem(flatWithoutBody)).rejects.toThrow(/wrong layout/); }); it("rejects block data shorter than the volume, and undefined palette ids", async () => { diff --git a/packages/mcbuilder/test/importer.test.ts b/packages/mcbuilder/test/importer.test.ts index 35ea10a..9e5f26e 100644 --- a/packages/mcbuilder/test/importer.test.ts +++ b/packages/mcbuilder/test/importer.test.ts @@ -1,5 +1,11 @@ // SPDX-License-Identifier: AGPL-3.0-only -import { describe, expect, it } from "vitest"; +import { execFileSync } from "node:child_process"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { readSchem, readStructureNbt } from "@voxwright/core"; +import { afterAll, describe, expect, it } from "vitest"; import { Build, cleanProperties, @@ -11,6 +17,7 @@ import { RENAMED_BLOCKS, Registry, type Structure, + structureFromVolume, summary, toScript, } from "../src/index.ts"; @@ -216,3 +223,65 @@ describe("import: specifier and renamed blocks", () => { expect(Object.keys(RENAMED_BLOCKS)).toContain("minecraft:chain"); }); }); + +describe("convert.ts -> mcbuilder importer on real .schem fixtures", () => { + const repoRoot = fileURLToPath(new URL("../../../", import.meta.url)); + const convertScript = join(repoRoot, "scripts/convert.ts"); + const workDir = mkdtempSync(join(tmpdir(), "voxwright-convert-import-")); + afterAll(() => rmSync(workDir, { recursive: true, force: true })); + + it.each([ + "fixtures/real/worldedit/fantasyhouse2.schem", + "fixtures/real/shulkr/raifox-medivial-house.schem", + "fixtures/real/flat-v3/smp_lobby_big.schem", + ])("converts %s to .nbt and imports back with identical blocks", async (relPath) => { + const src = join(repoRoot, relPath); + const outNbt = join(workDir, `${basename(relPath, ".schem")}.nbt`); + execFileSync(process.execPath, [convertScript, src, outNbt], { stdio: "pipe" }); + + const original = await readSchem(new Uint8Array(readFileSync(src))); + const converted = await readStructureNbt(new Uint8Array(readFileSync(outNbt))); + const structure = structureFromVolume(converted.volume, basename(outNbt), { + dataVersion: converted.meta.dataVersion, + droppedBlockEntities: converted.meta.droppedBlockEntities + converted.volume.blockEntityCount, + droppedEntities: converted.meta.droppedEntities + converted.volume.entities.length, + }); + const code = toScript(structure) + .replace('import { Build } from "@voxwright/mcbuilder";', "") + .replace("export const BUILD", "const BUILD"); + const rebuilt = (new Function("Build", `${code}\nreturn BUILD;`)(Build) as Build).grid.volume; + + expect(rebuilt.size).toBe(original.volume.size); + let mismatches = 0; + rebuilt.eachCell((x, y, z, idx) => { + if (original.volume.get(x, y, z) !== rebuilt.palette[idx]) mismatches++; + }); + expect(mismatches).toBe(0); + }); + + it("renames legacy 1.20 block ids when cleaning an imported flat-v3 build for 26.2", async () => { + const src = join(repoRoot, "fixtures/real/flat-v3/smp_lobby_big.schem"); + const outNbt = join(workDir, "smp_lobby_big_rename.nbt"); + execFileSync(process.execPath, [convertScript, src, outNbt], { stdio: "pipe" }); + const converted = await readStructureNbt(new Uint8Array(readFileSync(outNbt))); + const st = structureFromVolume(converted.volume, basename(outNbt), { + dataVersion: converted.meta.dataVersion, + }); + const reg = new Registry( + normalizeBlockData([ + { + name: "iron_chain", + states: [ + { name: "axis", type: "enum", values: ["x", "y", "z"] }, + { name: "waterlogged", type: "bool", values: ["true", "false"] }, + ], + }, + ]), + "26.2", + ); + const cleaned = cleanProperties(st, reg); + expect(cleaned.renamedBlocks?.get("chain -> iron_chain")).toBeGreaterThan(0); + expect(cleaned.blocks.some(([, , , b]) => b.startsWith("minecraft:iron_chain["))).toBe(true); + expect(cleaned.blocks.some(([, , , b]) => b.startsWith("minecraft:chain["))).toBe(false); + }); +}); diff --git a/packages/mcbuilder/test/skill-cli.test.ts b/packages/mcbuilder/test/skill-cli.test.ts index e7e9e0f..c1a115a 100644 --- a/packages/mcbuilder/test/skill-cli.test.ts +++ b/packages/mcbuilder/test/skill-cli.test.ts @@ -887,6 +887,38 @@ BUILD.scope(() => { expect(n).toBe(5); }); + it("inspects real-tool WorldEdit and flat-v3 .schem files", async () => { + const we = await cli([ + "inspect", + join(repo, "fixtures/real/worldedit/fantasyhouse2.schem"), + "--json", + ]); + expect(we.code).toBe(0); + expect(JSON.parse(we.stdout)).toMatchObject({ + format: "schem", + formatVersion: 3, + dataVersion: 5023, + blocks: 22953, + blockStates: 342, + blockEntities: 93, + entities: 41, + }); + + const flat = await cli([ + "inspect", + join(repo, "fixtures/real/flat-v3/smp_lobby_big.schem"), + "--json", + ]); + expect(flat.code).toBe(0); + expect(JSON.parse(flat.stdout)).toMatchObject({ + format: "schem", + formatVersion: 3, + dataVersion: 3465, + blocks: 18295, + warnings: ["Version is 3, but layout is Sponge v2 (read using v2 layout)"], + }); + }); + it("is what scripts/build-skill.ts produces from the current sources", async () => { const r = await run(process.execPath, [join(repo, "scripts/build-skill.ts"), "--check"]).then( (ok) => ({ code: 0, ...ok }), diff --git a/skills/voxwright/scripts/voxwright-cli.mjs b/skills/voxwright/scripts/voxwright-cli.mjs index d29a182..c8435c3 100644 --- a/skills/voxwright/scripts/voxwright-cli.mjs +++ b/skills/voxwright/scripts/voxwright-cli.mjs @@ -4725,9 +4725,11 @@ async function readSchem(bytes, options = {}) { if (version !== 2 && version !== 3) { throw new CodecError(`unsupported Sponge schematic version ${version} (expected 2 or 3)`); } - if (version === 3 !== isV3) { + const flatV2WithV3Tag = version === 3 && !isV3 && child(schem, "Blocks") === void 0 && child(schem, "Palette")?.type === "compound" && child(schem, "BlockData")?.type === "byteArray"; + if (version === 3 !== isV3 && !flatV2WithV3Tag) { throw new CodecError(`Sponge schematic version ${version} has the wrong layout`); } + const layoutVersion = flatV2WithV3Tag ? 2 : version; const width = dimension(schem, "Width"); const height = dimension(schem, "Height"); const length = dimension(schem, "Length"); @@ -4735,7 +4737,7 @@ async function readSchem(bytes, options = {}) { let paletteTag; let dataTag; let blockEntities; - if (version === 3) { + if (layoutVersion === 3) { const blocks = asCompound(child(schem, "Blocks"), "Blocks"); paletteTag = child(blocks, "Palette"); dataTag = child(blocks, "Data"); @@ -4791,15 +4793,18 @@ async function readSchem(bytes, options = {}) { volume.set(x2, y, z, kind === "air" ? AIR : state); } const lost = new Tally(); - readBlockEntities(volume, compoundList(blockEntities), version, lost); - readEntities(volume, compoundList(child(schem, "Entities")), version, lost); + readBlockEntities(volume, compoundList(blockEntities), layoutVersion, lost); + readEntities(volume, compoundList(child(schem, "Entities")), layoutVersion, lost); const warnings = [...lost.lines("their position holds no block, or is missing or malformed")]; warnings.push(...dropped.lines()); + if (flatV2WithV3Tag) { + warnings.push("Version is 3, but layout is Sponge v2 (read using v2 layout)"); + } const metadata = child(schem, "Metadata"); if (metadata?.type === "compound" && metadata.value.size > 0) { warnings.push(`Metadata not carried over (${[...metadata.value.keys()].join(", ")})`); } - if (child(schem, version === 3 ? "Biomes" : "BiomeData") !== void 0) { + if (child(schem, layoutVersion === 3 ? "Biomes" : "BiomeData") !== void 0 || flatV2WithV3Tag && child(schem, "Biomes") !== void 0) { warnings.push("biomes not carried over (a volume holds no biomes)"); } const dv = child(schem, "DataVersion");