Minetest-WorldEditAdditions/worldeditadditions_commands/commands/meta/multi.lua
Starbeamrainbowlabs 48619163d5
//multi: gracefully handle empty invocations; ensure args is always set
if it isn't found via the :find() call, then set it to an empty string.
This prevents crashes where the callee expects to be passed a string
2021-03-11 02:04:22 +00:00

91 lines
3.2 KiB
Lua

--- Executes multiple worldedit commands in sequence.
-- @module worldeditadditions.multi
-- explode(separator, string)
-- From http://lua-users.org/wiki/SplitJoin
local function explode(delim, str)
local ll, is_done
local delim_length = string.len(delim)
ll = 0
is_done = false
return function()
if is_done then return end
local result = nil
local loc = string.find(str, delim, ll, true) -- find the next d in the string
if loc ~= nil then -- if "not not" found then..
result = string.sub(str, ll, loc - 1) -- Save it in our array.
ll = loc + delim_length -- save just after where we found it for searching next time.
else
result = string.sub(str, ll) -- Save what's left in our array.
is_done = true
end
return result
end
end
-- From http://lua-users.org/wiki/StringTrim
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
minetest.register_chatcommand("/multi", {
params = "/<command_a> <args> //<command_b> <args> /<command_c> <args>.....",
description = "Executes multiple chat commands in sequence. Just prepend a list of space-separated chat commands with //multi, and you're good to go! The forward slashes at the beginning of each chat command must be the same as if you were executing it normally.",
privs = { worldedit = true },
func = function(name, params_text)
if not params_text then return false, "Error: No commands specified, so there's nothing to do." end
params_text = worldeditadditions.trim(params_text)
if #params_text == 0 then return false, "Error: No commands specified, so there's nothing to do." end
local i = 1 -- For feedback only
local master_start_time = worldeditadditions.get_ms_time()
local times = {}
-- Things start at 1, not 0 in Lua :-(
for command in explode(" /", string.sub(params_text, 2)) do
local start_time = worldeditadditions.get_ms_time()
local found, _, command_name, args = command:find("^([^%s]+)%s(.+)$")
if not found then command_name = command end
command_name = trim(command_name)
if not args then args = "" end
worldedit.player_notify(name, "#"..i..": /"..command)
local cmd = minetest.chatcommands[command_name]
if not cmd then
return false, "Error: "..command_name.." isn't a valid command."
end
if not minetest.check_player_privs(name, cmd.privs) then
return false, "Your privileges are insufficient to execute /"..command_name..". Abort."
end
-- print("[DEBUG] command_name", command_name, "cmd", dump2(cmd))
minetest.log("action", name.." runs "..command)
cmd.func(name, args)
times[#times + 1] = (worldeditadditions.get_ms_time() - start_time)
i = i + 1
end
local total_time = (worldeditadditions.get_ms_time() - master_start_time)
local done_message = {}
table.insert(done_message,
string.format("Executed %d commands in %s (",
#times,
worldeditadditions.human_time(total_time)
)
)
local message_parts = {}
for j=1,#times do
table.insert(message_parts, worldeditadditions.human_time(times[j]))
end
table.insert(done_message, table.concat(message_parts, ", "))
table.insert(done_message, ")")
worldedit.player_notify(name, table.concat(done_message, ""))
end
})