Add pack lib

This commit is contained in:
Joachim Stolberg 2022-01-31 18:59:17 +01:00
parent 68deb3723a
commit 93ce219eec
2 changed files with 63 additions and 0 deletions

62
basis/pack_lib.lua Normal file
View File

@ -0,0 +1,62 @@
--[[
TechAge
=======
Copyright (C) 2019-2022 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
Packing functions
]]--
-- for lazy programmers
local P = minetest.string_to_pos
local M = minetest.get_meta
--function techage.pack_nvm(pos)
-- return minetest.serialize(techage.)
--end
--function techage.unpack_nvm(pos)
--end
local function serialize_inventory(tbl)
local out1 = {}
for k,v in pairs(tbl) do
local out2 = {}
for i,item in ipairs(v) do
out2[i] = item:to_string()
end
out1[k] = minetest.serialize(out2)
end
return out1
end
function techage.pack_meta(pos)
local tbl = M(pos):to_table()
local fields, inventory
if tbl and tbl.fields then
fields = minetest.serialize(tbl.fields)
end
local inv = minetest.get_inventory({type="node", pos=pos})
local lists = inv:get_lists()
print(dump(lists))
if lists then
inventory = serialize_inventory(lists)
end
return minetest.serialize({fields = fields, inventory = inventory})
end
--function techage.unpack_meta(pos, s)
-- local tbl = minetest.deserialize(s or "") or {}
-- local out = {}
-- for i,item in ipairs(tbl) do
-- out[i] = ItemStack(item)
-- end
-- local inv = minetest.get_inventory({type="node", pos=pos})
--end

View File

@ -104,6 +104,7 @@ dofile(MP.."/basis/submenu.lua")
dofile(MP.."/basis/shared_inv.lua")
dofile(MP.."/basis/shared_tank.lua")
dofile(MP.."/basis/teleport.lua")
dofile(MP.."/basis/pack_lib.lua")
-- Main doc
dofile(MP.."/doc/manual_DE.lua")