nodecore-cd2025/mods/nc_api/item_touch_hurt.lua
Aaron Suen e9998bddf2 Negate touch damage by using appropriate tool
NodeCore vanilla shouldn't be affected, since things that are
damaging are already not diggable, but for mods, this allows
some nodes to be damaging if punched, but diggable with the right
tool.  Note that they will have to drop something other than
themselves because touch damage will still apply to carrying the
item in inventory (i.e. "hot potato" mechanic).
2020-04-09 07:16:33 -04:00

41 lines
1.3 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local ItemStack, minetest, nodecore
= ItemStack, minetest, nodecore
-- LUALOCALS > ---------------------------------------------------------
local function toolcandig(wield, node)
if (not wield) or wield:is_empty() then return end
local def = node and node.name and minetest.registered_items[node.name]
if not (def and def.groups) then return end
local toolspeed = nodecore.toolspeed(wield, def.groups)
if not toolspeed then return end
local handspeed = nodecore.toolspeed(ItemStack(""), def.groups)
return handspeed and (toolspeed < handspeed)
end
nodecore.register_on_register_item(function(_, def)
local dmg = def.groups and def.groups.damage_touch
if not (dmg and dmg > 0) then return end
def.on_punch = def.on_punch or function(pos, node, puncher, ...)
if puncher and puncher:is_player()
and (not toolcandig(puncher:get_wielded_item(), node)) then
nodecore.addphealth(puncher, -dmg, {
nc_type = "node_touch_hurt",
node = node
})
end
return minetest.node_punch(pos, node, puncher, ...)
end
def.on_scaling = def.on_scaling or function(_, _, player, node)
if player and player:is_player() then
nodecore.addphealth(player, -dmg, {
nc_type = "node_touch_hurt",
node = node
})
end
return true
end
end)