From 390e03898a8c259251dbd41d115527081dc8ea78 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Thu, 25 Aug 2022 16:20:44 +0200 Subject: [PATCH] Containers drop stuff when mined --- mods/rp_default/container.lua | 15 ++++---------- mods/rp_default/furnace.lua | 37 ++++++++++++----------------------- mods/rp_item_drop/init.lua | 32 ++++++++++++++++++++++++++++++ mods/rp_jewels/init.lua | 7 ++----- mods/rp_jewels/mod.conf | 2 +- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/mods/rp_default/container.lua b/mods/rp_default/container.lua index c6a14ee..d6de0e0 100644 --- a/mods/rp_default/container.lua +++ b/mods/rp_default/container.lua @@ -42,13 +42,8 @@ minetest.register_node( inv:set_size("main", 8 * 4) end, - -- Unlike other inventory nodes in this game, chests are NOT subject to protection. - -- This is done to allow something like "public chests" in protected areas. - -- To protect their belongings, players are supposed locked chests instead. - can_dig = function(pos, player) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - return inv:is_empty("main") + on_destruct = function(pos) + item_drop.drop_items_from_container(pos, {"main"}) end, write_name = function(pos, text) --[[ TODO: Bring back container naming @@ -95,10 +90,8 @@ minetest.register_node( allow_metadata_inventory_move = protection_check_move, allow_metadata_inventory_put = protection_check_put_take, allow_metadata_inventory_take = protection_check_put_take, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); - local inv = meta:get_inventory() - return inv:is_empty("main") + on_destruct = function(pos) + item_drop.drop_items_from_container(pos, {"main"}) end, write_name = function(pos, text) --[[ TODO: Bring back container naming diff --git a/mods/rp_default/furnace.lua b/mods/rp_default/furnace.lua index ca648a3..ffc6d3d 100644 --- a/mods/rp_default/furnace.lua +++ b/mods/rp_default/furnace.lua @@ -61,6 +61,15 @@ form_furnace = form_furnace .. "image[3.25,1.75;1,1;ui_arrow_bg.png^[transformR2 rp_formspec.register_page("rp_default:furnace_inactive", form_furnace) +local after_dig_node = function(pos, nodenode, oldmetadata, digger) + item_drop.drop_items_from_container_meta_table(pos, {"fuel", "src", "dst"}, oldmetadata) +end + +local on_blast = function(pos) + item_drop.drop_items_from_container(pos, {"fuel", "src", "dst"}) + minetest.remove_node(pos) +end + local check_put = function(pos, listname, index, stack, player) if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then @@ -123,18 +132,8 @@ minetest.register_node( allow_metadata_inventory_move = check_move, allow_metadata_inventory_put = check_put, allow_metadata_inventory_take = check_take, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty("fuel") then - return false - elseif not inv:is_empty("dst") then - return false - elseif not inv:is_empty("src") then - return false - end - return true - end, + after_dig_node = after_dig_node, + on_blast = on_blast, }) minetest.register_node( @@ -163,18 +162,8 @@ minetest.register_node( allow_metadata_inventory_move = check_move, allow_metadata_inventory_put = check_put, allow_metadata_inventory_take = check_take, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty("fuel") then - return false - elseif not inv:is_empty("dst") then - return false - elseif not inv:is_empty("src") then - return false - end - return true - end, + after_dig_node = after_dig_node, + on_blast = on_blast, }) local function swap_node(pos, name) diff --git a/mods/rp_item_drop/init.lua b/mods/rp_item_drop/init.lua index 6edacc2..0912b0b 100644 --- a/mods/rp_item_drop/init.lua +++ b/mods/rp_item_drop/init.lua @@ -35,6 +35,38 @@ function item_drop.drop_item(pos, itemstack) end end +function item_drop.drop_items_from_container(pos, invlists) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + for l=1, #invlists do + local list = invlists[l] + for i=1, inv:get_size(list) do + local item = inv:get_stack(list, i) + if not item:is_empty() then + item_drop.drop_item(pos, item) + end + end + end +end + +function item_drop.drop_items_from_container_meta_table(pos, invlists, meta_table) + if not meta_table or not meta_table.inventory then + return + end + for l=1, #invlists do + local list = invlists[l] + local list_table = meta_table.inventory[list] + if list_table then + for i=1, #list_table do + local item = ItemStack(list_table[i]) + if not item:is_empty() then + item_drop.drop_item(pos, item) + end + end + end + end +end + -- Overwrite Minetest's item_drop function minetest.item_drop = function(itemstack, dropper, pos) local dropper_is_player = dropper and dropper:is_player() diff --git a/mods/rp_jewels/init.lua b/mods/rp_jewels/init.lua index 8c13226..a87998c 100644 --- a/mods/rp_jewels/init.lua +++ b/mods/rp_jewels/init.lua @@ -305,11 +305,8 @@ minetest.register_node( local inv = meta:get_inventory() inv:set_size("main", 1) end, - can_dig = function(pos, player) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - - return inv:is_empty("main") + on_destruct = function(pos) + item_drop.drop_items_from_container(pos, {"main"}) end, allow_metadata_inventory_move = check_move, allow_metadata_inventory_put = check_put, diff --git a/mods/rp_jewels/mod.conf b/mods/rp_jewels/mod.conf index 8c7afe9..2565560 100644 --- a/mods/rp_jewels/mod.conf +++ b/mods/rp_jewels/mod.conf @@ -1,3 +1,3 @@ name = rp_jewels -depends = rp_sounds, rp_default, rp_formspec, rp_crafting, rp_achievements +depends = rp_sounds, rp_default, rp_formspec, rp_crafting, rp_achievements, rp_item_drop optional_depends = tt