82 lines
2.9 KiB
Lua
Raw Normal View History

2023-12-14 17:50:02 +01:00
minetest.register_node("chest_recovery:chest", {
2023-12-14 21:49:40 +01:00
description = "Coffre de récupération" .. "\n" .. "32 emplacements",
2023-12-14 17:50:02 +01:00
tiles = {"chest_chest.png^[sheet:2x2:0,0", "chest_chest.png^[sheet:2x2:0,0",
"chest_chest.png^[sheet:2x2:1,0", "chest_chest.png^[sheet:2x2:1,0",
"chest_chest.png^[sheet:2x2:1,0", "chest_chest.png^[sheet:2x2:0,1"},
paramtype2 = "facedir",
groups = {dig_immediate = 2, choppy = 3, meta_is_privatizable = 1},
is_ground_content = false,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec",
2023-12-14 21:49:40 +01:00
"size[9,10]"..
"list[current_name;main;0,0;9,4;]"..
"list[current_name;armor;0,4;9,1;]"..
"list[current_player;armor;0,5;9,1;]"..
"list[current_player;main;0,6;9,4;]"..
2023-12-14 17:50:02 +01:00
"listring[]")
2023-12-14 21:49:40 +01:00
meta:set_string("infotext", "Coffre")
2023-12-14 17:50:02 +01:00
local inv = meta:get_inventory()
2023-12-14 21:49:40 +01:00
inv:set_size("main", 9 * 4)
inv:set_size("armor", 9)
2023-12-14 17:50:02 +01:00
end,
2023-12-14 21:49:40 +01:00
on_destruct = function(pos)
2023-12-14 17:50:02 +01:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-12-14 21:49:40 +01:00
local drop_items = {}
for _, listname in ipairs({"main", "armor", "craft"}) do
for i = 1, inv:get_size(listname) do
local stack = inv:get_stack(listname, i)
if not stack:is_empty() then
table.insert(drop_items, stack:to_table())
end
end
2023-12-14 17:50:02 +01:00
end
2023-12-14 21:49:40 +01:00
-- Lâcher tous les objets au-dessus du nœud du coffre
for _, item in ipairs(drop_items) do
minetest.add_item(pos, ItemStack(item))
2023-12-14 17:50:02 +01:00
end
end,
2023-12-14 21:49:40 +01:00
-- (le reste du code reste inchangé)
2023-12-14 17:50:02 +01:00
})
2023-12-14 21:49:40 +01:00
-- Reste du code...
2023-12-14 17:50:02 +01:00
minetest.register_on_dieplayer(function(player)
local player_inv = player:get_inventory()
local pos = player:get_pos()
2023-12-14 21:49:40 +01:00
pos.y = pos.y
2023-12-14 17:50:02 +01:00
minetest.set_node(pos, {name = "chest_recovery:chest"})
local chest_meta = minetest.get_meta(pos)
local chest_inv = chest_meta:get_inventory()
local is_empty = true
2023-12-14 21:49:40 +01:00
for _, listname in ipairs({"main", "armor", "craft"}) do
for i = 1, player_inv:get_size(listname) do
local stack = player_inv:get_stack(listname, i)
chest_inv:set_stack(listname, i, stack)
player_inv:set_stack(listname, i, ItemStack(nil))
if not stack:is_empty() then
is_empty = false
end
2023-12-14 17:50:02 +01:00
end
end
if is_empty then
minetest.remove_node(pos)
else
2023-12-14 21:49:40 +01:00
local chest_formspec = "size[9,10]" ..
"list[current_name;main;0,0;9,4;]" ..
"list[current_name;armor;0,4;9,1;]" ..
"list[current_player;armor;0,5;9,1;]" ..
"list[current_player;main;0,6;9,4;]" ..
2023-12-14 17:50:02 +01:00
"listring[]"
chest_meta:set_string("formspec", chest_formspec)
end
end)