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

Commit

Permalink
Drop items of digged chests
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX authored and BlockMen committed Nov 25, 2015
1 parent 9e9e9fc commit fdd34cb
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions mods/default/nodes/chests.lua
Expand Up @@ -31,6 +31,24 @@ end

-- Helper functions

local function drop_chest_stuff()
return function(pos, oldnode, oldmetadata, digger)
local meta = minetest.get_meta(pos)
meta:from_table(oldmetadata)
local inv = meta:get_inventory()
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
local p = {
x = pos.x + math.random(0, 5)/5 - 0.5,
y = pos.y,
z = pos.z + math.random(0, 5)/5 - 0.5}
minetest.add_item(p, stack)
end
end
end
end

local function has_locked_chest_privilege(meta, player)
if player:get_player_name() ~= meta:get_string("owner") then
return false
Expand All @@ -51,17 +69,13 @@ minetest.register_node("default:chest", {
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),

after_dig_node = drop_chest_stuff(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", chest_formspec)
meta:set_string("infotext", "Chest")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
inv:set_size("main", 8 * 4)
end,
on_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
Expand All @@ -88,6 +102,7 @@ minetest.register_node("default:chest_locked", {
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),

after_dig_node = drop_chest_stuff(),
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
Expand All @@ -103,8 +118,7 @@ minetest.register_node("default:chest_locked", {
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
return has_locked_chest_privilege(meta, player)
end,
allow_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
Expand Down

0 comments on commit fdd34cb

Please sign in to comment.