nodecore-cd2025/mods/nc_api/item_punch_sounds.lua

34 lines
1.1 KiB
Lua
Raw Normal View History

2019-03-14 21:48:29 -04:00
-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, pairs
= minetest, nodecore, pairs
-- LUALOCALS > ---------------------------------------------------------
function nodecore.node_sound(pos, kind)
if nodecore.stack_sounds(pos, kind) then return end
2019-03-14 21:48:29 -04:00
local node = minetest.get_node(pos)
local def = minetest.registered_items[node.name] or {}
if (not def.sounds) or (not def.sounds[kind]) then return end
local t = {}
for k, v in pairs(def.sounds[kind]) do t[k] = v end
t.pos = pos
return minetest.sound_play(t.name, t)
end
2019-03-14 22:04:44 -04:00
local lasthit = {}
2019-03-14 21:48:29 -04:00
minetest.register_on_punchnode(function(pos, node, puncher, pointed)
if not puncher then return end
2019-03-14 22:04:44 -04:00
local pname = puncher:get_player_name()
local now = minetest.get_us_time() / 1000000
local last = lasthit[pname] or 0
if now - last < 0.25 then return end
local def = minetest.registered_items[node.name] or {}
2019-03-14 21:48:29 -04:00
local wield = puncher:get_wielded_item()
if (not def.groups) or (not nodecore.toolspeed(wield, def.groups))
or not(def.sounds) then
2019-03-14 21:48:29 -04:00
nodecore.node_sound(pos, "dig")
2019-03-14 22:04:44 -04:00
lasthit[pname] = now
2019-03-14 21:48:29 -04:00
end
end)