From 624ec4021eb3b08e835985f5697a4fc1670d2189 Mon Sep 17 00:00:00 2001 From: ssdaniel24 <107036969+ssdaniel24@users.noreply.github.com> Date: Thu, 13 Jun 2024 22:59:44 +0300 Subject: [PATCH] lottblocks: added sound cooldowns to music stuff (#1461). Closes #1405. --- mods/lord/Blocks/lottblocks/music.lua | 30 +++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/mods/lord/Blocks/lottblocks/music.lua b/mods/lord/Blocks/lottblocks/music.lua index 27f0394fe..dff1309a2 100644 --- a/mods/lord/Blocks/lottblocks/music.lua +++ b/mods/lord/Blocks/lottblocks/music.lua @@ -1,5 +1,27 @@ local S = minetest.get_translator("lottblocks") +---@readonly +---@type number +local SOUND_COOLDOWN = 5 +--- Contains sounds cache. Key is sum of coords (literally) or player name. +---@type table +local sound_cooldown_cache = {} + +---@param name string +---@param def table +---@param player_name? string +local function play_sound(name, def, player_name) + local p = def.pos + local hash = player_name or p.x + p.y + p.z -- HACK: rough "unique" hash + if not sound_cooldown_cache[hash] then + minetest.sound_play(name, def) + sound_cooldown_cache[hash] = true + minetest.after(SOUND_COOLDOWN, function() + sound_cooldown_cache[hash] = nil + end) + end +end + minetest.register_node("lottblocks:dwarf_harp", { description = S("Dwarvern Harp"), tiles = { @@ -14,7 +36,7 @@ minetest.register_node("lottblocks:dwarf_harp", { paramtype = "light", paramtype2 = "facedir", on_punch = function(pos) - minetest.sound_play("lottblocks_harp", { + play_sound("lottblocks_harp", { pos = pos, max_hear_distance = 12, gain = 1, @@ -95,11 +117,11 @@ for _, row in ipairs(whistle) do description = S(wood:gsub("^%l", string.upper) .. " (Note " .. note .. ") Whistle"), inventory_image = "lottblocks_" .. wood .. "_whistle.png", on_use = function(itemstack, user) - minetest.sound_play(note, { + play_sound(note, { pos = user:get_pos(), max_hear_distance = 7, gain = 1, - }) + }, user:get_player_name()) end }) minetest.register_craft({ @@ -120,7 +142,7 @@ minetest.register_node("lottblocks:gong", { paramtype = "light", paramtype2 = "facedir", on_punch = function(pos) - minetest.sound_play("gong", { + play_sound("gong", { pos = pos, max_hear_distance = 48, gain = 1,