From c7333c0b8260b0b7aca2aa776cdf78cfaab7453e Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 17 Jan 2017 18:30:35 +0100 Subject: [PATCH] Make hopper drop contents after digging --- mods/hopper/init.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/mods/hopper/init.lua b/mods/hopper/init.lua index 666688083f..06fbb36ed7 100644 --- a/mods/hopper/init.lua +++ b/mods/hopper/init.lua @@ -42,10 +42,19 @@ minetest.register_node("hopper:hopper", { local inv = meta:get_inventory() inv:set_size("main", 5) end, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); + after_dig_node = function(pos, oldnode, oldmetadata, digger) + local meta = minetest.get_meta(pos) + local meta2 = meta + meta:from_table(oldmetadata) local inv = meta:get_inventory() - return inv:is_empty("main") + 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, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5} + minetest.add_item(p, stack) + end + end + meta:from_table(meta2:to_table()) end, on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) minetest.log("action", player:get_player_name().. @@ -93,10 +102,19 @@ minetest.register_node("hopper:hopper_side", { local inv = meta:get_inventory() inv:set_size("main", 5) end, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); + after_dig_node = function(pos, oldnode, oldmetadata, digger) + local meta = minetest.get_meta(pos) + local meta2 = meta + meta:from_table(oldmetadata) local inv = meta:get_inventory() - return inv:is_empty("main") + 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, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5} + minetest.add_item(p, stack) + end + end + meta:from_table(meta2:to_table()) end, on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) minetest.log("action", player:get_player_name()..