Implemented rest of callbacks

This commit is contained in:
SX 2020-02-12 01:51:09 +02:00
parent f84efad091
commit 2524ad440c
9 changed files with 66 additions and 39 deletions

View File

@ -153,7 +153,6 @@ local join_channel = {
description = "Join channel named <Channel Name>. " ..
"After joining you will see messages sent to that channel (in addition to the other channels you have joined)",
func = function(name, param)
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel name as a minimum"
end
@ -207,7 +206,6 @@ local leave_channel = {
"When you leave the channel you can no longer send/ receive messages from that channel. " ..
"NOTE: You can also leave the main channel",
func = function(name, param)
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel name"
end

View File

@ -28,8 +28,7 @@ beerchat.send_message = function(name, message, channel)
end
minetest.chat_send_player(name, message)
-- TODO: read player settings for channel sounds
-- TODO: read player settings for channel sounds
if beerchat.enable_sounds and channel ~= beerchat.main_channel_name then
minetest.sound_play(beerchat.channel_message_sound, { to_player = name, gain = beerchat.sounds_default_gain } )
end

View File

@ -12,10 +12,11 @@ minetest.register_on_chat_message(function(name, message)
channel_name = hashchat_lastrecv[name]
end
if not beerchat.execute_callbacks('before_send', name, channel_name, message) then
return false
end
if channel_name and msg then
if beerchat.is_player_jailed(name) then
return false
end
if not beerchat.channels[channel_name] then
minetest.chat_send_player(name, "Channel "..channel_name.." does not exist. Make sure the channel still "..
"exists and you format its name properly, e.g. #channel message or #my channel: message")
@ -39,9 +40,6 @@ minetest.register_on_chat_message(function(name, message)
else
channel_name = string.match(message, "^#(.*)")
if channel_name then
if beerchat.is_player_jailed(name) then
return false
end
if not beerchat.channels[channel_name] then
minetest.chat_send_player(name, "Channel "..channel_name.." does not exist")
elseif not beerchat.is_player_subscribed_to_channel(name, channel_name) then

View File

@ -2,7 +2,9 @@
beerchat.cb = {} -- all custom callbacks
beerchat.cb.before_send = {} -- executed before sending message
beerchat.cb.before_receive = {} -- executed before receiving message
beerchat.cb.before_send_pm = {} -- executed before sending private message
beerchat.cb.before_send_me = {} -- executed before /me message is sent
beerchat.cb.before_whisper = {} -- executed before whisper message is sent
beerchat.cb.before_join = {} -- executed before channel is joined
beerchat.cb.before_leave = {} -- executed before channel is leaved
beerchat.cb.before_invite = {} -- excuted before channel invitation takes place
@ -18,10 +20,10 @@ beerchat.register_callback = function(trigger, fn)
print('Error: Invalid trigger argument for beerchat.register_callback, must be string')
return
end
local cb = beerchat.cb
local callback_key = trigger
if not cb[callback_key] then
print('Error: Invalid callback trigger event, possible triggers:')
for k,_ in pairs(cb) do
@ -29,7 +31,7 @@ beerchat.register_callback = function(trigger, fn)
end
return
end
table.insert(cb[callback_key], fn)
end

View File

@ -12,6 +12,26 @@ beerchat.is_player_jailed = function(name)
return true == beerchat.jail_list[name]
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
local meta = player:get_meta()
local jailed = 1 == meta:get_int("beerchat:jailed")
if jailed then
beerchat.jail_list[name] = true
beerchat.currentPlayerChannel[name] = beerchat.jail_channel_name
beerchat.playersChannels[name][beerchat.jail_channel_name] = "joined"
else
beerchat.jail_list[name] = nil
end
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
beerchat.jail_list[name] = nil
end)
beerchat.register_callback('before_invite', function(sender, recipient, channel)
if beerchat.is_player_jailed(player_name) then
return false, player_name .. " is in chat-jail, no inviting."
@ -46,6 +66,24 @@ beerchat.register_callback('before_send', function(name, message, channel)
end
end)
beerchat.register_callback('before_send_pm', function(name, message, target)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no PMs for you."
end
end)
beerchat.register_callback('before_send_me', function(name, message, channel)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, you may not use /me command."
end
end)
beerchat.register_callback('before_send_whisper', function(name, message, channel, range)
if beerchat.is_player_jailed(name) then
return false
end
end
beerchat.register_callback('before_check_muted', function(name, muted)
if beerchat.is_player_jailed(name) then
return false

7
me.lua
View File

@ -6,9 +6,6 @@ local me_override = {
description = "Send message in the \"* player message\" format, e.g. /me eats pizza becomes |#"..
beerchat.main_channel_name.."| * Player01 eats pizza",
func = function(name, param)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, you may not use /me command."
end
local msg = param
local channel_name = beerchat.main_channel_name
if not beerchat.channels[channel_name] then
@ -19,6 +16,10 @@ local me_override = {
minetest.chat_send_player(name, "You need to join channel " .. channel_name
.. " in order to be able to send messages to it")
else
local cb_result, cb_message = beerchat.execute_callbacks('before_send_me', name, msg, channel_name)
if not cb_result then
return cb_message and (false, cb_message) or false
end
for _,player in ipairs(minetest.get_connected_players()) do
local target = player:get_player_name()
-- Checking if the target is in this channel

17
pm.lua
View File

@ -12,12 +12,13 @@ minetest.register_on_chat_message(function(name, message)
minetest.log("action", "CHAT " .. name .. ": " .. message)
local players, msg = string.match(message, "^@([^%s:]*)[%s:](.*)")
if players and msg then
if beerchat.is_player_jailed(name) then
return false
end
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
return cb_message and (false, cb_message) or false
end
if players == "" then--reply
-- We need to get the target
players = atchat_lastrecv[name]
@ -71,9 +72,6 @@ minetest.register_on_chat_message(function(name, message)
-- Register the chat in the target persons last spoken to table
atchat_lastrecv[name] = players
if atleastonesent then
if beerchat.is_player_jailed(name) then
return false
end
successplayers = successplayers:sub(1, -2)
if (successplayers ~= name) then
minetest.chat_send_player(
@ -159,9 +157,6 @@ local msg_override = {
"for compatibility with the old chat command but with new style chat muting support "..
"(players will not receive your message if they muted you) and multiple (comma separated) player support",
func = function(name, param)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no PMs for you."
end
minetest.log("action", "PM " .. name .. ": " .. param)
local players, msg = string.match(param, "^(.-) (.*)")
if players and msg then
@ -172,6 +167,10 @@ 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
return cb_message and (false, cb_message) or false
end
if players and players ~= "" then
send_pm(players, name, msg)
end

View File

@ -21,15 +21,6 @@ minetest.register_on_joinplayer(function(player)
beerchat.currentPlayerChannel[name] = beerchat.main_channel_name
end
local jailed = 1 == meta:get_int("beerchat:jailed")
if jailed then
beerchat.jail_list[name] = true
beerchat.currentPlayerChannel[name] = beerchat.jail_channel_name
beerchat.playersChannels[name][beerchat.jail_channel_name] = "joined"
else
beerchat.jail_list[name] = nil
end
end)
minetest.register_on_leaveplayer(function(player)
@ -37,6 +28,5 @@ minetest.register_on_leaveplayer(function(player)
beerchat.playersChannels[name] = nil
atchat_lastrecv[name] = nil
beerchat.currentPlayerChannel[name] = nil
beerchat.jail_list[name] = nil
end)

View File

@ -12,9 +12,6 @@ beerchat.whisper = function(name, message)
if dollar ~= "$" then
return false
end
if beerchat.is_player_jailed(name) then
return false
end
local radius = tonumber(sradius)
if not radius then
radius = whisper_default_range
@ -25,6 +22,11 @@ beerchat.whisper = function(name, message)
elseif msg == "" then
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_send_whisper', name, msg, beerchat.main_channel_name, radius)
if not cb_result then
return cb_message and (false, cb_message) or false
end
local pl = minetest.get_player_by_name(name)
local pl_pos = pl:getpos()
local all_objects = minetest.get_objects_inside_radius({x=pl_pos.x, y=pl_pos.y, z=pl_pos.z}, radius)