Simplify callback usage, override /tell, fix/add info messages
This commit is contained in:
parent
998150ac38
commit
1d7e810928
@ -16,5 +16,3 @@ read_globals = {
|
||||
-- Deps
|
||||
"xban"
|
||||
}
|
||||
|
||||
files["jail.lua"] = { unused_args = false }
|
||||
|
@ -191,14 +191,8 @@ local join_channel = {
|
||||
end
|
||||
end
|
||||
|
||||
local cb_result, cb_message = beerchat.execute_callbacks(
|
||||
'before_join', name, channel_name)
|
||||
if not cb_result then
|
||||
if cb_message then
|
||||
return false, cb_message
|
||||
else
|
||||
return false
|
||||
end
|
||||
if not beerchat.execute_callbacks('before_join', name, channel_name) then
|
||||
return false
|
||||
end
|
||||
|
||||
beerchat.playersChannels[name] = beerchat.playersChannels[name] or {}
|
||||
@ -236,14 +230,8 @@ local leave_channel = {
|
||||
.. ", no need to leave."
|
||||
end
|
||||
|
||||
local cb_result, cb_message = beerchat.execute_callbacks(
|
||||
'before_leave', name, channel_name)
|
||||
if not cb_result then
|
||||
if cb_message then
|
||||
return false, cb_message
|
||||
else
|
||||
return false
|
||||
end
|
||||
if not beerchat.execute_callbacks('before_leave', name, channel_name) then
|
||||
return false
|
||||
end
|
||||
|
||||
beerchat.playersChannels[name][channel_name] = nil
|
||||
@ -304,14 +292,8 @@ local invite_channel = {
|
||||
if not minetest.get_player_by_name(player_name) then
|
||||
return false, "ERROR: " .. player_name .. " does not exist or is not online."
|
||||
else
|
||||
local cb_result, cb_message = beerchat.execute_callbacks(
|
||||
'before_invite', name, player_name, channel_name)
|
||||
if not cb_result then
|
||||
if cb_message then
|
||||
return false, cb_message
|
||||
else
|
||||
return false
|
||||
end
|
||||
if not beerchat.execute_callbacks('before_invite', name, player_name, channel_name) then
|
||||
return false
|
||||
end
|
||||
if not beerchat.has_player_muted_player(player_name, name) then
|
||||
if beerchat.enable_sounds then
|
||||
@ -345,13 +327,8 @@ local mute_player = {
|
||||
.. "messages of this user, regardless of what channel his user sends messages to.",
|
||||
func = function(name, param)
|
||||
|
||||
local cb_result, cb_message = beerchat.execute_callbacks('before_mute', name, param)
|
||||
if not cb_result then
|
||||
if cb_message then
|
||||
return false, cb_message
|
||||
else
|
||||
return false
|
||||
end
|
||||
if not beerchat.execute_callbacks('before_mute', name, param) then
|
||||
return false
|
||||
end
|
||||
|
||||
if not param or param == "" then
|
||||
@ -455,14 +432,8 @@ beerchat.force_player_to_channel = function(name, param)
|
||||
beerchat.currentPlayerChannel[player_name] = channel_name
|
||||
meta:set_string("beerchat:current_channel", channel_name)
|
||||
|
||||
local cb_result, cb_message = beerchat.execute_callbacks(
|
||||
'on_forced_join', name, player_name, channel_name, meta)
|
||||
if not cb_result then
|
||||
if cb_message then
|
||||
return false, cb_message
|
||||
else
|
||||
return false
|
||||
end
|
||||
if not beerchat.execute_callbacks('on_forced_join', name, player_name, channel_name, meta) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- inform user
|
||||
|
@ -45,7 +45,10 @@ beerchat.execute_callbacks = function(trigger, ...)
|
||||
for _,fn in ipairs(cb_list) do
|
||||
local result, msg = fn(unpack(arg))
|
||||
if result ~= nil then
|
||||
return result, msg
|
||||
if msg and type(arg[1]) == "string" then
|
||||
minetest.chat_send_player(arg[1], msg)
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
if trigger == 'before_check_muted' then
|
||||
|
6
init.lua
6
init.lua
@ -79,13 +79,15 @@ beerchat.http = nil
|
||||
|
||||
-- integrated extensions (could also be different mod)
|
||||
if minetest.settings:get_bool("beerchat.enable_jail") then
|
||||
dofile(MP.."/jail.lua")
|
||||
dofile(MP.."/plugin/jail.lua")
|
||||
end
|
||||
|
||||
if minetest.settings:get_bool("beerchat.enable_cleaner") then
|
||||
dofile(MP.."/cleaner.lua")
|
||||
dofile(MP.."/plugin/cleaner.lua")
|
||||
end
|
||||
|
||||
dofile(MP.."/plugin/override.lua")
|
||||
|
||||
if minetest.settings:get_bool("enable_beerchat_integration_test") then
|
||||
dofile(MP.."/integration_test.lua")
|
||||
end
|
||||
|
41
message.lua
41
message.lua
@ -16,36 +16,31 @@
|
||||
--
|
||||
|
||||
beerchat.send_on_channel = function(name, channel_name, message)
|
||||
local msg_data = {name=name, channel=channel_name,message=message}
|
||||
if beerchat.execute_callbacks('on_send_on_channel', msg_data) then
|
||||
name = msg_data.name
|
||||
channel_name = msg_data.channel_name
|
||||
message = msg_data.message
|
||||
else
|
||||
return false
|
||||
end
|
||||
local msg = {name=name, channel=channel_name,message=message}
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
local target = player:get_player_name()
|
||||
-- Checking if the target is in this channel
|
||||
if beerchat.is_player_subscribed_to_channel(target, channel_name) then
|
||||
if not beerchat.has_player_muted_player(target, name) then
|
||||
beerchat.send_message(
|
||||
target,
|
||||
beerchat.format_message(
|
||||
beerchat.main_channel_message_string, {
|
||||
channel_name = channel_name,
|
||||
to_player = target,
|
||||
from_player = name,
|
||||
message = message
|
||||
}
|
||||
),
|
||||
channel_name
|
||||
)
|
||||
end
|
||||
if beerchat.execute_callbacks('on_send_on_channel', msg, target) then
|
||||
beerchat.send_message(
|
||||
target,
|
||||
beerchat.format_message(
|
||||
beerchat.main_channel_message_string, {
|
||||
channel_name = msg.channel,
|
||||
to_player = target,
|
||||
from_player = msg.name,
|
||||
message = msg.message
|
||||
}
|
||||
),
|
||||
msg.channel
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
beerchat.register_callback("on_send_on_channel", function(msg, target)
|
||||
return beerchat.is_player_subscribed_to_channel(target, msg.channel)
|
||||
and not beerchat.has_player_muted_player(target, msg.name)
|
||||
end)
|
||||
|
||||
minetest.register_on_chat_message(function(name, message)
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
--luacheck: no_unused_args
|
||||
|
||||
-- Jail channel is where you put annoying missbehaving users with /force2channel
|
||||
beerchat.jail = {}
|
||||
|
||||
@ -140,6 +142,9 @@ beerchat.register_callback('before_send', function(name, message, channel)
|
||||
-- override default send method to mute pings for jailed users
|
||||
-- but allow chatting without pings on jail channel
|
||||
minetest.chat_send_player(name, message)
|
||||
else
|
||||
-- Inform player if trying to send to other channels
|
||||
return false, "You are in chat-jail, no changing channels for you."
|
||||
end
|
||||
return false
|
||||
end
|
||||
@ -147,7 +152,7 @@ end)
|
||||
|
||||
beerchat.register_callback('before_switch_chan', function(name, oldchannel, newchannel)
|
||||
if beerchat.is_player_jailed(name) then
|
||||
return false
|
||||
return false, "You are in chat-jail, no changing channels for you."
|
||||
end
|
||||
end)
|
||||
|
||||
@ -165,7 +170,7 @@ end)
|
||||
|
||||
beerchat.register_callback('before_whisper', function(name, message, channel, range)
|
||||
if beerchat.is_player_jailed(name) then
|
||||
return false
|
||||
return false, "You are in chat-jail, you may not whisper."
|
||||
end
|
||||
end)
|
||||
|
22
plugin/override.lua
Normal file
22
plugin/override.lua
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
--luacheck: globals minetest.registered_chatcommands.tell
|
||||
|
||||
minetest.register_on_mods_loaded(function()
|
||||
|
||||
-- Override /tell chat command to execute callbacks.
|
||||
-- Command is added by mesecons command block.
|
||||
if minetest.registered_chatcommands.tell then
|
||||
local tell = minetest.registered_chatcommands.tell.func
|
||||
minetest.registered_chatcommands.tell.func = function(name, param)
|
||||
local target, message = param:match("^([^%s]+)%s+(.*)$")
|
||||
local cb_result, cb_message = beerchat.execute_callbacks('before_send_pm', name, message, target)
|
||||
if cb_result then
|
||||
return tell(name, param)
|
||||
end
|
||||
if cb_message then
|
||||
minetest.chat_send_player(name, cb_message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end)
|
10
pm.lua
10
pm.lua
@ -30,9 +30,8 @@ minetest.register_on_chat_message(function(name, message)
|
||||
if msg == "" then
|
||||
minetest.chat_send_player(name, "Please enter the private message you would like to send")
|
||||
else
|
||||
local cb_result, cb_message = beerchat.execute_callbacks('before_send_pm', name, msg, players)
|
||||
if not cb_result then
|
||||
if cb_message then return false, cb_message else return false end
|
||||
if not beerchat.execute_callbacks('before_send_pm', name, msg, players) then
|
||||
return false
|
||||
end
|
||||
if players == "" then--reply
|
||||
-- We need to get the target
|
||||
@ -182,9 +181,8 @@ local msg_override = {
|
||||
minetest.chat_send_player(name, "ERROR: Please enter the private message you would like to send")
|
||||
return false
|
||||
else
|
||||
local cb_result, cb_message = beerchat.execute_callbacks('before_send_pm', name, msg, players)
|
||||
if not cb_result then
|
||||
if cb_message then return false, cb_message else return false end
|
||||
if not beerchat.execute_callbacks('before_send_pm', name, msg, players) then
|
||||
return false
|
||||
end
|
||||
if players and players ~= "" then
|
||||
send_pm(players, name, msg)
|
||||
|
@ -32,10 +32,8 @@ beerchat.whisper = function(name, message)
|
||||
minetest.chat_send_player(name, "Please enter the message you would like to "
|
||||
.. "whisper to nearby players")
|
||||
else
|
||||
local cb_result, cb_message = beerchat.execute_callbacks('before_whisper',
|
||||
name, msg, beerchat.main_channel_name, radius)
|
||||
if not cb_result then
|
||||
if cb_message then return false, cb_message else return false end
|
||||
if not beerchat.execute_callbacks('before_whisper', name, msg, beerchat.main_channel_name, radius) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- true if someone heard the player
|
||||
|
Loading…
x
Reference in New Issue
Block a user