mirror of
https://notabug.org/TenPlus1/protector.git
synced 2025-01-08 11:27:29 +01:00
tweak chest functions
This commit is contained in:
parent
60b9f5846f
commit
be364009c2
@ -544,7 +544,7 @@ minetest.register_node("protector:chest", {
|
||||
local inv = meta:get_inventory()
|
||||
|
||||
meta:set_string("infotext", S("Protected Chest"))
|
||||
meta:set_string("name", "")
|
||||
meta:set_string("name", S("Protected Chest"))
|
||||
inv:set_size("main", 8 * 4)
|
||||
end,
|
||||
|
||||
@ -575,7 +575,8 @@ minetest.register_node("protector:chest", {
|
||||
minetest.pos_to_string(pos))
|
||||
end,
|
||||
|
||||
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
||||
on_metadata_inventory_move = function(
|
||||
pos, from_list, from_index, to_list, to_index, count, player)
|
||||
|
||||
minetest.log("action", player:get_player_name() ..
|
||||
" moves stuff inside protected chest at " ..
|
||||
@ -600,7 +601,8 @@ minetest.register_node("protector:chest", {
|
||||
return stack:get_count()
|
||||
end,
|
||||
|
||||
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
||||
allow_metadata_inventory_move = function(
|
||||
pos, from_list, from_index, to_list, to_index, count, player)
|
||||
|
||||
if minetest.is_protected(pos, player:get_player_name()) then
|
||||
return 0
|
||||
@ -645,15 +647,39 @@ minetest.register_node("protector:chest", {
|
||||
on_blast = function() end,
|
||||
})
|
||||
|
||||
-- Container transfer helper
|
||||
local to_from = function(src, dst)
|
||||
|
||||
local stack, item, leftover
|
||||
local size = dst:get_size("main")
|
||||
|
||||
for i = 1, size do
|
||||
|
||||
stack = src:get_stack("main", i)
|
||||
item = stack:get_name()
|
||||
|
||||
if item ~= "" and dst:room_for_item("main", item) then
|
||||
|
||||
leftover = dst:add_item("main", stack)
|
||||
|
||||
if leftover and not leftover:is_empty() then
|
||||
src:set_stack("main", i, leftover)
|
||||
else
|
||||
src:set_stack("main", i, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Protected Chest formspec buttons
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
|
||||
if string.sub(formname, 0, string.len("protector:chest_")) ~= "protector:chest_" then
|
||||
if string.sub(formname, 0, 16) ~= "protector:chest_" then
|
||||
return
|
||||
end
|
||||
|
||||
local pos_s = string.sub(formname,string.len("protector:chest_") + 1)
|
||||
local pos_s = string.sub(formname, 17)
|
||||
local pos = minetest.string_to_pos(pos_s)
|
||||
|
||||
if minetest.is_protected(pos, player:get_player_name()) then
|
||||
@ -663,43 +689,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local meta = minetest.get_meta(pos) ; if not meta then return end
|
||||
local chest_inv = meta:get_inventory() ; if not chest_inv then return end
|
||||
local player_inv = player:get_inventory()
|
||||
local leftover
|
||||
|
||||
-- copy contents of player inventory to chest
|
||||
if fields.toup then
|
||||
|
||||
-- copy contents of players inventory to chest
|
||||
for i, v in ipairs(player_inv:get_list("main") or {}) do
|
||||
|
||||
if chest_inv:room_for_item("main", v) then
|
||||
|
||||
leftover = chest_inv:add_item("main", v)
|
||||
|
||||
player_inv:remove_item("main", v)
|
||||
|
||||
if leftover
|
||||
and not leftover:is_empty() then
|
||||
player_inv:add_item("main", v)
|
||||
end
|
||||
end
|
||||
end
|
||||
to_from(player_inv, chest_inv)
|
||||
|
||||
-- copy contents of chest to player inventory
|
||||
elseif fields.todn then
|
||||
|
||||
-- copy contents of chest to players inventory
|
||||
for i, v in ipairs(chest_inv:get_list("main") or {}) do
|
||||
|
||||
if player_inv:room_for_item("main", v) then
|
||||
|
||||
leftover = player_inv:add_item("main", v)
|
||||
|
||||
chest_inv:remove_item("main", v)
|
||||
|
||||
if leftover
|
||||
and not leftover:is_empty() then
|
||||
chest_inv:add_item("main", v)
|
||||
end
|
||||
end
|
||||
end
|
||||
to_from(chest_inv, player_inv)
|
||||
|
||||
elseif fields.chestname then
|
||||
|
||||
@ -707,9 +706,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if fields.chestname ~= "" then
|
||||
|
||||
meta:set_string("name", fields.chestname)
|
||||
meta:set_string("infotext",
|
||||
S("Protected Chest (@1)", fields.chestname))
|
||||
meta:set_string("infotext", fields.chestname)
|
||||
else
|
||||
meta:set_string("name", S("Protected Chest"))
|
||||
meta:set_string("infotext", S("Protected Chest"))
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user