Skip to content
This repository was archived by the owner on Dec 14, 2019. It is now read-only.

Commit fdd34cb

Browse files
lnjXBlockMen
authored andcommitted
Drop items of digged chests
1 parent 9e9e9fc commit fdd34cb

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

mods/default/nodes/chests.lua

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ end
3131

3232
-- Helper functions
3333

34+
local function drop_chest_stuff()
35+
return function(pos, oldnode, oldmetadata, digger)
36+
local meta = minetest.get_meta(pos)
37+
meta:from_table(oldmetadata)
38+
local inv = meta:get_inventory()
39+
for i = 1, inv:get_size("main") do
40+
local stack = inv:get_stack("main", i)
41+
if not stack:is_empty() then
42+
local p = {
43+
x = pos.x + math.random(0, 5)/5 - 0.5,
44+
y = pos.y,
45+
z = pos.z + math.random(0, 5)/5 - 0.5}
46+
minetest.add_item(p, stack)
47+
end
48+
end
49+
end
50+
end
51+
3452
local function has_locked_chest_privilege(meta, player)
3553
if player:get_player_name() ~= meta:get_string("owner") then
3654
return false
@@ -51,17 +69,13 @@ minetest.register_node("default:chest", {
5169
is_ground_content = false,
5270
sounds = default.node_sound_wood_defaults(),
5371

72+
after_dig_node = drop_chest_stuff(),
5473
on_construct = function(pos)
5574
local meta = minetest.get_meta(pos)
5675
meta:set_string("formspec", chest_formspec)
5776
meta:set_string("infotext", "Chest")
5877
local inv = meta:get_inventory()
59-
inv:set_size("main", 8*4)
60-
end,
61-
can_dig = function(pos,player)
62-
local meta = minetest.get_meta(pos);
63-
local inv = meta:get_inventory()
64-
return inv:is_empty("main")
78+
inv:set_size("main", 8 * 4)
6579
end,
6680
on_metadata_inventory_move = function(pos, from_list, from_index,
6781
to_list, to_index, count, player)
@@ -88,6 +102,7 @@ minetest.register_node("default:chest_locked", {
88102
is_ground_content = false,
89103
sounds = default.node_sound_wood_defaults(),
90104

105+
after_dig_node = drop_chest_stuff(),
91106
after_place_node = function(pos, placer)
92107
local meta = minetest.get_meta(pos)
93108
meta:set_string("owner", placer:get_player_name() or "")
@@ -103,8 +118,7 @@ minetest.register_node("default:chest_locked", {
103118
end,
104119
can_dig = function(pos,player)
105120
local meta = minetest.get_meta(pos);
106-
local inv = meta:get_inventory()
107-
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
121+
return has_locked_chest_privilege(meta, player)
108122
end,
109123
allow_metadata_inventory_move = function(pos, from_list, from_index,
110124
to_list, to_index, count, player)

0 commit comments

Comments
 (0)