ws-voxel: model/schematic (.schem v1-3, .schematic, .litematic read + write), codecs/model/Minecraft (block states, default colours, legacy ids, packed arrays)

Types change (backward compatible): Types.Voxels gains optional blocks (VoxelBlocks: exact block states as
u16 indices into a state palette, block entities, entities) and colors (exact u32 RGBA per cell).
This commit is contained in:
4DBug committed 2026-10-31 14:37:43 -05:00
1 parent 7fc5d3f5f0
commit fea56f4391
11 files changed
+524 -40

No files matched your search

+31 -6
View File
@@ -49,11 +49,22 @@ export type EncodeOptions = {
title: string?, -- .litematic Metadata.Name
compression: ("gzip" | "zlib" | "none")?, -- default gzip, as every tool writes them
level: number?, -- deflate level
format: string?, -- set by the api: the format key the encoder was picked by ("schem", "litematic", ...)
}
-- ------------------------------------------------------------------------------------------------ constants
local DEFAULT_DATA_VERSION = 3953
local LITEMATIC_HINTS = { "Regions", "EnclosingSize", "RegionCount", "TotalVolume", "MinecraftDataVersion" }
local SPONGE_HINTS = { "BlockData", "PaletteMax" } -- v1 / v2 fields (some writers leave the root unnamed)
local function hinted(head: string, hints: { string }): boolean
for _, h in hints do
if string.find(head, string.char(0, #h) .. h, 1, true) then
return true
end
end
return false
end
-- ------------------------------------------------------------------------------------------------ sniff
function Schematic.sniff(r: Bytes.Reader): number
@@ -65,12 +76,25 @@ function Schematic.sniff(r: Bytes.Reader): number
return 1
end
local head = buffer.tostring(p.head)
for _, h in LITEMATIC_HINTS do
if string.find(head, string.char(0, #h) .. h, 1, true) then
return 1
end
return if hinted(head, LITEMATIC_HINTS) or hinted(head, SPONGE_HINTS) then 1 else 0
end
-- variant(r) -> "schem" | "litematic" | "schematic" | nil: the flavour, for F.detect (from the first inflated bytes)
function Schematic.variant(r: Bytes.Reader): string?
local p = Nbt.peek(r.buf, r.base, r.limit - r.base)
if not p then
return nil
end
return 0
if p.firstName == "Schematic" then
return "schem"
end
local head = buffer.tostring(p.head)
if hinted(head, LITEMATIC_HINTS) then
return "litematic"
elseif hinted(head, SPONGE_HINTS) or (p.name == "Schematic" and hinted(head, { "Palette", "Version" })) then
return "schem"
end
return if p.name == "Schematic" then "schematic" else nil
end
-- ------------------------------------------------------------------------------------------------ decode
@@ -114,7 +138,8 @@ function Schematic.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?,
regions, extra = Mcedit.read(s, maxCells)
end
if not regions then
return nil, Err.from(extra)
local msg = tostring(extra)
return nil, Err.from(msg, nil, if string.find(msg, "limit: ", 1, true) then "limit" else nil)
end
local scene = Types.Scene.new()
scene.voxels = {}