From 62dcdd85f45385a7fb1a85a7d15a190e4114ba7d Mon Sep 17 00:00:00 2001 From: cheapie Date: Mon, 3 Apr 2017 02:09:51 -0400 Subject: allow sneak+dig to remove the protector (#2) without removing the area protection it created. In both cases, sneak+dig incurs a deliberate cost, in an effort to prevent a griefer using one protector to go around protecting stuff at random. * In creative mode, one protector is deducted from the player's inventory. * In survival mode, the protector is not returned, and is replaced with 6 steel ingots, unless there's no room for the ingots, then it just behaves as if sneak was not pressed and throws a warning. --- init.lua | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index 653f981..370e975 100644 --- a/init.lua +++ b/init.lua @@ -1,3 +1,6 @@ + +local creative_mode = minetest.setting_getbool("creative_mode") + local function cyan(str) return minetest.colorize("#00FFFF",str) end @@ -54,13 +57,31 @@ minetest.register_node("areasprotector:protector",{ end return itemstack end, - on_destruct = function(pos) - local meta = minetest.get_meta(pos) - local owner = meta:get_string("owner") - local id = meta:get_int("area_id") - if areas.areas[id] and areas:isAreaOwner(id,owner) then - areas:remove(id) - areas:save() + after_dig_node = function(pos, oldnode, oldmetadata, digger) + if oldmetadata and oldmetadata.fields then + local owner = oldmetadata.fields.owner + local id = tonumber(oldmetadata.fields.area_id) + local playername = digger:get_player_name() + if areas.areas[id] and areas:isAreaOwner(id,owner) then + if digger:get_player_control().sneak then + local inv = digger:get_inventory() + if not creative_mode then + if inv:room_for_item("main", "default:steel_ingot 6") then + inv:remove_item("main", "areasprotector:protector 1") + inv:add_item("main", "default:steel_ingot 6") + else + minetest.chat_send_player(playername, "No room for the replacement ingots, just digging the protector and deleting the area normally.") + areas:remove(id) + areas:save() + end + else + inv:remove_item("main", "areasprotector:protector 1") + end + else + areas:remove(id) + areas:save() + end + end end end, on_punch = function(pos, node, puncher) -- cgit v1.2.3