mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-11 23:54:52 +09:00
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>
66 lines
2.2 KiB
Lua
66 lines
2.2 KiB
Lua
--[[-- Control - Chat Auto Reply
|
|
Adds auto replies to chat messages, as well as chat commands
|
|
]]
|
|
|
|
local Feature = require("modules/exp_scenario/features")
|
|
local Roles = require("modules/exp_roles")
|
|
local feature = require("modules/exp_scenario/config/chat_auto_reply")
|
|
local config = feature.config
|
|
|
|
local find = string.find
|
|
local sub = string.sub
|
|
|
|
--- Check if a message has any trigger words
|
|
--- @param event EventData.on_console_chat
|
|
local function on_console_chat(event)
|
|
if not event.player_index then return end
|
|
local player = assert(game.get_player(event.player_index))
|
|
local message = event.message:lower():gsub("%s+", "")
|
|
local prefix = config.command_prefix
|
|
local prefix_len = string.len(prefix)
|
|
|
|
-- Check if the player can chat commands
|
|
local commands_allowed = true
|
|
if config.command_admin_only and not player.admin then commands_allowed = false end
|
|
if not Roles.player_has_permission(player, "exp_scenario.chat.commands") then commands_allowed = false end
|
|
|
|
-- Check if a key word appears in the message
|
|
for key_word, reply in pairs(config.messages) do
|
|
local start_pos = find(message, key_word)
|
|
if start_pos then
|
|
local is_command = sub(message, start_pos - prefix_len - 1, start_pos - 1) == prefix
|
|
if type(reply) == "function" then
|
|
reply = reply(player, is_command)
|
|
end
|
|
|
|
if is_command and commands_allowed then
|
|
game.print{ "exp_chat-auto-reply.chat-reply", reply }
|
|
elseif is_command then
|
|
player.print{ "exp_chat-auto-reply.chat-disallowed" }
|
|
elseif not commands_allowed then
|
|
player.print{ "exp_chat-auto-reply.chat-reply", reply }
|
|
end
|
|
end
|
|
end
|
|
|
|
if not commands_allowed then return end
|
|
|
|
-- Check if a command appears in the message
|
|
for key_word, reply in pairs(config.commands) do
|
|
if find(message, prefix .. key_word) then
|
|
if type(reply) == "function" then
|
|
reply = reply(player, true)
|
|
end
|
|
game.print{ "exp_chat-auto-reply.chat-reply", reply }
|
|
end
|
|
end
|
|
end
|
|
|
|
local e = defines.events
|
|
|
|
return feature:guard{
|
|
events = {
|
|
[e.on_console_chat] = on_console_chat,
|
|
},
|
|
}
|