2022-05-18 03:31:08 +02:00
-- ███████ █████ ███████ ███████ ██████ ███████ ██████ ██ ██████ ███ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
-- ███████ ███████ █████ █████ ██████ █████ ██ ███ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ███████ ██ ██ ██ ███████ ██ ██ ███████ ██████ ██ ██████ ██ ████
2023-08-03 15:37:05 +02:00
---
-- @module worldeditadditions_core
2022-05-18 03:31:08 +02:00
local worldedit_command_y , worldedit_command_n
if minetest.global_exists ( " worldedit " ) then
2023-07-09 20:49:38 +02:00
worldedit_command_y = minetest.registered_chatcommands [ " /y " ] . func
worldedit_command_n = minetest.registered_chatcommands [ " /n " ] . func
2022-05-18 03:31:08 +02:00
end
--- A table that holds at most 1 pending function call per player.
2023-07-07 02:31:06 +02:00
-- @value table<string,function>
-- @internal
2022-05-18 03:31:08 +02:00
local pending_calls = { }
--- Captures the given function in the safe_region subsystem for later execution.
2023-08-03 15:37:05 +02:00
-- CAUTION: This command is not available for general use.
-- @internal
2022-05-18 03:31:08 +02:00
-- @param player_name string The name of the player.
-- @param cmdname string The name of the command being executed.
2023-08-03 15:37:05 +02:00
-- @param func function The function to execute later. Will be passed NO ARGUMENTS should it ever get executed in the future (though its future execution is not guaranteed).
2022-05-18 03:31:08 +02:00
-- @returns nil
local function safe_region ( player_name , cmdname , func )
pending_calls [ player_name ] = {
cmdname = cmdname ,
func = func
}
end
minetest.override_chatcommand ( " /y " , {
params = " " ,
description = " Run a pending operation that was captured by the safe region system earlier. " ,
func = function ( player_name )
if pending_calls [ player_name ] == nil then
if minetest.global_exists ( " worldedit " ) and worldedit_command_y ~= nil then
worldedit_command_y ( player_name )
else
worldedit.player_notify ( player_name , " There aren't any pending operations at the moment. " )
end
else
pending_calls [ player_name ] . func ( )
pending_calls [ player_name ] = nil
end
end
} )
minetest.override_chatcommand ( " /n " , {
params = " " ,
description = " Abort a pending operation that was captured by the safe region system. " ,
func = function ( player_name )
if pending_calls [ player_name ] == nil then
if minetest.global_exists ( " worldedit " ) and worldedit_command_y ~= nil then
worldedit_command_n ( player_name )
else
worldedit.player_notify ( player_name , " There aren't any operations pending, so there's nothing to abort. " )
end
else
worldedit.player_notify ( player_name , " Aborting captured command / " .. pending_calls [ player_name ] . cmdname .. " . " )
pending_calls [ player_name ] = nil
end
end
} )
return safe_region