diff --git a/config/_file_loader.lua b/config/_file_loader.lua index 16a135af..3b22a83d 100644 --- a/config/_file_loader.lua +++ b/config/_file_loader.lua @@ -29,6 +29,7 @@ return { 'modules.commands.home', 'modules.commands.connect', 'modules.commands.last-location', + 'modules.commands.protection', --- Addons 'modules.addons.chat-popups', diff --git a/config/expcore/roles.lua b/config/expcore/roles.lua index 46cd7072..e363dfc1 100644 --- a/config/expcore/roles.lua +++ b/config/expcore/roles.lua @@ -98,6 +98,8 @@ Roles.new_role('Trainee','TrMod') 'command/give-warning', 'command/get-warnings', 'command/get-reports', + 'command/protect-entity', + 'command/protect-area', 'command/jail', 'command/unjail', 'command/kick', diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index dea7b935..ef117971 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -82,4 +82,14 @@ offline=You cannot connect as the server is currently offline: __1__ none-matching=No servers were found with that name, if you used an address please append true to the end of your command. [expcom-lastlocation] -response=Last location of __1__ was [gps=__2__,__3__] \ No newline at end of file +response=Last location of __1__ was [gps=__2__,__3__] + +[expcom-protection] +entered-entity-selection=Entered entity selection, select entites to protect, hold shift to remove protection. +entered-area-selection=Entered area selection, select areas to protect, hold shift to remove protection. +protected-entities=__1__ entities have been protected. +unprotected-entities=__1__ entities have been unprotected. +already-protected=This area is already protected. +protected-area=This area is now protected. +unprotected-area=This area is now unprotected. +repeat-offence=__1__ has removed __2__ at [gps=__3__,__4__] \ No newline at end of file diff --git a/modules/commands/protection.lua b/modules/commands/protection.lua index 753ec319..e43a0989 100644 --- a/modules/commands/protection.lua +++ b/modules/commands/protection.lua @@ -52,12 +52,12 @@ Commands.new_command('protect-area', 'Toggles area protection selection, hold sh Selection.stop(player) else Selection.start(player, SelectionProtectArea) - return Commands.success{'expcom-protection.entered-entity-selection'} + return Commands.success{'expcom-protection.entered-area-selection'} end end) --- When an area is selected to add protection to entities -Selection.on_selection(SelectionProtectEntity, function(_, event) +Selection.on_selection(SelectionProtectEntity, function(event) for _, entity in ipairs(event.entities) do EntityProtection.add_entity(entity) end @@ -65,7 +65,7 @@ Selection.on_selection(SelectionProtectEntity, function(_, event) end) --- When an area is selected to remove protection from entities -Selection.on_alt_selection(SelectionProtectEntity, function(_, event) +Selection.on_alt_selection(SelectionProtectEntity, function(event) for _, entity in ipairs(event.entities) do EntityProtection.remove_entity(entity) end @@ -73,7 +73,7 @@ Selection.on_alt_selection(SelectionProtectEntity, function(_, event) end) --- When an area is selected to add protection to the area -Selection.on_selection(SelectionProtectEntity, function(_, event) +Selection.on_selection(SelectionProtectArea, function(event) local area = aabb_align_expand(event.area) local areas = EntityProtection.get_areas(event.surface) for _, next_area in pairs(areas) do @@ -86,13 +86,19 @@ Selection.on_selection(SelectionProtectEntity, function(_, event) end) --- When an area is selected to remove protection from the area -Selection.on_alt_selection(SelectionProtectEntity, function(_, event) +Selection.on_alt_selection(SelectionProtectArea, function(event) local area = aabb_align_expand(event.area) local areas = EntityProtection.get_areas(event.surface) for _, next_area in pairs(areas) do if aabb_area_enclosed(next_area, area) then - EntityProtection.remove_area(event.surface, area) + EntityProtection.remove_area(event.surface, next_area) Commands.print{'expcom-protection.unprotected-area'} end end +end) + +--- When there is a repeat offence print it in chat +Event.add(EntityProtection.events.on_repeat_violation, function(event) + local player_name = format_chat_player_name(event.player_index) + Roles.print_to_roles_higher('Regular', {'expcom-protection.repeat-offence', player_name, event.entity.localised_name, event.entity.position.x, event.entity.position.y}) end) \ No newline at end of file diff --git a/modules/control/protection.lua b/modules/control/protection.lua index e7da4ec4..490e0c7d 100644 --- a/modules/control/protection.lua +++ b/modules/control/protection.lua @@ -104,6 +104,7 @@ end --- Check if an entity is protected function EntityProtection.is_entity_protected(entity) + if check_always_protected(entity) then return true end local entities = protected_entities[entity.surface.index] if not entities then return false end return entities[get_entity_key(entity)] == entity @@ -158,7 +159,7 @@ Event.add(defines.events.on_pre_player_mined_item, function(event) if config.ignore_permission and Roles.player_allowed(player, config.ignore_permission) then return end -- Check if the entity is protected - if check_always_protected(entity) or EntityProtection.is_entity_protected(entity) + if EntityProtection.is_entity_protected(entity) or EntityProtection.is_position_protected(entity.surface, entity.position) then -- Update repeats @@ -190,4 +191,14 @@ Event.on_nth_tick(config.refresh_rate, function() end end) +--- When an entity is removed remove it from the protection list +local function event_remove_entity(event) + EntityProtection.remove_entity(event.entity) +end + +Event.add(defines.events.on_pre_player_mined_item, event_remove_entity) +Event.add(defines.events.on_robot_pre_mined, event_remove_entity) +Event.add(defines.events.on_entity_died, event_remove_entity) +Event.add(defines.events.script_raised_destroy, event_remove_entity) + return EntityProtection \ No newline at end of file diff --git a/modules/control/selection.lua b/modules/control/selection.lua index 30df3e66..00f76c6c 100644 --- a/modules/control/selection.lua +++ b/modules/control/selection.lua @@ -82,27 +82,25 @@ function Selection.is_selecting(player, selection_name) end end ---- Filter on_player_selected_area to this custom selection, pretends with player and appends with selection arguments +--- Filter on_player_selected_area to this custom selection, appends the selection arguments -- @tparam string selection_name The name of the selection to listen for -- @tparam function handler The event handler function Selection.on_selection(selection_name, handler) Event.add(defines.events.on_player_selected_area, function(event) local selection = selections[event.player_index] if not selection or selection.name ~= selection_name then return end - local player = game.get_player(event.player_index) - handler(player, event, unpack(selection.arguments)) + handler(event, unpack(selection.arguments)) end) end ---- Filter on_player_alt_selected_area to this custom selection, pretends with player and appends with selection arguments +--- Filter on_player_alt_selected_area to this custom selection, appends the selection arguments -- @param string selection_name The name of the selection to listen for -- @param function handler The event handler function Selection.on_alt_selection(selection_name, handler) Event.add(defines.events.on_player_alt_selected_area, function(event) local selection = selections[event.player_index] if not selection or selection.name ~= selection_name then return end - local player = game.get_player(event.player_index) - handler(player, event, unpack(selection.arguments)) + handler(event, unpack(selection.arguments)) end) end