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,22 +78,25 @@ 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.groups.bagslots then if def_stack then
local list_name = "bag" .. i .. "contents" local def = def_stack:get_definition()
local size = inv:get_size(list_name) if def.groups.bagslots then
local used = 0 local list_name = "bag" .. i .. "contents"
for si = 1, size do local size = inv:get_size(list_name)
local stk = inv:get_stack(list_name, si) local used = 0
if not stk:is_empty() then for si = 1, size do
used = used + 1 local stk = inv:get_stack(list_name, si)
if not stk:is_empty() then
used = used + 1
end
end end
local img = def.inventory_image
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
formspec[n] = string.format("image_button[%f,0.4;1,1;%s;bag%i;%s]",
(i + 1.35)*1.25, img, i, label)
n = n + 1
end end
local img = def.inventory_image
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
formspec[n] = string.format("image_button[%f,0.4;1,1;%s;bag%i;%s]",
(i + 1.35)*1.25, img, i, label)
n = n + 1
end end
end end
return { formspec = table.concat(formspec) } return { formspec = table.concat(formspec) }
@ -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)