i3_extrabuttons/init.lua
2025-08-22 02:33:02 +02:00

87 lines
2.7 KiB
Lua

local S = minetest.get_translator(minetest.get_current_modname())
local extrabuttons = {}
i3_extrabuttons = {}
-- API-kompatibel: optionale priv/privs
function i3_extrabuttons.new_tab(name, def)
extrabuttons[#extrabuttons+1] = {
name = name,
def = def,
}
end
local function has_required_priv(player, def)
-- Einzelnes Privileg
if def.priv then
return minetest.check_player_privs(player, { [def.priv] = true })
end
-- Liste von Privs (alle müssen erfüllt sein)
if def.privs then
for _, priv in ipairs(def.privs) do
if not minetest.check_player_privs(player, { [priv] = true }) then
return false
end
end
return true
end
-- Kein Priv, immer anzeigen
return true
end
local function extras_formspec(player, data, fs)
-- Alphabetisch sortiert nach sichtbarer Beschriftung ("description" oder "name")
table.sort(extrabuttons, function(a, b)
local ad = a.def.description or a.name
local bd = b.def.description or b.name
return ad:lower() < bd:lower()
end)
local max_per_col = 10
local btn_width = 4
local btn_height = 0.8
local col_space = 7
local x_left = 1
local x_right = x_left + col_space
local btn_idx = 0
for _, tab in ipairs(extrabuttons) do
-- PRÜFUNG auf Rechte für diese Button-Definition:
if has_required_priv(player, tab.def) then
btn_idx = btn_idx + 1
local desc = tab.def.description or tab.name
local col = math.floor((btn_idx-1) / max_per_col)
local row = (btn_idx-1) % max_per_col
local x = (col == 0) and x_left or x_right
local y = 1 + row * (btn_height + 0.3)
fs(string.format(
'image_button[%.1f,%.1f;%.1f,%.1f;i3_extrabuttons_blank.png;extrabtn_%s;%s;;i3_extrabuttons_button_hover.png]',
x, y, btn_width, btn_height,
tab.name, minetest.formspec_escape(desc)
))
end
end
end
local function extras_fields(player, data, fields)
for _, tab in ipairs(extrabuttons) do
if fields["extrabtn_"..tab.name] and tab.def.fields then
return tab.def.fields(player, data, fields)
elseif fields["extrabtn_"..tab.name] and tab.def.formspec then
i3.set_tab(player, "inventory")
tab.def.formspec(player, data, function(...) end)
return
end
end
end
-- Tabs registrieren (wie gehabt)
if i3 then
i3.new_tab("i3_extrabuttons", {
description = S("Extras"),
formspec = extras_formspec,
fields = extras_fields,
})
end
_G.i3_extrabuttons = i3_extrabuttons