2023-07-07 00:25:42 +00:00
-- Executes multiple worldedit commands in sequence.
-- **Warning:** If a command is asynchronous (i.e. it doesn't finish when the function registered in the chat command returns), the next command will be executed before the previous one is finished! The solution to this is an implementation of Promise<any> and then ensuring that all functions registered are thenable as in JS, but this has not been implemented yet.
2022-09-18 21:37:26 +00:00
local wea_c = worldeditadditions_core
2018-10-13 23:19:32 +00:00
minetest.register_chatcommand ( " /multi " , {
2020-06-10 00:38:09 +00:00
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. " ,
2018-10-13 23:19:32 +00:00
privs = { worldedit = true } ,
func = function ( name , params_text )
2021-03-11 02:04:22 +00:00
if not params_text then return false , " Error: No commands specified, so there's nothing to do. " end
2022-09-18 21:37:26 +00:00
params_text = wea_c.trim ( params_text )
2021-03-11 02:04:22 +00:00
if # params_text == 0 then return false , " Error: No commands specified, so there's nothing to do. " end
2022-09-18 21:37:26 +00:00
local master_start_time = wea_c.get_ms_time ( )
2018-10-19 21:35:02 +00:00
local times = { }
2021-05-29 00:17:24 +00:00
-- Tokenise the input into a list of commands
2022-09-18 21:37:26 +00:00
local success , commands = wea_c.parse . tokenise_commands ( params_text )
2021-05-29 00:17:24 +00:00
if not success then return success , commands end
2023-05-24 21:00:16 +00:00
if type ( commands ) ~= " table " then return success , commands end
2021-05-29 00:17:24 +00:00
for i , command in ipairs ( commands ) do
2022-09-18 21:37:26 +00:00
local start_time = wea_c.get_ms_time ( )
2021-05-29 00:17:24 +00:00
2018-10-13 23:19:32 +00:00
local found , _ , command_name , args = command : find ( " ^([^%s]+)%s(.+)$ " )
if not found then command_name = command end
2021-05-29 00:17:24 +00:00
-- Things start at 1, not 0 in Lua :-(
2022-09-18 21:37:26 +00:00
command_name = wea_c.trim ( command_name ) : sub ( 2 ) -- Strip the leading /
2021-03-11 02:04:22 +00:00
if not args then args = " " end
2021-07-04 13:35:02 +00:00
-- print("command_name", command_name)
2018-10-13 23:19:32 +00:00
2021-05-29 00:17:24 +00:00
worldedit.player_notify ( name , " # " .. i .. " : " .. command )
2018-10-13 23:19:32 +00:00
2023-07-09 18:49:38 +00:00
local cmd = minetest.registered_chatcommands [ command_name ]
2018-10-13 23:19:32 +00:00
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
2021-05-29 00:17:24 +00:00
return false , " Your privileges are insufficient to execute " .. command_name .. " . Abort. "
2018-10-13 23:19:32 +00:00
end
2021-03-11 02:04:22 +00:00
-- print("[DEBUG] command_name", command_name, "cmd", dump2(cmd))
2018-10-13 23:19:32 +00:00
minetest.log ( " action " , name .. " runs " .. command )
cmd.func ( name , args )
2022-09-18 21:37:26 +00:00
times [ # times + 1 ] = ( wea_c.get_ms_time ( ) - start_time )
2021-05-29 00:17:24 +00:00
-- i = i + 1
2018-10-13 23:19:32 +00:00
end
2018-10-19 21:35:02 +00:00
2022-09-18 21:37:26 +00:00
local total_time = ( wea_c.get_ms_time ( ) - master_start_time )
2020-06-25 22:45:27 +00:00
local done_message = { }
table.insert ( done_message ,
string.format ( " Executed %d commands in %s ( " ,
# times ,
2022-09-18 21:37:26 +00:00
wea_c.format . human_time ( total_time )
2020-06-25 22:45:27 +00:00
)
)
local message_parts = { }
2018-10-19 21:35:02 +00:00
for j = 1 , # times do
2022-09-18 21:37:26 +00:00
table.insert ( message_parts , wea_c.format . human_time ( times [ j ] ) )
2018-10-19 21:35:02 +00:00
end
2020-06-25 22:45:27 +00:00
table.insert ( done_message , table.concat ( message_parts , " , " ) )
table.insert ( done_message , " ) " )
2018-10-19 21:35:02 +00:00
2020-06-25 22:45:27 +00:00
worldedit.player_notify ( name , table.concat ( done_message , " " ) )
2018-10-13 23:19:32 +00:00
end
} )