Minetest-WorldEditAdditions/worldeditadditions_commands/commands/meta/many.lua

102 lines
3.3 KiB
Lua
Raw Normal View History

-- Executes multiple worldedit commands in sequence.
2022-09-18 21:37:26 +00:00
local wea_c = worldeditadditions_core
2020-08-11 20:39:28 +00:00
local function step(params)
2022-09-18 21:37:26 +00:00
local start_time = wea_c.get_ms_time()
2020-08-11 20:39:28 +00:00
local full_cmd = params.cmd_name.." "..params.args
2024-10-17 02:22:34 +00:00
wea_c.notify.info(params.name, string.format("[ many | /%s ] %d / %d (~%.2f%%) complete | last: %s, average: %s, ETA: ~%s",
full_cmd,
(params.i + 1), params.count,
((params.i + 1) / params.count)*100,
2022-09-18 21:37:26 +00:00
wea_c.format.human_time(params.times[#params.times] or 0),
wea_c.format.human_time(wea_c.average(params.times)),
wea_c.format.human_time(wea_c.eta(
2021-02-07 02:45:34 +00:00
params.times,
params.i,
params.count
))
))
2020-08-11 20:39:28 +00:00
local cmd = minetest.registered_chatcommands[params.cmd_name]
2020-08-11 20:39:28 +00:00
2020-08-11 20:47:52 +00:00
minetest.log("action", params.name.." runs "..full_cmd.." (time "..tostring(params.i).." of "..tostring(params.count)..")")
2020-08-11 20:39:28 +00:00
cmd.func(params.name, params.args)
2022-09-18 21:37:26 +00:00
table.insert(params.times, wea_c.get_ms_time() - start_time)
2020-08-11 20:39:28 +00:00
params.i = params.i + 1
if params.i < params.count then
minetest.after(0, step, params)
else
2022-09-18 21:37:26 +00:00
local total_time = (wea_c.get_ms_time() - params.master_start_time)
2020-08-11 20:39:28 +00:00
local done_message = {}
table.insert(done_message,
string.format("Executed '"..full_cmd.."' %d times in %s (~%s / time)",
2020-08-11 20:39:28 +00:00
#params.times,
2022-09-18 21:37:26 +00:00
wea_c.format.human_time(total_time),
wea_c.format.human_time(
wea_c.average(params.times)
2020-08-11 20:39:28 +00:00
)
)
)
-- Don't drown the player if the command was executed many times
if #params.times < 10 then
local message_parts = {}
for j=1,#params.times do
2022-09-18 21:37:26 +00:00
table.insert(message_parts, wea_c.format.human_time(params.times[j]))
2020-08-11 20:39:28 +00:00
end
table.insert(done_message, "; ")
table.insert(done_message, table.concat(message_parts, ", "))
end
table.insert(done_message, ")")
2024-10-17 02:22:34 +00:00
wea_c.notify.ok(params.name, table.concat(done_message, ""))
2020-08-11 20:39:28 +00:00
end
end
minetest.register_chatcommand("/many", {
params = "<times> /<command_a> <args>",
description = "Executes a single chat command multiple times. The number of times to repeat the command should be specified first. The command to execute follows, and the forward slashes at the beginning thereof must be the same as if you were executing it normally. Note that this command yields with minetest.after to allow other things to happen at the same time.",
privs = { worldedit = true },
func = function(name, params_text)
local i = 1 -- For feedback only
2022-09-18 21:37:26 +00:00
local master_start_time = wea_c.get_ms_time()
2020-08-11 20:39:28 +00:00
local times = {}
local count, cmd_name, args = params_text:match("^(%d+)%s([^%s]+)%s(.+)$")
if not count then
count, cmd_name = params_text:match("^(%d+)%s([^%s]+)$")
if not count then return false, "Error: Invalid syntax" end
end
if not args then args = "" end
2022-09-18 21:37:26 +00:00
args = wea_c.trim(args)
-- print("[many] count", count, "cmd_name", cmd_name, "args", args)
2020-08-11 20:39:28 +00:00
count = tonumber(count)
2022-09-18 21:37:26 +00:00
cmd_name = wea_c.trim(cmd_name):sub(2) -- Things start at 1, not 0 in Lua :-(
2020-08-11 20:39:28 +00:00
-- Check the command we're going to execute
local cmd = minetest.registered_chatcommands[cmd_name]
2020-08-11 20:39:28 +00:00
if not cmd then
return false, "Error: "..cmd_name.." isn't a valid command."
end
if not minetest.check_player_privs(name, cmd.privs) then
return false, "Your privileges are insufficient to run that command."
end
step({
master_start_time = master_start_time,
count = count,
i = 0,
times = {},
cmd_name = cmd_name,
args = args,
name = name
})
end
})