ws-voxel: model/mca (Anvil regions read-only: gzip / zlib / LZ4Block / stored / external .mcc chunks; 1.18+, 1.13-1.17 and pre-1.13 chunk layouts) into Voxels with exact block states; block entity shapes moved into codecs/model/Minecraft

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:47:05 -05:00
1 parent 1eeca02fed
commit 4cbde068d2
9 files changed
+1032 -80

No files matched your search

@@ -497,4 +497,91 @@ function Minecraft.blocksOf(v: Types.Voxels): ({ string }?, any)
return states, out
end
-- ------------------------------------------------------------------------------------------------ block entities
-- Block entity records in one shape, { pos = { x, y, z }, id, data } (Sponge v3's), from / to the flat
-- { x, y, z, id, ... } of MCEdit, Litematica and chunks, and Sponge v1 / v2's { Pos, Id, ... }.
local function int(v: any): number?
local n = Nbt.number(v)
if n == nil or n ~= n or n % 1 ~= 0 then
return nil
end
return n
end
local function ints(v: any): { number }?
if Nbt.isTag(v) and v.type == "intArray" then
return v.value
elseif type(v) == "table" and not Nbt.isTag(v) and (v :: any)[1] ~= nil then
local out: { number } = {}
for i, x in v :: { any } do
out[i] = Nbt.number(x) or 0
end
return out
end
return nil
end
Minecraft.int = int -- the integer value of an NBT number | nil
Minecraft.ints = ints -- the numbers of an intArray or a list | nil
local function rest(c: any, skip: { [string]: boolean }): { [string]: any }
local out, keys = {}, {}
for _, k in Types.keys(c) do
if not skip[k] then
out[k] = c[k]
table.insert(keys, k)
end
end
return Types.object(out, keys)
end
local FLAT_SKIP = { x = true, y = true, z = true, id = true }
local POS_SKIP = { Pos = true, Id = true }
-- blockEntity(c, style) -> the common shape of one record: style "flat" (MCEdit, Litematica), "pos" (Sponge v1 / v2),
-- "data" (Sponge v3)
function Minecraft.blockEntity(c: any, style: string): any
if type(c) ~= "table" or Nbt.isTag(c) then
return nil
end
if style == "flat" then
return { pos = { int(c.x) or 0, int(c.y) or 0, int(c.z) or 0 }, id = if type(c.id) == "string" then c.id else nil, data = rest(c, FLAT_SKIP) }
end
local p = ints(c.Pos) or { 0, 0, 0 }
local data = if style == "data" then (if type(c.Data) == "table" then c.Data else Types.object()) else rest(c, POS_SKIP)
return { pos = { p[1] or 0, p[2] or 0, p[3] or 0 }, id = if type(c.Id) == "string" then c.Id else nil, data = data }
end
-- flatBlockEntity(be, style) -> the record in a flavour's shape (the inverse of blockEntity)
function Minecraft.flatBlockEntity(be: any, style: string): any
local o = Types.object()
local data = if type(be.data) == "table" then be.data else Types.object()
if style == "flat" then
Types.set(o, "x", be.pos[1])
Types.set(o, "y", be.pos[2])
Types.set(o, "z", be.pos[3])
if be.id then
Types.set(o, "id", be.id)
end
for _, k in Types.keys(data) do
if not FLAT_SKIP[k] then
Types.set(o, k, data[k])
end
end
return o
end
Types.set(o, "Pos", Nbt.intArray({ be.pos[1], be.pos[2], be.pos[3] }))
if be.id then
Types.set(o, "Id", be.id)
end
if style == "data" then
Types.set(o, "Data", data)
else
for _, k in Types.keys(data) do
if not POS_SKIP[k] then
Types.set(o, k, data[k])
end
end
end
return o
end
return Minecraft