Files
factorio-expcluster/exp_scenario/module/control/nuke_protection.lua
T
bbassieandClaude Fable 5.1 f90b8fcee1 Address the second inline review of the features framework
- Feature.register returns the config as a second value, so files
  write `local feature, config = Feature.register(...)`.
- The lib is ExpScenario.Features and the instance class with its
  fields sits on Feature._prototype, which is the __index, the same
  shape as exp_commands.
- update_config only looks at values whose override was added,
  removed, or replaced. _apply_field sets one value and returns it, so
  the comparison happens while applying and the second loop only
  raises. Overrides dropped from the previous set count as candidates
  because the value reverts to its default.
- The `@setting name` tags are gone from the config comments.
- Protection is guarded as a whole instead of checking config.enabled
  in one handler. The handlers also forget mined entities, so
  get_entities and is_entity_protected now drop invalid references,
  which also covers entities destroyed without an event. The list
  getters are gone, the selection overlay builds its lists from
  Protection.config.
- Chat and damage popups are one feature in control/popups.lua.
- ignore_admins and command_admin_only are removed, roles cover them.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-03 07:35:58 +00:00

65 lines
2.3 KiB
Lua

--[[-- Control - Nuke Protection
Disable new players from having certain items in their inventory, most commonly nukes
]]
local ExpUtil = require("modules/exp_util")
local Feature = require("modules/exp_scenario/features")
local Roles = require("modules/exp_roles")
local feature, config = Feature.register("nuke_protection", {
banned_items = Feature.set{ "atomic-bomb" }, -- Items which are removed from the inventory of players without the bypass permission
})
--- The inventories which are checked and the events which trigger the check
local inventories = {
[defines.events.on_player_ammo_inventory_changed] = defines.inventory.character_ammo,
[defines.events.on_player_armor_inventory_changed] = defines.inventory.character_armor,
[defines.events.on_player_gun_inventory_changed] = defines.inventory.character_guns,
[defines.events.on_player_main_inventory_changed] = defines.inventory.character_main,
}
--- Check all items in the given inventory
--- @param player LuaPlayer
--- @param type defines.inventory
local function check_items(player, type)
-- If the player has perms to be ignored, then they should be
if Roles.player_has_permission(player, "exp_scenario.bypass.nuke_protection") then return end
local banned_items = config.banned_items
local items = {} --- @type LuaItemStack[]
local inventory = player.get_inventory(type)
if not inventory then return end
-- Check what items the player has
for i = 1, #inventory do
local item = inventory[i --[[@as uint]]]
if item.valid_for_read and banned_items[item.name] then
player.print{ "exp_nuke-protection.chat-found", item.prototype.localised_name }
items[#items + 1] = item
end
end
-- Move any items they aren't allowed
ExpUtil.move_items_to_surface{
items = items,
surface = game.planets.nauvis.surface,
allow_creation = true,
name = "iron-chest",
}
end
--- Add event handlers for the different inventories
local events = {}
for event_id, inventory in pairs(inventories) do
--- @param event { player_index: number }
events[event_id] = function(event)
local player = assert(game.get_player(event.player_index))
if player.valid then
check_items(player, inventory)
end
end
end
return feature:guard{
events = events,
}