From d002eeaf54e0db2a54081db35e2f1a1264888d87 Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Sat, 25 Jan 2025 00:43:03 +0900 Subject: [PATCH 1/2] Add option for vlayer on space platforms (#365) * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua --- exp_legacy/module/config/vlayer.lua | 1 + exp_legacy/module/modules/gui/vlayer.lua | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/exp_legacy/module/config/vlayer.lua b/exp_legacy/module/config/vlayer.lua index c008c41a..a3eac3ad 100644 --- a/exp_legacy/module/config/vlayer.lua +++ b/exp_legacy/module/config/vlayer.lua @@ -9,6 +9,7 @@ return { unlimited_capacity = false, --- @setting unlimited_capacity When true the vlayer has an unlimited energy capacity, accumulators are not required unlimited_surface_area = false, --- @setting unlimited_surface_area When true the vlayer has an unlimited surface area, landfill is not required modded_auto_downgrade = false, --- @setting modded_auto_downgrade When true modded items will be converted into their base game equivalent, original items can not be recovered + power_on_space = false, --- @setting power_on_space When true allow on spaceship mimic_surface = "nauvis", --- @setting mimic_surface Surface name/index the vlayer will copy its settings from, use nil to use the settings below surface = { --- @setting surface When mimic_surface is nil these settings will be used instead, see LuaSurface for details diff --git a/exp_legacy/module/modules/gui/vlayer.lua b/exp_legacy/module/modules/gui/vlayer.lua index 12595259..15e6af02 100644 --- a/exp_legacy/module/modules/gui/vlayer.lua +++ b/exp_legacy/module/modules/gui/vlayer.lua @@ -81,10 +81,16 @@ Selection.on_selection(SelectionConvertArea, function(event) return nil end - local entities = event.surface.find_entities_filtered{ area = area, name = "steel-chest", force = player.force } local frame = Gui.get_left_element(player, vlayer_container) local disp = frame.container["vlayer_st_2"].disp.table local target = vlayer_control_type_list[disp[vlayer_gui_control_type.name].selected_index] + local entities + + if config.power_on_space and event.surface and event.surface.platform and target == "energy" then + entities = event.surface.find_entities_filtered{ area = area, name = "constant-combinator", force = player.force } + else + entities = event.surface.find_entities_filtered{ area = area, name = "steel-chest", force = player.force } + end if #entities == 0 then player.print{ "vlayer.steel-chest-detect" } @@ -100,9 +106,9 @@ Selection.on_selection(SelectionConvertArea, function(event) local e = entities[1] local e_pos = { x = string.format("%.1f", e.position.x), y = string.format("%.1f", e.position.y) } - local e_circ = {} -- e.circuit_connected_entities --- TODO use new circuit api + local e_circ = nil -- e.get_wire_connectors{ or_create = false } - if not e.get_inventory(defines.inventory.chest).is_empty() then + if e.name and e.name == "steel-chest" and (not e.get_inventory(defines.inventory.chest).is_empty()) then player.print{ "vlayer.steel-chest-empty" } return nil end From 1a17f11062f5d250dd03c8e41b3f802f4b6c3cab Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Sat, 25 Jan 2025 18:26:59 +0000 Subject: [PATCH 2/2] Multiple fixes for role commands - Fix server being 'Guest' for commands - Make autocomplete of arguments more intuitive - Fix usage text for commands --- exp_commands/module/locale/en.cfg | 3 +- exp_commands/module/locale/zh-CN.cfg | 3 +- exp_commands/module/locale/zh-TW.cfg | 3 +- exp_commands/module/module_exports.lua | 8 +++-- exp_legacy/module/expcore/roles.lua | 2 +- exp_util/module/module_exports.lua | 49 ++++++++++++++++++++------ 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/exp_commands/module/locale/en.cfg b/exp_commands/module/locale/en.cfg index 5691ca4c..844a2e02 100644 --- a/exp_commands/module/locale/en.cfg +++ b/exp_commands/module/locale/en.cfg @@ -1,5 +1,6 @@ [exp-commands] help=__1__- __2____3__ +usage=/__1__ __2__ aliases=\n Aliaies: __1__ argument=<__1__> optional=[__1__] @@ -10,7 +11,7 @@ error=Command failed to run: __1__ error-default=Please check you gave the correct arguments. unauthorized=Unauthorized: __1__ unauthorized-default=Access is denied due to insufficient permissions. -invalid-usage=Wrong number of arguments, usage: /__1__ __2__ +invalid-usage=Wrong number of arguments, usage: __1__ invalid-input=Invalid input: Please ensure everything is formated correctly. invalid-argument=Invalid argument "__1__"; __2__ internal-error=Internal Error, Please contact an admin: __1__ diff --git a/exp_commands/module/locale/zh-CN.cfg b/exp_commands/module/locale/zh-CN.cfg index 8eb8c6ea..c5da0260 100644 --- a/exp_commands/module/locale/zh-CN.cfg +++ b/exp_commands/module/locale/zh-CN.cfg @@ -1,5 +1,6 @@ [exp-commands] help=__1__- __2____3__ +usage=/__1__ __2__ aliases=\n 別名: __1__ argument=<__1__> optional=[__1__] @@ -10,7 +11,7 @@ error=指令運行失敗: __1__ error-default=檢查指令參數。 unauthorized=未授權: __1__ unauthorized-default=由於權限不足,存取被拒絕。 -invalid-usage=參數數量和用法錯誤,參考: /__1__ __2__ +invalid-usage=參數數量和用法錯誤,參考: __1__ invalid-input=輸入錯誤: 確保格式正確。 invalid-argument=參數錯誤 "__1__"; __2__ internal-error=內部錯誤,請聯絡管理員: __1__ diff --git a/exp_commands/module/locale/zh-TW.cfg b/exp_commands/module/locale/zh-TW.cfg index 8eb8c6ea..c5da0260 100644 --- a/exp_commands/module/locale/zh-TW.cfg +++ b/exp_commands/module/locale/zh-TW.cfg @@ -1,5 +1,6 @@ [exp-commands] help=__1__- __2____3__ +usage=/__1__ __2__ aliases=\n 別名: __1__ argument=<__1__> optional=[__1__] @@ -10,7 +11,7 @@ error=指令運行失敗: __1__ error-default=檢查指令參數。 unauthorized=未授權: __1__ unauthorized-default=由於權限不足,存取被拒絕。 -invalid-usage=參數數量和用法錯誤,參考: /__1__ __2__ +invalid-usage=參數數量和用法錯誤,參考: __1__ invalid-input=輸入錯誤: 確保格式正確。 invalid-argument=參數錯誤 "__1__"; __2__ internal-error=內部錯誤,請聯絡管理員: __1__ diff --git a/exp_commands/module/module_exports.lua b/exp_commands/module/module_exports.lua index 48821104..4f150c98 100644 --- a/exp_commands/module/module_exports.lua +++ b/exp_commands/module/module_exports.lua @@ -106,6 +106,7 @@ end --- @class Commands.Command --- @field name string The name of the command --- @field description LocalisedString The description of the command +--- @field usage LocalisedString The usage of the command --- @field help_text LocalisedString The full help text for the command --- @field aliases string[] Aliases that the command will also be registered under --- @field defined_at? string If present then this is an ExpCommand @@ -536,6 +537,7 @@ function Commands._prototype:register(callback) -- Generates a description to be used local argument_names = { "" } --- @type LocalisedString local argument_verbose = { "" } --- @type LocalisedString + self.usage = { "exp-commands.usage", self.name, argument_names } self.help_text = { "exp-commands.help", argument_names, self.description, argument_verbose } --- @type LocalisedString if next(self.aliases) then argument_verbose[2] = { "exp-commands.aliases", table.concat(self.aliases, ", ") } @@ -670,20 +672,20 @@ function Commands._event_handler(event) -- Check the edge case of parameter being nil if command.min_arg_count > 0 and event.parameter == nil then log_command("Too few arguments", command, player, event.parameter, { minimum = command.min_arg_count, maximum = command.max_arg_count }) - return Commands.error{ "exp-commands.invalid-usage", command.name, command.description } + return Commands.error{ "exp-commands.invalid-usage", command.usage } end -- Get the arguments for the command, returns nil if there are too many arguments local raw_arguments = extract_arguments(event.parameter, command.max_arg_count, command.auto_concat) if raw_arguments == nil then log_command("Too many arguments", command, player, event.parameter, { minimum = command.min_arg_count, maximum = command.max_arg_count }) - return Commands.error{ "exp-commands.invalid-usage", command.name, command.description } + return Commands.error{ "exp-commands.invalid-usage", command.usage } end -- Check the minimum number of arguments is fulfilled if #raw_arguments < command.min_arg_count then log_command("Too few arguments", command, player, event.parameter, { minimum = command.min_arg_count, maximum = command.max_arg_count }) - return Commands.error{ "exp-commands.invalid-usage", command.name, command.description } + return Commands.error{ "exp-commands.invalid-usage", command.usage } end -- Parse the arguments, optional arguments will attempt to use a default if provided diff --git a/exp_legacy/module/expcore/roles.lua b/exp_legacy/module/expcore/roles.lua index 880eca75..0d281a26 100644 --- a/exp_legacy/module/expcore/roles.lua +++ b/exp_legacy/module/expcore/roles.lua @@ -329,7 +329,7 @@ local roles = Roles.get_player_roles(game.player) ]] function Roles.get_player_roles(player) - if not player then return { Roles.config.roles[Roles.config.internal.root] } end + if not player or player.index == 0 then return { Roles.config.roles[Roles.config.internal.root] } end local roles = Roles.config.players[player.name] or {} local default = Roles.config.roles[Roles.config.internal.default] local rtn = { default } diff --git a/exp_util/module/module_exports.lua b/exp_util/module/module_exports.lua index 3583847d..b484d54c 100644 --- a/exp_util/module/module_exports.lua +++ b/exp_util/module/module_exports.lua @@ -167,6 +167,24 @@ function ExpUtil.get_function_name(func, raw) return "<" .. file_name .. ":" .. func_name .. ">" end +--- Attempt a simple autocomplete search from a set of options +--- @param options table The table representing the possible options which can be selected +--- @param input string The user input string which should be matched to an option +--- @param use_key boolean? When true the keys will be searched, when false the values will be searched +--- @param rtn_key boolean? When true the selected key will be returned, when false the selected value will be returned +--- @param use_pattern boolean? When true the input will be treated as a lua pattern string +--- @return any # The selected key or value which first matches the input text +function ExpUtil.auto_complete_first(options, input, use_key, rtn_key, use_pattern) + input = input:lower() + local plain = use_pattern ~= true + for k, v in pairs(options) do + local str = use_key and k or v + if str:lower():find(input, nil, plain) then + if rtn_key then return k else return v end + end + end +end + --- Attempt a simple autocomplete search from a set of options --- @param options table The table representing the possible options which can be selected --- @param input string The user input string which should be matched to an option @@ -177,19 +195,30 @@ end function ExpUtil.auto_complete(options, input, use_key, rtn_key, use_pattern) input = input:lower() local plain = use_pattern ~= true - if use_key then - for k, v in pairs(options) do - if k:lower():find(input, nil, plain) then - if rtn_key then return k else return v end - end - end - else - for k, v in pairs(options) do - if v:lower():find(input, nil, plain) then - if rtn_key then return k else return v end + local found = {} --- @type { length: number, index: number, start: boolean, value: any } + for k, v in pairs(options) do + local str = use_key and k or v + local index = str:lower():find(input, nil, plain) + if index then + local length = #str + local start = index == 1 or str:sub(index - 1, index - 1) == " " + if found.value == nil -- None found yet + or not found.start and start -- Prefer matches at the start of words + or found.length > length -- Prefer shorter matches + or found.index > index -- Prefer earlier matches + then + found.length = length + found.index = index + found.start = start + if rtn_key then + found.value = k + else + found.value = v + end end end end + return found.value end --- Formats any value into a safe representation, useful with inspect