New mod to add simple chat sounds.

This commit is contained in:
Aaron Suen 2020-03-18 22:10:54 -04:00
parent a690a12959
commit 463b3c20e1
4 changed files with 114 additions and 0 deletions

0
szutil_chatsounds/README Normal file
View File

114
szutil_chatsounds/init.lua Normal file
View File

@ -0,0 +1,114 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, pairs, tonumber, tostring
= ipairs, minetest, pairs, tonumber, tostring
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local function parsespec(param)
local config = {}
for _, s in ipairs(param:split(" ")) do
local n = tonumber(s)
if n then
config[#config + 1] = {gain = n}
else
local gain, pitch = s:match("^(%S+):(%S+)$")
gain = tonumber(gain)
pitch = tonumber(pitch)
if gain and pitch then
config[#config + 1] = {gain = gain, pitch = pitch}
else
return false, "failed to parse spec"
end
end
end
return config
end
local default = parsespec("0.5:1.2 0.25 0.25 0.25:0.8 0")
local function dosound(player, pname, msgtype)
local config = player:get_meta():get_string(modname) or ""
config = config ~= "" and minetest.deserialize(config) or nil
config = config and #config > 0 and config or default
local data = (msgtype > #config) and config[#config] or config[msgtype]
if (data.gain or 0) <= 0 then return end
minetest.sound_play(modname, {
to_player = pname,
pitchvary = 0,
gain = data.gain,
pitch = data.pitch or 1
})
end
minetest.register_chatcommand(modname, {
description = "change chat sound configuration",
params = "<gain[:pitch] for DM> [... for chat] [...emote]"
.. " [...join/part] [...server] [...other]",
func = function(pname, param)
local player = minetest.get_player_by_name(pname)
if not player then return false, "player not connected" end
local config = parsespec(param)
if #config < 1 then return false, "empty config" end
player:get_meta():set_string(modname, minetest.serialize(config))
end
})
local pending = {}
minetest.register_globalstep(function()
for _, player in pairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
local msgtype = pending[pname]
if msgtype then dosound(player, pname, msgtype) end
end
pending = {}
end)
local function send(pname, text)
text = tostring(text)
local msgtype
if text:match("^%s*DM%sfrom%s") then msgtype = 1 -- DM
elseif text:match("^%s*%<") then msgtype = 2 -- public chat
elseif text:match("^%s*%*%s") then msgtype = 3 -- emotes
elseif text:match("^%s*%*%*%*%s") then msgtype = 4 -- join/part
elseif text:match("^%s*%#%s") then msgtype = 5 -- server
else msgtype = 6 end -- unknown/misc
local n = pending[pname] or msgtype
pending[pname] = msgtype > n and n or msgtype
end
local function sendall(text)
for _, player in pairs(minetest.get_connected_players()) do
send(player:get_player_name(), text)
end
end
do
local old_sendall = minetest.chat_send_all
function minetest.chat_send_all(text, ...)
sendall(text)
return old_sendall(text, ...)
end
end
minetest.register_on_chat_message(function(pname, text)
if text:sub(1, 1) ~= "/"
and minetest.check_player_privs(pname, "shout") then
sendall("<" .. pname .. "> " .. text)
end
end)
do
local old_sendp = minetest.chat_send_player
function minetest.chat_send_player(pname, text, ...)
send(pname, text)
return old_sendp(pname, text, ...)
end
end

Binary file not shown.

Binary file not shown.