Rework features into a Feature class with per field events

Review points from Cooldude:

- Feature.register(name[, config]) is required again and returns a
  feature object. Each feature has its config, its fields, and its
  overrides, so one registry holds all the data. Only the overrides are
  stored. On load each feature points at its own stored table again.
- The methods are feature:guard(lib), feature:update_config(enabled,
  values), and feature:is_enabled(). Feature.get(name) looks up a
  feature, and commands flag the feature object.
- update_config raises on_config_updated once per changed value, with
  the feature name, path, old value, and new value. The gui refresh only
  reacts to the "enabled" path.
- apply only sets values. on_apply is gone. Lookup sets built from
  lists use Feature.to_set, which is cached by the list itself. A
  replaced list gets a new set, so the sets are also correct after a
  load. Protection exposes getters for its protected names and types.

Web: settings use small inputs, the same size as the config pages.
Fields are shaped like clusterio config field definitions. A field
with an inputComponent renders the component registered with the web
interface, so the role setting now uses core's role input, and other
plugin inputs can be used later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-10-01 13:03:11 +00:00
1 parent 4bc1773c82
commit 628e4df514
26 files changed
+468 -430

No files matched your search

+6 -5
View File
@@ -3,17 +3,18 @@ Kicks players when all players on the server are afk
]]
local Async = require("modules/exp_util/async")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Roles = require("modules/exp_roles")
local Storage = require("modules/exp_util/storage")
local config = Features.config("afk_kick", {
local feature = Feature.register("afk_kick", {
admin_as_active = true, --- @setting admin_as_active When true admins will be treated as active regardless of afk time
trust_as_active = true, --- @setting trust_as_active When true trusted players (by playtime) will be treated as active regardless of afk time
active_role_id = Features.optional("number"), --- @setting active_role_id Players with this role or higher are treated as active regardless of afk time
active_role_id = Feature.optional("number"), --- @setting active_role_id Players with this role or higher are treated as active regardless of afk time
afk_minutes = 10, --- @setting afk_minutes The time in minutes that must pass for a player to be considered afk
kick_minutes = 30, --- @setting kick_minutes The time in minutes that must pass without any active players for all players to be kicked
trust_minutes = 600, --- @setting trust_minutes The time in minutes that a player must be online for to count as trusted
})
local config = feature.config
local ticks_per_minute = 3600
@@ -92,7 +93,7 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_player_joined_game] = on_player_joined_game,
},
@@ -100,4 +101,4 @@ return Features.guard(config, {
[60 * 60 * 30] = check_afk_players,
},
has_active_player = has_active_player,
})
}
@@ -2,9 +2,10 @@
Adds auto replies to chat messages, as well as chat commands
]]
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Roles = require("modules/exp_roles")
local config = require("modules/exp_scenario/config/chat_auto_reply")
local feature = require("modules/exp_scenario/config/chat_auto_reply")
local config = feature.config
local find = string.find
local sub = string.sub
@@ -57,8 +58,8 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_console_chat] = on_console_chat,
},
})
}
+5 -4
View File
@@ -3,8 +3,9 @@ Creates flying text entities when a player sends a message in chat
]]
local FlyingText = require("modules/exp_util/flying_text")
local Features = require("modules/exp_scenario/features")
local config = require("modules/exp_scenario/config/popups")
local Feature = require("modules/exp_scenario/features")
local feature = require("modules/exp_scenario/config/popups")
local config = feature.config
local lower = string.lower
local find = string.find
@@ -42,8 +43,8 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_console_chat] = on_console_chat,
},
})
}
@@ -4,8 +4,9 @@ also shows player health when a player is attacked
]]
local FlyingText = require("modules/exp_util/flying_text")
local Features = require("modules/exp_scenario/features")
local config = require("modules/exp_scenario/config/popups")
local Feature = require("modules/exp_scenario/features")
local feature = require("modules/exp_scenario/config/popups")
local config = feature.config
local random = math.random
local floor = math.floor
@@ -45,8 +46,8 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_entity_damaged] = on_entity_damaged,
},
})
}
@@ -3,10 +3,10 @@ Makes markers on the map where places have died and reclaims items if not recove
]]
local ExpUtil = require("modules/exp_util")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Storage = require("modules/exp_util/storage")
local config = Features.config("death_markers", {
local feature = Feature.register("death_markers", {
collect_corpses = true, --- @setting collect_corpses enables items being returned to the spawn point in chests upon corpse expiring
show_map_markers = true, --- @setting show_map_markers shows markers on the map where bodies are
clean_map_markers = false, --- @setting clean_map_markers removes the map marker once the body is gone
@@ -15,6 +15,7 @@ local config = Features.config("death_markers", {
show_light_at_corpse = true, --- @setting show_light_at_corpse if a light should be rendered at the corpse
show_line_to_corpse = true, --- @setting show_line_to_corpse if a line should be rendered from you to your corpse
})
local config = feature.config
local map_tag_time_format = ExpUtil.format_time_factory{ format = "short", hours = true, minutes = true }
@@ -147,7 +148,7 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
on_nth_tick = {
[60 * 60 * 5] = check_map_tags,
},
@@ -157,4 +158,4 @@ return Features.guard(config, {
[e.on_player_respawned] = on_player_respawned,
[e.on_character_corpse_expired] = on_character_corpse_expired,
},
})
}
+6 -14
View File
@@ -4,11 +4,11 @@ Sends alert messages to our discord server when certain events are triggered
local ExpUtil = require("modules/exp_util")
local Colors = require("modules/exp_util/include/color")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local EntityProtection = require("modules/exp_scenario/control/protection")
local Jail = require("modules/exp_scenario/control/jail")
local config = Features.config("discord_alerts", {
local feature = Feature.register("discord_alerts", {
show_playtime = true, --- @setting show_playtime Add the playtime of players to alerts
entity_protection = true, --- @setting entity_protection Alert when a player repeatedly removes protected entities
player_bans = true, --- @setting player_bans Alert when a player is banned or unbanned
@@ -20,15 +20,7 @@ local config = Features.config("discord_alerts", {
"config", "purge", "c", "command", "silent-command", "measured-command", "banlist", "permissions", "editor", "cheat",
},
})
--- Set of the logged commands, rebuilt when the config changes
local logged_commands = {} --- @type table<string, true>
Features.on_apply(config, function()
logged_commands = {}
for _, command in ipairs(config.logged_commands) do
logged_commands[command] = true
end
end)
local config = feature.config
--- Wrap an event handler so it only runs while the alert is enabled
--- @param setting string
@@ -267,7 +259,7 @@ end)
events[e.on_console_command] = function(event)
if event.player_index then
local player_name = get_player_name(event)
if logged_commands[event.command] then
if Feature.to_set(config.logged_commands)[event.command] then
emit_event{
title = event.command:gsub("^%l", string.upper),
description = "/" .. event.command .. " was used",
@@ -281,6 +273,6 @@ events[e.on_console_command] = function(event)
end
end
return Features.guard(config, {
return feature:guard{
events = events,
})
}
+5 -3
View File
@@ -2,11 +2,13 @@
Refreshes the gui of every player when a feature is enabled or disabled, see features.lua
]]
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Gui = require("modules/exp_gui")
--- Toolbar buttons of disabled features are hidden, which also hides their left elements
local function on_feature_changed()
--- @param event EventData.ExpScenario.on_config_updated
local function on_config_updated(event)
if event.path ~= "enabled" then return end
for _, player in pairs(game.connected_players) do
Gui._ensure_consistency{ player_index = player.index }
end
@@ -14,6 +16,6 @@ end
return {
events = {
[Features.events.on_feature_changed] = on_feature_changed,
[Feature.events.on_config_updated] = on_config_updated,
},
}
@@ -3,13 +3,14 @@ Marks mining drills for deconstruction when resources deplete
]]
local Async = require("modules/exp_util/async")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local config = Features.config("mine_depletion", {
local feature = Feature.register("mine_depletion", {
fluid = true, --- @setting fluid When true, checks for for fluid pipes when removing miners
chest = true, --- @setting chest When true, checks for for chest when removing miners
beacon = true, --- @setting beacon When true, checks for for beacon when removing miners
})
local config = feature.config
local floor = math.floor
@@ -276,8 +277,8 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_resource_depleted] = on_resource_depleted,
},
})
}
@@ -3,22 +3,14 @@ Disable new players from having certain items in their inventory, most commonly
]]
local ExpUtil = require("modules/exp_util")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Roles = require("modules/exp_roles")
local config = Features.config("nuke_protection", {
local feature = Feature.register("nuke_protection", {
ignore_admins = true, --- @setting ignore_admins Admins can hold banned items
banned_items = { "atomic-bomb" }, --- @setting banned_items Items which are removed from the inventory of players without the bypass permission
})
--- Set of the banned items, rebuilt when the config changes
local banned_items = {} --- @type table<string, true>
Features.on_apply(config, function()
banned_items = {}
for _, item_name in ipairs(config.banned_items) do
banned_items[item_name] = true
end
end)
local config = feature.config
--- The inventories which are checked and the events which trigger the check
local inventories = {
@@ -36,6 +28,7 @@ local function check_items(player, type)
if Roles.player_has_permission(player, "exp_scenario.bypass.nuke_protection") then return end
if config.ignore_admins and player.admin then return end
local banned_items = Feature.to_set(config.banned_items)
local items = {} --- @type LuaItemStack[]
local inventory = assert(player.get_inventory(type))
-- Check what items the player has
@@ -68,6 +61,6 @@ for event_id, inventory in pairs(inventories) do
end
end
return Features.guard(config, {
return feature:guard{
events = events,
})
}
+14 -30
View File
@@ -3,10 +3,10 @@ Protects entities and areas from being mined by players other than the one who p
]]
local Storage = require("modules/exp_util/storage")
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local Roles = require("modules/exp_roles")
local config = Features.config("protection", {
local feature = Feature.register("protection", {
ignore_admins = true, --- @setting ignore_admins If admins are ignored by the protection filter
repeat_count = 5, --- @setting repeat_count Number of protected entities that must be removed within repeat_minutes in order to trigger repeated removal protection
repeat_minutes = 20, --- @setting repeat_minutes The length of time, in minutes, that protected removals will be remembered for
@@ -19,6 +19,7 @@ local config = Features.config("protection", {
"reactor", "fusion-reactor", "rocket-silo",
},
})
local config = feature.config
local format_string = string.format
local floor = math.floor
@@ -31,12 +32,6 @@ local Protection = {
--- Raised when a player mines protected entities repeatedly, or one which always counts as repeated
--- @type EventData.ExpScenario_Protection.on_repeat_violation
on_repeat_violation = script.generate_event_name(),
--- Names of entities which are always protected
--- @type string[]
protected_entity_names = {},
--- Types of entities which are always protected
--- @type string[]
protected_entity_types = {},
--- @package
events = {},
--- @package
@@ -50,28 +45,17 @@ local Protection = {
--- @field last uint Tick of the last protected removal
--- @field count number Protected removals since the last repeat violation
--- @param values string[]
--- @return table<string, true>
local function to_set(values)
local set = {}
for _, value in ipairs(values) do
set[value] = true
end
return set
--- Names of entities which are always protected
--- @return string[]
function Protection.get_protected_entity_names()
return config.always_protected_names
end
local always_protected_names = {} --- @type table<string, true>
local always_protected_types = {} --- @type table<string, true>
local always_trigger_repeat_names = {} --- @type table<string, true>
local always_trigger_repeat_types = {} --- @type table<string, true>
Features.on_apply(config, function()
Protection.protected_entity_names = config.always_protected_names
Protection.protected_entity_types = config.always_protected_types
always_protected_names = to_set(config.always_protected_names)
always_protected_types = to_set(config.always_protected_types)
always_trigger_repeat_names = to_set(config.always_trigger_repeat_names)
always_trigger_repeat_types = to_set(config.always_trigger_repeat_types)
end)
--- Types of entities which are always protected
--- @return string[]
function Protection.get_protected_entity_types()
return config.always_protected_types
end
local protected_entities = {} --- @type table<uint, table<string, LuaEntity>> Keyed by surface index then entity key
local protected_areas = {} --- @type table<uint, table<string, BoundingBox>> Keyed by surface index then area key
@@ -131,7 +115,7 @@ end
--- @param entity LuaEntity
--- @return boolean
function Protection.is_entity_protected(entity)
if always_protected_names[entity.name] or always_protected_types[entity.type] then return true end
if Feature.to_set(config.always_protected_names)[entity.name] or Feature.to_set(config.always_protected_types)[entity.type] then return true end
local entities = protected_entities[entity.surface.index]
if not entities then return false end
return entities[Protection.get_entity_key(entity)] == entity
@@ -209,7 +193,7 @@ local function raise_violation(event, player)
script.raise_event(Protection.on_player_mined_protected, event)
local entity = event.entity
local always_repeat = always_trigger_repeat_names[entity.name] or always_trigger_repeat_types[entity.type]
local always_repeat = Feature.to_set(config.always_trigger_repeat_names)[entity.name] or Feature.to_set(config.always_trigger_repeat_types)[entity.type]
if always_repeat or player_repeats.count >= config.repeat_count then
player_repeats.count = 0
script.raise_event(Protection.on_repeat_violation, event)
+5 -4
View File
@@ -2,8 +2,9 @@
Adds a custom spawn area with chests and afk turrets
]]
local Features = require("modules/exp_scenario/features")
local config = require("modules/exp_scenario/config/spawn_area")
local Feature = require("modules/exp_scenario/features")
local feature = require("modules/exp_scenario/config/spawn_area")
local config = feature.config
--- Apply an offset to a LuaPosition
--- @param position MapPosition.struct
@@ -299,9 +300,9 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
on_nth_tick = on_nth_tick,
events = {
[e.on_player_created] = on_player_created,
},
})
}
@@ -2,13 +2,14 @@
Automatically name stations when they are placed based on closest resource and direction from spawn
]]
local Features = require("modules/exp_scenario/features")
local Feature = require("modules/exp_scenario/features")
local config = Features.config("station_auto_name", {
local feature = Feature.register("station_auto_name", {
--- @setting station_name the name given to new stations, placeholders:
-- __icon__ __item_name__ __backer_name__ __direction__ __x__ __y__
station_name = "[L] __icon__",
})
local config = feature.config
local get_direction do
local directions = {
@@ -104,9 +105,9 @@ end
local e = defines.events
return Features.guard(config, {
return feature:guard{
events = {
[e.on_built_entity] = rename_station,
[e.on_robot_built_entity] = rename_station,
},
})
}