mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-08-20 13:34:05 +02:00
Migrate //torus to worldedit.register_command
This commit is contained in:
parent
ae068989e2
commit
bdf0bdea93
@ -17,76 +17,60 @@ local function parse_params_torus(params_text)
|
||||
|
||||
replace_node = worldedit.normalize_nodename(replace_node)
|
||||
|
||||
return replace_node, major_radius, minor_radius
|
||||
if not replace_node then
|
||||
return false, "Error: Invalid replace_node."
|
||||
end
|
||||
if not major_radius or major_radius < 1 then
|
||||
return false, "Error: Invalid major radius (expected integer greater than 0)"
|
||||
end
|
||||
if not minor_radius or minor_radius < 1 then
|
||||
return false, "Error: Invalid minor radius (expected integer greater than 0)"
|
||||
end
|
||||
|
||||
return true, replace_node, major_radius, minor_radius
|
||||
end
|
||||
|
||||
minetest.register_chatcommand("/torus", {
|
||||
worldedit.register_command("torus", {
|
||||
params = "<major_radius> <minor_radius> <replace_node>",
|
||||
description = "Creates a 3D torus with a major radius of <major_radius> and a minor radius of <minor_radius> at pos1, filled with <replace_node>.",
|
||||
privs = { worldedit = true },
|
||||
func = we_c.safe_region(function(name, params_text)
|
||||
local target_node, major_radius, minor_radius = parse_params_torus(params_text)
|
||||
|
||||
if not target_node then
|
||||
worldedit.player_notify(name, "Error: Invalid node name.")
|
||||
return false
|
||||
end
|
||||
if not major_radius or not minor_radius then
|
||||
worldedit.player_notify(name, "Error: Invalid radius(es).")
|
||||
return false
|
||||
end
|
||||
|
||||
if not worldedit.pos1[name] then
|
||||
worldedit.player_notify(name, "Error: No pos1 specified (try //1 or left click with the wand tool)")
|
||||
end
|
||||
|
||||
require_pos = 1,
|
||||
parse = function(params_text)
|
||||
local values = {parse_params_torus(params_text)}
|
||||
return unpack(values)
|
||||
end,
|
||||
nodes_needed = function(name, target_node, major_radius, minor_radius)
|
||||
return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius)
|
||||
end,
|
||||
func = function(name, target_node, major_radius, minor_radius)
|
||||
local start_time = os.clock()
|
||||
local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, false)
|
||||
local time_taken = os.clock() - start_time
|
||||
|
||||
worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s")
|
||||
minetest.log("action", name .. " used //torus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s")
|
||||
end, function(name, params_text)
|
||||
local target_node, major_radius, minor_radius = parse_params_torus(params_text)
|
||||
if not target_node or not major_radius or not minor_radius then
|
||||
worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /torus' to learn how to use this command.")
|
||||
return 0
|
||||
end
|
||||
|
||||
return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius)
|
||||
end)
|
||||
return true, replaced .. " nodes replaced in " .. time_taken .. "s"
|
||||
end
|
||||
})
|
||||
|
||||
-- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit?
|
||||
minetest.register_chatcommand("/hollowtorus", {
|
||||
worldedit.register_command("hollowtorus", {
|
||||
params = "<major_radius> <minor_radius> <replace_node>",
|
||||
description = "Creates a 3D hollow torus with a major radius of <major_radius> and a minor radius of <minor_radius> at pos1, made out of <replace_node>.",
|
||||
privs = { worldedit = true },
|
||||
func = we_c.safe_region(function(name, params_text)
|
||||
local target_node, major_radius, minor_radius = parse_params_torus(params_text)
|
||||
|
||||
if not target_node then
|
||||
worldedit.player_notify(name, "Error: Invalid node name.")
|
||||
return false
|
||||
end
|
||||
if not major_radius or not minor_radius then
|
||||
worldedit.player_notify(name, "Error: Invalid radius(es).")
|
||||
return false
|
||||
end
|
||||
|
||||
require_pos = 1,
|
||||
parse = function(params_text)
|
||||
local values = {parse_params_torus(params_text)}
|
||||
return unpack(values)
|
||||
end,
|
||||
nodes_needed = function(name, target_node, major_radius, minor_radius)
|
||||
return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius)
|
||||
end,
|
||||
func = function(name, target_node, major_radius, minor_radius)
|
||||
local start_time = os.clock()
|
||||
local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, true)
|
||||
local time_taken = os.clock() - start_time
|
||||
|
||||
worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s")
|
||||
minetest.log("action", name .. " used //hollowtorus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s")
|
||||
end, function(name, params_text)
|
||||
local target_node, major_radius, minor_radius = parse_params_torus(params_text)
|
||||
if not target_node or not major_radius or not minor_radius then
|
||||
worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /hollowtorus' to learn how to use this command.")
|
||||
return 0
|
||||
end
|
||||
|
||||
return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius)
|
||||
end)
|
||||
return true, replaced .. " nodes replaced in " .. time_taken .. "s"
|
||||
end
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user