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)
return minetest.get_inventory({
local inventory = minetest.get_inventory({
type = "detached",
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
for bag_i = 1, 4 do
ui.register_page("bag" .. bag_i, {
get_formspec = function(player)
local stack = get_player_bag_stack(player, bag_i)
if not stack then return end
local image = stack:get_definition().inventory_image
local slots = stack:get_definition().groups.bagslots
@ -75,7 +78,9 @@ for bag_i = 1, 4 do
end
local inv = player:get_inventory()
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
local list_name = "bag" .. i .. "contents"
local size = inv:get_size(list_name)
@ -93,6 +98,7 @@ for bag_i = 1, 4 do
n = n + 1
end
end
end
return { formspec = table.concat(formspec) }
end,
})
@ -105,7 +111,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
for i = 1, 4 do
if fields["bag" .. i] then
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
end
ui.set_inventory_formspec(player, "bag" .. i)