From 43a59b68ff21a46db1bb58839e6075c49b572d4c Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 28 Jul 2021 13:35:52 -0700 Subject: [PATCH] converted //for to step function --- .../commands/meta/for.lua | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/worldeditadditions_commands/commands/meta/for.lua b/worldeditadditions_commands/commands/meta/for.lua index bbc6e4f..14deaa8 100644 --- a/worldeditadditions_commands/commands/meta/for.lua +++ b/worldeditadditions_commands/commands/meta/for.lua @@ -14,6 +14,37 @@ -- ?Comma deliniation support for values local wea = worldeditadditions +local function step(params) + -- Initialize additional params on first call + if not params.first then + params.i = 1 -- Iteration number + params.time = 0 -- Total execution time + params.first = true + end + + -- Load current value to use + local v = params.values[params.i] + + -- Start a timer + local start_time = wea.get_ms_time() + -- Execute command + params.cmd.func(params.player_name, params.args:gsub("%%+",v)) + -- Finish timer and add to total + params.time = params.time + wea.get_ms_time() - start_time + -- Increment iteration state + params.i = params.i + 1 + + if params.i <= #params.values then + -- If we haven't run out of values call function again + minetest.after(0, step, params) + else + worldedit.player_notify(params.player_name, "For ".. + table.concat(params.values,", ").. + ", /"..params.cmd_name.." completed in " .. + wea.format.human_time(params.time)) + end +end + worldedit.register_command("for", { params = " ... do // %% ", description = "Executes a chat command for each value before \" do \" replacing any instances of \"%%\" with those values. The forward slashes at the beginning of the chat command must be the same as if you were executing it normally.", @@ -44,14 +75,14 @@ worldedit.register_command("for", { if not minetest.check_player_privs(name, cmd.privs) then return false, "Your privileges are insufficient to run \""..command.."\"." end - -- Start a timer - local start_time = wea.get_ms_time() - for _,v in pairs(values) do - cmd.func(name, args:gsub("%%+",v)) - end - -- Finish timer - local time_taken = wea.get_ms_time() - start_time - return true, "For "..table.concat(values,", ")..", command completed in " .. wea.format.human_time(time_taken) + step({ + player_name = name, + cmd_name = command, + values = values, + cmd = cmd, + args = args + }) + end })