Fix Survival Inventory Tab API (#4147)

* restored some old code that had gone missing
* fixed the survival inventory tab API not working
* fixed some grammar

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/4147
Reviewed-by: the-real-herowl <the-real-herowl@noreply.git.minetest.land>
Co-authored-by: James David Clarke <james@jamesdavidclarke.com>
Co-committed-by: James David Clarke <james@jamesdavidclarke.com>
This commit is contained in:
James David Clarke 2024-01-20 00:42:56 +00:00 committed by the-real-herowl
parent 59ad110e6b
commit aa4d5738c7
2 changed files with 25 additions and 12 deletions

@ -3,6 +3,19 @@ mcl_inventory = {}
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua") dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/survival.lua") dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/survival.lua")
local old_is_creative_enabled = minetest.is_creative_enabled
function minetest.is_creative_enabled(name)
if old_is_creative_enabled(name) then return true end
if not name then return false end
assert(type(name) == "string", "minetest.is_creative_enabled requires a string (the playername) argument.")
local p = minetest.get_player_by_name(name)
if p then
return p:get_meta():get_string("gamemode") == "creative"
end
return false
end
---@param player mt.PlayerObjectRef ---@param player mt.PlayerObjectRef
---@param armor_change_only? boolean ---@param armor_change_only? boolean
local function set_inventory(player, armor_change_only) local function set_inventory(player, armor_change_only)
@ -73,12 +86,14 @@ end)
---@param player mt.PlayerObjectRef ---@param player mt.PlayerObjectRef
function mcl_inventory.update_inventory(player) function mcl_inventory.update_inventory(player)
local player_gamemode = mcl_gamemode.get_gamemode(player) local player_name = player:get_player_name()
if player_gamemode == "creative" then local is_gamemode_creative = minetest.is_creative_enabled(player_name)
if is_gamemode_creative then
mcl_inventory.set_creative_formspec(player) mcl_inventory.set_creative_formspec(player)
elseif player_gamemode == "survival" then elseif not is_gamemode_creative then
player:set_inventory_formspec(mcl_inventory.build_survival_formspec(player)) player:set_inventory_formspec(mcl_inventory.build_survival_formspec(player))
end end
mcl_meshhand.update_player(player)
end end
mcl_gamemode.register_on_gamemode_change(function(player, old_gamemode, new_gamemode) mcl_gamemode.register_on_gamemode_change(function(player, old_gamemode, new_gamemode)

@ -1,18 +1,15 @@
---@diagnostic disable need-check-nil ---@diagnostic disable need-check-nil
local table = table
local ipairs = ipairs
local S = minetest.get_translator("mcl_inventory") local S = minetest.get_translator("mcl_inventory")
local F = minetest.formspec_escape local F = minetest.formspec_escape
---@type {id: string, description: string, item_icon: string, build: (fun(player: ObjectRef): string), handle: fun(player: ObjectRef, fields: table), access: (fun(player): boolean), show_inventory: boolean}[] ---@type {id: string, description: string, item_icon: string, build: (fun(player: ObjectRef): string), handle: fun(player: ObjectRef, fields: table), access: (fun(player): boolean), show_inventory: boolean}[]
mcl_inventory.registered_survival_inventory_tabs = {} mcl_inventory.registered_survival_inventory_tabs = {}
---@param def {id: string, description: string, item_icon: string, build: (fun(player: ObjectRef): string), handle: fun(player: ObjectRef, fields: table), access: (fun(player): boolean), show_inventory: boolean} ---@param def {id: string, description: string, item_icon: string, build: (fun(player: ObjectRef): string), handle: fun(player: ObjectRef, fields: table), access: (fun(player): boolean), show_inventory: boolean}
function mcl_inventory.register_survival_inventory_tab(def) function mcl_inventory.register_survival_inventory_tab(def)
if #mcl_inventory.registered_survival_inventory_tabs == 7 then if #mcl_inventory.registered_survival_inventory_tabs == 7 then
error("Too much tabs registered!") error("Too many tabs registered!")
end end
assert(def.id) assert(def.id)
@ -134,10 +131,10 @@ local main_page_static = table.concat({
--Listring --Listring
"listring[current_player;main]", "listring[current_player;main]",
"listring[current_player;armor]",
"listring[current_player;main]",
"listring[current_player;craft]", "listring[current_player;craft]",
"listring[current_player;main]", "listring[current_player;main]",
"listring[current_player;armor]",
"listring[current_player;main]",
}) })
mcl_inventory.register_survival_inventory_tab({ mcl_inventory.register_survival_inventory_tab({
@ -204,13 +201,14 @@ function mcl_inventory.build_survival_formspec(player)
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
local player_name = player:get_player_name()
if formname == "" and #mcl_inventory.registered_survival_inventory_tabs ~= 1 and if formname == "" and #mcl_inventory.registered_survival_inventory_tabs ~= 1 and
mcl_gamemode.get_gamemode(player) == "survival" then not minetest.is_creative_enabled(player_name) then
for _, d in ipairs(mcl_inventory.registered_survival_inventory_tabs) do for _, d in ipairs(mcl_inventory.registered_survival_inventory_tabs) do
if fields["tab_" .. d.id] and d.access(player) then if fields["tab_" .. d.id] and d.access(player) then
player_current_tab[player] = d.id player_current_tab[player] = d.id
mcl_inventory.update_inventory(player) mcl_inventory.update_inventory(player)
return break
end end
end end