104 lines
2.4 KiB
Lua
104 lines
2.4 KiB
Lua
--[[
|
|
|
|
TechAge
|
|
=======
|
|
|
|
Copyright (C) 2019-2024 Joachim Stolberg
|
|
|
|
AGPL v3
|
|
See LICENSE.txt for more information
|
|
|
|
Packing functions
|
|
|
|
]]--
|
|
|
|
-- for lazy programmers
|
|
local S = techage.S
|
|
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
|
local M = minetest.get_meta
|
|
|
|
-- string/usercode conversion
|
|
local function usercode_to_string(tbl)
|
|
if tbl and tbl.inventory then
|
|
for list_name,list in pairs(tbl.inventory) do
|
|
for i,item in ipairs(list) do
|
|
tbl.inventory[list_name][i] = item:to_string()
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function string_to_usercode(tbl)
|
|
if tbl and tbl.inventory then
|
|
for list_name,list in pairs(tbl.inventory) do
|
|
for i,item in ipairs(list) do
|
|
tbl.inventory[list_name][i] = ItemStack(item)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- pack/unpack node nvm data
|
|
local function pack_nvm(pos)
|
|
if techage.has_nvm(pos) then
|
|
local s = minetest.serialize(techage.get_nvm(pos))
|
|
techage.del_mem(pos)
|
|
return s
|
|
end
|
|
end
|
|
|
|
local function unpack_nvm(pos, s)
|
|
if s and s ~= "" then
|
|
local tbl = minetest.deserialize(s)
|
|
local nvm = techage.get_nvm(pos)
|
|
for k,v in pairs(tbl) do
|
|
nvm[k] = v
|
|
end
|
|
end
|
|
end
|
|
|
|
-- pack/unpack node metedata
|
|
local function pack_meta(pos)
|
|
local tbl = M(pos):to_table() or {}
|
|
usercode_to_string(tbl)
|
|
return minetest.serialize(tbl)
|
|
end
|
|
|
|
local function unpack_meta(pos, s)
|
|
if s and s ~= "" then
|
|
local tbl = minetest.deserialize(s) or {}
|
|
string_to_usercode(tbl)
|
|
M(pos):from_table(tbl)
|
|
end
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
-- preserve/restore API functions for the assembly tool
|
|
-------------------------------------------------------------------------------
|
|
|
|
function techage.preserve_nodedata(pos)
|
|
local smeta = pack_meta(pos)
|
|
local snvm = pack_nvm(pos)
|
|
return minetest.serialize({smeta = smeta, snvm = snvm})
|
|
end
|
|
|
|
function techage.restore_nodedata(pos, s)
|
|
local tbl = minetest.deserialize(s) or {}
|
|
unpack_nvm(pos, tbl.snvm)
|
|
unpack_meta(pos, tbl.smeta)
|
|
end
|
|
|
|
-- Don't allow placing if node in player inventory has metadata
|
|
function techage.cordless_screwdriver_only(pos, placer, itemstack)
|
|
local meta = itemstack:get_meta()
|
|
if placer and meta and meta:to_table() then
|
|
local tbl = meta:to_table()
|
|
if tbl.fields and next(tbl.fields) then
|
|
minetest.chat_send_player(placer:get_player_name(), S("Use the cordless screwdriver to place it! (item must be in hotbar slot 1)"))
|
|
minetest.remove_node(pos)
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|