2018-11-18 15:18:43 +02:00
--antigrief things made by mewmew
2020-07-11 09:32:36 +02:00
--rewritten by gerkiz--
2019-07-07 11:52:12 +02:00
--as an admin, write either /trust or /untrust and the players name in the chat to grant/revoke immunity from protection
2018-11-18 15:18:43 +02:00
2020-06-29 15:50:01 +02:00
local Event = require ' utils.event '
2021-07-20 22:17:49 +02:00
local Session = require ' utils.datastore.session_data '
2020-06-29 15:50:01 +02:00
local Global = require ' utils.global '
2020-07-07 16:30:17 +02:00
local Utils = require ' utils.core '
2020-07-10 16:08:50 +02:00
local Color = require ' utils.color_presets '
2020-07-07 16:30:17 +02:00
local Server = require ' utils.server '
2020-08-14 22:07:54 +02:00
local Jail = require ' utils.datastore.jail_data '
2021-04-30 20:16:19 +02:00
local FancyTime = require ' tools.fancy_time '
2018-09-22 16:37:48 +02:00
2020-06-29 15:50:01 +02:00
local Public = { }
2020-07-07 16:30:17 +02:00
local match = string.match
2020-07-10 16:08:50 +02:00
local capsule_bomb_threshold = 8
2020-07-30 22:27:19 +02:00
local de = defines.events
2020-07-10 16:08:50 +02:00
local format = string.format
2018-09-24 06:43:15 +02:00
2020-06-29 15:50:01 +02:00
local this = {
2020-08-22 17:20:59 +02:00
enabled = true ,
2020-07-07 23:42:44 +02:00
landfill_history = { } ,
capsule_history = { } ,
friendly_fire_history = { } ,
mining_history = { } ,
2021-05-09 17:11:18 +02:00
whitelist_mining_history = { } ,
2020-07-07 23:42:44 +02:00
corpse_history = { } ,
2021-05-09 17:11:18 +02:00
message_history = { } ,
2020-07-07 23:42:44 +02:00
cancel_crafting_history = { } ,
2020-06-29 15:50:01 +02:00
whitelist_types = { } ,
2020-07-30 22:27:19 +02:00
permission_group_editing = { } ,
2020-07-10 16:08:50 +02:00
players_warned = { } ,
2020-07-30 19:09:18 +02:00
damage_history = { } ,
2020-07-26 17:14:57 +02:00
punish_cancel_craft = false ,
2020-07-10 16:08:50 +02:00
do_not_check_trusted = true ,
2020-07-30 17:47:50 +02:00
enable_autokick = false ,
2020-07-26 22:06:19 +02:00
enable_autoban = false ,
2020-07-30 17:47:50 +02:00
enable_jail = false ,
2020-07-28 16:48:55 +02:00
enable_capsule_warning = false ,
2020-07-30 22:27:19 +02:00
enable_capsule_cursor_warning = false ,
required_playtime = 2592000 ,
damage_entity_threshold = 20 ,
2021-07-20 22:17:49 +02:00
explosive_threshold = 16 ,
limit = 2000
2020-06-29 20:08:18 +02:00
}
local blacklisted_types = {
[ ' transport-belt ' ] = true ,
[ ' wall ' ] = true ,
[ ' underground-belt ' ] = true ,
[ ' inserter ' ] = true ,
[ ' land-mine ' ] = true ,
[ ' gate ' ] = true ,
[ ' lamp ' ] = true ,
[ ' mining-drill ' ] = true ,
[ ' splitter ' ] = true
}
local ammo_names = {
2020-07-10 16:08:50 +02:00
[ ' artillery-targeting-remote ' ] = true ,
2020-06-29 20:08:18 +02:00
[ ' poison-capsule ' ] = true ,
[ ' cluster-grenade ' ] = true ,
[ ' grenade ' ] = true ,
[ ' atomic-bomb ' ] = true ,
2020-07-07 16:30:17 +02:00
[ ' cliff-explosives ' ] = true ,
[ ' rocket ' ] = true
2020-06-29 15:50:01 +02:00
}
2018-11-18 15:18:43 +02:00
2020-07-29 19:30:45 +02:00
local chests = {
[ ' container ' ] = true ,
[ ' logistic-container ' ] = true
}
2020-06-29 15:50:01 +02:00
Global.register (
this ,
function ( t )
this = t
end
)
2018-11-18 15:18:43 +02:00
2020-07-30 19:09:18 +02:00
local function increment ( t , v )
t [ # t + 1 ] = ( v or 1 )
end
2020-07-07 23:42:44 +02:00
2020-07-10 16:08:50 +02:00
local function get_entities ( item_name , entities )
local set = { }
for i = 1 , # entities do
local e = entities [ i ]
local name = e.name
if name ~= item_name and name ~= ' entity-ghost ' then
local count = set [ name ]
if count then
set [ name ] = count + 1
else
set [ name ] = 1
end
end
end
local list = { }
local i = 1
for k , v in pairs ( set ) do
list [ i ] = v
i = i + 1
list [ i ] = ' '
i = i + 1
list [ i ] = k
i = i + 1
list [ i ] = ' , '
i = i + 1
end
list [ i - 1 ] = nil
return table.concat ( list )
end
2020-07-11 17:04:53 +02:00
local function damage_player ( player , kill , print_to_all )
2020-07-10 16:08:50 +02:00
local msg = ' tried to destroy our base, but it backfired! '
2020-07-07 16:30:17 +02:00
if player.character then
2020-07-10 16:08:50 +02:00
if kill then
player.character . die ( ' enemy ' )
2020-07-11 17:04:53 +02:00
if print_to_all then
game.print ( player.name .. msg , Color.yellow )
end
2020-07-10 16:08:50 +02:00
return
end
2020-07-07 16:30:17 +02:00
player.character . health = player.character . health - math.random ( 50 , 100 )
player.character . surface.create_entity ( { name = ' water-splash ' , position = player.position } )
local messages = {
' Ouch.. That hurt! Better be careful now. ' ,
' Just a fleshwound. ' ,
' Better keep those hands to yourself or you might loose them. '
}
2020-07-10 16:08:50 +02:00
player.print ( messages [ math.random ( 1 , # messages ) ] , Color.yellow )
2020-07-07 16:30:17 +02:00
if player.character . health <= 0 then
player.character . die ( ' enemy ' )
2020-07-10 16:08:50 +02:00
game.print ( player.name .. msg , Color.yellow )
2020-07-07 16:30:17 +02:00
return
end
end
end
2020-07-11 17:04:53 +02:00
local function do_action ( player , prefix , msg , ban_msg , kill )
if not prefix or not msg or not ban_msg then
return
end
kill = kill or false
damage_player ( player , kill )
Utils.action_warning ( prefix , msg )
if this.players_warned [ player.index ] == 2 then
if this.enable_autoban then
Server.ban_sync ( player.name , ban_msg , ' <script> ' )
end
elseif this.players_warned [ player.index ] == 1 then
this.players_warned [ player.index ] = 2
2020-07-30 17:47:50 +02:00
if this.enable_jail then
Jail.try_ul_data ( player , true , ' script ' )
elseif this.enable_autokick then
2020-07-11 17:04:53 +02:00
game.kick_player ( player , msg )
end
else
this.players_warned [ player.index ] = 1
end
end
2018-09-22 16:37:48 +02:00
local function on_marked_for_deconstruction ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2021-07-20 22:17:49 +02:00
2020-05-19 23:00:52 +02:00
if not event.player_index then
return
end
2021-07-20 22:17:49 +02:00
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2021-07-20 22:17:49 +02:00
if Session.get_trusted_player ( player.name ) or this.do_not_check_trusted then
2020-05-19 23:00:52 +02:00
return
end
2019-07-07 11:52:12 +02:00
2020-05-19 23:00:52 +02:00
local playtime = player.online_time
2021-07-20 22:17:49 +02:00
if Session.get_session_player ( player.name ) then
playtime = player.online_time + Session.get_session_player ( player.name )
2020-05-19 23:00:52 +02:00
end
if playtime < 2592000 then
2020-07-30 22:27:19 +02:00
event.entity . cancel_deconstruction ( game.get_player ( event.player_index ) . force.name )
2020-05-19 23:00:52 +02:00
player.print ( ' You have not grown accustomed to this technology yet. ' , { r = 0.22 , g = 0.99 , b = 0.99 } )
end
2018-09-22 16:37:48 +02:00
end
2018-09-23 03:17:31 +02:00
local function on_player_ammo_inventory_changed ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2021-07-20 22:17:49 +02:00
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-06-29 20:08:18 +02:00
if player.admin then
2020-05-19 23:00:52 +02:00
return
end
2021-07-20 22:17:49 +02:00
if Session.get_trusted_player ( player.name ) or this.do_not_check_trusted then
2020-05-19 23:00:52 +02:00
return
end
2019-07-07 11:52:12 +02:00
2020-06-05 18:00:57 +02:00
local playtime = player.online_time
2021-07-20 22:17:49 +02:00
if Session.get_session_player ( player.name ) then
playtime = player.online_time + Session.get_session_player ( player.name )
2020-06-05 18:00:57 +02:00
end
if playtime < 1296000 then
2020-08-14 22:07:54 +02:00
if this.enable_capsule_cursor_warning then
2020-07-26 22:06:19 +02:00
local nukes = player.remove_item ( { name = ' atomic-bomb ' , count = 1000 } )
if nukes > 0 then
2021-07-23 17:23:33 +02:00
Utils.action_warning ( ' [Nuke] ' , player.name .. ' tried to equip nukes but was not trusted. ' )
2020-07-26 22:06:19 +02:00
damage_player ( player )
end
2020-06-05 18:00:57 +02:00
end
end
2018-09-23 03:17:31 +02:00
end
2020-07-07 16:30:17 +02:00
local function on_player_joined_game ( event )
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-08-22 17:20:59 +02:00
if not this.enabled then
2021-07-20 22:17:49 +02:00
if not Session.get_trusted_player ( player.name ) then
Session.set_trusted_player ( player.name )
2020-08-22 17:20:59 +02:00
end
return
end
2020-07-07 16:30:17 +02:00
if match ( player.name , ' ^[Ili1|]+$ ' ) then
Server.ban_sync ( player.name , ' ' , ' <script> ' ) -- No reason given, to not give them any hints to change their name
end
end
2018-09-23 07:40:37 +02:00
local function on_player_built_tile ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-05-19 23:00:52 +02:00
local placed_tiles = event.tiles
2021-03-24 17:46:00 +02:00
if placed_tiles [ 1 ] . old_tile.name ~= ' deepwater ' and placed_tiles [ 1 ] . old_tile.name ~= ' water ' and placed_tiles [ 1 ] . old_tile.name ~= ' water-green ' then
2020-05-19 23:00:52 +02:00
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2019-07-07 11:52:12 +02:00
2020-07-17 16:59:15 +02:00
local surface = event.surface_index
2020-07-11 17:04:53 +02:00
2020-06-05 18:00:57 +02:00
--landfill history--
2020-06-29 15:50:01 +02:00
2020-07-30 19:09:18 +02:00
if not this.landfill_history then
this.landfill_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.landfill_history > this.limit then
2020-07-30 19:09:18 +02:00
this.landfill_history = { }
2020-06-05 18:00:57 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-05 18:00:57 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' at X: '
str = str .. placed_tiles [ 1 ] . position.x
str = str .. ' Y: '
str = str .. placed_tiles [ 1 ] . position.y
2020-06-29 15:50:01 +02:00
str = str .. ' '
2020-07-11 17:04:53 +02:00
str = str .. ' surface: ' .. surface
2020-07-30 19:09:18 +02:00
increment ( this.landfill_history , str )
2018-09-23 07:40:37 +02:00
end
2018-11-03 19:34:29 +02:00
local function on_built_entity ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2021-07-20 22:17:49 +02:00
2020-05-19 23:00:52 +02:00
if game.tick < 1296000 then
return
end
if event.created_entity . type == ' entity-ghost ' then
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-05-19 23:00:52 +02:00
2020-06-29 20:08:18 +02:00
if player.admin then
2020-05-19 23:00:52 +02:00
return
end
2021-07-20 22:17:49 +02:00
if Session.get_trusted_player ( player.name ) or this.do_not_check_trusted then
2020-05-19 23:00:52 +02:00
return
end
local playtime = player.online_time
2021-07-20 22:17:49 +02:00
if Session.get_session_player ( player.name ) then
playtime = player.online_time + Session.get_session_player ( player.name )
2020-05-19 23:00:52 +02:00
end
if playtime < 432000 then
event.created_entity . destroy ( )
player.print ( ' You have not grown accustomed to this technology yet. ' , { r = 0.22 , g = 0.99 , b = 0.99 } )
end
end
2018-11-03 19:34:29 +02:00
end
2020-06-29 15:50:01 +02:00
--Capsule History and Antigrief
2018-11-25 17:04:57 +02:00
local function on_player_used_capsule ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2021-07-20 22:17:49 +02:00
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-07-28 16:48:55 +02:00
2021-07-20 22:17:49 +02:00
if Session.get_trusted_player ( player.name ) or this.do_not_check_trusted then
2020-06-29 20:08:18 +02:00
return
end
2020-07-28 16:48:55 +02:00
2020-06-29 15:50:01 +02:00
local item = event.item
if not item then
2020-06-05 18:00:57 +02:00
return
end
2020-06-29 15:50:01 +02:00
local name = item.name
2020-07-10 16:08:50 +02:00
local position = event.position
local x , y = position.x , position.y
local surface = player.surface
2020-06-29 20:08:18 +02:00
if ammo_names [ name ] then
2020-07-28 16:48:55 +02:00
local msg
2020-07-26 22:06:19 +02:00
if this.enable_capsule_warning then
2020-11-23 23:10:32 +02:00
if surface.count_entities_filtered ( { force = ' enemy ' , area = { { x - 10 , y - 10 } , { x + 10 , y + 10 } } , limit = 1 } ) > 0 then
2020-07-28 16:48:55 +02:00
return
end
2020-07-26 22:06:19 +02:00
local count = 0
2020-11-23 23:10:32 +02:00
local entities = player.surface . find_entities_filtered { force = player.force , area = { { x - 5 , y - 5 } , { x + 5 , y + 5 } } }
2020-07-26 22:06:19 +02:00
for i = 1 , # entities do
local e = entities [ i ]
local entity_name = e.name
if entity_name ~= name and entity_name ~= ' entity-ghost ' and not blacklisted_types [ e.type ] then
count = count + 1
end
2020-07-10 16:08:50 +02:00
end
2020-07-26 22:06:19 +02:00
if count <= capsule_bomb_threshold then
return
end
2020-07-10 16:08:50 +02:00
2021-07-23 17:23:33 +02:00
local prefix = ' [Capsule] '
2020-07-26 22:06:19 +02:00
msg = format ( player.name .. ' damaged: %s with: %s ' , get_entities ( name , entities ) , name )
2021-03-24 17:46:00 +02:00
local ban_msg = format ( ' Damaged: %s with: %s. This action was performed automatically. Visit getcomfy.eu/discord for forgiveness ' , get_entities ( name , entities ) , name )
2020-07-11 17:04:53 +02:00
2020-07-26 22:06:19 +02:00
do_action ( player , prefix , msg , ban_msg , true )
2020-07-28 16:48:55 +02:00
else
msg = player.name .. ' used ' .. name
2020-07-26 22:06:19 +02:00
end
2020-07-10 16:08:50 +02:00
2020-07-30 19:09:18 +02:00
if not this.capsule_history then
this.capsule_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.capsule_history > this.limit then
2020-07-30 19:09:18 +02:00
this.capsule_history = { }
2020-06-29 15:50:01 +02:00
end
2020-06-29 20:08:18 +02:00
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 20:08:18 +02:00
local str = ' [ ' .. t .. ' ] '
2020-07-10 16:08:50 +02:00
str = str .. msg
2020-06-29 20:08:18 +02:00
str = str .. ' at X: '
str = str .. math.floor ( position.x )
str = str .. ' Y: '
str = str .. math.floor ( position.y )
str = str .. ' '
str = str .. ' surface: ' .. player.surface . index
2020-07-30 19:09:18 +02:00
increment ( this.capsule_history , str )
2020-06-05 18:00:57 +02:00
end
2018-11-25 17:04:57 +02:00
end
2018-12-02 06:47:58 +02:00
--Friendly Fire History
local function on_entity_died ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-06-29 15:50:01 +02:00
local cause = event.cause
local name
2020-06-05 18:00:57 +02:00
2021-03-24 17:46:00 +02:00
if ( cause and cause.name == ' character ' and cause.player and cause.force . name == event.entity . force.name and not blacklisted_types [ event.entity . type ] ) then
2020-06-29 15:50:01 +02:00
local player = cause.player
name = player.name
2020-07-30 19:09:18 +02:00
if not this.friendly_fire_history then
this.friendly_fire_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.friendly_fire_history > this.limit then
2020-07-30 19:09:18 +02:00
this.friendly_fire_history = { }
2020-06-29 15:50:01 +02:00
end
2020-07-29 19:30:45 +02:00
local chest
if chests [ event.entity . type ] then
local entity = event.entity
local inv = entity.get_inventory ( 1 )
local contents = inv.get_contents ( )
local item_types = ' '
for n , count in pairs ( contents ) do
if n == ' explosives ' then
2020-07-30 19:09:18 +02:00
item_types = item_types .. ' [color=yellow] ' .. n .. ' [/color] count: ' .. count .. ' '
2020-07-29 19:30:45 +02:00
end
end
2020-07-30 19:09:18 +02:00
if string.len ( item_types ) > 0 then
chest = event.entity . name .. ' with content ' .. item_types
else
chest = ' [color=yellow] ' .. event.entity . name .. ' [/color] '
end
2020-07-29 19:30:45 +02:00
else
2020-07-30 19:09:18 +02:00
chest = ' [color=yellow] ' .. event.entity . name .. ' [/color] '
2020-07-29 19:30:45 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 15:50:01 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. name .. ' destroyed '
2020-07-29 19:30:45 +02:00
str = str .. chest
2020-06-29 15:50:01 +02:00
str = str .. ' at X: '
str = str .. math.floor ( event.entity . position.x )
str = str .. ' Y: '
str = str .. math.floor ( event.entity . position.y )
str = str .. ' '
str = str .. ' surface: ' .. event.entity . surface.index
2020-07-30 19:09:18 +02:00
increment ( this.friendly_fire_history , str )
2020-06-29 15:50:01 +02:00
elseif not blacklisted_types [ event.entity . type ] and this.whitelist_types [ event.entity . type ] then
if cause then
if cause.force . name ~= ' player ' then
return
end
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 15:50:01 +02:00
local str = ' [ ' .. t .. ' ] '
if cause and cause.name == ' character ' and cause.player then
str = str .. cause.player . name .. ' destroyed '
else
str = str .. ' someone destroyed '
end
2020-07-30 19:09:18 +02:00
str = str .. ' [color=yellow] ' .. event.entity . name .. ' [/color] '
2020-06-29 15:50:01 +02:00
str = str .. ' at X: '
str = str .. math.floor ( event.entity . position.x )
str = str .. ' Y: '
str = str .. math.floor ( event.entity . position.y )
str = str .. ' '
str = str .. ' surface: ' .. event.entity . surface.index
if cause and cause.name == ' character ' and cause.player then
2020-07-30 19:09:18 +02:00
increment ( this.friendly_fire_history , str )
2020-06-29 15:50:01 +02:00
else
2020-07-30 19:09:18 +02:00
increment ( this.friendly_fire_history , str )
2020-06-29 15:50:01 +02:00
end
end
2018-12-02 06:47:58 +02:00
end
2018-12-02 07:39:54 +02:00
--Mining Thieves History
local function on_player_mined_entity ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-07-11 17:04:53 +02:00
if not player or not player.valid then
return
end
2020-06-29 15:50:01 +02:00
2020-07-11 17:04:53 +02:00
local entity = event.entity
if not entity or not entity.valid then
2020-06-29 15:50:01 +02:00
return
end
2020-07-11 17:04:53 +02:00
if this.whitelist_types [ entity.type ] then
2021-05-09 17:11:18 +02:00
if not this.whitelist_mining_history then
this.whitelist_mining_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.whitelist_mining_history > this.limit then
2021-05-09 17:11:18 +02:00
this.whitelist_mining_history = { }
2020-06-29 15:50:01 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 15:50:01 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' mined '
2020-07-30 19:09:18 +02:00
str = str .. ' [color=yellow] ' .. entity.name .. ' [/color] '
2020-06-29 15:50:01 +02:00
str = str .. ' at X: '
2020-07-11 17:04:53 +02:00
str = str .. math.floor ( entity.position . x )
2020-06-29 15:50:01 +02:00
str = str .. ' Y: '
2020-07-11 17:04:53 +02:00
str = str .. math.floor ( entity.position . y )
2020-06-29 15:50:01 +02:00
str = str .. ' '
2020-07-11 17:04:53 +02:00
str = str .. ' surface: ' .. entity.surface . index
2021-05-09 17:11:18 +02:00
increment ( this.whitelist_mining_history , str )
2020-06-29 15:50:01 +02:00
return
end
2020-07-11 17:04:53 +02:00
if not entity.last_user then
2020-06-05 18:00:57 +02:00
return
end
2020-07-11 17:04:53 +02:00
if entity.last_user . name == player.name then
2020-06-05 18:00:57 +02:00
return
end
2020-07-11 17:04:53 +02:00
if entity.force . name ~= player.force . name then
2020-06-05 18:00:57 +02:00
return
end
if blacklisted_types [ event.entity . type ] then
return
end
2020-07-30 19:09:18 +02:00
if not this.mining_history then
this.mining_history = { }
2020-07-07 16:30:17 +02:00
end
2020-06-05 18:00:57 +02:00
2021-07-20 22:17:49 +02:00
if # this.mining_history > this.limit then
2020-07-30 19:09:18 +02:00
this.mining_history = { }
2020-06-05 18:00:57 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-05 18:00:57 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' mined '
2020-07-30 19:09:18 +02:00
str = str .. ' [color=yellow] ' .. event.entity . name .. ' [/color] '
2020-06-05 18:00:57 +02:00
str = str .. ' at X: '
str = str .. math.floor ( event.entity . position.x )
str = str .. ' Y: '
str = str .. math.floor ( event.entity . position.y )
2020-06-29 15:50:01 +02:00
str = str .. ' '
str = str .. ' surface: ' .. event.entity . surface.index
2020-07-30 19:09:18 +02:00
increment ( this.mining_history , str )
2018-12-02 07:39:54 +02:00
end
2018-11-26 17:50:30 +02:00
local function on_gui_opened ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-05-19 23:00:52 +02:00
if not event.entity then
return
end
if event.entity . name ~= ' character-corpse ' then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
local corpse_owner = game.get_player ( event.entity . character_corpse_player_index )
2020-05-19 23:00:52 +02:00
if not corpse_owner then
return
end
2020-06-29 15:50:01 +02:00
2020-05-19 23:00:52 +02:00
if corpse_owner.force . name ~= player.force . name then
return
end
2020-06-29 15:50:01 +02:00
local corpse_content = # event.entity . get_inventory ( defines.inventory . character_corpse )
if corpse_content <= 0 then
return
end
2020-05-19 23:00:52 +02:00
if player.name ~= corpse_owner.name then
2021-07-23 17:23:33 +02:00
Utils.action_warning ( ' [Corpse] ' , player.name .. ' is looting ' .. corpse_owner.name .. ' ´s body. ' )
2020-07-30 19:09:18 +02:00
if not this.corpse_history then
this.corpse_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.corpse_history > this.limit then
2020-07-30 19:09:18 +02:00
this.corpse_history = { }
2020-06-29 15:50:01 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 15:50:01 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' opened '
2020-07-30 19:09:18 +02:00
str = str .. ' [color=yellow] ' .. corpse_owner.name .. ' [/color] body '
2020-06-29 15:50:01 +02:00
str = str .. ' at X: '
str = str .. math.floor ( event.entity . position.x )
str = str .. ' Y: '
str = str .. math.floor ( event.entity . position.y )
str = str .. ' '
str = str .. ' surface: ' .. event.entity . surface.index
2020-07-30 19:09:18 +02:00
increment ( this.corpse_history , str )
2020-05-19 23:00:52 +02:00
end
2018-11-26 17:50:30 +02:00
end
local function on_pre_player_mined_item ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-07-11 17:04:53 +02:00
if not player or not player.valid then
2020-05-19 23:00:52 +02:00
return
end
2020-07-11 17:04:53 +02:00
2020-05-19 23:00:52 +02:00
local entity = event.entity
2020-07-11 17:04:53 +02:00
if not entity or not entity.valid then
return
end
if entity.name ~= ' character-corpse ' then
2020-05-19 23:00:52 +02:00
return
end
2020-07-11 17:04:53 +02:00
2020-07-30 22:27:19 +02:00
local corpse_owner = game.get_player ( entity.character_corpse_player_index )
2020-07-11 17:04:53 +02:00
if not corpse_owner then
return
end
2020-05-19 23:00:52 +02:00
local corpse_content = # entity.get_inventory ( defines.inventory . character_corpse )
if corpse_content <= 0 then
return
end
if corpse_owner.force . name ~= player.force . name then
return
end
if player.name ~= corpse_owner.name then
2021-07-23 17:23:33 +02:00
Utils.action_warning ( ' [Corpse] ' , player.name .. ' has looted ' .. corpse_owner.name .. ' ´s body. ' )
2020-07-30 19:09:18 +02:00
if not this.corpse_history then
this.corpse_history = { }
2020-07-07 16:30:17 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.corpse_history > this.limit then
2020-07-30 19:09:18 +02:00
this.corpse_history = { }
2020-06-29 15:50:01 +02:00
end
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-06-29 15:50:01 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' mined '
2020-07-30 19:09:18 +02:00
str = str .. ' [color=yellow] ' .. corpse_owner.name .. ' [/color] body '
2020-06-29 15:50:01 +02:00
str = str .. ' at X: '
2020-07-11 17:04:53 +02:00
str = str .. math.floor ( entity.position . x )
2020-06-29 15:50:01 +02:00
str = str .. ' Y: '
2020-07-11 17:04:53 +02:00
str = str .. math.floor ( entity.position . y )
2020-06-29 15:50:01 +02:00
str = str .. ' '
2020-07-11 17:04:53 +02:00
str = str .. ' surface: ' .. entity.surface . index
2020-07-30 19:09:18 +02:00
increment ( this.corpse_history , str )
2020-05-19 23:00:52 +02:00
end
2018-11-26 17:50:30 +02:00
end
2021-05-07 01:19:38 +02:00
local function on_console_chat ( event )
if not event.player_index then
return
end
local player = game.get_player ( event.player_index )
if not this.message_history then
this.message_history = { }
end
2021-07-20 22:17:49 +02:00
if # this.message_history > this.limit then
2021-05-07 01:19:38 +02:00
this.message_history = { }
end
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
local message = event.message
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' said: '
str = str .. ' [color=yellow] ' .. message .. ' [/color] '
increment ( this.message_history , str )
end
2020-06-29 15:50:01 +02:00
local function on_player_cursor_stack_changed ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2021-07-20 22:17:49 +02:00
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-06-29 20:08:18 +02:00
if player.admin then
return
end
2021-07-20 22:17:49 +02:00
if Session.get_trusted_player ( player.name ) or this.do_not_check_trusted then
2020-06-29 20:08:18 +02:00
return
end
2020-06-29 15:50:01 +02:00
2020-06-29 20:08:18 +02:00
local item = player.cursor_stack
2020-06-29 15:50:01 +02:00
2020-06-29 20:08:18 +02:00
if not item then
return
end
2020-06-29 15:50:01 +02:00
2020-06-29 20:08:18 +02:00
if not item.valid_for_read then
return
end
local name = item.name
local playtime = player.online_time
2021-07-20 22:17:49 +02:00
if Session.get_session_player ( player.name ) then
playtime = player.online_time + Session.get_session_player ( player.name )
2020-06-29 20:08:18 +02:00
end
2020-06-29 15:50:01 +02:00
2020-06-29 20:08:18 +02:00
if playtime < 1296000 then
2020-07-28 16:48:55 +02:00
if this.enable_capsule_cursor_warning then
2020-07-26 22:06:19 +02:00
if ammo_names [ name ] then
local item_to_remove = player.remove_item ( { name = name , count = 1000 } )
if item_to_remove > 0 then
2021-07-23 17:23:33 +02:00
Utils.action_warning ( ' [Capsule] ' , player.name .. ' equipped ' .. name .. ' but was not trusted. ' )
2020-07-26 22:06:19 +02:00
damage_player ( player )
end
2020-06-29 20:08:18 +02:00
end
end
end
2020-06-29 15:50:01 +02:00
end
2020-07-10 16:08:50 +02:00
2020-07-06 23:22:21 +02:00
local function on_player_cancelled_crafting ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
2020-07-06 23:22:21 +02:00
2020-07-21 13:00:13 +02:00
local crafting_queue_item_count = event.items . get_item_count ( )
2020-07-26 17:14:57 +02:00
local free_slots = player.get_main_inventory ( ) . count_empty_stacks ( )
local crafted_items = # event.items
if crafted_items > free_slots then
if this.punish_cancel_craft then
player.character . character_inventory_slots_bonus = crafted_items + # player.get_main_inventory ( )
for i = 1 , crafted_items do
player.character . get_main_inventory ( ) . insert ( event.items [ i ] )
end
2020-07-21 08:40:32 +02:00
2020-07-26 17:14:57 +02:00
player.character . die ( ' player ' )
Utils.action_warning (
2021-07-23 17:23:33 +02:00
' [Crafting] ' ,
2020-07-26 17:14:57 +02:00
player.name ..
' canceled their craft of item ' ..
2021-03-24 17:46:00 +02:00
event.recipe . name .. ' of total count ' .. crafting_queue_item_count .. ' in raw items ( ' .. crafted_items .. ' slots) but had no inventory left. '
2020-07-26 17:14:57 +02:00
)
2020-07-21 08:40:32 +02:00
end
2020-07-21 13:00:13 +02:00
2020-07-30 19:09:18 +02:00
if not this.cancel_crafting_history then
this.cancel_crafting_history = { }
2020-07-17 16:59:15 +02:00
end
2021-07-20 22:17:49 +02:00
if # this.cancel_crafting_history > this.limit then
2020-07-30 19:09:18 +02:00
this.cancel_crafting_history = { }
2020-07-06 23:22:21 +02:00
end
2020-07-17 16:59:15 +02:00
2021-04-30 20:16:19 +02:00
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
2020-07-17 16:59:15 +02:00
local str = ' [ ' .. t .. ' ] '
str = str .. player.name .. ' canceled '
2020-07-30 19:09:18 +02:00
str = str .. ' item [color=yellow] ' .. event.recipe . name .. ' [/color] '
2020-07-21 13:00:13 +02:00
str = str .. ' count was a total of: ' .. crafting_queue_item_count
2020-07-17 16:59:15 +02:00
str = str .. ' at X: '
str = str .. math.floor ( player.position . x )
str = str .. ' Y: '
str = str .. math.floor ( player.position . y )
str = str .. ' '
str = str .. ' surface: ' .. player.surface . index
2020-07-30 19:09:18 +02:00
increment ( this.cancel_crafting_history , str )
2020-07-06 23:22:21 +02:00
end
end
2020-06-29 15:50:01 +02:00
2020-07-11 17:04:53 +02:00
local function on_init ( )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-11 17:04:53 +02:00
local branch_version = ' 0.18.35 '
local sub = string.sub
local is_branch_18 = sub ( branch_version , 3 , 4 )
local get_active_version = sub ( game.active_mods . base , 3 , 4 )
local default = game.permissions . get_group ( ' Default ' )
2020-07-27 11:07:32 +02:00
game.forces . player.research_queue_enabled = true
2020-07-11 17:04:53 +02:00
is_branch_18 = is_branch_18 .. sub ( branch_version , 6 , 7 )
get_active_version = get_active_version .. sub ( game.active_mods . base , 6 , 7 )
if get_active_version >= is_branch_18 then
default.set_allows_action ( defines.input_action . flush_opened_entity_fluid , false )
default.set_allows_action ( defines.input_action . flush_opened_entity_specific_fluid , false )
end
end
2020-07-30 22:27:19 +02:00
local function on_permission_group_added ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
if not player or not player.valid then
return
end
local group = event.group
if group then
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' added ' .. group.name )
2020-07-30 22:27:19 +02:00
end
end
local function on_permission_group_deleted ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
if not player or not player.valid then
return
end
local name = event.group_name
local id = event.id
if name then
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' deleted ' .. name .. ' with ID: ' .. id )
2020-07-30 22:27:19 +02:00
end
end
local function on_permission_group_edited ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
if not player or not player.valid then
return
end
local group = event.group
if group then
local action = ' '
for k , v in pairs ( defines.input_action ) do
if event.action == v then
action = k
end
end
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' edited ' .. group.name .. ' with type: ' .. event.type .. ' with action: ' .. action )
2020-07-30 22:27:19 +02:00
end
if event.other_player_index then
local other_player = game.get_player ( event.other_player_index )
if other_player and other_player.valid then
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' moved ' .. other_player.name .. ' with type: ' .. event.type .. ' to group: ' .. group.name )
2020-07-30 22:27:19 +02:00
end
end
local old_name = event.old_name
local new_name = event.new_name
if old_name and new_name then
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' renamed ' .. group.name .. ' . New name: ' .. new_name .. ' . Old Name: ' .. old_name )
2020-07-30 22:27:19 +02:00
end
end
local function on_permission_string_imported ( event )
2020-08-22 17:20:59 +02:00
if not this.enabled then
return
end
2020-07-30 22:27:19 +02:00
local player = game.get_player ( event.player_index )
if not player or not player.valid then
return
end
2021-07-23 17:23:33 +02:00
Utils.log_msg ( ' [Permission_Group] ' , player.name .. ' imported a permission string ' )
2020-07-30 22:27:19 +02:00
end
2021-07-20 22:17:49 +02:00
--- This is used for the RPG module, when casting capsules.
---@param player <LuaPlayer>
---@param position <EventPosition>
---@param msg <string>
function Public . insert_into_capsule_history ( player , position , msg )
if not this.capsule_history then
this.capsule_history = { }
end
if # this.capsule_history > this.limit then
this.capsule_history = { }
end
local t = math.abs ( math.floor ( ( game.tick ) / 60 ) )
t = FancyTime.short_fancy_time ( t )
local str = ' [ ' .. t .. ' ] '
str = str .. ' [color=yellow] ' .. msg .. ' [/color] '
str = str .. ' at X: '
str = str .. math.floor ( position.x )
str = str .. ' Y: '
str = str .. math.floor ( position.y )
str = str .. ' '
str = str .. ' surface: ' .. player.surface . index
increment ( this.capsule_history , str )
end
2020-07-30 17:47:50 +02:00
--- This will reset the table of antigrief
2020-07-07 16:30:17 +02:00
function Public . reset_tables ( )
2020-07-07 23:42:44 +02:00
this.landfill_history = { }
this.capsule_history = { }
this.friendly_fire_history = { }
this.mining_history = { }
2021-05-09 17:11:18 +02:00
this.whitelist_mining_history = { }
2020-07-07 23:42:44 +02:00
this.corpse_history = { }
2021-05-09 17:11:18 +02:00
this.message_history = { }
2020-07-07 16:30:17 +02:00
this.cancel_crafting_history = { }
end
2020-06-29 15:50:01 +02:00
--- Add entity type to the whitelist so it gets logged.
2020-07-26 22:06:19 +02:00
---@param key <string>
---@param value <string>
2020-06-29 15:50:01 +02:00
function Public . whitelist_types ( key , value )
if key and value then
this.whitelist_types [ key ] = value
end
2020-07-26 22:06:19 +02:00
return this.whitelist_types [ key ]
2020-06-29 15:50:01 +02:00
end
2020-07-11 17:04:53 +02:00
--- If the event should also check trusted players.
2020-07-26 22:06:19 +02:00
---@param value <string>
2020-06-29 20:08:18 +02:00
function Public . do_not_check_trusted ( value )
2021-07-20 22:17:49 +02:00
this.do_not_check_trusted = value or false
2020-07-26 22:06:19 +02:00
return this.do_not_check_trusted
end
--- If ANY actions should be performed when a player misbehaves.
---@param value <string>
function Public . enable_capsule_warning ( value )
2021-07-20 22:17:49 +02:00
this.enable_capsule_warning = value or false
2020-07-26 22:06:19 +02:00
return this.enable_capsule_warning
end
2020-07-28 16:48:55 +02:00
--- If ANY actions should be performed when a player misbehaves.
---@param value <string>
function Public . enable_capsule_cursor_warning ( value )
2021-07-20 22:17:49 +02:00
this.enable_capsule_cursor_warning = value or false
2020-07-28 16:48:55 +02:00
return this.enable_capsule_cursor_warning
end
2020-07-30 17:47:50 +02:00
--- If the script should jail a person instead of kicking them
---@param value <string>
function Public . enable_jail ( value )
2021-07-20 22:17:49 +02:00
this.enable_jail = value or false
2020-07-30 17:47:50 +02:00
return this.enable_jail
end
2020-07-30 22:27:19 +02:00
--- Defines what the threshold for amount of explosives in chest should be - logged or not.
---@param value <string>
function Public . explosive_threshold ( value )
if value then
this.explosive_threshold = value
end
return this.explosive_threshold
end
--- Defines what the threshold for amount of times before the script should take action.
---@param value <string>
function Public . damage_entity_threshold ( value )
if value then
this.damage_entity_threshold = value
end
return this.damage_entity_threshold
end
2020-07-11 17:04:53 +02:00
--- Returns the table.
2020-06-29 15:50:01 +02:00
---@param key string
function Public . get ( key )
if key then
return this [ key ]
else
return this
end
end
2020-07-11 17:04:53 +02:00
Event.on_init ( on_init )
2020-07-30 22:27:19 +02:00
Event.add ( de.on_player_mined_entity , on_player_mined_entity )
Event.add ( de.on_entity_died , on_entity_died )
Event.add ( de.on_built_entity , on_built_entity )
Event.add ( de.on_gui_opened , on_gui_opened )
Event.add ( de.on_marked_for_deconstruction , on_marked_for_deconstruction )
Event.add ( de.on_player_ammo_inventory_changed , on_player_ammo_inventory_changed )
Event.add ( de.on_player_built_tile , on_player_built_tile )
Event.add ( de.on_pre_player_mined_item , on_pre_player_mined_item )
Event.add ( de.on_player_used_capsule , on_player_used_capsule )
Event.add ( de.on_player_cursor_stack_changed , on_player_cursor_stack_changed )
Event.add ( de.on_player_cancelled_crafting , on_player_cancelled_crafting )
Event.add ( de.on_player_joined_game , on_player_joined_game )
Event.add ( de.on_permission_group_added , on_permission_group_added )
Event.add ( de.on_permission_group_deleted , on_permission_group_deleted )
Event.add ( de.on_permission_group_edited , on_permission_group_edited )
Event.add ( de.on_permission_string_imported , on_permission_string_imported )
2021-05-07 01:19:38 +02:00
Event.add ( defines.events . on_console_chat , on_console_chat )
2020-06-29 15:50:01 +02:00
return Public