summaryrefslogtreecommitdiff
path: root/worldedit_commands/safe.lua
blob: 6f83078582bd0a846bf322901729e8bb6cea5b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local safe_region_callback
local safe_region_name
local safe_region_param

--`callback` is a callback to run when the user confirms
--`nodes_needed` is a function accepting `param`, `pos1`, and `pos2` to calculate the number of nodes needed
safe_region = function(callback, nodes_needed)
	--default node volume calculation
	nodes_needed = nodes_needed or check_region

	return function(name, param)
		--check if the operation applies to a safe number of nodes
		local count = nodes_needed(name, param)
		if count == nil then return end --invalid command
		if count < 10000 then
			return callback(name, param)
		end

		--save callback to call later
		safe_region_callback, safe_region_name, safe_region_param = callback, name, param
		worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel")
	end
end

minetest.register_chatcommand("/y", {
	params = "",
	description = "Confirm a pending operation",
	func = function()
		local callback, name, param = safe_region_callback, safe_region_name, safe_region_param
		if not callback then
			worldedit.player_notify(name, "no operation pending")
			return
		end

		--obtain positions
		local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
		if pos1 == nil or pos2 == nil then
			worldedit.player_notify(name, "no region selected")
			return
		end

		safe_region_callback, safe_region_name, safe_region_param = nil, nil, nil --reset pending operation
		callback(name, param, pos1, pos2)
	end,
})

minetest.register_chatcommand("/n", {
	params = "",
	description = "Confirm a pending operation",
	func = function()
		if not safe_region_callback then 
			worldedit.player_notify(name, "no operation pending")
			return
		end
		safe_region_callback, safe_region_name, safe_region_param = nil, nil, nil
	end,
})