move rate checks to seperate function

This commit is contained in:
chmodsayshello 2023-04-09 18:45:23 +02:00
parent d9ac803f5b
commit 75595115a0
1 changed files with 12 additions and 6 deletions

View File

@ -446,6 +446,17 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
local function exceeded_rate_limit(playername) --Note: will also take care of increasing value
if playermessagecounter[playername] == nil then
playermessagecounter[playername] = 0
end
if playermessagecounter[playername] >= message_rate_limit then -- == should do as well
return true
end
playermessagecounter[playername] = playermessagecounter[playername] + 1
return false
end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "mcl_beds_form" then if formname ~= "mcl_beds_form" then
return return
@ -457,16 +468,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return return
end end
if playermessagecounter[player:get_player_name()] == nil then if exceeded_rate_limit(player:get_player_name()) then
playermessagecounter[player:get_player_name()] = 0
end
if playermessagecounter[player:get_player_name()] >= message_rate_limit then -- == should do as well
minetest.chat_send_player(player:get_player_name(),S("You exceeded the maximum number of messages per 10 seconds!") .. " (" .. tostring(message_rate_limit) .. ")") minetest.chat_send_player(player:get_player_name(),S("You exceeded the maximum number of messages per 10 seconds!") .. " (" .. tostring(message_rate_limit) .. ")")
return return
end end
playermessagecounter[player:get_player_name()] = playermessagecounter[player:get_player_name()] + 1
minetest.chat_send_all(minetest.format_chat_message(player:get_player_name(), fields.chatmessage)) minetest.chat_send_all(minetest.format_chat_message(player:get_player_name(), fields.chatmessage))
end end