mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-25 12:01:06 +01:00
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:
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()) .. "/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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue