0
0
Fork 0

Fix Survival Tab API, closes #3902

This commit is contained in:
James Clarke 2024-01-15 16:21:28 +00:00 committed by James Clarke
parent 59ad110e6b
commit 4431bc9ac0
2 changed files with 22 additions and 18 deletions

View File

@ -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. This is likely an error in a non-mineclonia mod.")
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,15 @@ end)
---@param player mt.PlayerObjectRef
function mcl_inventory.update_inventory(player)
local player_gamemode = mcl_gamemode.get_gamemode(player)
local player_gamemode = player:get_meta():get_string("gamemode")
if player_gamemode == "" then player_gamemode = "survival" end
if player_gamemode == "creative" then
mcl_inventory.set_creative_formspec(player)
elseif player_gamemode == "survival" 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)

View File

@ -1,18 +1,11 @@
---@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)
@ -48,10 +41,6 @@ minetest.register_on_leaveplayer(function(player, timed_out)
player_current_tab[player] = nil
end)
---@param player ObjectRef
---@param content string
---@param inventory boolean
---@param tabname string
local function build_page(player, content, inventory, tabname)
local tab_buttons = "style_type[image;noclip=true]"
@ -134,10 +123,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({
@ -180,7 +169,6 @@ mcl_inventory.register_survival_inventory_tab({
end,
})]]
---@param player ObjectRef
function mcl_inventory.build_survival_formspec(player)
local inv = player:get_inventory()
@ -205,12 +193,12 @@ end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "" and #mcl_inventory.registered_survival_inventory_tabs ~= 1 and
mcl_gamemode.get_gamemode(player) == "survival" then
player:get_meta():get_string("gamemode") ~= "creative" 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