From f1d10809d53aad510f8d67caf1fc52cc92c103fd Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 26 Feb 2019 08:40:48 +0000 Subject: [PATCH] Make commands.collapseArgs a little more sane We now generate a table and concatinate the elements together. This has several benefits: - We no longer emit emit trailing spaces, which caused issues on 1.13's command system. - We no longer need the error level variable, nor have the weird recursion system - it's just easier to understand. --- .../lua/rom/apis/command/commands.lua | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/command/commands.lua b/src/main/resources/assets/computercraft/lua/rom/apis/command/commands.lua index 791173692..849bf8e7c 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/command/commands.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/command/commands.lua @@ -4,17 +4,20 @@ if not commands then end native = commands.native or commands -local function collapseArgs( errorDepth, bJSONIsNBT, arg1, ... ) - if arg1 ~= nil then - if type(arg1) == "boolean" or type(arg1) == "number" or type(arg1) == "string" then - return tostring(arg1) .. " " .. collapseArgs( errorDepth + 1, bJSONIsNBT, ... ) - elseif type(arg1) == "table" then - return textutils.serialiseJSON( arg1, bJSONIsNBT ) .. " " .. collapseArgs( errorDepth + 1, bJSONIsNBT, ... ) +local function collapseArgs( bJSONIsNBT, ... ) + local args = table.pack(...) + for i = 1, #args do + local arg = args[i] + if type(arg) == "boolean" or type(arg) == "number" or type(arg) == "string" then + args[i] = tostring(arg) + elseif type(arg) == "table" then + args[i] = textutils.serialiseJSON( arg, bJSONIsNBT ) else - error( "Expected string, number, boolean or table", errorDepth ) + error( "Expected string, number, boolean or table", 3 ) end end - return "" + + return table.concat(args, " ") end -- Put native functions into the environment @@ -34,11 +37,11 @@ for n,sCommandName in ipairs(tCommands) do if env[ sCommandName ] == nil then local bJSONIsNBT = (tNonNBTJSONCommands[ sCommandName ] == nil) env[ sCommandName ] = function( ... ) - local sCommand = sCommandName .. " " .. collapseArgs( 3, bJSONIsNBT, ... ) + local sCommand = collapseArgs( bJSONIsNBT, sCommandName, ... ) return native.exec( sCommand ) end tAsync[ sCommandName ] = function( ... ) - local sCommand = sCommandName .. " " .. collapseArgs( 3, bJSONIsNBT, ... ) + local sCommand = collapseArgs( bJSONIsNBT, sCommandName, ... ) return native.execAsync( sCommand ) end end