migrate //floodfill to worldedit.register_command

This commit is contained in:
Starbeamrainbowlabs 2020-05-10 21:52:35 +01:00
parent bdf0bdea93
commit b7888a2b3f
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 26 additions and 43 deletions

View file

@ -1,64 +1,49 @@
local we_c = worldeditadditions_commands
-- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██ -- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ -- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████ -- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████
worldedit.register_command("floodfill", {
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", {
params = "[<replace_node> [<radius>]]", params = "[<replace_node> [<radius>]]",
description = "Floods all connected nodes of the same type starting at pos1 with <replace_node> (which defaults to `water_source`), in a sphere with a radius of <radius> (which defaults to 20).", description = "Floods all connected nodes of the same type starting at pos1 with <replace_node> (which defaults to `water_source`), in a sphere with a radius of <radius> (which defaults to 20).",
privs = { worldedit = true }, privs = { worldedit = true },
func = we_c.safe_region(function(name, params_text) require_pos = 1,
local replace_node, radius = parse_params_floodfill(params_text) 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 if not replace_node then
worldedit.player_notify(name, "Error: Invalid node name.") worldedit.player_notify(name, "Error: Invalid node name.")
return false return false
end end
if not worldedit.pos1[name] then return true, replace_node, radius
worldedit.player_notify(name, "Error: No pos1 defined.") end,
return false nodes_needed = function(name, replace_node, radius)
end -- 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 start_time = os.clock()
local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node) local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node)
local time_taken = os.clock() - start_time local time_taken = os.clock() - start_time
if nodes_replaced == false then if nodes_replaced == false then
worldedit.player_notify(name, "Error: The search node is the same as the replace node.") return false, "Error: The search node is the same as the replace node."
return false
end 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") 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) return true, nodes_replaced .. " nodes replaced in " .. time_taken .. "s"
local replace_node, radius = parse_params_floodfill(params_text) end
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)
}) })

View file

@ -1,5 +1,3 @@
local we_c = worldeditadditions_commands
-- ████████ ██████ ██████ ██ ██ ███████ -- ████████ ██████ ██████ ██ ██ ███████
-- ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██████ ██ ██ ███████ -- ██ ██ ██ ██████ ██ ██ ███████