mirror of
https://git.0x7be.net/dirk/uniham.git
synced 2024-10-04 00:23:11 +02:00
add node sounds checking function
Some nodes do not provide sounds. With implementation of this function and saubsequent checks no sounds are played if not available. Fixes https://gitlab.com/4w/uniham/issues/2
This commit is contained in:
parent
430c5b1a84
commit
7014c9ad5a
@ -13,6 +13,25 @@ local replacements = {
|
|||||||
['default:silver_sandstone'] = 'default:silver_sand',
|
['default:silver_sandstone'] = 'default:silver_sand',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Get a node sound
|
||||||
|
--
|
||||||
|
-- This helper function returns a sound table or nil depending on wheter the
|
||||||
|
-- requested sound type exists in the provided node definition or not.
|
||||||
|
--
|
||||||
|
-- Common sound types are `dug`, footstep`, or `place`. For example: When
|
||||||
|
-- requesting the `dug` sounds of a node definition the function checks if
|
||||||
|
-- the table node_definition.sounds.dug exists and if so it returns that
|
||||||
|
-- table. If the table does not exist `nil` is returned.
|
||||||
|
--
|
||||||
|
-- @param node_definition The desired node’s definition
|
||||||
|
-- @param sound_type Name of a sound
|
||||||
|
local get_sound = function (node_definition, sound_type)
|
||||||
|
if not node_definition then return end
|
||||||
|
if not node_definition.sounds then return end
|
||||||
|
if not node_definition.sounds[sound_type] then return end
|
||||||
|
return node_definition.sounds[sound_type]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Hammer use function
|
-- Hammer use function
|
||||||
--
|
--
|
||||||
@ -29,26 +48,30 @@ uniham.crush_nodes = function (itemstack, player, pointed_thing)
|
|||||||
if pointed_thing.type ~= 'node' then return end
|
if pointed_thing.type ~= 'node' then return end
|
||||||
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
|
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
|
||||||
local node_name = minetest.get_node(node_pos).name
|
local node_name = minetest.get_node(node_pos).name
|
||||||
local node_sounds = minetest.registered_nodes[node_name].sounds
|
local node_sound = get_sound(minetest.registered_nodes[node_name], 'dug')
|
||||||
|
|
||||||
if not replacements[node_name] then
|
if not replacements[node_name] then
|
||||||
minetest.sound_play(node_sounds.dug.name, {
|
if node_sound then
|
||||||
|
minetest.sound_play(node_sound.name, {
|
||||||
pos = node_pos,
|
pos = node_pos,
|
||||||
max_hear_distance = 8,
|
max_hear_distance = 8,
|
||||||
gain = node_sounds.dug.gain,
|
gain = node_sound.gain,
|
||||||
})
|
})
|
||||||
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local replacement = replacements[node_name]
|
local replacement = replacements[node_name]
|
||||||
local replacement_sounds = minetest.registered_nodes[replacement].sounds
|
local rep_sound = get_sound(minetest.registered_nodes[replacement], 'dug')
|
||||||
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
|
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
|
||||||
|
|
||||||
minetest.sound_play(replacement_sounds.dug.name, {
|
if rep_sound then
|
||||||
|
minetest.sound_play(rep_sound.name, {
|
||||||
pos = node_pos,
|
pos = node_pos,
|
||||||
max_hear_distance = 8,
|
max_hear_distance = 8,
|
||||||
gain = replacement_sounds.dug.gain,
|
gain = rep_sound.gain,
|
||||||
})
|
})
|
||||||
|
end
|
||||||
|
|
||||||
minetest.set_node(node_pos, { name = replacement })
|
minetest.set_node(node_pos, { name = replacement })
|
||||||
minetest.check_for_falling(node_pos)
|
minetest.check_for_falling(node_pos)
|
||||||
|
Loading…
Reference in New Issue
Block a user