diff --git a/config/action_buttons.lua b/config/action_buttons.lua
index 4e821e1f..f48606d3 100644
--- a/config/action_buttons.lua
+++ b/config/action_buttons.lua
@@ -8,7 +8,7 @@ local Roles = require 'expcore.roles'
local Store = require 'expcore.store'
local Game = require 'utils.game'
local Reports = require 'modules.control.reports'
-local Warnings = require 'modules.addons.warnings-control'
+local Warnings = require 'modules.addons.warnings'
local Jail = require 'modules.control.jail'
local Colors = require 'resources.color_presets'
local format_chat_player_name = ext_require('expcore.common','format_chat_player_name')
@@ -135,7 +135,7 @@ local function warn_player_callback(player,reason)
local action_player_name,action_player_name_color = get_action_player_name(player)
local by_player_name_color = format_chat_player_name(player)
game.print{'expcom-warnings.received',action_player_name_color,by_player_name_color,reason}
- Warnings.add_warnings(action_player_name,player.name)
+ Warnings.add_warning(action_player_name,player.name,reason)
end
-- jails the action player, requires a reason
diff --git a/config/warnings.lua b/config/warnings.lua
index 7fd627a4..73696544 100644
--- a/config/warnings.lua
+++ b/config/warnings.lua
@@ -14,6 +14,6 @@ return {
game.ban_player(player,{'warnings.received',by_player_name,number_of_warnings,{'warnings.ban',{'links.website'}}})
end
},
- temp_warning_cool_down=30, -- time for a temp warning (given by script) to be removed (in minutes)
- temp_warning_limit=5 -- the number of temp warnings (given by script) that are allowed before full warnings are given
+ script_warning_cool_down=30, -- time for a script warning (given by script) to be removed (in minutes)
+ script_warning_limit=5 -- the number of script warnings (given by script) that are allowed before full warnings are given
}
\ No newline at end of file
diff --git a/doc/index.html b/doc/index.html
index 13ec6e62..6e2fc4e9 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -278,12 +278,17 @@
Control Module - Jail
- Adds a way to jail players and temp ban players.
+ - Adds a way to jail players and temp ban players.
@@ -1002,7 +1003,7 @@
generated by LDoc
diff --git a/doc/modules/Reports.html b/doc/modules/Reports.html
index 291c812f..b55cf3e1 100644
--- a/doc/modules/Reports.html
+++ b/doc/modules/Reports.html
@@ -42,6 +42,7 @@
Control Module - Warnings
+ - Adds a way to give and remove warnings to players.
+
+
+
+
+
+
+
+
Usage
+
+ -- This will add a warning to the player
+ Warnings.add_warning('MrBiter','Cooldude2606','Killed too many biters')
+
+ -- This will remove a warning from a player, second name is just who is doing the action
+ Warnings.remove_warning('MrBiter','Cooldude2606')
+
+ -- Script warning as similar to normal warning but are designed to have no effect for a short amount of time
+-- this is so it can be used for greifer protection without being too agressive
+ Warnings.add_script_warning('MrBiter','Killed too many biters')
+
+ -- Both normal and script warnings can also be cleared, this will remove all warnings
+ Warnings.clear_warnings('MrBiter','Cooldude2606')
@@ -246,7 +247,7 @@ fraction will decide a chance to spawn. 1 alien for 2 spawners will have 50% on
generated by LDoc
diff --git a/doc/modules/utils.core.html b/doc/modules/utils.core.html
index 72d690ef..03d91418 100644
--- a/doc/modules/utils.core.html
+++ b/doc/modules/utils.core.html
@@ -42,6 +42,7 @@
@@ -262,7 +263,7 @@
generated by LDoc
diff --git a/modules/addons/discord-alerts.lua b/modules/addons/discord-alerts.lua
index 9c7f63f7..d9452336 100644
--- a/modules/addons/discord-alerts.lua
+++ b/modules/addons/discord-alerts.lua
@@ -102,18 +102,19 @@ end
--- Warnings added and removed
if config.player_warnings then
- local Warnings = require 'modules.addons.warnings-control'
- Event.add(Warnings.events.on_player_warned,function(event)
+ local Warnings = require 'modules.addons.warnings'
+ Event.add(Warnings.events.on_warning_added,function(event)
local player_name,by_player_name = get_player_name(event)
emit_event{
title='Warning',
description='A player has been given a warning',
color=Colors.yellow,
['Player:']=''..player_name,
- ['By:']=''..by_player_name
+ ['By:']=''..by_player_name,
+ ['Reason:']=event.reason
}
end)
- Event.add(Warnings.events.on_player_warning_removed,function(event)
+ Event.add(Warnings.events.on_warning_removed,function(event)
local player_name,by_player_name = get_player_name(event)
emit_event{
title='Warning Removed',
diff --git a/modules/addons/jail-control.lua b/modules/addons/jail-control.lua
deleted file mode 100644
index 41d5c910..00000000
--- a/modules/addons/jail-control.lua
+++ /dev/null
@@ -1,99 +0,0 @@
-local Roles = require 'expcore.roles'
-local Game = require 'utils.game'
-local Global = require 'utils.global'
-local move_items = ext_require('expcore.common','move_items')
-
-local Jail = {
- old_roles = {},
- temp_bans = {},
- events = {
- on_player_jailed=script.generate_event_name(),
- on_player_unjailed=script.generate_event_name(),
- on_player_temp_banned=script.generate_event_name(),
- on_player_temp_ban_cleared=script.generate_event_name()
- }
-}
-
-Global.register({
- old_roles = Jail.old_roles,
- temp_bans = Jail.temp_bans
-},function(tbl)
- Jail.old_roles = tbl.old_roles
- Jail.temp_bans = tbl.temp_bans
-end)
-
-local function event_emit(event,player,by_player_name,reason)
- script.raise_event(event,{
- name=event,
- tick=game.tick,
- player_index=player.index,
- by_player_name=by_player_name,
- reason=reason
- })
-end
-
---- Jails a player, this is only the logic there is no output to players
--- @tparam LuaPlayer player the player that will be jailed, must not be in jail
--- @tparam[opt=''] string by_player_name the name of the player doing the action used in logs
--- @treturn the number of roles that were removed, nil if there was an error
-function Jail.jail_player(player,by_player_name)
- player = Game.get_player_from_any(player)
- if not player then return end
- if Roles.player_has_role(player,'Jail') then return end
- local old_roles = Roles.get_player_roles(player)
- Jail.old_roles[player.name] = old_roles
- Roles.unassign_player(player,old_roles,by_player_name,true)
- Roles.assign_player(player,'Jail',by_player_name,true)
- event_emit(Jail.events.on_player_jailed,player,by_player_name)
- return #old_roles
-end
-
---- Unjails a player, this is only the logic there is no output to players
--- @tparam LuaPlayer player the player that will be unjailed, must be in jail
--- @tparam[opt=''] string string by_player_name the name of the player who is doing the action
--- @treturn the number of roles that were added, nil if there was an error
-function Jail.unjail_player(player,by_player_name)
- player = Game.get_player_from_any(player)
- if not player then return end
- if not Roles.player_has_role(player,'Jail') then return end
- local old_roles = Jail.old_roles[player.name]
- Roles.unassign_player(player,'Jail',by_player_name,true)
- Roles.assign_player(player,old_roles,by_player_name,true)
- event_emit(Jail.events.on_player_unjailed,player,by_player_name)
- return #old_roles
-end
-
---- Temp bans a player which is similar to jail but will store the reason for the action and clears items
--- @tparam LuaPlayer player the player that will be temp baned, must not be temp banned
--- @tparam[opt=''] string by_player_name the name of the player that is doing the action
--- @tparam[opt='None string Given.'] reason the reason that will be stored for this temp ban
--- @treturn boolean true if successful else will return nil
-function Jail.temp_ban_player(player,by_player_name,reason)
- player = Game.get_player_from_any(player)
- reason = reason or 'None Given.'
- if not player then return end
- if Jail.temp_bans[player.name] then return end
- Jail.jail_player(player,by_player_name)
- Jail.temp_bans[player.name] = {reason,by_player_name}
- local inv = player.get_main_inventory()
- move_items(inv.get_contents())
- inv.clear()
- event_emit(Jail.events.on_player_temp_banned,player,by_player_name,reason)
- return true
-end
-
---- Removes temp ban from a player, note this does not restore the items
--- @tparam LuaPlayer player the player that will be cleared from temp baned, must be temp banned
--- @tparam[opt=''] string by_player_name the name of the player that is doing the action
--- @treturn boolean true if successful else will return nil
-function Jail.clear_temp_ban_player(player,by_player_name)
- player = Game.get_player_from_any(player)
- if not player then return end
- if not Jail.temp_bans[player.name] then return end
- Jail.unjail_player(player,by_player_name)
- Jail.temp_bans[player.name] = nil
- event_emit(Jail.events.on_player_temp_ban_cleared,player,by_player_name)
- return true
-end
-
-return Jail
\ No newline at end of file
diff --git a/modules/addons/warnings-control.lua b/modules/addons/warnings-control.lua
deleted file mode 100644
index 44b07cfd..00000000
--- a/modules/addons/warnings-control.lua
+++ /dev/null
@@ -1,221 +0,0 @@
-local Game = require 'utils.game'
-local Global = require 'utils.global'
-local Event = require 'utils.event'
-local config = require 'config.warnings'
-local format_chat_player_name = ext_require('expcore.common','format_chat_player_name')
-require 'utils.table'
-
-local Warnings = {
- user_warnings={},
- user_temp_warnings={},
- events = {
- on_player_warned = script.generate_event_name(),
- on_player_warning_removed = script.generate_event_name(),
- on_temp_warning_added = script.generate_event_name(),
- on_temp_warning_removed = script.generate_event_name(),
- }
-}
-
-Global.register({
- user_warnings = Warnings.user_warnings,
- user_temp_warnings = Warnings.user_temp_warnings
-},function(tbl)
- Warnings.user_warnings = tbl.user_warnings
- Warnings.user_temp_warnings = tbl.user_temp_warnings
-end)
-
-local function event_emit(event,player,by_player_name)
- local warnings = Warnings.user_warnings[player.name] or {}
- local temp_warnings = Warnings.user_temp_warnings[player.name] or {}
- script.raise_event(event,{
- name=event,
- tick=game.tick,
- player_index=player.index,
- by_player_name=by_player_name,
- warning_count=#warnings,
- temp_warning_count=#temp_warnings
- })
-end
-
---- Adds X number (default 1) of warnings to a player from the given player
--- @tparam LuaPlayer player the player to add the warning to
--- @tparam[opt=''] string by_player_name the name of the player doing the action
--- @tparam[opt=1] number count the number of warnings to add
--- @treturn number the new number of warnings
-function Warnings.add_warnings(player,by_player_name,count)
- player = Game.get_player_from_any(player)
- if not player then return end
- count = count or 1
- by_player_name = by_player_name or ''
- local warnings = Warnings.user_warnings[player.name]
- if not warnings then
- Warnings.user_warnings[player.name] = {}
- warnings = Warnings.user_warnings[player.name]
- end
- for _=1,count do
- table.insert(warnings,by_player_name)
- event_emit(Warnings.events.on_player_warned,player,by_player_name)
- end
- return #warnings
-end
-
---- Removes X number (default 1) of warnings from a player, removes in order fifo
--- @tparam LuaPlayer player the player to remove the warnings from
--- @tparam[opt=''] string by_playey_name the name of the player doing the action
--- @tparam[opt=1] number count the number of warnings to remove (if greater than current warning count then all are removed)
--- @treturn number the new number of warnings
-function Warnings.remove_warnings(player,by_player_name,count)
- player = Game.get_player_from_any(player)
- if not player then return end
- count = count or 1
- by_player_name = by_player_name or ''
- local warnings = Warnings.user_warnings[player.name]
- if not warnings then return end
- for _=1,count do
- if #warnings == 0 then break end
- table.remove(warnings,1)
- event_emit(Warnings.events.on_player_warning_removed,player,by_player_name)
- end
- if #warnings == 0 then
- Warnings.user_warnings[player.name] = nil
- return 0
- end
- return #warnings
-end
-
---- Clears all warnings from a player, emits event multiple times as if remove_warnings was used
--- @tparam LuaPlayer player the player to clear the warnings of
--- @tparam[oot=''] string by_player_name the name of the player who is doing the action
--- @treturn boolean true if the warnings were cleared, nil if error
-function Warnings.clear_warnings(player,by_player_name)
- player = Game.get_player_from_any(player)
- if not player then return end
- local warnings = Warnings.user_warnings[player.name]
- if not warnings then return end
- by_player_name = by_player_name or ''
- for _=1,#warnings do
- event_emit(Warnings.events.on_player_warning_removed,player,by_player_name)
- end
- Warnings.user_warnings[player.name] = {}
- return true
-end
-
---- Gets the number of warnings that a player has, raw table will contain the names of who gave warnings
--- @tparam LuaPlayer player the player to get the warnings of
--- @tparam[opt=false] table table raw_table when true will return a which contains who gave warnings (the stored in global)
--- @treturn number the number of warnings a player has, a table if raw_table is true
-function Warnings.get_warnings(player,raw_table)
- player = Game.get_player_from_any(player)
- if not player then return end
- local warnings = Warnings.user_warnings[player.name] or {}
- if raw_table then
- return warnings
- else
- return #warnings
- end
-end
-
---- Adds a temp warning to a player that will timeout after some time, used for script given warnings (ie silent to outside players as a buffer)
--- @tparam LuaPlayer player the player to give the warnings to
--- @tparam[opt=1] number count the number of warnings to give to the player
--- @treturn number the new number of warnings
-function Warnings.add_temp_warnings(player,count)
- player = Game.get_player_from_any(player)
- if not player then return end
- count = count or 1
- local warnings = Warnings.user_temp_warnings[player.name]
- if not warnings then
- Warnings.user_temp_warnings[player.name] = {}
- warnings = Warnings.user_temp_warnings[player.name]
- end
- for _=1,count do
- table.insert(warnings,game.tick)
- event_emit(Warnings.events.on_temp_warning_added,player,'')
- end
- return #warnings
-end
-
--- temp warnings cant be removed on demand only after X amount of time
-local temp_warning_cool_down = config.temp_warning_cool_down*3600
-Event.on_nth_tick(temp_warning_cool_down/4,function()
- local check_time = game.tick-temp_warning_cool_down
- for player_name,temp_warnings in pairs(Warnings.user_temp_warnings) do
- local player = Game.get_player_from_any(player)
- for index,time in pairs(temp_warnings) do
- if time <= check_time then
- table.remove(temp_warnings,index)
- player.print{'warnings.script-warning-removed',#temp_warnings,config.temp_warning_limit}
- event_emit(Warnings.events.on_temp_warning_removed,player,'')
- end
- end
- if #temp_warnings == 0 then
- Warnings.user_temp_warnings[player_name] = nil
- end
- end
-end)
-
---- Clears all temp warnings from a player, emits events as if the warnings had been removed due to time
--- @tparam LuaPlayer player the player to clear the warnings of
--- @tparam[opt=''] string by_player_name the name of the player doing the action
--- @treturn boolean true if the warnings were cleared, nil for error
-function Warnings.clear_temp_warnings(player,by_player_name)
- player = Game.get_player_from_any(player)
- if not player then return end
- local warnings = Warnings.user_temp_warnings[player.name]
- if not warnings then return end
- by_player_name = by_player_name or ''
- for _=1,#warnings do
- event_emit(Warnings.events.on_temp_warning_removed,player,by_player_name)
- end
- Warnings.user_temp_warnings[player.name] = {}
- return true
-end
-
---- Gets the number of temp warnings, raw table is a table of when temp warnings were given
--- @tparam LuaPlayer player the player to get the warnings of
--- @tparam[opt=false] table raw_table if true will return a of ticks when warnings were added (the global table)
--- @treturn number the number of warnings which the player has, a table if raw_table is true
-function Warnings.get_temp_warnings(player,raw_table)
- player = Game.get_player_from_any(player)
- if not player then return end
- local warnings = Warnings.user_temp_warnings[player.name] or {}
- if raw_table then
- return warnings
- else
- return #warnings
- end
-end
-
--- when a player gets a warning the actions in config are ran
-Event.add(Warnings.events.on_player_warned,function(event)
- local action = config.actions[event.warning_count]
- if not action then return end
- local player = Game.get_player_by_index(event.player_index)
- if type(action) == 'function' then
- -- player: player who got the warnings,by_player_name: player who gave the last warning,number_of_warnings: the current number of warnings
- local success,err = pcall(action,player,event.by_player_name,event.warning_count)
- if not success then error(err) end
- elseif type(action) == 'table' then
- -- {locale,by_player_name,number_of_warning,...}
- local current_action = table.deep_copy(action)
- table.insert(current_action,2,event.by_player_name)
- table.insert(current_action,3,event.warning_count)
- player.print(current_action)
- elseif type(action) == 'string' then
- player.print(action)
- end
-end)
-
--- when a player gets a tempo warnings it is checked that it is not above the max
-Event.add(Warnings.events.on_temp_warning_added,function(event)
- local player = Game.get_player_by_index(event.player_index)
- if event.temp_warning_count > config.temp_warning_limit then
- Warnings.add_warnings(event.player_index,event.by_player_name)
- local player_name_color = format_chat_player_name(player)
- game.print{'warnings.script-warning-limit',player_name_color}
- else
- player.print{'warnings.script-warning',event.temp_warning_count,config.temp_warning_limit}
- end
-end)
-
-return Warnings
\ No newline at end of file
diff --git a/modules/commands/warnings.lua b/modules/commands/warnings.lua
index 1a86e4bd..9e453acf 100644
--- a/modules/commands/warnings.lua
+++ b/modules/commands/warnings.lua
@@ -1,5 +1,5 @@
local Commands = require 'expcore.commands'
-local WarningsControl = require 'modules.addons.warnings-control'
+local Warnings = require 'modules.addons.warnings'
local format_chat_player_name = ext_require('expcore.common','format_chat_player_name')
local config = require 'config.warnings'
require 'config.expcore-commands.parse_roles'
@@ -10,7 +10,7 @@ Commands.new_command('give-warning','Gives a warning to a player; may lead to au
:add_alias('warn')
:enable_auto_concat()
:register(function(player,action_player,reason,raw)
- WarningsControl.add_warnings(action_player,player.name)
+ Warnings.add_warning(action_player,player.name,reason)
local action_player_name_color = format_chat_player_name(action_player)
local by_player_name_color = format_chat_player_name(player)
game.print{'expcom-warnings.received',action_player_name_color,by_player_name_color,reason}
@@ -21,18 +21,18 @@ Commands.new_command('get-warnings','Gets the number of warnings a player has. I
:add_alias('warnings','list-warnings')
:register(function(player,action_player,raw)
if action_player then
- local warnings = WarningsControl.get_warnings(action_player)
- local script_warnings = WarningsControl.get_temp_warnings(action_player)
+ local warnings = Warnings.get_warnings(action_player)
+ local script_warnings = Warnings.get_script_warnings(action_player)
local action_player_name_color = format_chat_player_name(action_player)
Commands.print{'expcom-warnings.player',action_player_name_color,warnings,script_warnings,config.temp_warning_limit}
else
local rtn = {}
- local user_warnings = WarningsControl.user_warnings
- local user_temp_warnings = WarningsControl.user_temp_warnings
+ local user_warnings = Warnings.user_warnings
+ local user_script_warnings = Warnings.user_script_warnings
for player_name,warnings in pairs(user_warnings) do
rtn[player_name] = {#warnings,0}
end
- for player_name,warnings in pairs(user_temp_warnings) do
+ for player_name,warnings in pairs(user_script_warnings) do
if not rtn[player_name] then
rtn[player_name] = {0,0}
end
@@ -49,8 +49,8 @@ end)
Commands.new_command('clear-warnings','Clears all warnings (and script warnings) from a player')
:add_param('player',false,'player')
:register(function(player,action_player,raw)
- WarningsControl.clear_warnings(action_player,player.name)
- WarningsControl.clear_temp_warnings(action_player,player.name)
+ Warnings.clear_warnings(action_player,player.name)
+ Warnings.clear_script_warnings(action_player,player.name)
local action_player_name_color = format_chat_player_name(action_player)
local by_player_name_color = format_chat_player_name(player)
game.print{'expcom-warnings.cleared',action_player_name_color,by_player_name_color}
diff --git a/modules/control/jail.lua b/modules/control/jail.lua
index e299db30..e1b67018 100644
--- a/modules/control/jail.lua
+++ b/modules/control/jail.lua
@@ -1,5 +1,5 @@
--[[-- Control Module - Jail
- Adds a way to jail players and temp ban players.
+ - Adds a way to jail players and temp ban players.
@module Jail
@alias Jail
diff --git a/modules/control/reports.lua b/modules/control/reports.lua
index b9005e12..d869964c 100644
--- a/modules/control/reports.lua
+++ b/modules/control/reports.lua
@@ -1,5 +1,5 @@
--[[-- Control Module - Reports
- Adds a way to report players and store report messages.
+ - Adds a way to report players and store report messages.
@module Reports
@alias Reports
diff --git a/modules/control/warnings.lua b/modules/control/warnings.lua
new file mode 100644
index 00000000..a00bf878
--- /dev/null
+++ b/modules/control/warnings.lua
@@ -0,0 +1,309 @@
+--[[-- Control Module - Warnings
+ - Adds a way to give and remove warnings to players.
+ @module Warnings
+ @alias Warnings
+
+ @usage
+ -- This will add a warning to the player
+ Warnings.add_warning('MrBiter','Cooldude2606','Killed too many biters')
+
+ -- This will remove a warning from a player, second name is just who is doing the action
+ Warnings.remove_warning('MrBiter','Cooldude2606')
+
+ -- Script warning as similar to normal warning but are designed to have no effect for a short amount of time
+ -- this is so it can be used for greifer protection without being too agressive
+ Warnings.add_script_warning('MrBiter','Killed too many biters')
+
+ -- Both normal and script warnings can also be cleared, this will remove all warnings
+ Warnings.clear_warnings('MrBiter','Cooldude2606')
+]]
+
+local Event = require 'utils.event'
+local Game = require 'utils.game'
+local Global = require 'utils.global'
+local config = require 'config.warnings'
+
+local valid_player = Game.get_player_from_any
+
+local Warnings = {
+ user_warnings={},
+ user_script_warnings={},
+ events = {
+ --- When a warning is added to a player
+ -- @event on_warning_added
+ -- @tparam number player_index the index of the player who recived the warning
+ -- @tparam string by_player_name the name of the player who gave the warning
+ -- @tparam string reason the reason that the player was given a warning
+ -- @tparam number warning_count the new number of warnings that the player has
+ on_warning_added = script.generate_event_name(),
+ --- When a warning is removed from a player
+ -- @event on_warning_removed
+ -- @tparam number player_index the index of the player who is having the warning removed
+ -- @tparam string warning_by_name the name of the player who gave the warning
+ -- @tparam string removed_by_name the name of the player who is removing the warning
+ -- @tparam number warning_count the new number of warnings that the player has
+ on_warning_removed = script.generate_event_name(),
+ --- When a warning is added to a player, by the script
+ -- @event on_script_warning_added
+ -- @tparam number player_index the index of the player who recived the warning
+ -- @tparam string reason the reason that the player was given a warning
+ -- @tparam number warning_count the new number of warnings that the player has
+ on_script_warning_added = script.generate_event_name(),
+ --- When a warning is remnoved from a player, by the script
+ -- @event on_script_warning_removed
+ -- @tparam number player_index the index of the player who is having the warning removed
+ -- @tparam number warning_count the new number of warnings that the player has
+ on_script_warning_removed = script.generate_event_name(),
+ }
+}
+
+local user_warnings = Warnings.user_warnings
+local user_script_warnings = Warnings.user_script_warnings
+Global.register({
+ user_warnings = user_warnings,
+ user_script_warnings = user_script_warnings
+},function(tbl)
+ user_warnings = tbl.user_warnings
+ user_script_warnings = tbl.user_script_warnings
+end)
+
+--- Gets an array of warnings that the player has, always returns a list even if emtpy
+-- @tparam LuaPlayer player the player to get the warning for
+-- @treturn table an array of all the warnings on this player, contains tick, by_player_name and reason
+function Warnings.get_warnings(player)
+ return user_warnings[player.name] or {}
+end
+
+--- Gets the number of warnings that a player has on them
+-- @tparam LuaPlayer player the player to count the warnings for
+-- @treturn number the number of warnings that the player has
+function Warnings.count_warnings(player)
+ local warnings = user_warnings[player.name] or {}
+ return #warnings
+end
+
+--- Adds a warning to a player, when a warning is added a set action is done based on the number of warnings and the config file
+-- @tparam LuaPlayer player the player to add a warning to
+-- @tparam string by_player_name the name of the player who is doing the action
+-- @tparam[opt='Non given.'] string reason the reason that the player is being warned
+-- @treturn number the number of warnings that the player has
+function Warnings.add_warning(player,by_player_name,reason)
+ player = valid_player(player)
+ if not player then return end
+ if not by_player_name then return end
+
+ reason = reason or 'Non given.'
+
+ local warnings = user_warnings[player.name]
+ if not warnings then
+ warnings = {}
+ user_warnings[player.name] = warnings
+ end
+
+ table.insert(warnings,{
+ tick = game.tick,
+ by_player_name = by_player_name,
+ reason = reason
+ })
+
+ local warning_count = #warnings
+
+ script.raise_event(Warnings.on_warning_added,{
+ name = Warnings.on_warning_added,
+ tick = game.tick,
+ player_index = player.index,
+ warning_count = warning_count,
+ by_player_name = by_player_name,
+ reason = reason
+ })
+
+ local action = config.actions[#warnings]
+ if action then
+ local _type = type(action)
+ if _type == 'function' then
+ action(player,by_player_name,warning_count)
+ elseif _type == 'table' then
+ local current = table.deepcopy(action)
+ table.insert(current,2,by_player_name)
+ table.insert(current,3,warning_count)
+ player.print(current)
+ elseif type(action) == 'string' then
+ player.print(action)
+ end
+ end
+
+ return warning_count
+end
+
+--- Event trigger for removing a waring due to it being looped in clear warnings
+-- @tparam LuaPlayer player the player who is having a warning removed
+-- @tparam string warning_by_name the name of the player who made the warning
+-- @tparam string removed_by_name the name of the player who is doing the action
+-- @tparam number warning_count the number of warnings that the player how has
+local function warning_removed_event(player,warning_by_name,removed_by_name,warning_count)
+ script.raise_event(Warnings.on_warning_removed,{
+ name = Warnings.on_warning_removed,
+ tick = game.tick,
+ player_index = player.index,
+ warning_count = warning_count,
+ warning_by_name = warning_by_name,
+ removed_by_name = removed_by_name
+ })
+end
+
+--- Removes a warning from a player, always removes the earlyist warning, fifo
+-- @tparam LuaPlayer player the player to remove a warning from
+-- @tparam string by_player_name the name of the player who is doing the action
+-- @treturn number the number of warnings that the player has
+function Warnings.remove_warning(player,by_player_name)
+ player = valid_player(player)
+ if not player then return end
+ if not by_player_name then return end
+
+ local warnings = user_warnings[player.name]
+ if not warnings then return end
+
+ local warning = table.remove(warnings,1)
+
+ warning_removed_event(player,warning.by_player_name,by_player_name,#warnings)
+
+ return #warnings
+end
+
+--- Removes all warnings from a player, will trigger remove event for each warning
+-- @tparam LuaPlayer player the player to clear the warnings from
+-- @tparam string by_player_name the name of the player who is doing the action
+-- @treturn boolean true when warnings were cleared succesfully
+function Warnings.clear_warnings(player,by_player_name)
+ player = valid_player(player)
+ if not player then return end
+ if not by_player_name then return end
+
+ local warnings = user_warnings[player.name]
+ if not warnings then return end
+
+ local warning_count = #warnings
+ for n,warning in pairs(warnings) do
+ warning_removed_event(player,warning.by_player_name,by_player_name,warning_count-n)
+ end
+
+ user_warnings[player.name] = nil
+ return true
+end
+
+--- Gets an array of all the script warnings that a player has
+-- @tparam LuaPlayer player the player to get the script warnings of
+-- @treturn table a table of all the script warnings a player has, contains tick and reason
+function Warnings.get_script_warnings(player)
+ return user_script_warnings[player.name] or {}
+end
+
+--- Gets the number of script warnings that a player has on them
+-- @tparam LuaPlayer player the player to count the script warnings of
+-- @treturn number the number of script warnings that the player has
+function Warnings.count_script_warnings(player)
+ local warnings = user_script_warnings[player.name] or {}
+ return #warnings
+end
+
+--- Adds a script warning to a player, this may add a full warning if max script warnings is met
+-- @tparam LuaPlayer player the player to add a script warning to
+-- @tparam[opt='Non given.'] string reason the reason that the player is being warned
+-- @treturn number the number of script warnings that the player has
+function Warnings.add_script_warning(player,reason)
+ player = valid_player(player)
+ if not player then return end
+
+ reason = reason or 'Non given.'
+
+ local warnings = user_script_warnings[player.name]
+ if not warnings then
+ warnings = {}
+ user_script_warnings[player.name] = warnings
+ end
+
+ table.insert(warnings,{
+ tick = game.tick,
+ reason = reason
+ })
+
+ local warning_count = #warnings
+
+ script.raise_event(Warnings.on_script_warning_added,{
+ name = Warnings.on_script_warning_added,
+ tick = game.tick,
+ player_index = player.index,
+ warning_count = warning_count,
+ reason = reason
+ })
+
+ if warning_count > config.script_warning_limit then
+ Warnings.add_warning(player,'',reason)
+ end
+
+ return warning_count
+end
+
+--- Script warning removed event tigger due to it being looped in clear script warnings
+-- @tparam LuaPlayer player the player who is having a script warning removed
+-- @tparam number warning_count the number of warning that the player has
+local function script_warning_removed_event(player,warning_count)
+ script.raise_event(Warnings.on_script_warning_removed,{
+ name = Warnings.on_script_warning_removed,
+ tick = game.tick,
+ player_index = player.index,
+ warning_count = warning_count
+ })
+end
+
+--- Removes a script warning from a player
+-- @tparam LuaPlayer player the player to remove a script warning from
+-- @treturn number the number of script warnings that the player has
+function Warnings.remove_script_warning(player)
+ player = valid_player(player)
+ if not player then return end
+
+ local warnings = user_script_warnings[player.name]
+ if not warnings then return end
+
+ table.remove(warnings,1)
+
+ script_warning_removed_event(player)
+
+ return #warnings
+end
+
+--- Removes all script warnings from a player, emits event for each warning removed
+-- @tparam LuaPlayer player the player to clear the script warnings from
+function Warnings.clear_script_warnings(player)
+ player = valid_player(player)
+ if not player then return end
+
+ local warnings = user_script_warnings[player.name]
+ if not warnings then return end
+
+ local warning_count = #warnings
+ for n,_ in pairs(warnings) do
+ script_warning_removed_event(player,warning_count-n)
+ end
+
+ user_script_warnings[player.name] = nil
+ return true
+end
+
+-- script warnings are removed after a certain amount of time to make them even more lienient
+local script_warning_cool_down = config.script_warning_cool_down*3600
+Event.on_nth_tick(script_warning_cool_down/4,function()
+ local cutoff = game.tick - script_warning_cool_down
+ for player_name,script_warnings in pairs(user_script_warnings) do
+ if #script_warnings > 0 then
+ for _,warning in pairs(script_warnings) do
+ if warning.tick < cutoff then
+ Warnings.remove_script_warning(player_name)
+ end
+ end
+ end
+ end
+end)
+
+return Warnings
\ No newline at end of file