X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=worldedit_commands%2Fsafe.lua;h=c83cac8a8f7f2ba788dd396ce1e2b95efcf240f2;hb=4378750498fdaa4f862296d3a00f32138f27de27;hp=834462c8bfc7b8734b8ae3435ca08095793692c7;hpb=2784a25561be45894d2a138828013c96f387d470;p=worldedit.git diff --git a/worldedit_commands/safe.lua b/worldedit_commands/safe.lua index 834462c..c83cac8 100644 --- a/worldedit_commands/safe.lua +++ b/worldedit_commands/safe.lua @@ -1,68 +1,48 @@ -local safe_region_callback -local safe_region_name -local safe_region_param +local safe_region_callback = {} -check_region = function(name, param) - --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 nil +--`count` is the number of nodes that would possibly be modified +--`callback` is a callback to run when the user confirms +local function safe_region(name, count, callback) + if count < 20000 then + return callback() end - return worldedit.volume(pos1, pos2) + --save callback to call later + safe_region_callback[name] = callback + worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel") end ---`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 +local function reset_pending(name) + safe_region_callback[name] = nil 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 + func = function(name) + local callback = safe_region_callback[name] 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) + reset_pending(name) + callback(name) end, }) minetest.register_chatcommand("/n", { params = "", - description = "Confirm a pending operation", - func = function() - if not safe_region_callback then + description = "Abort a pending operation", + func = function(name) + if not safe_region_callback[name] then worldedit.player_notify(name, "no operation pending") return end - safe_region_callback, safe_region_name, safe_region_param = nil, nil, nil + + reset_pending(name) end, }) + + +return safe_region, reset_pending