Make sure the server does not crash when trying to change bags in the inventory while cloaked

This commit is contained in:
Oversword 2024-07-19 03:52:29 +01:00
parent 1b074828a6
commit 028876c240

View File

@ -40,16 +40,19 @@ ui.register_button("bags", {
}) })
local function get_player_bag_stack(player, i) local function get_player_bag_stack(player, i)
return minetest.get_inventory({ local inventory = minetest.get_inventory({
type = "detached", type = "detached",
name = player:get_player_name() .. "_bags" name = player:get_player_name() .. "_bags"
}):get_stack("bag" .. i, 1) })
if not inventory then return nil end
return inventory:get_stack("bag" .. i, 1)
end end
for bag_i = 1, 4 do for bag_i = 1, 4 do
ui.register_page("bag" .. bag_i, { ui.register_page("bag" .. bag_i, {
get_formspec = function(player) get_formspec = function(player)
local stack = get_player_bag_stack(player, bag_i) local stack = get_player_bag_stack(player, bag_i)
if not stack then return end
local image = stack:get_definition().inventory_image local image = stack:get_definition().inventory_image
local slots = stack:get_definition().groups.bagslots local slots = stack:get_definition().groups.bagslots
@ -75,7 +78,9 @@ for bag_i = 1, 4 do
end end
local inv = player:get_inventory() local inv = player:get_inventory()
for i = 1, 4 do for i = 1, 4 do
local def = get_player_bag_stack(player, i):get_definition() local def_stack = get_player_bag_stack(player, i)
if def_stack then
local def = def_stack:get_definition()
if def.groups.bagslots then if def.groups.bagslots then
local list_name = "bag" .. i .. "contents" local list_name = "bag" .. i .. "contents"
local size = inv:get_size(list_name) local size = inv:get_size(list_name)
@ -93,6 +98,7 @@ for bag_i = 1, 4 do
n = n + 1 n = n + 1
end end
end end
end
return { formspec = table.concat(formspec) } return { formspec = table.concat(formspec) }
end, end,
}) })
@ -105,7 +111,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
for i = 1, 4 do for i = 1, 4 do
if fields["bag" .. i] then if fields["bag" .. i] then
local stack = get_player_bag_stack(player, i) local stack = get_player_bag_stack(player, i)
if not stack:get_definition().groups.bagslots then if not stack or not stack:get_definition().groups.bagslots then
return return
end end
ui.set_inventory_formspec(player, "bag" .. i) ui.set_inventory_formspec(player, "bag" .. i)