X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=worldedit_commands%2Finit.lua;h=131c49b8509b646934eeb3490eae6fec1fb6395e;hb=b37605943bd384cc1809df7f00740cbe1e266111;hp=e7b0f748a19dce839afd1c894dd6f633a9c64981;hpb=b32aadd7fafdaef8c6ab1dd578860717d144d4f9;p=worldedit.git diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index e7b0f74..131c49b 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -1,7 +1,5 @@ minetest.register_privilege("worldedit", "Can use WorldEdit commands") ---wip: fold the hollow stuff into the main functions and add a hollow flag at the end, then add the compatibility stuff - worldedit.set_pos = {} worldedit.inspect = {} @@ -12,10 +10,12 @@ if minetest.place_schematic then worldedit.prob_list = {} end +dofile(minetest.get_modpath("worldedit_commands") .. "/cuboid.lua") dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua") -dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua") +dofile(minetest.get_modpath("worldedit_commands") .. "/wand.lua") +local safe_region, check_region, reset_pending = dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua") -local get_position = function(name) +local function get_position(name) --position 1 retrieval function for when not using `safe_region` local pos1 = worldedit.pos1[name] if pos1 == nil then worldedit.player_notify(name, "no position 1 selected") @@ -23,7 +23,8 @@ local get_position = function(name) return pos1 end -local get_node = function(name, nodename) +-- normalize_nodename wrapper for convenience purposes +local function get_node(name, nodename) local node = worldedit.normalize_nodename(nodename) if not node then worldedit.player_notify(name, "invalid node name: " .. nodename) @@ -32,33 +33,54 @@ local get_node = function(name, nodename) return node end -worldedit.player_notify = function(name, message) +function worldedit.player_notify(name, message) minetest.chat_send_player(name, "WorldEdit -!- " .. message, false) end ---determines whether `nodename` is a valid node name, returning a boolean +local function string_endswith(full, part) + return full:find(part, 1, true) == #full - #part + 1 +end + +-- normalizes node "description" `nodename`, returning a string (or nil) worldedit.normalize_nodename = function(nodename) + nodename = nodename:gsub("^%s*(.-)%s*$", "%1") -- strip spaces if nodename == "" then return nil end - local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names - if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename + + local fullname = ItemStack({name=nodename}):get_name() -- resolve aliases + if minetest.registered_nodes[fullname] or fullname == "air" then -- full name return fullname end for key, value in pairs(minetest.registered_nodes) do - if key:find(":" .. nodename, 1, true) then --found in mod + if string_endswith(key, ":" .. nodename) then -- matches name (w/o mod part) return key end end - nodename = nodename:lower() --lowercase both for case insensitive comparison + nodename = nodename:lower() -- lowercase both for case insensitive comparison for key, value in pairs(minetest.registered_nodes) do - if value.description:lower() == nodename then --found in description + local desc = value.description:lower() + if desc == nodename then -- matches description + return key + end + if string_endswith(desc, " block") and desc == nodename.." block" then + -- fuzzy description match (e.g. "Steel" == "Steel Block") return key end end - return nil + + local match = nil + for key, value in pairs(minetest.registered_nodes) do + if value.description:lower():find(nodename, 1, true) ~= nil then + if match ~= nil then + return nil + end + match = key -- substring description match (only if no ambiguities) + end + end + return match end ---determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1) -worldedit.player_axis = function(name) +-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1) +function worldedit.player_axis(name) local dir = minetest.get_player_by_name(name):get_look_dir() local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z) if x > y then @@ -71,11 +93,74 @@ worldedit.player_axis = function(name) return "z", dir.z > 0 and 1 or -1 end +local function mkdir(path) + if minetest.mkdir then + minetest.mkdir(path) + else + os.execute('mkdir "' .. path .. '"') + end +end + +local function check_filename(name) + return name:find("^[%w%s%^&'@{}%[%],%$=!%-#%(%)%%%.%+~_]+$") ~= nil +end + + minetest.register_chatcommand("/about", { params = "", description = "Get information about the mod", func = function(name, param) - worldedit.player_notify(name, "WorldEdit " .. worldedit.version_string .. " is available on this server. Type /help to get a list of commands, or get more information at https://github.com/Uberi/MineTest-WorldEdit/") + worldedit.player_notify(name, "WorldEdit " .. worldedit.version_string .. " is available on this server. Type /help to get a list of commands, or get more information at https://github.com/Uberi/Minetest-WorldEdit/") + end, +}) + +-- mostly copied from builtin/chatcommands.lua with minor modifications +minetest.register_chatcommand("/help", { + privs = {}, + params = "[all/]", + description = "Get help for WorldEdit commands", + func = function(name, param) + local function is_we_command(cmd) + return cmd:sub(0, 1) == "/" + end + local function format_help_line(cmd, def) + local msg = minetest.colorize("#00ffff", "/"..cmd) + if def.params and def.params ~= "" then + msg = msg .. " " .. def.params + end + if def.description and def.description ~= "" then + msg = msg .. ": " .. def.description + end + return msg + end + + if not minetest.check_player_privs(name, "worldedit") then + return false, "You are not allowed to use any WorldEdit commands." + end + if param == "" then + local msg = "" + local cmds = {} + for cmd, def in pairs(minetest.chatcommands) do + if is_we_command(cmd) and minetest.check_player_privs(name, def.privs) then + cmds[#cmds + 1] = cmd:sub(2) -- strip the / + end + end + table.sort(cmds) + return true, "Available commands: " .. table.concat(cmds, " ") .. "\n" + .. "Use '//help ' to get more information," + .. " or '//help all' to list everything." + elseif param == "all" then + local cmds = {} + for cmd, def in pairs(minetest.chatcommands) do + if is_we_command(cmd) and minetest.check_player_privs(name, def.privs) then + cmds[#cmds + 1] = format_help_line(cmd, def) + end + end + table.sort(cmds) + return true, "Available commands:\n"..table.concat(cmds, "\n") + else + return minetest.chatcommands["help"].func(name, "/" .. param) + end end, }) @@ -98,16 +183,28 @@ minetest.register_chatcommand("/inspect", { end, }) +local function get_node_rlight(pos) + local vecs = { -- neighboring nodes + {x= 1, y= 0, z= 0}, + {x=-1, y= 0, z= 0}, + {x= 0, y= 1, z= 0}, + {x= 0, y=-1, z= 0}, + {x= 0, y= 0, z= 1}, + {x= 0, y= 0, z=-1}, + } + local ret = 0 + for _, v in ipairs(vecs) do + ret = math.max(ret, minetest.get_node_light(vector.add(pos, v))) + end + return ret +end + minetest.register_on_punchnode(function(pos, node, puncher) local name = puncher:get_player_name() if worldedit.inspect[name] then - if minetest.check_player_privs(name, {worldedit=true}) then - local axis, sign = worldedit.player_axis(name) - message = string.format("inspector: %s at %s (param1=%d, param2=%d) punched by %s facing the %s axis", - node.name, minetest.pos_to_string(pos), node.param1, node.param2, name, axis .. (sign > 0 and "+" or "-")) - else - message = "inspector: worldedit privileges required" - end + local axis, sign = worldedit.player_axis(name) + local message = string.format("inspector: %s at %s (param1=%d, param2=%d, received light=%d) punched facing the %s axis", + node.name, minetest.pos_to_string(pos), node.param1, node.param2, get_node_rlight(pos), axis .. (sign > 0 and "+" or "-")) worldedit.player_notify(name, message) end end) @@ -122,6 +219,8 @@ minetest.register_chatcommand("/reset", { worldedit.mark_pos1(name) worldedit.mark_pos2(name) worldedit.set_pos[name] = nil + --make sure the user does not try to confirm an operation after resetting pos: + reset_pending(name) worldedit.player_notify(name, "region reset") end, }) @@ -278,21 +377,71 @@ minetest.register_chatcommand("/volume", { end, }) +minetest.register_chatcommand("/deleteblocks", { + params = "", + description = "remove all MapBlocks (16x16x16) containing the selected area from the map", + privs = {worldedit=true}, + func = safe_region(function(name, param) + local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] + local success = minetest.delete_area(pos1, pos2) + if success then + worldedit.player_notify(name, "Area deleted.") + else + worldedit.player_notify(name, "There was an error during deletion of the area.") + end + end), +}) + minetest.register_chatcommand("/set", { params = "", description = "Set the current WorldEdit region to ", privs = {worldedit=true}, func = safe_region(function(name, param) - local nodes = {} + local node = get_node(name, param) + if not node then return end - for nodename in param:gmatch("[^%s]+") do - local node = get_node(name, nodename) - if not node then - worldedit.player_notify(name, 'Could not identify node "'..name..'"') - return - end - nodes[#nodes+1] = node - end + local count = worldedit.set(worldedit.pos1[name], worldedit.pos2[name], node) + worldedit.player_notify(name, count .. " nodes set") + end, check_region), +}) + +minetest.register_chatcommand("/param2", { + params = "", + description = "Set param2 of all nodes in the current WorldEdit region to ", + privs = {worldedit=true}, + func = safe_region(function(name, param) + local param2 = tonumber(param) + if not param2 then + worldedit.player_notify(name, "Invalid or missing param2 argument") + return + elseif param2 < 0 or param2 > 255 then + worldedit.player_notify(name, "Param2 is out of range (must be between 0 and 255 inclusive)!") + return + end + + local count = worldedit.set_param2(worldedit.pos1[name], worldedit.pos2[name], param2) + worldedit.player_notify(name, count .. " nodes altered") + end, check_region), +}) + +minetest.register_chatcommand("/mix", { + params = " [] [ []] ...", + description = "Fill the current WorldEdit region with a random mix of , ...", + privs = {worldedit=true}, + func = safe_region(function(name, param) + local nodes = {} + for nodename in param:gmatch("[^%s]+") do + if tonumber(nodename) ~= nil and #nodes > 0 then + local last_node = nodes[#nodes] + for i = 1, tonumber(nodename) do + nodes[#nodes + 1] = last_node + end + else + local node = get_node(name, nodename) + if not node then return end + nodes[#nodes + 1] = node + end + end local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local count = worldedit.set(pos1, pos2, nodes) @@ -324,11 +473,11 @@ minetest.register_chatcommand("/replace", { description = "Replace all instances of with in the current WorldEdit region", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$") - local newsearchnode = worldedit.normalize_nodename(searchnode) - local newreplacenode = worldedit.normalize_nodename(replacenode) - local count = worldedit.replace(pos1, pos2, newsearchnode, newreplacenode) + local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$") + local norm_search_node = worldedit.normalize_nodename(search_node) + local norm_replace_node = worldedit.normalize_nodename(replace_node) + local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name], + norm_search_node, norm_replace_node) worldedit.player_notify(name, count .. " nodes replaced") end, check_replace), }) @@ -338,15 +487,54 @@ minetest.register_chatcommand("/replaceinverse", { description = "Replace all nodes other than with in the current WorldEdit region", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$") - local newsearchnode = worldedit.normalize_nodename(searchnode) - local newreplacenode = worldedit.normalize_nodename(replacenode) - local count = worldedit.replaceinverse(pos1, pos2, searchnode, replacenode) + local found, _, search_node, replace_node = param:find("^([^%s]+)%s+(.+)$") + local norm_search_node = worldedit.normalize_nodename(search_node) + local norm_replace_node = worldedit.normalize_nodename(replace_node) + local count = worldedit.replace(worldedit.pos1[name], worldedit.pos2[name], + norm_search_node, norm_replace_node, true) worldedit.player_notify(name, count .. " nodes replaced") end, check_replace), }) +local check_cube = function(name, param) + if worldedit.pos1[name] == nil then + worldedit.player_notify(name, "no position 1 selected") + return nil + end + local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + if found == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return nil + end + local node = get_node(name, nodename) + if not node then return nil end + return tonumber(w) * tonumber(h) * tonumber(l) +end + +minetest.register_chatcommand("/hollowcube", { + params = " ", + description = "Add a hollow cube with its ground level centered at WorldEdit position 1 with dimensions x x , composed of .", + privs = {worldedit=true}, + func = safe_region(function(name, param) + local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + local node = get_node(name, nodename) + local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node, true) + worldedit.player_notify(name, count .. " nodes added") + end, check_cube), +}) + +minetest.register_chatcommand("/cube", { + params = " ", + description = "Add a cube with its ground level centered at WorldEdit position 1 with dimensions x x , composed of .", + privs = {worldedit=true}, + func = safe_region(function(name, param) + local found, _, w, h, l, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + local node = get_node(name, nodename) + local count = worldedit.cube(worldedit.pos1[name], tonumber(w), tonumber(h), tonumber(l), node) + worldedit.player_notify(name, count .. " nodes added") + end, check_cube), +}) + local check_sphere = function(name, param) if worldedit.pos1[name] == nil then worldedit.player_notify(name, "no position 1 selected") @@ -367,10 +555,9 @@ minetest.register_chatcommand("/hollowsphere", { description = "Add hollow sphere centered at WorldEdit position 1 with radius , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local node = get_node(name, nodename) - local count = worldedit.hollow_sphere(pos, tonumber(radius), node) + local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node, true) worldedit.player_notify(name, count .. " nodes added") end, check_sphere), }) @@ -380,10 +567,9 @@ minetest.register_chatcommand("/sphere", { description = "Add sphere centered at WorldEdit position 1 with radius , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local node = get_node(name, nodename) - local count = worldedit.sphere(pos, tonumber(radius), node) + local count = worldedit.sphere(worldedit.pos1[name], tonumber(radius), node) worldedit.player_notify(name, count .. " nodes added") end, check_sphere), }) @@ -408,10 +594,9 @@ minetest.register_chatcommand("/hollowdome", { description = "Add hollow dome centered at WorldEdit position 1 with radius , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local node = get_node(name, nodename) - local count = worldedit.hollow_dome(pos, tonumber(radius), node) + local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node, true) worldedit.player_notify(name, count .. " nodes added") end, check_dome), }) @@ -421,10 +606,9 @@ minetest.register_chatcommand("/dome", { description = "Add dome centered at WorldEdit position 1 with radius , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$") local node = get_node(name, nodename) - local count = worldedit.dome(pos, tonumber(radius), node) + local count = worldedit.dome(worldedit.pos1[name], tonumber(radius), node) worldedit.player_notify(name, count .. " nodes added") end, check_dome), }) @@ -434,83 +618,121 @@ local check_cylinder = function(name, param) worldedit.player_notify(name, "no position 1 selected") return nil end - local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + -- two radii + local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + if found == nil then + -- single radius + found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + radius2 = radius1 + end if found == nil then worldedit.player_notify(name, "invalid usage: " .. param) return nil end local node = get_node(name, nodename) if not node then return nil end - return math.ceil(math.pi * (tonumber(radius) ^ 2) * tonumber(length)) + local radius = math.max(tonumber(radius1), tonumber(radius2)) + return math.ceil(math.pi * (radius ^ 2) * tonumber(length)) end minetest.register_chatcommand("/hollowcylinder", { - params = "x/y/z/? ", - description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of ", + params = "x/y/z/? [radius2] ", + description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length , base radius (and top radius [radius2]), composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] - local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + -- two radii + local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + if found == nil then + -- single radius + found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + radius2 = radius1 + end length = tonumber(length) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) length = length * sign end local node = get_node(name, nodename) - local count = worldedit.hollow_cylinder(pos, axis, length, tonumber(radius), node) + local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node, true) worldedit.player_notify(name, count .. " nodes added") end, check_cylinder), }) minetest.register_chatcommand("/cylinder", { - params = "x/y/z/? ", - description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length and radius , composed of ", + params = "x/y/z/? [radius2] ", + description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length , base radius (and top radius [radius2]), composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] - local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + -- two radii + local found, _, axis, length, radius1, radius2, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(%d+)%s+(.+)$") + if found == nil then + -- single radius + found, _, axis, length, radius1, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$") + radius2 = radius1 + end length = tonumber(length) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) length = length * sign end local node = get_node(name, nodename) - local count = worldedit.cylinder(pos, axis, length, tonumber(radius), node) + local count = worldedit.cylinder(worldedit.pos1[name], axis, length, tonumber(radius1), tonumber(radius2), node) worldedit.player_notify(name, count .. " nodes added") end, check_cylinder), }) -minetest.register_chatcommand("/pyramid", { +local check_pyramid = function(name, param) + if worldedit.pos1[name] == nil then + worldedit.player_notify(name, "no position 1 selected") + return nil + end + local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$") + if found == nil then + worldedit.player_notify(name, "invalid usage: " .. param) + return nil + end + local node = get_node(name, nodename) + if not node then return nil end + height = tonumber(height) + return math.ceil(((height * 2 + 1) ^ 2) * height / 3) +end + +minetest.register_chatcommand("/hollowpyramid", { params = "x/y/z/? ", - description = "Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height , composed of ", + description = "Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = get_position(name) local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$") height = tonumber(height) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) height = height * sign end local node = get_node(name, nodename) - local count = worldedit.pyramid(pos, axis, height, node) + local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node, true) worldedit.player_notify(name, count .. " nodes added") - end, - function(name, param) - if worldedit.pos1[name] == nil then - worldedit.player_notify(name, "no position 1 selected") - return nil - end + end, check_pyramid), +}) + +minetest.register_chatcommand("/pyramid", { + params = "x/y/z/? ", + description = "Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height , composed of ", + privs = {worldedit=true}, + func = safe_region(function(name, param) local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$") - if found == nil then - worldedit.player_notify(name, "invalid usage: " .. param) - return nil + height = tonumber(height) + if axis == "?" then + local sign + axis, sign = worldedit.player_axis(name) + height = height * sign end local node = get_node(name, nodename) - if not node then return nil end - height = tonumber(height) - return math.ceil(((height * 2 + 1) ^ 2) * height / 3) - end), + local count = worldedit.pyramid(worldedit.pos1[name], axis, height, node) + worldedit.player_notify(name, count .. " nodes added") + end, check_pyramid), }) minetest.register_chatcommand("/spiral", { @@ -518,10 +740,9 @@ minetest.register_chatcommand("/spiral", { description = "Add spiral centered at WorldEdit position 1 with side length , height , space between walls , composed of ", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos = worldedit.pos1[name] local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$") local node = get_node(name, nodename) - local count = worldedit.spiral(pos, tonumber(length), tonumber(height), tonumber(space), node) + local count = worldedit.spiral(worldedit.pos1[name], tonumber(length), tonumber(height), tonumber(space), node) worldedit.player_notify(name, count .. " nodes added") end, function(name, param) @@ -536,7 +757,7 @@ minetest.register_chatcommand("/spiral", { end local node = get_node(name, nodename) if not node then return nil end - return check_region(name, param) + return 1 -- TODO: return an useful value end), }) @@ -545,7 +766,6 @@ minetest.register_chatcommand("/copy", { description = "Copy the current WorldEdit region along the x/y/z/? axis by nodes", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$") if found == nil then worldedit.player_notify(name, "invalid usage: " .. param) @@ -553,11 +773,12 @@ minetest.register_chatcommand("/copy", { end amount = tonumber(amount) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) amount = amount * sign end - local count = worldedit.copy(pos1, pos2, axis, amount) + local count = worldedit.copy(worldedit.pos1[name], worldedit.pos2[name], axis, amount) worldedit.player_notify(name, count .. " nodes copied") end, function(name, param) @@ -579,6 +800,7 @@ minetest.register_chatcommand("/move", { end amount = tonumber(amount) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) amount = amount * sign end @@ -598,20 +820,21 @@ minetest.register_chatcommand("/stack", { description = "Stack the current WorldEdit region along the x/y/z/? axis times", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$") repetitions = tonumber(repetitions) if axis == "?" then + local sign axis, sign = worldedit.player_axis(name) repetitions = repetitions * sign end - local count = worldedit.stack(pos1, pos2, axis, repetitions) + local count = worldedit.stack(worldedit.pos1[name], worldedit.pos2[name], axis, repetitions) worldedit.player_notify(name, count .. " nodes stacked") end, function(name, param) local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$") if found == nil then worldedit.player_notify(name, "invalid usage: " .. param) + return end local count = check_region(name, param) if count then return (tonumber(repetitions) + 1) * count end @@ -620,47 +843,37 @@ minetest.register_chatcommand("/stack", { }) minetest.register_chatcommand("/stack2", { - params = " //", - description = "Stack the current WorldEdit region times translating each time by x, y and z in the respective directions.", + params = " ", + description = "Stack the current WorldEdit region times by offset , , ", privs = {worldedit=true}, func = function(name, param) local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - if pos1 == nil or pos2 == nil then - worldedit.player_notify(name, "Select a position first!") - return - end - local repetitions, incs = param:match("([0-9]+)%s*(.+)") - repetitions = repetitions and tonumber(repetitions) - if repetitions == nil then + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, "Select a position first!") + return + end + local repetitions, incs = param:match("(%d+)%s*(.+)") + if repetitions == nil then worldedit.player_notify(name, "invalid count: " .. param) - return - end - - local x,y,z = incs:match("(.+)/(.+)/(.+)") - if x == nil then - worldedit.player_notify(name, "invalid increments: " .. param) - return - end - x = tonumber(x) - y = tonumber(y) - z = tonumber(z) - if x == nil or y == nil or z == nil then - worldedit.player_notify(name, "increments must be numbers: " .. param) - return - end - - local count = worldedit.volume(pos1,pos2) * repetitions - - return safe_region(function() - worldedit.stack2(pos1, pos2, {x=x,y=y,z=z}, repetitions, - function() - worldedit.player_notify(name, count .. " nodes stacked") - end) - - end, - function() - return count - end)(name,param) -- more hax + return + end + repetitions = tonumber(repetitions) + + local x, y, z = incs:match("([+-]?%d+) ([+-]?%d+) ([+-]?%d+)") + if x == nil then + worldedit.player_notify(name, "invalid increments: " .. param) + return + end + x, y, z = tonumber(x), tonumber(y), tonumber(z) + + local count = worldedit.volume(pos1, pos2) * repetitions + + return safe_region(function() + worldedit.stack2(pos1, pos2, {x=x, y=y, z=z}, repetitions, + function() worldedit.player_notify(name, count .. " nodes stacked") end) + end, function() + return count + end)(name,param) -- more hax --wip: clean this up a little bit end }) @@ -737,9 +950,8 @@ minetest.register_chatcommand("/flip", { description = "Flip the current WorldEdit region along the x/y/z/? axis", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] if param == "?" then param = worldedit.player_axis(name) end - local count = worldedit.flip(pos1, pos2, param) + local count = worldedit.flip(worldedit.pos1[name], worldedit.pos2[name], param) worldedit.player_notify(name, count .. " nodes flipped") end, function(name, param) @@ -788,9 +1000,8 @@ minetest.register_chatcommand("/orient", { description = "Rotate oriented nodes in the current WorldEdit region around the Y axis by angle (90 degree increment)", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local found, _, angle = param:find("^([+-]?%d+)$") - local count = worldedit.orient(pos1, pos2, angle) + local count = worldedit.orient(worldedit.pos1[name], worldedit.pos2[name], angle) worldedit.player_notify(name, count .. " nodes oriented") end, function(name, param) @@ -812,8 +1023,31 @@ minetest.register_chatcommand("/fixlight", { description = "Fix the lighting in the current WorldEdit region", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local count = worldedit.fixlight(pos1, pos2) + local count = worldedit.fixlight(worldedit.pos1[name], worldedit.pos2[name]) + worldedit.player_notify(name, count .. " nodes updated") + end), +}) + +minetest.register_chatcommand("/drain", { + params = "", + description = "Remove any fluid node within the current WorldEdit region", + privs = {worldedit=true}, + func = safe_region(function(name, param) + -- TODO: make an API function for this + local count = 0 + local pos1, pos2 = worldedit.sort_pos(worldedit.pos1[name], worldedit.pos2[name]) + for x = pos1.x, pos2.x do + for y = pos1.y, pos2.y do + for z = pos1.z, pos2.z do + local n = minetest.get_node({x=x, y=y, z=z}).name + local d = minetest.registered_nodes[n] + if d ~= nil and (d["drawtype"] == "liquid" or d["drawtype"] == "flowingliquid") then + minetest.remove_node({x=x, y=y, z=z}) + count = count + 1 + end + end + end + end worldedit.player_notify(name, count .. " nodes updated") end), }) @@ -823,8 +1057,7 @@ minetest.register_chatcommand("/hide", { description = "Hide all nodes in the current WorldEdit region non-destructively", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local count = worldedit.hide(pos1, pos2) + local count = worldedit.hide(worldedit.pos1[name], worldedit.pos2[name]) worldedit.player_notify(name, count .. " nodes hidden") end), }) @@ -834,11 +1067,10 @@ minetest.register_chatcommand("/suppress", { description = "Suppress all in the current WorldEdit region non-destructively", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local node = get_node(name, param) - local count = worldedit.suppress(pos1, pos2, node) + local count = worldedit.suppress(worldedit.pos1[name], worldedit.pos2[name], node) worldedit.player_notify(name, count .. " nodes suppressed") - end, check_set), + end, check_region), }) minetest.register_chatcommand("/highlight", { @@ -846,11 +1078,10 @@ minetest.register_chatcommand("/highlight", { description = "Highlight in the current WorldEdit region by hiding everything else non-destructively", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local node = get_node(name, param) - local count = worldedit.highlight(pos1, pos2, node) + local count = worldedit.highlight(worldedit.pos1[name], worldedit.pos2[name], node) worldedit.player_notify(name, count .. " nodes highlighted") - end, check_set), + end, check_region), }) minetest.register_chatcommand("/restore", { @@ -858,8 +1089,7 @@ minetest.register_chatcommand("/restore", { description = "Restores nodes hidden with WorldEdit in the current WorldEdit region", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local count = worldedit.restore(pos1, pos2) + local count = worldedit.restore(worldedit.pos1[name], worldedit.pos2[name]) worldedit.player_notify(name, count .. " nodes restored") end), }) @@ -869,25 +1099,25 @@ minetest.register_chatcommand("/save", { description = "Save the current WorldEdit region to \"(world folder)/schems/.we\"", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] if param == "" then worldedit.player_notify(name, "invalid usage: " .. param) return end - if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then - worldedit.player_notify(name, "invalid file name: " .. param) + if not check_filename(param) then + worldedit.player_notify(name, "Disallowed file name: " .. param) return end - - local result, count = worldedit.serialize(pos1, pos2) + local result, count = worldedit.serialize(worldedit.pos1[name], + worldedit.pos2[name]) local path = minetest.get_worldpath() .. "/schems" + -- Create directory if it does not already exist + mkdir(path) + local filename = path .. "/" .. param .. ".we" - filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters - os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist local file, err = io.open(filename, "wb") if err ~= nil then - worldedit.player_notify(name, "could not save file to \"" .. filename .. "\"") + worldedit.player_notify(name, "Could not save file to \"" .. filename .. "\"") return end file:write(result) @@ -910,8 +1140,8 @@ minetest.register_chatcommand("/allocate", { worldedit.player_notify(name, "invalid usage: " .. param) return end - if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then - worldedit.player_notify(name, "invalid file name: " .. param) + if not check_filename(param) then + worldedit.player_notify(name, "Disallowed file name: " .. param) return end @@ -924,9 +1154,12 @@ minetest.register_chatcommand("/allocate", { local value = file:read("*a") file:close() - if worldedit.valueversion(value) == 0 then --unknown version - worldedit.player_notify(name, "invalid file: file is invalid or created with newer version of WorldEdit") + local version = worldedit.read_header(value) + if version == 0 then + worldedit.player_notify(name, "File is invalid!") return + elseif version > worldedit.LATEST_SERIALIZATION_VERSION then + worldedit.player_notify(name, "File was created with newer version of WorldEdit!") end local nodepos1, nodepos2, count = worldedit.allocate(pos, value) @@ -976,8 +1209,12 @@ minetest.register_chatcommand("/load", { local value = file:read("*a") file:close() - if worldedit.valueversion(value) == 0 then --unknown version - worldedit.player_notify(name, "invalid file: file is invalid or created with newer version of WorldEdit") + local version = worldedit.read_header(value) + if version == 0 then + worldedit.player_notify(name, "File is invalid!") + return + elseif version > worldedit.LATEST_SERIALIZATION_VERSION then + worldedit.player_notify(name, "File was created with newer version of WorldEdit!") return end @@ -992,16 +1229,13 @@ minetest.register_chatcommand("/lua", { description = "Executes as a Lua chunk in the global namespace", privs = {worldedit=true, server=true}, func = function(name, param) - local admin = minetest.setting_get("name") - if not admin or not name == admin then - worldedit.player_notify(name, "this command can only be run by the server administrator") - return - end local err = worldedit.lua(param) if err then worldedit.player_notify(name, "code error: " .. err) + minetest.log("action", name.." tried to execute "..param) else worldedit.player_notify(name, "code successfully executed", false) + minetest.log("action", name.." executed "..param) end end, }) @@ -1011,43 +1245,44 @@ minetest.register_chatcommand("/luatransform", { description = "Executes as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region", privs = {worldedit=true, server=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local admin = minetest.setting_get("name") - if not admin or not name == admin then - worldedit.player_notify(name, "this command can only be run by the server administrator") - return - end - - local err = worldedit.luatransform(pos1, pos2, param) + local err = worldedit.luatransform(worldedit.pos1[name], worldedit.pos2[name], param) if err then worldedit.player_notify(name, "code error: " .. err, false) + minetest.log("action", name.." tried to execute luatransform "..param) else worldedit.player_notify(name, "code successfully executed", false) + minetest.log("action", name.." executed luatransform "..param) end end), }) minetest.register_chatcommand("/mtschemcreate", { params = "", - description = "Save the current WorldEdit region using the Minetest Schematic format to \"(world folder)/schems/.mts\"", + description = "Save the current WorldEdit region using the Minetest ".. + "Schematic format to \"(world folder)/schems/.mts\"", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] if param == nil then worldedit.player_notify(name, "No filename specified") return end + if not check_filename(param) then + worldedit.player_notify(name, "Disallowed file name: " .. param) + return + end local path = minetest.get_worldpath() .. "/schems" - local filename = path .. "/" .. param .. ".mts" - filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters - os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist + -- Create directory if it does not already exist + mkdir(path) - local ret = minetest.create_schematic(pos1, pos2, worldedit.prob_list[name], filename) + local filename = path .. "/" .. param .. ".mts" + local ret = minetest.create_schematic(worldedit.pos1[name], + worldedit.pos2[name], worldedit.prob_list[name], + filename) if ret == nil then - worldedit.player_notify(name, "failed to create Minetest schematic", false) + worldedit.player_notify(name, "Failed to create Minetest schematic", false) else - worldedit.player_notify(name, "saved Minetest schematic to " .. param, false) + worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false) end worldedit.prob_list[name] = {} end), @@ -1058,10 +1293,14 @@ minetest.register_chatcommand("/mtschemplace", { description = "Load nodes from \"(world folder)/schems/.mts\" with position 1 of the current WorldEdit region as the origin", privs = {worldedit=true}, func = function(name, param) - if param == nil then + if param == "" then worldedit.player_notify(name, "no filename specified") return end + if not check_filename(param) then + worldedit.player_notify(name, "Disallowed file name: " .. param) + return + end local pos = get_position(name) if pos == nil then return end @@ -1095,8 +1334,8 @@ minetest.register_chatcommand("/mtschemprob", { return end for k,v in pairs(problist) do - local prob = math.floor(((v["prob"] / 256) * 100) * 100 + 0.5) / 100 - text = text .. minetest.pos_to_string(v["pos"]) .. ": " .. prob .. "% | " + local prob = math.floor(((v.prob / 256) * 100) * 100 + 0.5) / 100 + text = text .. minetest.pos_to_string(v.pos) .. ": " .. prob .. "% | " end worldedit.player_notify(name, "currently set node probabilities:") worldedit.player_notify(name, text) @@ -1106,24 +1345,21 @@ minetest.register_chatcommand("/mtschemprob", { end, }) -minetest.register_on_player_receive_fields( - function(player, formname, fields) - if (formname == "prob_val_enter") and (fields.text ~= "") then - local name = player:get_player_name() - local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)} - local index = table.getn(worldedit.prob_list[name]) + 1 - worldedit.prob_list[name][index] = prob_entry - end +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "prob_val_enter" and not (fields.text == "" or fields.text == nil) then + local name = player:get_player_name() + local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)} + local index = table.getn(worldedit.prob_list[name]) + 1 + worldedit.prob_list[name][index] = prob_entry end -) +end) minetest.register_chatcommand("/clearobjects", { params = "", description = "Clears all objects within the WorldEdit region", privs = {worldedit=true}, func = safe_region(function(name, param) - local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] - local count = worldedit.clearobjects(pos1, pos2) + local count = worldedit.clear_objects(worldedit.pos1[name], worldedit.pos2[name]) worldedit.player_notify(name, count .. " objects cleared") end), })