mirror of
https://github.com/mt-mods/xcompat.git
synced 2024-12-22 17:10:18 +01:00
Avoid crash when sound_api function for a key is undefined (#4)
* Avoid crash when sound_api function for a key is undefined * Refactor function to override sounds using sound_api * Change override_sounds for validate_sounds
This commit is contained in:
parent
42d876fa75
commit
d13501cc10
17
init.lua
17
init.lua
@ -2,9 +2,20 @@ local modpath = minetest.get_modpath("sound_api")
|
|||||||
|
|
||||||
sound_api = dofile(modpath .. "/sound_api_core/init.lua")
|
sound_api = dofile(modpath .. "/sound_api_core/init.lua")
|
||||||
|
|
||||||
|
local function validate_sound(key)
|
||||||
|
if key and sound_api[key] then
|
||||||
|
return true
|
||||||
|
elseif key then
|
||||||
|
minetest.log("warning", "attempted to call invalid sound: "..key)
|
||||||
|
else
|
||||||
|
minetest.log("warning", "sound_def is missing a sound_api key")
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
minetest.register_on_mods_loaded(function()
|
minetest.register_on_mods_loaded(function()
|
||||||
for name, def in pairs(minetest.registered_nodes) do
|
for name, def in pairs(minetest.registered_nodes) do
|
||||||
if def._sound_def and def._sound_def.key then
|
if def._sound_def and validate_sound(def._sound_def.key) then
|
||||||
minetest.override_item(name, {
|
minetest.override_item(name, {
|
||||||
sounds = sound_api[def._sound_def.key](def._sound_def.input)
|
sounds = sound_api[def._sound_def.key](def._sound_def.input)
|
||||||
})
|
})
|
||||||
@ -13,10 +24,10 @@ minetest.register_on_mods_loaded(function()
|
|||||||
|
|
||||||
local old_reg_node = minetest.register_node
|
local old_reg_node = minetest.register_node
|
||||||
function minetest.register_node(name, def)
|
function minetest.register_node(name, def)
|
||||||
if def._sound_def and def._sound_def.key then
|
if def._sound_def and validate_sound(def._sound_def.key) then
|
||||||
def.sounds = sound_api[def._sound_def.key](def._sound_def.input)
|
def.sounds = sound_api[def._sound_def.key](def._sound_def.input)
|
||||||
end
|
end
|
||||||
|
|
||||||
old_reg_node(name, def)
|
old_reg_node(name, def)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user