properly implement at replies and deaf import

dev
Tai @ Flex 2017-03-03 17:26:28 +00:00
parent 76532e3771
commit 4ea2f7a667
2 changed files with 41 additions and 22 deletions

15
api.lua
View File

@ -7,6 +7,8 @@ chat_modes = {}
minetest.register_privilege("cmodeswitch", "Player can switch their chat mode.")
-- ========================================================
-- Don't litter
local debug_on = false
function chat_modes.dodebug(message, artefact)
if not debug_on then return end
@ -54,6 +56,7 @@ function chat_modes.register_interceptor(name, handler)
end
-- Send a player chat
-- Residual function, should be removed
function chat_modes.chatsend(player, message)
if type(player) ~= "string" then
player = player:get_player_name()
@ -233,7 +236,7 @@ end)
-- ================================
-- Load defaults
dodebug("Checking for default functions")
dodebug("Loading default modes")
if loadmodes then
dodebug("Default modes found:",loadmodes:split(","))
@ -246,8 +249,18 @@ else
dofile(minetest.get_modpath("chat_modes").."/shout_mode.lua" )
end
-- ================================
-- Load standard interceptors
dodebug("Loading default interceptors")
-- Allow direct pinging
dofile(minetest.get_modpath("chat_modes").."/atreply_interceptor.lua")
-- Deafness
-- Deaf filter is processed after any other interceptors may have
-- modified the target list
-- FIXME if other interceptors discard the original list, this effect is nulled....
dofile(minetest.get_modpath("chat_modes").."/deaf_interceptor.lua")
dodebug("Loaded chat modes")

View File

@ -21,16 +21,14 @@ local function playping(playername)
return
end
local function removefrom(players, playername)
local i = 1
while players[i] do
if players[i]:get_player_name() == playername then
table.remove(players, i)
break
-- Returns the target if it does not exist in the set
-- else returns nil
local function needstarget(targetset, target)
for _,testtarget in pairs(targetset) do
if testtarget == target then
return target
end
end
return players
end
chat_modes.register_interceptor("atreply", function(sender, message, targets)
@ -42,21 +40,23 @@ chat_modes.register_interceptor("atreply", function(sender, message, targets)
local includes = {"triggerone"}
local private = false
local newtargets = {}
-- Players mentioned at start of message
-- Only send to them
while messageparts[1] and messageparts[1]:sub(1,1) == '@' do
local token = table.remove(messageparts, 1)
local dmstring = "DM from "..sender..": "
local tname = token:sub(2, #token)
chat_modes.chatsend(tname, dmstring..message)
playping(tname)
local targetplayer = needstarget(targets, minetest.get_player_by_name(tname) )
if targetplayer then
newtargets[#newtargets+1] = targetplayer
playping(tname)
end
private = true
end
if private then
chat_modes.dodebug("DECLINE")
return false
return newtargets
end
while includes[1] do -- hence "triggerone" to do it at least once
@ -64,16 +64,22 @@ chat_modes.register_interceptor("atreply", function(sender, message, targets)
includes = nextin.includes
messageparts = nextin.mparts
local dmstring = "DM from "..sender..": "
for _,pname in pairs(includes) do
chat_modes.chatsend(pname, dmstring..message)
playping(pname)
targets = removefrom(table.copy(targets), playername)
local targetplayer = needstarget(targets, minetest.get_player_by_name(tname) )
if targetplayer then
newtargets[#newtargets+1] = targetplayer
playping(tname)
end
end
end
chat_modes.dodebug("ALLOW")
return targets -- Allow processing the rest
for _,residualplayer in pairs(targets) do
local targetplayer = needstarget(newtargets, residualplayer )
if targetplayer then
newtargets[#newtargets+1] = targetplayer
end
end
return newtargets
end)