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
This commit is contained in:
1 parent
09354cbd12
commit
286d193f14
9 files changed
+1032
-80
No files matched your search
@@ -4,6 +4,7 @@
|
||||
-- converted from / to MCEdit's and Litematica's flat { x, y, z, id, ... } and Sponge v2's { Pos, Id, ... }).
|
||||
local Types = require("@formats/Types")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
|
||||
local Common = {}
|
||||
|
||||
@@ -19,13 +20,7 @@ export type Region = {
|
||||
}
|
||||
|
||||
-- int(v) -> the integer value of an NBT number (byte / short / int / long tag or plain number) | nil
|
||||
function Common.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
|
||||
Common.int = Mc.int
|
||||
|
||||
-- dims(a, b, c, max) -> sx, sy, sz | nil, message: positive sizes whose product stays under max cells
|
||||
function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number?)
|
||||
@@ -43,80 +38,11 @@ function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number
|
||||
end
|
||||
|
||||
-- ints(v) -> the numbers of an intArray tag or a list of numbers | nil
|
||||
function Common.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
|
||||
Common.ints = Mc.ints
|
||||
|
||||
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 Common.blockEntity(c: any, style: string): any
|
||||
if type(c) ~= "table" or Nbt.isTag(c) then
|
||||
return nil
|
||||
end
|
||||
if style == "flat" then
|
||||
return { pos = { Common.int(c.x) or 0, Common.int(c.y) or 0, Common.int(c.z) or 0 }, id = if type(c.id) == "string" then c.id else nil, data = rest(c, FLAT_SKIP) }
|
||||
end
|
||||
local p = Common.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 Common.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
|
||||
-- blockEntity(c, style) / flatBlockEntity(be, style): codecs/model/Minecraft's (shared with model/mca)
|
||||
Common.blockEntity = Mc.blockEntity
|
||||
Common.flatBlockEntity = Mc.flatBlockEntity
|
||||
|
||||
-- list(v) -> v when it is an NBT list (array), else an empty list
|
||||
function Common.list(v: any): { any }
|
||||
|
||||
Reference in new issue
Block a user