2024-10-16 23:27:33 +00:00
local wea_c = worldeditadditions_core
2021-07-28 02:56:45 +00:00
-- ███████ ██████ ██████
-- ██ ██ ██ ██ ██
-- █████ ██ ██ ██████
-- ██ ██ ██ ██ ██
-- ██ ██████ ██ ██
-- Process:
-- 1: Split `params_text` into two vars with unpack(wea.split(params_text,"%sdo%s"))
-- 2: Further split the two vars into two tables, one of values and the other of {command, args} sub tables
-- 3: For each entry in the values table execute each {command, args} sub table using gsub to replace "%%" in the args with the current value
-- Specs:
-- Command cluster support using ()
-- ?Basename support for values
-- ?Comma deliniation support for values
2021-07-28 20:35:52 +00:00
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
2022-09-18 21:37:26 +00:00
local start_time = wea_c.get_ms_time ( )
2021-07-28 20:35:52 +00:00
-- Execute command
params.cmd . func ( params.player_name , params.args : gsub ( " %%+ " , v ) )
-- Finish timer and add to total
2022-09-18 21:37:26 +00:00
params.time = params.time + wea_c.get_ms_time ( ) - start_time
2021-07-28 20:35:52 +00:00
-- 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
2024-10-16 23:27:33 +00:00
wea_c.notify . ok ( params.player_name , " For " ..
2021-07-28 20:35:52 +00:00
table.concat ( params.values , " , " ) ..
" , / " .. params.cmd_name .. " completed in " ..
2022-09-18 21:37:26 +00:00
wea_c.format . human_time ( params.time ) )
2021-07-28 20:35:52 +00:00
end
end
2022-05-19 21:10:09 +00:00
worldeditadditions_core.register_command ( " for " , {
2021-07-28 02:56:45 +00:00
params = " <value1> <value2> <value3>... do //<command> <arg> %% <arg> " ,
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. " ,
privs = { worldedit = true } ,
parse = function ( params_text )
if not params_text : match ( " %sdo%s " ) then
return false , " Error: \" do \" argument is not present. "
end
2022-09-18 21:37:26 +00:00
local parts = wea_c.split ( params_text , " %sdo%s " )
2023-05-24 20:03:41 +00:00
if parts [ 1 ] == " " then
2021-07-28 02:56:45 +00:00
return false , " Error: No values specified. "
end
if not parts [ 2 ] then
return false , " Error: No command specified. "
end
2022-09-18 21:37:26 +00:00
local values = wea_c.split ( parts [ 1 ] , " %s " )
2021-07-28 02:56:45 +00:00
local command , args = parts [ 2 ] : match ( " /([^%s]+)%s*(.*)$ " )
if not args then args = " "
2022-09-18 21:37:26 +00:00
else args = wea_c.trim ( args ) end
2021-07-28 02:56:45 +00:00
return true , values , command , args
end ,
func = function ( name , values , command , args )
2023-07-09 18:49:38 +00:00
local cmd = minetest.registered_chatcommands [ command ]
2021-07-28 02:56:45 +00:00
if not cmd then
return false , " Error: " .. command .. " isn't a valid command. "
end
if not minetest.check_player_privs ( name , cmd.privs ) then
2021-07-29 01:06:26 +00:00
return false , " Your privileges are insufficient to run / \" " .. command .. " \" . "
2021-07-28 02:56:45 +00:00
end
2021-07-28 20:35:52 +00:00
step ( {
player_name = name ,
cmd_name = command ,
values = values ,
cmd = cmd ,
args = args
} )
2021-07-28 02:56:45 +00:00
end
} )