From b7888a2b3f9a74305b26b21ea43803f4edcfb8c0 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 10 May 2020 21:52:35 +0100 Subject: [PATCH] migrate //floodfill to worldedit.register_command --- .../commands/floodfill.lua | 67 +++++++------------ .../commands/torus.lua | 2 - 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/worldeditadditions_commands/commands/floodfill.lua b/worldeditadditions_commands/commands/floodfill.lua index 237f9b0..36fbd1b 100644 --- a/worldeditadditions_commands/commands/floodfill.lua +++ b/worldeditadditions_commands/commands/floodfill.lua @@ -1,64 +1,49 @@ -local we_c = worldeditadditions_commands - -- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████ - -local function parse_params_floodfill(params_text) - if not params_text then params_text = "" end - local found, _, replace_node, radius = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") - - if found == nil then - found, _, replace_node = params_text:find("([a-z:_\\-]+)") - radius = 20 - end - if found == nil then - replace_node = "default:water_source" - end - radius = tonumber(radius) - - replace_node = worldedit.normalize_nodename(replace_node) - - return replace_node, radius -end - -minetest.register_chatcommand("/floodfill", { +worldedit.register_command("floodfill", { params = "[ []]", description = "Floods all connected nodes of the same type starting at pos1 with (which defaults to `water_source`), in a sphere with a radius of (which defaults to 20).", privs = { worldedit = true }, - func = we_c.safe_region(function(name, params_text) - local replace_node, radius = parse_params_floodfill(params_text) + require_pos = 1, + parse = function(params_text) + if not params_text then params_text = "" end + local found, _, replace_node, radius = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") + + if found == nil then + found, _, replace_node = params_text:find("([a-z:_\\-]+)") + radius = 20 + end + if found == nil then + replace_node = "default:water_source" + end + radius = tonumber(radius) + + replace_node = worldedit.normalize_nodename(replace_node) if not replace_node then worldedit.player_notify(name, "Error: Invalid node name.") return false end - if not worldedit.pos1[name] then - worldedit.player_notify(name, "Error: No pos1 defined.") - return false - end - + return true, replace_node, radius + end, + nodes_needed = function(name, replace_node, radius) + -- Volume of a hemisphere + return math.ceil(((4 * math.pi * (tonumber(radius) ^ 3)) / 3) / 2) + end, + func = function(name, replace_node, radius) local start_time = os.clock() local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node) local time_taken = os.clock() - start_time if nodes_replaced == false then - worldedit.player_notify(name, "Error: The search node is the same as the replace node.") - return false + return false, "Error: The search node is the same as the replace node." end - worldedit.player_notify(name, nodes_replaced .. " nodes replaced in " .. time_taken .. "s") minetest.log("action", name .. " used //floodfill at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. nodes_replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local replace_node, radius = parse_params_floodfill(params_text) - - if not worldedit.pos1[name] then - return 0 -- The actual command will send the error message to the client - end - -- Volume of a hemisphere - return math.ceil(((4 * math.pi * (tonumber(radius) ^ 3)) / 3) / 2) - end) + return true, nodes_replaced .. " nodes replaced in " .. time_taken .. "s" + end }) diff --git a/worldeditadditions_commands/commands/torus.lua b/worldeditadditions_commands/commands/torus.lua index 7d5839e..cbb5885 100644 --- a/worldeditadditions_commands/commands/torus.lua +++ b/worldeditadditions_commands/commands/torus.lua @@ -1,5 +1,3 @@ -local we_c = worldeditadditions_commands - -- ████████ ██████ ██████ ██ ██ ███████ -- ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██████ ██ ██ ███████