From 2c442710d4c5615d00cef0337dd43c1bdc0263f3 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Mon, 18 Jun 2018 12:59:08 +0300 Subject: [PATCH] Prevent digging when there's items inside --- elepower_machines/bases/storage.lua | 11 ++++++++++- elepower_papi/machine.lua | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/elepower_machines/bases/storage.lua b/elepower_machines/bases/storage.lua index 1c28b01..51aeb6e 100644 --- a/elepower_machines/bases/storage.lua +++ b/elepower_machines/bases/storage.lua @@ -1,4 +1,10 @@ +local function can_dig(pos, player) + local meta = minetest.get_meta(pos); + local inv = meta:get_inventory() + return inv:is_empty("in") and inv:is_empty("out") +end + local function item_in_group(stack, grp) return minetest.get_item_group(stack:get_name(), grp) > 0 end @@ -14,6 +20,8 @@ function elepm.register_storage(nodename, nodedef) nodedef.groups["ele_storage"] = 1 nodedef.groups["ele_provider"] = 1 + nodedef.can_dig = can_dig + nodedef.on_timer = function (pos, elapsed) local meta = minetest.get_meta(pos) local refresh = false @@ -27,7 +35,8 @@ function elepm.register_storage(nodename, nodedef) ele.helpers.swap_node(pos, nodename .. "_" .. level) meta:set_string("formspec", ele.formspec.get_storage_formspec(rounded)) - meta:set_string("infotext", ("%s Active"):format(nodedef.description) .. "\n" .. ele.capacity_text(capacity, storage)) + meta:set_string("infotext", ("%s Active"):format(nodedef.description) .. "\n" .. + ele.capacity_text(capacity, storage)) local inv = meta:get_inventory() diff --git a/elepower_papi/machine.lua b/elepower_papi/machine.lua index 808a548..0247379 100644 --- a/elepower_papi/machine.lua +++ b/elepower_papi/machine.lua @@ -39,6 +39,12 @@ Intended use: to set textures or light output. ]] +local function can_dig(pos, player) + local meta = minetest.get_meta(pos); + local inv = meta:get_inventory() + return inv:is_empty("dst") and inv:is_empty("src") +end + local function allow_metadata_inventory_put(pos, listname, index, stack, player) if minetest.is_protected(pos, player:get_player_name()) then return 0 @@ -143,6 +149,11 @@ function ele.register_base_device(nodename, nodedef) nodedef.preserve_metadata = preserve_metadata nodedef.after_place_node = retrieve_metadata + -- Prevent digging when there's items inside + if not nodedef.can_dig then + nodedef.can_dig = can_dig + end + -- Finally, register the damn thing already minetest.register_node(nodename, nodedef)