From aa4d5738c73957a3943edd28fb10b33c758df565 Mon Sep 17 00:00:00 2001 From: James David Clarke Date: Sat, 20 Jan 2024 00:42:56 +0000 Subject: [PATCH] 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 Co-authored-by: James David Clarke Co-committed-by: James David Clarke --- mods/HUD/mcl_inventory/init.lua | 21 ++++++++++++++++++--- mods/HUD/mcl_inventory/survival.lua | 16 +++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index c2555581c..2383295bc 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -3,6 +3,19 @@ mcl_inventory = {} dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.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 armor_change_only? boolean local function set_inventory(player, armor_change_only) @@ -73,12 +86,14 @@ end) ---@param player mt.PlayerObjectRef function mcl_inventory.update_inventory(player) - local player_gamemode = mcl_gamemode.get_gamemode(player) - if player_gamemode == "creative" then + local player_name = player:get_player_name() + local is_gamemode_creative = minetest.is_creative_enabled(player_name) + if is_gamemode_creative then 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)) end + mcl_meshhand.update_player(player) end mcl_gamemode.register_on_gamemode_change(function(player, old_gamemode, new_gamemode) diff --git a/mods/HUD/mcl_inventory/survival.lua b/mods/HUD/mcl_inventory/survival.lua index 5b5c6032c..4922c8f88 100644 --- a/mods/HUD/mcl_inventory/survival.lua +++ b/mods/HUD/mcl_inventory/survival.lua @@ -1,18 +1,15 @@ ---@diagnostic disable need-check-nil - -local table = table -local ipairs = ipairs - local S = minetest.get_translator("mcl_inventory") 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}[] 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} function mcl_inventory.register_survival_inventory_tab(def) if #mcl_inventory.registered_survival_inventory_tabs == 7 then - error("Too much tabs registered!") + error("Too many tabs registered!") end assert(def.id) @@ -134,10 +131,10 @@ local main_page_static = table.concat({ --Listring "listring[current_player;main]", - "listring[current_player;armor]", - "listring[current_player;main]", "listring[current_player;craft]", "listring[current_player;main]", + "listring[current_player;armor]", + "listring[current_player;main]", }) mcl_inventory.register_survival_inventory_tab({ @@ -204,13 +201,14 @@ function mcl_inventory.build_survival_formspec(player) end 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 - 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 if fields["tab_" .. d.id] and d.access(player) then player_current_tab[player] = d.id mcl_inventory.update_inventory(player) - return + break end end