1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-28 03:57:22 +02:00

Merge pull request #285 from ComfyFactory/antigrief

Antigrief - fix deconstruct bug
This commit is contained in:
Gerkiz 2022-07-20 00:42:04 +02:00 committed by GitHub
commit 2f9310899b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 128 additions and 53 deletions

View File

@ -227,6 +227,7 @@ function Public.reset_map()
AntiGrief.damage_entity_threshold(20)
AntiGrief.explosive_threshold(32)
AntiGrief.decon_surface_blacklist(surface.name)
AntiGrief.filtered_types_on_decon({'tree', 'simple-entity', 'fish'})
PL.show_roles_in_list(true)
PL.rpg_enabled(true)

View File

@ -22,6 +22,7 @@ local format = string.format
local floor = math.floor
local random = math.random
local abs = math.abs
local max_count_decon = 500
local this = {
enabled = true,
@ -49,8 +50,10 @@ local this = {
damage_entity_threshold = 20,
explosive_threshold = 16,
enable_jail_when_decon = true,
filtered_types_on_decon = {},
decon_surface_blacklist = 'nauvis',
players_warn_when_decon = {},
on_cancelled_deconstruction = {tick = 0, count = 0},
limit = 2000
}
@ -204,7 +207,7 @@ local function on_marked_for_deconstruction(event)
end
local player = game.get_player(event.player_index)
if Session.get_trusted_player(player.name) or this.do_not_check_trusted then
if Session.get_trusted_player(player) or this.do_not_check_trusted then
return
end
@ -227,7 +230,7 @@ local function on_player_ammo_inventory_changed(event)
if player.admin then
return
end
if Session.get_trusted_player(player.name) or this.do_not_check_trusted then
if Session.get_trusted_player(player) or this.do_not_check_trusted then
return
end
@ -249,7 +252,7 @@ end
local function on_player_joined_game(event)
local player = game.get_player(event.player_index)
if not this.enabled then
if not Session.get_trusted_player(player.name) then
if not Session.get_trusted_player(player) then
Session.set_trusted_player(player.name)
end
return
@ -306,7 +309,7 @@ local function on_built_entity(event)
if player.admin then
return
end
if Session.get_trusted_player(player.name) or this.do_not_check_trusted then
if Session.get_trusted_player(player) or this.do_not_check_trusted then
return
end
@ -330,7 +333,7 @@ local function on_player_used_capsule(event)
local player = game.get_player(event.player_index)
if Session.get_trusted_player(player.name) or this.do_not_check_trusted then
if this.do_not_check_trusted then
return
end
@ -695,7 +698,7 @@ local function on_player_cursor_stack_changed(event)
if player.admin then
return
end
if Session.get_trusted_player(player.name) or this.do_not_check_trusted then
if Session.get_trusted_player(player) or this.do_not_check_trusted then
return
end
@ -826,6 +829,10 @@ local function on_permission_group_deleted(event)
end
local function on_player_deconstructed_area(event)
if not this.enabled then
return
end
local surface = event.surface
local surface_name = this.decon_surface_blacklist
@ -835,64 +842,122 @@ local function on_player_deconstructed_area(event)
local player = game.get_player(event.player_index)
local area = event.area
local count = surface.count_entities_filtered({area = area, force = 'neutral'})
if count and count > 0 then
local count = surface.count_entities_filtered({area = area, type = 'resource', invert = true})
local max_count = 0
local is_trusted = Session.get_trusted_player(player)
if is_trusted then
max_count = max_count_decon
end
if next(this.filtered_types_on_decon) then
local filtered_count = surface.count_entities_filtered({area = area, type = this.filtered_types_on_decon})
if filtered_count and filtered_count > 0 then
surface.cancel_deconstruct_area {
area = area,
force = player.force
}
end
end
if count and count >= max_count then
surface.cancel_deconstruct_area {
area = area,
force = player.force
}
if count >= 2000 then
local msg = '[Deconstruct] ' .. player.name .. ' tried to deconstruct: ' .. count .. ' entities!'
Utils.print_to(nil, msg)
Server.to_discord_embed(msg)
if not is_trusted then
return
end
if not this.deconstruct_history then
this.deconstruct_history = {}
local msg = '[Deconstruct] ' .. player.name .. ' tried to deconstruct: ' .. count .. ' entities!'
Utils.print_to(nil, msg)
Server.to_discord_embed(msg)
if not this.deconstruct_history then
this.deconstruct_history = {}
end
if #this.deconstruct_history > this.limit then
overflow(this.deconstruct_history)
end
local t = abs(floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. msg
str = str .. ' at lt_x:'
str = str .. floor(area.left_top.x)
str = str .. ' at lt_y:'
str = str .. floor(area.left_top.y)
str = str .. ' at rb_x:'
str = str .. floor(area.right_bottom.x)
str = str .. ' at rb_y:'
str = str .. floor(area.right_bottom.y)
str = str .. ' '
str = str .. 'surface:' .. player.surface.index
increment(this.deconstruct_history, str)
if this.enable_jail_when_decon and not player.admin then
if not this.players_warn_when_decon[player.index] then
this.players_warn_when_decon[player.index] = 1
local r = random(7200, 18000)
Task.set_timeout_in_ticks(r, clear_player_decon_warnings, {player_index = player.index})
end
if #this.deconstruct_history > this.limit then
overflow(this.deconstruct_history)
end
local t = abs(floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. msg
str = str .. ' at lt_x:'
str = str .. floor(area.left_top.x)
str = str .. ' at lt_y:'
str = str .. floor(area.left_top.y)
str = str .. ' at rb_x:'
str = str .. floor(area.right_bottom.x)
str = str .. ' at rb_y:'
str = str .. floor(area.right_bottom.y)
str = str .. ' '
str = str .. 'surface:' .. player.surface.index
increment(this.deconstruct_history, str)
if this.enable_jail_when_decon and not player.admin then
if not this.players_warn_when_decon[player.index] then
this.players_warn_when_decon[player.index] = 1
local r = random(7200, 18000)
Task.set_timeout_in_ticks(r, clear_player_decon_warnings, {player_index = player.index})
end
local warnings = this.players_warn_when_decon[player.index]
if warnings then
if warnings == 1 or warnings == 2 then
Utils.print_to(player, '[Deconstruct] Warning! Do not deconstruct that many entities at once!')
this.players_warn_when_decon[player.index] = this.players_warn_when_decon[player.index] + 1
elseif warnings == 3 then
Utils.print_to(player, '[Deconstruct] Warning! Do not deconstruct that many entities at once! This is your final warning!')
this.players_warn_when_decon[player.index] = this.players_warn_when_decon[player.index] + 1
else
Jail.try_ul_data(player, true, 'script', 'Deconstructed ' .. count .. ' entities. Has been warned 3 times before getting jailed.')
this.players_warn_when_decon[player.index] = nil
end
local warnings = this.players_warn_when_decon[player.index]
if warnings then
if warnings == 1 or warnings == 2 then
Utils.print_to(player, '[Deconstruct] Warning! Do not deconstruct that many entities at once!')
this.players_warn_when_decon[player.index] = this.players_warn_when_decon[player.index] + 1
elseif warnings == 3 then
Utils.print_to(player, '[Deconstruct] Warning! Do not deconstruct that many entities at once! This is your final warning!')
this.players_warn_when_decon[player.index] = this.players_warn_when_decon[player.index] + 1
else
Jail.try_ul_data(player, true, 'script', 'Deconstructed ' .. count .. ' entities. Has been warned 3 times before getting jailed.')
this.players_warn_when_decon[player.index] = nil
end
end
end
end
end
local function on_cancelled_deconstruction(event)
local player_index = event.player_index
if player_index then
return
end
local tick = event.tick
local entity = event.entity
if not entity or not entity.valid then
return
end
local handler = this.on_cancelled_deconstruction
if tick ~= handler.tick then
handler.tick = tick
handler.count = 0
end
handler.count = handler.count + 1
local player = entity.last_user
if player and player.valid and player.connected then
local is_trusted = Session.get_trusted_player(player)
if not is_trusted then
return
end
end
if entity.force.name == 'neutral' then
return
end
if tick == handler.tick and handler.count >= max_count_decon then
return
end
entity.order_deconstruction(entity.force)
end
local function on_permission_group_edited(event)
if not this.enabled then
return
@ -1036,6 +1101,14 @@ function Public.explosive_threshold(value)
return this.explosive_threshold
end
--- Defines if on_player_deconstructed_area should also check for other types.
---@param tbl table
function Public.filtered_types_on_decon(tbl)
if tbl then
this.filtered_types_on_decon = tbl
end
end
--- Defines what the threshold for amount of times before the script should take action.
---@param value number
function Public.damage_entity_threshold(value)
@ -1062,6 +1135,8 @@ 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_deconstructed_area, on_player_deconstructed_area)
Event.add(de.on_cancelled_deconstruction, on_cancelled_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)
@ -1074,6 +1149,5 @@ 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)
Event.add(de.on_console_chat, on_console_chat)
Event.add(de.on_player_deconstructed_area, on_player_deconstructed_area)
return Public