uniham/system/use_hammer.lua

47 lines
1.6 KiB
Lua

-- Localize
local replacements = uniham.replacements
-- Hammer use function
--
-- The function is called when using the hammer. It either converts the node to
-- its replacement or does nothing. When converting the modified (added wear)
-- itemstack is returned.
--
-- @param itemstack The `itemstack` as per on_use definition
-- @param player The `player` as per on_use definition
-- @param pointed_thing The `pointed_thing` as per on_use definition
-- @see <https://dev.minetest.net/on_use#on_use>
-- @return itemstack|void
uniham.use_hammer = function (itemstack, player, pointed_thing)
if pointed_thing.type == 'nothing' then return end
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
local node_name = minetest.get_node(node_pos).name
local node_sounds = minetest.registered_nodes[node_name].sounds
if not replacements[node_name] then
minetest.sound_play(node_sounds.dug.name, {
pos = node_pos,
max_hear_distance = 8,
gain = node_sounds.dug.gain,
})
return
end
local replacement = replacements[node_name]
local replacement_sounds = minetest.registered_nodes[replacement].sounds
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
minetest.sound_play(replacement_sounds.dug.name, {
pos = node_pos,
max_hear_distance = 8,
gain = replacement_sounds.dug.gain,
})
minetest.set_node(node_pos, { name = replacement })
minetest.check_for_falling(node_pos)
itemstack:add_wear(math.ceil(65535 / uses) + 1)
return itemstack
end