From 44a625658980bc7293611f84bb9415376a1e8429 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 21 Mar 2022 00:06:51 +0100 Subject: [PATCH] Fix tab access on priv revoking --- src/api.lua | 11 +++++++++-- src/gui.lua | 11 ++++++++++- tests/test_tabs.lua | 22 +++++----------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/api.lua b/src/api.lua index 5863dc4..e2124ca 100644 --- a/src/api.lua +++ b/src/api.lua @@ -167,6 +167,13 @@ function i3.set_fs(player) sort_inventory(player, data) end + for i, tab in ipairs(i3.tabs) do + if data.tab == i and tab.access and not tab.access(player, data) then + data.tab = 1 + break + end + end + local fs = make_fs(player, data) player:set_inventory_formspec(fs) end @@ -221,8 +228,8 @@ function i3.set_tab(player, tabname) return end - for i, def in ipairs(i3.tabs) do - if def.name == tabname then + for i, tab in ipairs(i3.tabs) do + if tab.name == tabname then data.tab = i return end diff --git a/src/gui.lua b/src/gui.lua index f1a2b3a..87985da 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -1516,6 +1516,7 @@ local function make_fs(player, data) fs("bg9", 0, 0, data.inv_width, full_height, PNG.bg_full, 10) local tab = i3.tabs[data.tab] + if tab then tab.formspec(player, data, fs) end @@ -1526,7 +1527,15 @@ local function make_fs(player, data) get_items_fs(fs, data, player, full_height) end - if #i3.tabs > 1 then + local visible_tabs = 1 + + for _, def in ipairs(i3.tabs) do + if visible_tabs < 2 and def.access and def.access(player, data) then + visible_tabs++ + end + end + + if visible_tabs > 1 then get_tabs_fs(fs, player, data, full_height) end diff --git a/tests/test_tabs.lua b/tests/test_tabs.lua index 207fd6a..70e9847 100644 --- a/tests/test_tabs.lua +++ b/tests/test_tabs.lua @@ -16,30 +16,18 @@ i3.new_tab("test2", { end, }) -i3.new_tab("test3", { - description = "Test 3", +i3.new_tab("test_creative", { + description = "Test creative", access = function(player, data) local name = player:get_player_name() - if name == "singleplayer" then - return true - end + return core.is_creative_enabled(name) end, formspec = function(player, data, fs) - fs("label[3,1;Test 3]") + fs("label[3,1;Creative enabled]") end, - fields = function(player, data, fields) - i3.set_fs(player, "label[3,2;Test extra_fs]") - end, + fields = i3.set_fs, }) -i3.override_tab("test2", { - description = "Test override", - image = "i3_mesepick.png", - - formspec = function(player, data, fs) - fs("label[3,1;Override!]") - end, -})