200 lines
7.7 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",
"size[9,11]"..
"background[-0.19,-0.25;9.45,11.75;mcl_formspec_itemslot.png]"..
"list[current_name;main;0,0;9,4;]"..
"list[current_name;armor;0,4;5,1;]"..
"list[current_name;offhand;6,4;10,1;]" ..
"label[0,5.5;-------------------------------------------]"..
"list[current_player;offhand;6,6;9,1;]" ..
"list[current_player;armor;0,6;9,1;]"..
"list[current_player;main;0,7;9,4;]"..
"listring[]"..
"button[5,5;3,1;transfer;Transférer tout]")
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)
inv:set_size("offhand", 1)
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 = {}
2023-12-14 22:39:46 +01:00
for _, listname in ipairs({"main", "armor", "offhand", "craft"}) do
2023-12-14 21:49:40 +01:00
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 23:52:32 +01:00
on_receive_fields = function(pos, formname, fields, sender)
if fields.transfer then
-- Transférer tout le contenu du coffre vers l'inventaire du joueur
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local player_inv = sender:get_inventory()
-- Transférer l'armure
2023-12-15 00:00:16 +01:00
player_inv:set_stack("armor", 1, ItemStack("chest_recovery:inutile"))
2023-12-14 23:52:32 +01:00
for i = 1, inv:get_size("armor") do
local stack = inv:get_stack("armor", i)
if i > 1 or not stack:is_empty() then
local player_armor_stack = player_inv:get_stack("armor", i)
local leftover = player_inv:add_item("armor", stack)
if not leftover:is_empty() then
-- L'ajout n'a pas réussi, rétablissons l'emplacement d'armure dans le coffre
inv:set_stack("armor", i, stack)
else
-- L'ajout a réussi, vidons l'emplacement d'armure dans le coffre
inv:set_stack("armor", i, ItemStack(nil))
end
end
end
2023-12-15 00:00:16 +01:00
2023-12-14 23:52:32 +01:00
-- Transférer l'offhand
local offhand_stack = inv:get_stack("offhand", 1)
local leftover_offhand = player_inv:add_item("offhand", offhand_stack)
inv:set_stack("offhand", 1, leftover_offhand)
-- Transférer le reste de l'inventaire principal
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
local leftover = player_inv:add_item("main", stack)
inv:set_stack("main", i, leftover)
end
-- Vérifier si le coffre est vide, puis le supprimer
if inv:is_empty("main") and inv:is_empty("armor") and inv:is_empty("offhand") then
minetest.remove_node(pos)
end
2023-12-14 23:52:32 +01:00
end
end,
2023-12-14 17:50:02 +01:00
})
2023-12-14 23:52:32 +01:00
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 22:39:46 +01:00
for _, listname in ipairs({"main", "armor", "offhand", "craft"}) do
2023-12-14 21:49:40 +01:00
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 23:52:32 +01:00
local chest_formspec = "size[9,11]"..
"background[-0.19,-0.25;9.45,11.75;mcl_formspec_itemslot.png]"..
2023-12-14 22:39:46 +01:00
"list[current_name;main;0,0;9,4;]"..
"list[current_name;armor;0,4;5,1;]"..
"list[current_name;offhand;6,4;10,1;]" ..
2023-12-14 23:52:32 +01:00
"label[0,5.5;-------------------------------------------]"..
"list[current_player;offhand;6,6;9,1;]" ..
"list[current_player;armor;0,6;9,1;]"..
"list[current_player;main;0,7;9,4;]"..
"listring[]"..
"button[5,5;3,1;transfer;Transférer tout]"
2023-12-14 17:50:02 +01:00
chest_meta:set_string("formspec", chest_formspec)
end
end)
2023-12-15 00:00:16 +01:00
2023-12-15 00:04:41 +01:00
minetest.register_craftitem("chest_recovery:inutile", {
2023-12-15 00:00:16 +01:00
description = "Item Inutile",
2023-12-15 00:05:48 +01:00
--inventory_image = "chest_recovery_inutile.png", -- Remplacez cela par le chemin de votre texture
2023-12-15 00:04:41 +01:00
groups = {not_in_creative_inventory = 1},
2023-12-15 00:00:16 +01:00
on_use = function(itemstack, user, pointed_thing)
2023-12-15 00:04:41 +01:00
itemstack:clear()
2023-12-15 00:00:16 +01:00
return itemstack
end,
})
2023-12-15 00:01:43 +01:00
-- Define compass_frames as a global variable
compass_frames = 32
minetest.register_on_respawnplayer(function(player)
-- Generate a new random frame for the recovery compass
local random_frame = math.random(0, compass_frames - 1)
-- Create the recovery compass item
local recovery_compass = ItemStack("mcl_compass:" .. random_frame .. "_recovery")
local player_inv = player:get_inventory()
if player_inv then
player_inv:add_item("main", recovery_compass)
end
local pos = player:get_pos()
local chest_pos = minetest.find_node_near(pos, 5, "chest_recovery:chest")
if chest_pos then
local chest_meta = minetest.get_meta(chest_pos)
local chest_inv = chest_meta:get_inventory()
local is_empty = true
for _, listname in ipairs({"main", "armor", "offhand", "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
end
end
if is_empty then
minetest.remove_node(chest_pos)
else
local chest_formspec = "size[9,11]" ..
"background[-0.19,-0.25;9.45,11.75;mcl_formspec_itemslot.png]" ..
"list[current_name;main;0,0;9,4;]" ..
"list[current_name;armor;0,4;5,1;]" ..
"list[current_name;offhand;6,4;10,1;]" ..
"label[0,5.5;-------------------------------------------]" ..
"list[current_player;offhand;6,6;9,1;]" ..
"list[current_player;armor;0,6;9,1;]" ..
"list[current_player;main;0,7;9,4;]" ..
"listring[]" ..
"button[5,5;3,1;transfer;Transférer tout]"
chest_meta:set_string("formspec", chest_formspec)
end
end
end)
2023-12-15 00:04:41 +01:00