Skip to content

Commit

Permalink
Fix duplicate on_receive and before_send events
Browse files Browse the repository at this point in the history
Remove extra before_send trigger from hash plugin

on_receive handle chat commands
  • Loading branch information
S-S-X committed Aug 13, 2022
1 parent 7192d91 commit bd13790
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
2 changes: 0 additions & 2 deletions plugin/hash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ beerchat.register_on_chat_message(function(name, message)
)
elseif msg == "" then
switch_channel(name, channel_name)
elseif not beerchat.execute_callbacks('before_send', name, msg, channel_name) then
return false
elseif not beerchat.is_player_subscribed_to_channel(name, channel_name) then
minetest.chat_send_player(name, "You need to join this channel in order to be able to send messages to it")
else
Expand Down
13 changes: 8 additions & 5 deletions plugin/me.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

local me_message_string = "|#${channel_name}| * ${from_player} ${message}"

local me_override = {
minetest.register_chatcommand("me", {
params = "<Message>",
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)
local msg = param
local msg_data = beerchat.default_on_receive(name, param)
if not msg_data then
return true
end
local msg = msg_data.message
name = msg_data.name
local channel = beerchat.get_player_channel(name)
if not channel then
beerchat.fix_player_channel(name, true)
Expand Down Expand Up @@ -42,6 +47,4 @@ local me_override = {
end
return true
end
}

minetest.register_chatcommand("me", me_override)
})
14 changes: 7 additions & 7 deletions plugin/pm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ end)
beerchat.register_on_chat_message(function(name, message)
minetest.log("action", "CHAT " .. name .. ": " .. message)

local msg_data = {name=name,message=message}
if beerchat.execute_callbacks('on_receive', msg_data) then
message = msg_data.message
else
return false
end

local players, msg = string.match(message, "^@([^%s:]*)[%s:](.*)")
if players and msg then
if msg == "" then
Expand Down Expand Up @@ -170,6 +163,13 @@ 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)
local msg_data = beerchat.default_on_receive(name, param)
if not msg_data then
return true
end
name = msg_data.name
param = msg_data.message

minetest.log("action", "PM " .. name .. ": " .. param)
local players, msg = string.match(param, "^(.-) (.*)")
if players and msg then
Expand Down
8 changes: 7 additions & 1 deletion plugin/whisper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,11 @@ beerchat.register_on_chat_message(beerchat.whisper)
minetest.register_chatcommand("whis", {
params = "<message>",
description = "Whisper command for those who can't use $",
func = function(name, param) beerchat.whisper(name, "$ " .. param) end
func = function(name, param)
local msg = beerchat.default_on_receive(name, param)
if msg then
beerchat.whisper(msg.name, "$ " .. msg.message)
end
return true
end
})
24 changes: 14 additions & 10 deletions router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ local function default_message_handler(name, message)
return true
end

function beerchat.default_on_receive(name, message)
local msg_data = { name = name, message = message }
if beerchat.execute_callbacks('on_receive', msg_data) then
return msg_data
end
minetest.log("verbose", "Beerchat message discarded by on_receive hook, contents went to /dev/null")
end

-- All messages are handled either by sending to channel or through special plugin function.
minetest.register_on_chat_message(function(name, message)

-- Execute or_receive callbacks allowing modifications to sender and message
local msg_data = {name=name,message=message}
if beerchat.execute_callbacks('on_receive', msg_data) then
message = msg_data.message
name = msg_data.name
else
minetest.log("verbose", "Beerchat message discarded by on_receive hook, contents went to /dev/null")
-- Execute on_receive callbacks allowing modifications to sender and message
local msg = beerchat.default_on_receive(name, message)
if not msg then
return true
end

-- Execute mesasge handlers
for _, handler in ipairs(on_chat_message_handlers) do
if handler(name, message) then
if handler(msg.name, msg.message) then
-- Last executed handler marked message as handled, return
return true
end
end

-- None of extensions handled current message, call through default message handler
return default_message_handler(name, message)
return default_message_handler(msg.name, msg.message)
end)

0 comments on commit bd13790

Please sign in to comment.