Properly handle starting page again

master
Wuzzy 2022-08-12 21:02:33 +02:00
parent f6a66c36a0
commit 4f9725fe46
3 changed files with 33 additions and 6 deletions

View File

@ -4,6 +4,8 @@ local S = minetest.get_translator("rp_crafting")
local MODE_CRAFTABLE = 1 -- crafting guide mode, show all recipes (default)
local MODE_GUIDE = 2 -- craftable mode, only show recipes craftable from input slots
local mod_creative = minetest.get_modpath("rp_creative") ~= nil
--
-- API
--
@ -424,13 +426,16 @@ function crafting.get_formspec(name, select_craft_id)
return form
end
rp_formspec.register_page("rp_crafting:crafting", form)
rp_formspec.register_invpage("rp_crafting:crafting", {
is_startpage = function(pname)
return true
end,
get_formspec = crafting.get_formspec,
_is_startpage = function(pname)
if mod_creative and minetest.is_creative_enabled(pname) then
return false
else
return true
end
end,
})
rp_formspec.register_invtab("rp_crafting:crafting", {

View File

@ -260,6 +260,13 @@ end
rp_formspec.register_page("rp_creative:creative", form)
rp_formspec.register_invpage("rp_creative:creative", {
get_formspec = creative.get_formspec,
_is_startpage = function(pname)
if minetest.is_creative_enabled(pname) then
return true
else
return false
end
end,
})
if minetest.is_creative_enabled("") then
rp_formspec.register_invtab("rp_creative:creative", {

View File

@ -403,14 +403,29 @@ minetest.register_on_joinplayer(
-- Initialize player formspec and set initial invpage
player:set_formspec_prepend(formspec_prepend)
local pname = player:get_player_name()
local first_page
for invpagename,def in pairs(rp_formspec.registered_invpages) do
if def.is_startpage and def.is_startpage(pname) then
if not first_page then
first_page = invpagename
end
-- _is_startpage returns true if this page
-- is a startpage, false otherwise.
-- As this function only makes sense within the context of a game,
-- it is undocumented in API.md.
if def._is_startpage and def._is_startpage(pname) then
rp_formspec.set_current_invpage(player, invpagename)
break
end
end
-- Fallback invpage
if not rp_formspec.get_current_invpage(player) then
rp_formspec.set_current_invpage(player, "rp_formspec:inventory")
if first_page then
-- First invpage of the pairs() above (kinda unpredicable tho)
rp_formspec.set_current_invpage(player, first_page)
else
-- Empty inventory fallback page if no invpages registered
rp_formspec.set_current_invpage(player, "rp_formspec:inventory")
end
end
end)