1 local safe_region_callback = {}
\r
2 local safe_region_param = {}
\r
4 local function check_region(name, param)
\r
5 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] --obtain positions
\r
6 if pos1 == nil or pos2 == nil then
\r
7 worldedit.player_notify(name, "no region selected")
\r
10 return worldedit.volume(pos1, pos2)
\r
13 --`callback` is a callback to run when the user confirms
\r
14 --`nodes_needed` is a function accepting `param`, `pos1`, and `pos2` to calculate the number of nodes needed
\r
15 local function safe_region(callback, nodes_needed)
\r
16 --default node volume calculation
\r
17 nodes_needed = nodes_needed or check_region
\r
19 return function(name, param)
\r
20 --check if the operation applies to a safe number of nodes
\r
21 local count = nodes_needed(name, param)
\r
22 if count == nil then return end --invalid command
\r
23 if count < 10000 then
\r
24 return callback(name, param)
\r
27 --save callback to call later
\r
28 safe_region_callback[name], safe_region_param[name] = callback, param
\r
29 worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel")
\r
33 local function reset_pending(name)
\r
34 safe_region_callback[name], safe_region_param[name] = nil, nil
\r
37 minetest.register_chatcommand("/y", {
\r
39 description = "Confirm a pending operation",
\r
40 func = function(name)
\r
41 local callback, param = safe_region_callback[name], safe_region_param[name]
\r
42 if not callback then
\r
43 worldedit.player_notify(name, "no operation pending")
\r
47 safe_region_callback[name], safe_region_param[name] = nil, nil --reset pending operation
\r
48 callback(name, param)
\r
52 minetest.register_chatcommand("/n", {
\r
54 description = "Confirm a pending operation",
\r
55 func = function(name)
\r
56 if not safe_region_callback[name] then
\r
57 worldedit.player_notify(name, "no operation pending")
\r
60 safe_region_callback[name], safe_region_param[name] = nil, nil
\r
65 return safe_region, check_region, reset_pending
\r