From eb7de9bc2ec361bfa3caa7f24359eb745b7effdf Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 00:34:02 +0100 Subject: [PATCH 01/12] Added a new command wrapper to reduce boilerplate --- utils/command.lua | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 utils/command.lua diff --git a/utils/command.lua b/utils/command.lua new file mode 100644 index 00000000..5e127a02 --- /dev/null +++ b/utils/command.lua @@ -0,0 +1,90 @@ +local Utils = require 'utils.utils' +local insert = table.insert + +local Command = {} + +---Adds a command to be executed. +--- +---Options table accepts the following structure: { +--- description = 'Teleports you to the player', +--- arguments = {'foo', 'bar'}, +--- admin_only = true, -- defaults to false +--- log_command = true, -- defaults to false unless admin only, then always true +---} +--- +---The callback receives the following arguments: +--- - arguments (indexed by name, value is extracted from the parameters) +--- - the LuaPlayer or nil if it doesn't exist +--- - the game tick in which the command was executed +--- +---@param command_name string +---@param options table +---@param callback function +function Command.add(command_name, options, callback) + local description = options.description or '[Undocumented command]' + local arguments = options.arguments or {} + local admin_only = options.admin_only or false + local log_command = options.log_command or options.admin_only or false + local argument_list = '' + + for _, argument in ipairs(arguments) do + argument_list = string.format('%s<%s> ', argument_list, argument) + end + + commands.add_command(command_name, argument_list .. description .. (admin_only and ' (Admin Only)' or ''), function (command) + local print + local player_index = command.player_index + local player = game.players[player_index] + if not player or not player.valid then + print = function (message) + log(string.format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) + end + else + print = player.print + end + + local named_arguments = {} + local from_command = {} + for param in string.gmatch(command.parameter or '', '%S+') do + insert(from_command, param) + end + + for index, argument in ipairs(arguments) do + local parameter = from_command[index] + if not parameter then + print(string.format('Argument %s from command %s is missing.', argument, command_name)) + return + end + + named_arguments[argument] = parameter + end + + if log_command then + log(string.format( + '[%s Command] %s, used: %s %s', + admin_only and 'Admin' or 'Player', + player and player.valid and player.name or '', + command_name, + serpent.line(named_arguments) + )) + end + + if _DEBUG then + -- in debug mode it will crash and report errors directly + callback(named_arguments, player, command.tick) + return + end + + -- safety check for the command + local success, error = pcall(function () + callback(named_arguments, player, command.tick) + end) + + if not success then + log(error) + print(string.format('There was an error running %s, it has been logged.', command_name)) + end + end) +end + +return Command From 1dcc5cefdc2cf52a0f42bdcac55cbd1e99892eb4 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 14:47:33 +0100 Subject: [PATCH 02/12] Ensured player can be nil in case of being the server --- utils/command.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 5e127a02..cf481991 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -1,4 +1,3 @@ -local Utils = require 'utils.utils' local insert = table.insert local Command = {} @@ -14,7 +13,7 @@ local Command = {} --- ---The callback receives the following arguments: --- - arguments (indexed by name, value is extracted from the parameters) ---- - the LuaPlayer or nil if it doesn't exist +--- - the LuaPlayer or nil if it doesn't exist (such as the server player) --- - the game tick in which the command was executed --- ---@param command_name string @@ -32,9 +31,9 @@ function Command.add(command_name, options, callback) end commands.add_command(command_name, argument_list .. description .. (admin_only and ' (Admin Only)' or ''), function (command) - local print + local print -- custom print reference in case no player is present local player_index = command.player_index - local player = game.players[player_index] + local player = game.player if not player or not player.valid then print = function (message) log(string.format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) @@ -81,7 +80,7 @@ function Command.add(command_name, options, callback) end) if not success then - log(error) + log(string.format('Error while running %s: %s', command_name, error)) print(string.format('There was an error running %s, it has been logged.', command_name)) end end) From bd1425956f19e5bda4e6d5d9237af7dfc1de60f0 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 14:59:49 +0100 Subject: [PATCH 03/12] Added admin check and aggregate errors --- utils/command.lua | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index cf481991..0b525a45 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -40,6 +40,11 @@ function Command.add(command_name, options, callback) end else print = player.print + + if admin_only and not player.admin then + print(string.format('The %s command is only available to admins.', command_name)) + return + end end local named_arguments = {} @@ -48,14 +53,26 @@ function Command.add(command_name, options, callback) insert(from_command, param) end + local errors = {} + for index, argument in ipairs(arguments) do local parameter = from_command[index] if not parameter then - print(string.format('Argument %s from command %s is missing.', argument, command_name)) - return + insert(errors, string.format('Argument %s from command %s is missing.', argument, command_name)) + else + named_arguments[argument] = parameter end + end - named_arguments[argument] = parameter + local return_early = false + + for _, error in ipairs(errors) do + return_early = true + print(error) + end + + if return_early then + return end if log_command then From 749c9ad15b2f93db773e263d0808b29746b62a33 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 15:35:31 +0100 Subject: [PATCH 04/12] Added changelog note --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1bb383c..38234698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ### Internal - [Core] Added a new command wrapper #443 - [Core] Overhaul utils and add minor functionality #464 +- [Core] Added a new command wrapper to reduce boilerplate #443 ## v1.1.0 - Persian Longhair From 9d35776849bae0a286f218a9dab32edf49cc295d Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 20:44:02 +0100 Subject: [PATCH 05/12] Server/Debug/Player only flags and Debug extraction --- map_gen/Diggy/Debug.lua | 70 ++++----------------------------- map_gen/Diggy/Scenario.lua | 15 +------- map_gen/combined/diggy.lua | 2 +- utils/command.lua | 79 ++++++++++++++++++++++++++++++++------ utils/debug.lua | 49 +++++++++++++++++++++++ 5 files changed, 127 insertions(+), 88 deletions(-) create mode 100644 utils/debug.lua diff --git a/map_gen/Diggy/Debug.lua b/map_gen/Diggy/Debug.lua index 83b21740..bd7674ef 100644 --- a/map_gen/Diggy/Debug.lua +++ b/map_gen/Diggy/Debug.lua @@ -1,4 +1,5 @@ -- dependencies +local BaseDebug = require 'utils.debug' local min = math.min local max = math.max local floor = math.floor @@ -10,76 +11,19 @@ local Debug = {} local default_base_color = {r = 1, g = 1, b = 1} local default_delta_color = {r = 0, g = 0, b = 0} --- private state -local debug = false -local cheats = false - -function Debug.enable_debug() - debug = true -end - -function Debug.disable_debug() - debug = false -end - -function Debug.enable_cheats() - cheats = true -end - -function Debug.disable_cheats() - cheats = true -end - -global.message_count = 0 - ---[[-- - Shows the given message if debug is enabled. - - @param message string -]] +---@deprecated use 'utils.debug'.print instead function Debug.print(message) - if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end - global.message_count = global.message_count + 1 - if (debug) then - game.print('[' .. global.message_count .. '] ' .. tostring(message)) - log('[' .. global.message_count .. '] ' .. tostring(message)) - end + BaseDebug.print(message) end ---[[-- - Shows the given message with serpent enabled, if debug is enabled. - - @param message string -]] -function Debug.print_serpent(message) - Debug.print(serpent.line(message)) -end - ---[[-- - Shows the given message if debug is on. - - @param x number - @param y number - @param message string -]] +---@deprecated use 'utils.debug'.print_position instead function Debug.print_position(position, message) - message = message or '' - if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end - global.message_count = global.message_count + 1 - if (debug) then - game.print('[' .. global.message_count .. '] {x=' .. position.x .. ', y=' .. position.y .. '} ' .. tostring(message)) - end + BaseDebug.print_position(position, message) end ---[[-- - Executes the given callback if cheating is enabled. - - @param callback function -]] +---@deprecated use 'utils.debug'.cheat instead function Debug.cheat(callback) - if (cheats) then - callback() - end + BaseDebug.cheat(callback) end --[[-- diff --git a/map_gen/Diggy/Scenario.lua b/map_gen/Diggy/Scenario.lua index 1d564335..c0e1c404 100644 --- a/map_gen/Diggy/Scenario.lua +++ b/map_gen/Diggy/Scenario.lua @@ -1,6 +1,5 @@ -- dependencies local Config = require 'map_gen.Diggy.Config' -local Debug = require 'map_gen.Diggy.Debug' local ScenarioInfo = require 'features.gui.info' local Event = require 'utils.event' @@ -36,10 +35,8 @@ local function each_enabled_feature(if_enabled) end end ---[[-- - Register the events required to initialize the scenario. -]] -function Scenario.register(debug, cheats) +---Register the events required to initialize the scenario. +function Scenario.register() if global.diggy_scenario_registered then error('Cannot register the Diggy scenario multiple times.') return @@ -50,14 +47,6 @@ function Scenario.register(debug, cheats) global.config.fish_market.enable = nil end - if debug then - Debug.enable_debug() - end - - if cheats then - Debug.enable_cheats() - end - local extra_map_info = '' each_enabled_feature( diff --git a/map_gen/combined/diggy.lua b/map_gen/combined/diggy.lua index bab23ec3..8bd1c9e0 100644 --- a/map_gen/combined/diggy.lua +++ b/map_gen/combined/diggy.lua @@ -1,2 +1,2 @@ -- authors Linaori, valansch -require 'map_gen.Diggy.Scenario'.register(_DEBUG, _CHEATS) +require 'map_gen.Diggy.Scenario'.register() diff --git a/utils/command.lua b/utils/command.lua index 0b525a45..89755eeb 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -1,13 +1,18 @@ local insert = table.insert +local format = string.format local Command = {} ---Adds a command to be executed. --- ---Options table accepts the following structure: { ---- description = 'Teleports you to the player', ---- arguments = {'foo', 'bar'}, +--- description = 'A description of the command', +--- arguments = {'foo', 'bar'}, -- maps arguments to these names in the given sequence +--- default_values = {'bar' = nil}, -- gives a default value to 'bar' when omitted --- admin_only = true, -- defaults to false +--- debug_only = false, -- only registers it if _DEBUG is set to true when false +--- allowed_by_server = false -- lets the server execute this, defaults to false +--- allowed_by_player = true -- lets players execute this, defaults to true --- log_command = true, -- defaults to false unless admin only, then always true ---} --- @@ -22,27 +27,69 @@ local Command = {} function Command.add(command_name, options, callback) local description = options.description or '[Undocumented command]' local arguments = options.arguments or {} + local default_values = options.default_values or {} local admin_only = options.admin_only or false + local debug_only = options.debug_only or false + local allowed_by_server = options.allowed_by_server or false + local allowed_by_player = options.allowed_by_player local log_command = options.log_command or options.admin_only or false local argument_list = '' - for _, argument in ipairs(arguments) do - argument_list = string.format('%s<%s> ', argument_list, argument) + if nil == options.allowed_by_player then + allowed_by_player = true end - commands.add_command(command_name, argument_list .. description .. (admin_only and ' (Admin Only)' or ''), function (command) + if not _DEBUG and debug_only then + return + end + + if not allowed_by_player and not allowed_by_server then + error(format("The command '%s' is not allowed by the server nor player, please enable at least one of them.", command_name)) + end + + for _, argument_name in ipairs(arguments) do + local argument_display = argument_name + for default_value_name, _ in pairs(default_values) do + if default_value_name == argument_name then + argument_display = argument_display .. ':optional' + break + end + end + + argument_list = format('%s<%s> ', argument_list, argument_display) + end + + local extra = '' + + if allowed_by_server and not allowed_by_player then + extra = ' (Server Only)' + elseif allowed_by_player and admin_only then + extra = ' (Admin Only)' + end + + commands.add_command(command_name, argument_list .. description .. extra, function (command) local print -- custom print reference in case no player is present local player_index = command.player_index local player = game.player if not player or not player.valid then print = function (message) - log(string.format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) + log(format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) + end + + if not allowed_by_server then + log(format("The command '%s' is not allowed to be executed by the server.", command_name)) + return end else print = player.print + if not allowed_by_player then + print(format("The command '%s' is not allowed to be executed by players.", command_name)) + return + end + if admin_only and not player.admin then - print(string.format('The %s command is only available to admins.', command_name)) + print(format("The command '%s' requires an admin to be be executed", command_name)) return end end @@ -57,8 +104,18 @@ function Command.add(command_name, options, callback) for index, argument in ipairs(arguments) do local parameter = from_command[index] + if not parameter then - insert(errors, string.format('Argument %s from command %s is missing.', argument, command_name)) + for default_value_name, default_value in pairs(default_values) do + if default_value_name == argument then + parameter = default_value + break + end + end + end + + if not parameter then + insert(errors, format('Argument %s from command %s is missing.', argument, command_name)) else named_arguments[argument] = parameter end @@ -76,7 +133,7 @@ function Command.add(command_name, options, callback) end if log_command then - log(string.format( + log(format( '[%s Command] %s, used: %s %s', admin_only and 'Admin' or 'Player', player and player.valid and player.name or '', @@ -97,8 +154,8 @@ function Command.add(command_name, options, callback) end) if not success then - log(string.format('Error while running %s: %s', command_name, error)) - print(string.format('There was an error running %s, it has been logged.', command_name)) + log(format('Error while running %s: %s', command_name, error)) + print(format('There was an error running %s, it has been logged.', command_name)) end end) end diff --git a/utils/debug.lua b/utils/debug.lua new file mode 100644 index 00000000..aee24706 --- /dev/null +++ b/utils/debug.lua @@ -0,0 +1,49 @@ +-- dependencies +local format = string.format +local serialize = serpent.line + +-- this +local Debug = {} + +global.debug_message_count = 0 + +---@return number next index +local function increment() + local next = global.debug_message_count + 1 + global.debug_message_count = next + + return next +end + +---Shows the given message if debug is enabled. Uses serpent to print non scalars. +---@param message table +function Debug.print(message) + if not _DEBUG then + return + end + + if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then + message = serialize(message) + end + + message = format('[%d] %s', increment(), tostring(message)) + game.print(message) + log(message) +end + +---Shows the given message if debug is on. +---@param position Position +---@param message string +function Debug.print_position(position, message) + Debug.print(format('%s %s', serialize(position), message)) +end + +---Executes the given callback if cheating is enabled. +---@param callback function +function Debug.cheat(callback) + if _CHEATS then + callback() + end +end + +return Debug From 8c0874a10dcc7988cf4f7d755a7415ce6b226866 Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 21:41:05 +0100 Subject: [PATCH 06/12] Added support to capture excess arguments as sentence --- utils/command.lua | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 89755eeb..c27d6ab3 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -1,4 +1,7 @@ +require 'utils.list_utils' + local insert = table.insert +local size = table.size local format = string.format local Command = {} @@ -14,6 +17,7 @@ local Command = {} --- allowed_by_server = false -- lets the server execute this, defaults to false --- allowed_by_player = true -- lets players execute this, defaults to true --- log_command = true, -- defaults to false unless admin only, then always true +--- capture_excess_arguments = true, defaults to false, captures excess arguments in the last argument, useful for sentences ---} --- ---The callback receives the following arguments: @@ -30,9 +34,11 @@ function Command.add(command_name, options, callback) local default_values = options.default_values or {} local admin_only = options.admin_only or false local debug_only = options.debug_only or false + local capture_excess_arguments = options.capture_excess_arguments or false local allowed_by_server = options.allowed_by_server or false local allowed_by_player = options.allowed_by_player local log_command = options.log_command or options.admin_only or false + local argument_list_size = size(arguments) local argument_list = '' if nil == options.allowed_by_player then @@ -47,7 +53,7 @@ function Command.add(command_name, options, callback) error(format("The command '%s' is not allowed by the server nor player, please enable at least one of them.", command_name)) end - for _, argument_name in ipairs(arguments) do + for index, argument_name in ipairs(arguments) do local argument_display = argument_name for default_value_name, _ in pairs(default_values) do if default_value_name == argument_name then @@ -56,6 +62,10 @@ function Command.add(command_name, options, callback) end end + if argument_list_size == index and capture_excess_arguments then + argument_display = argument_display .. ':sentence' + end + argument_list = format('%s<%s> ', argument_list, argument_display) end @@ -96,8 +106,18 @@ function Command.add(command_name, options, callback) local named_arguments = {} local from_command = {} + local raw_parameter_index = 1 for param in string.gmatch(command.parameter or '', '%S+') do - insert(from_command, param) + if capture_excess_arguments and raw_parameter_index == argument_list_size then + if not from_command[raw_parameter_index] then + from_command[raw_parameter_index] = param + else + from_command[raw_parameter_index] = from_command[raw_parameter_index] .. ' ' .. param + end + else + from_command[raw_parameter_index] = param + raw_parameter_index = raw_parameter_index + 1 + end end local errors = {} From 8cdf747a2e66a6f2b1b8ef4e1f3387605332015b Mon Sep 17 00:00:00 2001 From: Lynn Date: Sat, 24 Nov 2018 23:33:10 +0100 Subject: [PATCH 07/12] Improved debug logging and error reporting during development --- utils/command.lua | 58 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index c27d6ab3..8f2591da 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -3,9 +3,39 @@ require 'utils.list_utils' local insert = table.insert local size = table.size local format = string.format +local next = next +local serialize = serpent.line local Command = {} +local option_names = { + ['description'] = true, + ['arguments'] = true, + ['default_values'] = true, + ['admin_only'] = true, + ['debug_only'] = true, + ['allowed_by_server'] = true, + ['allowed_by_player'] = true, + ['log_command'] = true, + ['capture_excess_arguments'] = true, +} + +---Validates if there aren't any wrong fields in the options. +---@param command_name string +---@param options table +local function assert_existing_options(command_name, options) + local invalid = {} + for name, _ in pairs(options) do + if not option_names[name] then + insert(invalid, name) + end + end + + if next(invalid) then + error(format("The following options were given to the command '%s' but are invalid: %s", command_name, serialize(invalid))) + end +end + ---Adds a command to be executed. --- ---Options table accepts the following structure: { @@ -41,6 +71,8 @@ function Command.add(command_name, options, callback) local argument_list_size = size(arguments) local argument_list = '' + assert_existing_options(command_name, options) + if nil == options.allowed_by_player then allowed_by_player = true end @@ -81,6 +113,7 @@ function Command.add(command_name, options, callback) local print -- custom print reference in case no player is present local player_index = command.player_index local player = game.player + local player_name = player and player.valid and player.name or '' if not player or not player.valid then print = function (message) log(format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) @@ -153,29 +186,22 @@ function Command.add(command_name, options, callback) end if log_command then - log(format( - '[%s Command] %s, used: %s %s', - admin_only and 'Admin' or 'Player', - player and player.valid and player.name or '', - command_name, - serpent.line(named_arguments) - )) + log(format('[%s Command] %s, used: %s %s', admin_only and 'Admin' or 'Player', player_name, command_name, serialize(named_arguments))) end - if _DEBUG then - -- in debug mode it will crash and report errors directly - callback(named_arguments, player, command.tick) - return - end - - -- safety check for the command local success, error = pcall(function () callback(named_arguments, player, command.tick) end) if not success then - log(format('Error while running %s: %s', command_name, error)) - print(format('There was an error running %s, it has been logged.', command_name)) + local serialized_arguments = serialize(named_arguments) + if _DEBUG then + game.print(format("%s triggered an error running a command and has been logged: '%s' with arguments %s", player_name, command_name, serialized_arguments)) + game.print(error) + else + print(format('There was an error running %s, it has been logged.', command_name)) + end + log(format("Error while running '%s' with arguments %s: %s", command_name, serialized_arguments, error)) end end) end From f88b05448ee43c44a25effc92b28447204054573 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 30 Nov 2018 18:50:57 +0100 Subject: [PATCH 08/12] Using global print if server --- utils/command.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 8f2591da..03c21992 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -111,16 +111,13 @@ function Command.add(command_name, options, callback) commands.add_command(command_name, argument_list .. description .. extra, function (command) local print -- custom print reference in case no player is present - local player_index = command.player_index local player = game.player local player_name = player and player.valid and player.name or '' if not player or not player.valid then - print = function (message) - log(format('Trying to print message to player #%d, but not such player found: %s', player_index, message)) - end + print = _G.print if not allowed_by_server then - log(format("The command '%s' is not allowed to be executed by the server.", command_name)) + print(format("The command '%s' is not allowed to be executed by the server.", command_name)) return end else @@ -196,11 +193,12 @@ function Command.add(command_name, options, callback) if not success then local serialized_arguments = serialize(named_arguments) if _DEBUG then - game.print(format("%s triggered an error running a command and has been logged: '%s' with arguments %s", player_name, command_name, serialized_arguments)) - game.print(error) - else - print(format('There was an error running %s, it has been logged.', command_name)) + print(format("%s triggered an error running a command and has been logged: '%s' with arguments %s", player_name, command_name, serialized_arguments)) + print(error) + return end + + print(format('There was an error running %s, it has been logged.', command_name)) log(format("Error while running '%s' with arguments %s: %s", command_name, serialized_arguments, error)) end end) From 1f99757654e220e39560cdb07725641848b9e09a Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 30 Nov 2018 19:15:11 +0100 Subject: [PATCH 09/12] Added a bit more documentation --- utils/command.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 03c21992..9ac4644f 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -9,15 +9,15 @@ local serialize = serpent.line local Command = {} local option_names = { - ['description'] = true, - ['arguments'] = true, - ['default_values'] = true, - ['admin_only'] = true, - ['debug_only'] = true, - ['allowed_by_server'] = true, - ['allowed_by_player'] = true, - ['log_command'] = true, - ['capture_excess_arguments'] = true, + ['description'] = 'A description of the command', + ['arguments'] = 'A table of arguments, example: {"foo", "bar"} would map the first 2 arguments to foo and bar', + ['default_values'] = 'A default value for a given argument when omitted, example: {bar = false}', + ['admin_only'] = 'Set this to true if only admins may execute this command', + ['debug_only'] = 'Set this to true if it should only be registered when _DEBUG is true', + ['allowed_by_server'] = 'Set to true if the server (host) may execute this command', + ['allowed_by_player'] = 'Set to false to disable players from executing this command', + ['log_command'] = 'Set to true to log commands. Always true when admin_only is enabled', + ['capture_excess_arguments'] = 'Allows the last argument to be the remaining text in the command', } ---Validates if there aren't any wrong fields in the options. @@ -41,7 +41,7 @@ end ---Options table accepts the following structure: { --- description = 'A description of the command', --- arguments = {'foo', 'bar'}, -- maps arguments to these names in the given sequence ---- default_values = {'bar' = nil}, -- gives a default value to 'bar' when omitted +--- default_values = {bar = false}, -- gives a default value to 'bar' when omitted --- admin_only = true, -- defaults to false --- debug_only = false, -- only registers it if _DEBUG is set to true when false --- allowed_by_server = false -- lets the server execute this, defaults to false From bfe804e2f02425b44c30f18573aed09f545a0aef Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 30 Nov 2018 19:21:46 +0100 Subject: [PATCH 10/12] ipairs -> pairs --- utils/command.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index 9ac4644f..5ff63860 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -85,7 +85,7 @@ function Command.add(command_name, options, callback) error(format("The command '%s' is not allowed by the server nor player, please enable at least one of them.", command_name)) end - for index, argument_name in ipairs(arguments) do + for index, argument_name in pairs(arguments) do local argument_display = argument_name for default_value_name, _ in pairs(default_values) do if default_value_name == argument_name then @@ -152,7 +152,7 @@ function Command.add(command_name, options, callback) local errors = {} - for index, argument in ipairs(arguments) do + for index, argument in pairs(arguments) do local parameter = from_command[index] if not parameter then @@ -173,7 +173,7 @@ function Command.add(command_name, options, callback) local return_early = false - for _, error in ipairs(errors) do + for _, error in pairs(errors) do return_early = true print(error) end From cb3c9ba1d0b2032a41d47c34a4c13ec7a0dddb09 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 30 Nov 2018 19:25:05 +0100 Subject: [PATCH 11/12] Fixed refactored util import --- utils/command.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/command.lua b/utils/command.lua index 5ff63860..be43bdc7 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -1,4 +1,4 @@ -require 'utils.list_utils' +require 'utils.table' local insert = table.insert local size = table.size From fcbae24ff0ad1b0621aaadde219976a2acdac179 Mon Sep 17 00:00:00 2001 From: Lynn Date: Fri, 30 Nov 2018 19:29:06 +0100 Subject: [PATCH 12/12] table.size -> table_size --- utils/command.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/utils/command.lua b/utils/command.lua index be43bdc7..ec8ef7f1 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -1,7 +1,6 @@ require 'utils.table' local insert = table.insert -local size = table.size local format = string.format local next = next local serialize = serpent.line @@ -68,7 +67,7 @@ function Command.add(command_name, options, callback) local allowed_by_server = options.allowed_by_server or false local allowed_by_player = options.allowed_by_player local log_command = options.log_command or options.admin_only or false - local argument_list_size = size(arguments) + local argument_list_size = table_size(arguments) local argument_list = '' assert_existing_options(command_name, options)