Skip to content

Commit d41124c

Browse files
committed
Improve BetterCommands
1 parent 84c2dfc commit d41124c

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

models/BetterCommands/control.lua

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ end
3030
---@param player_index? number
3131
-- Sends message to a player or server
3232
local function print_to_caller(message, player_index)
33-
if game == nil or player_index == nil or player_index == 0 then
33+
if not (game and player_index) then
3434
print(message) -- this message for server
3535
else
3636
local player = game.get_player(player_index)
@@ -43,8 +43,8 @@ end
4343

4444
---@param error_message string
4545
---@param player_index? number
46-
---@param command_name string
47-
local function disable_setting(error_message, player_index, command_name)
46+
---@param orig_command_name string
47+
local function disable_setting(error_message, player_index, orig_command_name)
4848
print_to_caller(error_message, player_index)
4949

5050
local is_message_sended = false
@@ -61,8 +61,8 @@ local function disable_setting(error_message, player_index, command_name)
6161
end
6262

6363
-- Turns off the command
64-
if command_name then
65-
local setting_name = MOD_SHORT_NAME .. command_name
64+
if orig_command_name then
65+
local setting_name = MOD_SHORT_NAME .. orig_command_name
6666
if settings.global[setting_name] then
6767
settings.global[setting_name] = {
6868
value = false
@@ -81,7 +81,7 @@ local input_types = {
8181
---@param command_settings table
8282
---@param original_func function
8383
---@return boolean
84-
local function add_custom_command(command_settings, original_func)
84+
local function add_custom_command(orig_command_name, command_settings, original_func)
8585
local input_type = input_types[command_settings.input_type]
8686
local is_allowed_empty_args = command_settings.is_allowed_empty_args
8787
local command_name = command_settings.name
@@ -151,7 +151,7 @@ local function add_custom_command(command_settings, original_func)
151151
if is_ok then
152152
return
153153
else
154-
disable_setting(error_message, cmd.player_index, command_name)
154+
disable_setting(error_message, cmd.player_index, orig_command_name)
155155
end
156156
end)
157157

@@ -187,12 +187,12 @@ function M:handle_custom_commands(module)
187187
end
188188

189189
if setting == nil then
190-
local is_added = add_custom_command(command_settings, func)
190+
local is_added = add_custom_command(command_name, command_settings, func)
191191
if is_added == false then
192192
log(script.mod_name .. " can't add command \"" .. command_settings.name .. "\"")
193193
end
194194
elseif setting.value then
195-
local is_added = add_custom_command(command_settings, func)
195+
local is_added = add_custom_command(command_name, command_settings, func)
196196
if is_added == false then
197197
local message = script.mod_name .. " can't add command \"" .. command_settings.name .. "\""
198198
disable_setting(message, nil, command_name)
@@ -219,18 +219,19 @@ end
219219
---@return boolean
220220
local function on_runtime_mod_setting_changed(event)
221221
if event.setting_type ~= "runtime-global" then return end
222-
if string.find(event.setting, '^' .. MOD_SHORT_NAME) ~= 1 then return end
222+
local setting_name = event.setting
223+
if string.find(setting_name, '^' .. MOD_SHORT_NAME) ~= 1 then return end
223224

224-
local command_name = string.gsub(event.setting, '^' .. MOD_SHORT_NAME, "")
225+
local command_name = string.gsub(setting_name, '^' .. MOD_SHORT_NAME, "")
225226
local func = find_func_by_command_name(command_name) -- WARNING: check this throughly!
226227
if func == nil then
227228
log("Didn't find '" .. command_name .. "' among commands in modules")
228229
end
229230
local command_settings = SWITCHABLE_COMMANDS[command_name] or {}
230-
local state = settings.global[event.setting].value
231+
local state = settings.global[setting_name].value
231232
command_settings.name = command_settings.name or command_name
232233
if state == true then
233-
local is_added = add_custom_command(command_settings, func)
234+
local is_added = add_custom_command(command_name, command_settings, func)
234235
if is_added then
235236
game.print("Added command: " .. command_settings.name or command_name)
236237
else
@@ -253,13 +254,14 @@ function M:create_settings()
253254
for key, command in pairs(SWITCHABLE_COMMANDS) do
254255
local command_name = command.name or key
255256
local description = command.description or {MOD_NAME .. "-commands." .. command_name}
257+
command_name = '/' .. command_name
256258
new_settings[#new_settings + 1] = {
257259
type = "bool-setting",
258260
name = MOD_SHORT_NAME .. key,
259261
setting_type = "runtime-global",
260262
default_value = command.default_value or true,
261-
localised_name = '/' .. command_name,
262-
localised_description = {'', '/' .. command_name, ' ', description}
263+
localised_name = command_name,
264+
localised_description = {'', command_name, ' ', description}
263265
}
264266
end
265267
if #new_settings > 0 then

switchable-commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Returns tables of commands without functions as command "settings". All paramete
1212
"team" - Stops execution if can't find a team (force) by parameter
1313
allow_for_server :: boolean: Allow execution of a command from a server (default: false)
1414
only_for_admin :: boolean: The command can be executed only by admins (default: false)
15-
default_value :: boolean: default value for settings (default: false)
15+
default_value :: boolean: default value for settings (default: true)
1616
]]--
1717
---@type table<string, table>
1818
return {

0 commit comments

Comments
 (0)