2020-06-10 08:04:59 -04:00
|
|
|
-- LUALOCALS < ---------------------------------------------------------
|
2021-12-10 08:01:45 -05:00
|
|
|
local ipairs, minetest, nodecore, pairs, table, type
|
|
|
|
= ipairs, minetest, nodecore, pairs, table, type
|
|
|
|
local table_concat, table_insert, table_sort
|
|
|
|
= table.concat, table.insert, table.sort
|
2020-06-10 08:04:59 -04:00
|
|
|
-- LUALOCALS > ---------------------------------------------------------
|
|
|
|
|
2021-12-02 06:55:09 -05:00
|
|
|
local modname = minetest.get_current_modname()
|
2020-06-10 08:04:59 -04:00
|
|
|
|
2020-09-27 09:30:57 -04:00
|
|
|
local tabs = {}
|
|
|
|
nodecore.registered_inventory_tabs = tabs
|
|
|
|
function nodecore.register_inventory_tab(def)
|
|
|
|
tabs[#tabs + 1] = def
|
|
|
|
nodecore.translate_inform(def.title)
|
|
|
|
if type(def.content) == "table" then
|
|
|
|
for i = 1, #def.content do
|
|
|
|
nodecore.translate_inform(def.content[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-10 08:04:59 -04:00
|
|
|
|
|
|
|
local nct = nodecore.translate
|
|
|
|
local fse = minetest.formspec_escape
|
2020-09-27 09:30:57 -04:00
|
|
|
|
2021-07-03 11:30:26 -04:00
|
|
|
local formwidth = 15
|
2021-06-26 09:51:56 -04:00
|
|
|
local formheight = formwidth / 2
|
|
|
|
|
2021-12-10 07:42:33 -05:00
|
|
|
local alltabswidth = formwidth - 0.25
|
|
|
|
local maxtabs = 7
|
2021-06-26 09:51:56 -04:00
|
|
|
local tabheight = 0.5
|
2021-12-10 07:42:33 -05:00
|
|
|
local tabmarginx = 0.2
|
|
|
|
local tabmarginy = 0.25
|
2021-06-26 09:51:56 -04:00
|
|
|
local tabmax = formwidth - 0.5
|
|
|
|
|
|
|
|
local textmarginx = 0.25
|
|
|
|
local textmarginy = 0.1
|
|
|
|
local textheight = formheight + 0.8
|
|
|
|
|
2021-12-02 06:55:09 -05:00
|
|
|
local metakey = modname .. "_inventory_key"
|
|
|
|
function nodecore.inventory_tab_get(player)
|
|
|
|
local n = player:get_meta():get_int(metakey)
|
|
|
|
return nodecore.registered_inventory_tabs[n] and n or 1
|
|
|
|
end
|
|
|
|
function nodecore.inventory_tab_set(player, tab)
|
|
|
|
player:get_meta():set_int(metakey,
|
|
|
|
tab and nodecore.registered_inventory_tabs[tab] and tab or 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function nodecore.inventory_formspec(player)
|
2020-06-10 08:04:59 -04:00
|
|
|
local t = {
|
|
|
|
"bgcolor[#000000C0;true]",
|
|
|
|
"listcolors[#00000000;#00000000;#00000000;#000000FF;#FFFFFFFF]"
|
|
|
|
}
|
|
|
|
|
2021-12-10 07:42:33 -05:00
|
|
|
local tablist = {}
|
2020-06-10 08:04:59 -04:00
|
|
|
for i, v in ipairs(nodecore.registered_inventory_tabs) do
|
2020-06-25 07:07:51 -04:00
|
|
|
local vis = v.visible
|
|
|
|
if type(vis) == "function" then vis = vis(v, player) end
|
|
|
|
if vis == nil or vis then
|
2021-12-10 07:42:33 -05:00
|
|
|
tablist[#tablist + 1] = {idx = i, tab = v}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local tabqty = #tablist
|
|
|
|
if tabqty > maxtabs then tabqty = maxtabs end
|
|
|
|
local tabwidth = alltabswidth / tabqty
|
|
|
|
local x = 0
|
|
|
|
local y = 0
|
|
|
|
local tabdata
|
|
|
|
local curtab = nodecore.inventory_tab_get(player)
|
|
|
|
for i, v in ipairs(tablist) do
|
|
|
|
if curtab == v.idx then
|
|
|
|
tabdata = v.tab
|
|
|
|
t[#t + 1] = "box[" .. x .. "," .. (y + tabheight) .. ";"
|
|
|
|
.. (tabwidth - 0.04) .. ",0.1;#ffffff]"
|
|
|
|
end
|
|
|
|
t[#t + 1] = "button[" .. x .. "," .. y .. ";"
|
|
|
|
.. (tabwidth + tabmarginx) .. "," .. tabheight .. ";tab" .. i
|
|
|
|
.. ";" .. fse(nct(v.tab.title)) .. "]"
|
|
|
|
x = x + tabwidth
|
|
|
|
if x >= tabmax then
|
|
|
|
x = 0
|
|
|
|
y = y + tabheight + tabmarginy
|
2020-06-10 08:04:59 -04:00
|
|
|
end
|
|
|
|
end
|
2021-12-10 07:42:33 -05:00
|
|
|
if x > 0 then y = y + tabheight + tabmarginy end
|
2020-06-10 08:04:59 -04:00
|
|
|
|
2021-06-26 09:51:56 -04:00
|
|
|
table_insert(t, 1, "size[" .. formwidth .. "," .. formheight + y .. "]")
|
2020-06-10 08:04:59 -04:00
|
|
|
|
2021-08-14 08:54:02 -04:00
|
|
|
if tabdata then
|
|
|
|
local content = tabdata.content
|
|
|
|
if type(content) == "function" then
|
|
|
|
content = content(player, {
|
|
|
|
w = formwidth,
|
|
|
|
h = textheight,
|
|
|
|
x = textmarginx,
|
|
|
|
y = y + textmarginy
|
|
|
|
}, t)
|
|
|
|
end
|
|
|
|
if not content then return end
|
|
|
|
if tabdata.raw then
|
|
|
|
if type(content) == "table" then
|
|
|
|
for _, v in ipairs(content) do t[#t + 1] = v end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
t[#t + 1] = "textarea[" .. textmarginx .. "," .. (y + textmarginy)
|
|
|
|
.. ";" .. formwidth .. "," .. textheight .. ";;;"
|
|
|
|
for _, v in ipairs(content) do t[#t + 1] = fse(nct(v) .. "\n") end
|
|
|
|
t[#t + 1] = "]"
|
|
|
|
end
|
2020-06-10 08:04:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return table_concat(t)
|
|
|
|
end
|
|
|
|
|
2021-12-02 06:55:09 -05:00
|
|
|
local invspeccache = {}
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
invspeccache[player:get_player_name()] = nil
|
2020-06-10 08:04:59 -04:00
|
|
|
end)
|
2021-12-02 06:55:09 -05:00
|
|
|
function nodecore.inventory_formspec_update(player)
|
|
|
|
local str = nodecore.inventory_formspec(player)
|
|
|
|
local pname = player:get_player_name()
|
|
|
|
if invspeccache[pname] == str then return str end
|
|
|
|
player:set_inventory_formspec(str)
|
|
|
|
invspeccache[pname] = str
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
nodecore.register_on_joinplayer("join set inv formspec", nodecore.inventory_formspec_update)
|
2020-06-10 08:04:59 -04:00
|
|
|
|
2020-06-15 07:21:39 -04:00
|
|
|
nodecore.register_on_player_receive_fields("player inv formspec returned",
|
|
|
|
function(player, formname, fields)
|
2020-06-10 08:04:59 -04:00
|
|
|
if formname == "" then
|
|
|
|
local tab
|
|
|
|
for i = 1, #nodecore.registered_inventory_tabs do
|
|
|
|
if fields["tab" .. i] then
|
|
|
|
tab = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if tab then
|
2021-12-02 06:55:09 -05:00
|
|
|
nodecore.inventory_tab_set(player, tab)
|
2021-12-12 00:36:24 -05:00
|
|
|
return minetest.show_formspec(player:get_player_name(),
|
2021-12-02 06:55:09 -05:00
|
|
|
formname, nodecore.inventory_formspec_update(player))
|
2020-06-10 08:04:59 -04:00
|
|
|
end
|
2021-12-12 00:36:24 -05:00
|
|
|
nodecore.inventory_formspec_update(player)
|
2020-06-10 08:04:59 -04:00
|
|
|
end
|
|
|
|
end)
|
2021-12-02 07:09:07 -05:00
|
|
|
|
|
|
|
local pending = {}
|
|
|
|
function nodecore.inventory_notify(pname, event)
|
|
|
|
pname = type(pname) == "string" and pname or pname:get_player_name()
|
|
|
|
local key = pname .. "|" .. event
|
|
|
|
if pending[key] then return end
|
|
|
|
pending[key] = true
|
|
|
|
minetest.after(0, function()
|
|
|
|
pending[key] = nil
|
|
|
|
|
|
|
|
local player = minetest.get_player_by_name(pname)
|
|
|
|
if not player then return end
|
|
|
|
|
|
|
|
local tab = nodecore.inventory_tab_get(player)
|
|
|
|
tab = tab and nodecore.registered_inventory_tabs[tab]
|
|
|
|
local evt = tab and tab["on_" .. event]
|
|
|
|
if type(evt) == "function" then evt = evt(player, pname) end
|
|
|
|
if evt then return nodecore.inventory_formspec_update(player) end
|
|
|
|
end)
|
|
|
|
end
|
2021-12-10 08:01:45 -05:00
|
|
|
|
|
|
|
nodecore.register_playerstep({
|
|
|
|
label = "hint tab watch interact",
|
|
|
|
action = function(player, data)
|
|
|
|
local privs = {}
|
2022-10-07 06:59:04 -04:00
|
|
|
for k, v in pairs(minetest.get_player_privs(data.pname)) do
|
2021-12-10 08:01:45 -05:00
|
|
|
if v then privs[#privs + 1] = k end
|
|
|
|
end
|
|
|
|
table_sort(privs)
|
|
|
|
privs = table_concat(privs, ",")
|
|
|
|
if privs == data.privstring then return end
|
|
|
|
data.privstring = privs
|
|
|
|
return nodecore.inventory_notify(player, "privchange")
|
|
|
|
end
|
|
|
|
})
|