2023-10-23 20:52:04 +02:00
local S = minetest.get_translator ( " worldedit_commands " )
minetest.register_privilege ( " worldedit " , S ( " Can use WorldEdit commands " ) )
2012-10-14 03:45:50 +02:00
2019-12-19 14:12:45 +01:00
worldedit.pos1 = { }
worldedit.pos2 = { }
2012-10-14 03:45:50 +02:00
worldedit.set_pos = { }
2013-07-29 18:43:24 +02:00
worldedit.inspect = { }
2019-12-19 14:12:45 +01:00
worldedit.prob_pos = { }
worldedit.prob_list = { }
2012-10-14 03:45:50 +02:00
2019-12-19 14:12:45 +01:00
local safe_region , reset_pending = dofile ( minetest.get_modpath ( " worldedit_commands " ) .. " /safe.lua " )
function worldedit . player_notify ( name , message )
minetest.chat_send_player ( name , " WorldEdit -!- " .. message , false )
2013-06-22 08:24:54 +02:00
end
2012-10-14 03:45:50 +02:00
2019-12-19 14:12:45 +01:00
worldedit.registered_commands = { }
2024-04-20 13:45:00 +02:00
local function copy_state ( which , name )
if which == 0 then
return { }
elseif which == 1 then
return {
worldedit.pos1 [ name ] and vector.copy ( worldedit.pos1 [ name ] )
}
else
return {
worldedit.pos1 [ name ] and vector.copy ( worldedit.pos1 [ name ] ) ,
worldedit.pos2 [ name ] and vector.copy ( worldedit.pos2 [ name ] )
}
end
end
2019-12-19 14:12:45 +01:00
local function chatcommand_handler ( cmd_name , name , param )
local def = assert ( worldedit.registered_commands [ cmd_name ] )
2012-10-14 03:45:50 +02:00
2019-12-19 14:12:45 +01:00
if def.require_pos == 2 then
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
if pos1 == nil or pos2 == nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " no region selected " ) )
2019-12-19 14:12:45 +01:00
return
end
elseif def.require_pos == 1 then
local pos1 = worldedit.pos1 [ name ]
if pos1 == nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " no position 1 selected " ) )
2019-12-19 14:12:45 +01:00
return
end
end
local parsed = { def.parse ( param ) }
local success = table.remove ( parsed , 1 )
if not success then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , parsed [ 1 ] or S ( " invalid usage " ) )
2019-12-19 14:12:45 +01:00
return
end
2024-04-20 13:45:00 +02:00
local run = function ( )
2023-06-19 18:24:31 +02:00
local _ , msg = def.func ( name , unpack ( parsed ) )
2019-12-19 14:12:45 +01:00
if msg then
minetest.chat_send_player ( name , msg )
end
2014-03-10 22:57:49 +01:00
end
2024-04-20 13:45:00 +02:00
if not def.nodes_needed then
-- no safe region check
run ( )
return
end
local count = def.nodes_needed ( name , unpack ( parsed ) )
local old_state = copy_state ( def.require_pos , name )
safe_region ( name , count , function ( )
local state = copy_state ( def.require_pos , name )
local ok = true
for i , v in ipairs ( state ) do
ok = ok and ( ( v == nil and old_state [ i ] == nil ) or vector.equals ( v , old_state [ i ] ) )
end
if not ok then
worldedit.player_notify ( name , S ( " ERROR: the operation was cancelled because the region has changed. " ) )
return
end
run ( )
end )
2014-03-10 22:57:49 +01:00
end
2019-12-19 14:12:45 +01:00
-- Registers a chatcommand for WorldEdit
-- name = "about" -- Name of the chat command (without any /)
-- def = {
-- privs = {}, -- Privileges needed
-- params = "", -- Human readable parameter list (optional)
2024-04-20 13:45:00 +02:00
-- -- if params = "" then a parse() implementation will automatically be provided
2019-12-19 14:12:45 +01:00
-- description = "", -- Description
2024-04-21 22:43:36 +02:00
-- category = "", -- Category of the command (optional)
2019-12-19 14:12:45 +01:00
-- require_pos = 0, -- Number of positions required to be set (optional)
-- parse = function(param)
-- return true, foo, bar, ...
-- -- or
-- return false
-- -- or
-- return false, "error message"
-- end,
-- nodes_needed = function(name, foo, bar, ...), -- (optional)
-- return n
-- end,
-- func = function(name, foo, bar, ...)
-- return success, "message"
-- end,
-- }
function worldedit . register_command ( name , def )
local def = table.copy ( def )
assert ( name and # name > 0 )
2024-04-20 20:36:07 +02:00
def.name = name
2019-12-19 14:12:45 +01:00
assert ( def.privs )
2024-04-21 22:43:36 +02:00
def.category = def.category or " "
2019-12-19 14:12:45 +01:00
def.require_pos = def.require_pos or 0
assert ( def.require_pos >= 0 and def.require_pos < 3 )
if def.params == " " and not def.parse then
def.parse = function ( param ) return true end
else
assert ( def.parse )
2014-03-10 22:57:49 +01:00
end
2019-12-19 14:12:45 +01:00
assert ( def.nodes_needed == nil or type ( def.nodes_needed ) == " function " )
assert ( def.func )
-- for development
--[[if def.require_pos == 2 and not def.nodes_needed then
minetest.log ( " warning " , " // " .. name .. " might be missing nodes_needed " )
end --]]
2024-04-20 20:36:07 +02:00
-- disable further modification
setmetatable ( def , { __newindex = { } } )
2019-12-19 14:12:45 +01:00
minetest.register_chatcommand ( " / " .. name , {
privs = def.privs ,
params = def.params ,
description = def.description ,
func = function ( player_name , param )
return chatcommand_handler ( name , player_name , param )
end ,
} )
worldedit.registered_commands [ name ] = def
2014-03-10 22:57:49 +01:00
end
2019-12-19 14:12:45 +01:00
dofile ( minetest.get_modpath ( " worldedit_commands " ) .. " /cuboid.lua " )
dofile ( minetest.get_modpath ( " worldedit_commands " ) .. " /mark.lua " )
dofile ( minetest.get_modpath ( " worldedit_commands " ) .. " /wand.lua " )
local function check_region ( name )
return worldedit.volume ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2013-05-15 23:52:12 +02:00
end
2020-03-29 21:55:47 +02:00
-- Strips any kind of escape codes (translation, colors) from a string
2019-09-18 18:38:49 +02:00
-- https://github.com/minetest/minetest/blob/53dd7819277c53954d1298dfffa5287c306db8d0/src/util/string.cpp#L777
2020-03-29 21:55:47 +02:00
local function strip_escapes ( input )
2019-09-18 18:38:49 +02:00
local s = function ( idx ) return input : sub ( idx , idx ) end
local out = " "
local i = 1
while i <= # input do
if s ( i ) == " \027 " then -- escape sequence
i = i + 1
if s ( i ) == " ( " then -- enclosed
i = i + 1
while i <= # input and s ( i ) ~= " ) " do
if s ( i ) == " \\ " then
i = i + 2
else
i = i + 1
end
end
end
else
out = out .. s ( i )
end
i = i + 1
end
--print(("%q -> %q"):format(input, out))
return out
end
2017-08-31 20:38:38 +02:00
local function string_endswith ( full , part )
return full : find ( part , 1 , true ) == # full - # part + 1
end
2020-05-12 21:53:01 +02:00
local description_cache = nil
2017-08-31 20:38:38 +02:00
-- normalizes node "description" `nodename`, returning a string (or nil)
2013-06-13 01:59:13 +02:00
worldedit.normalize_nodename = function ( nodename )
2017-08-31 20:38:38 +02:00
nodename = nodename : gsub ( " ^%s*(.-)%s*$ " , " %1 " ) -- strip spaces
2014-03-10 22:57:49 +01:00
if nodename == " " then return nil end
2017-08-31 20:38:38 +02:00
local fullname = ItemStack ( { name = nodename } ) : get_name ( ) -- resolve aliases
if minetest.registered_nodes [ fullname ] or fullname == " air " then -- full name
2013-06-24 23:13:10 +02:00
return fullname
2013-06-13 01:59:13 +02:00
end
2019-12-19 16:58:24 +01:00
nodename = nodename : lower ( )
2020-05-12 21:53:01 +02:00
for key , _ in pairs ( minetest.registered_nodes ) do
if string_endswith ( key : lower ( ) , " : " .. nodename ) then -- matches name (w/o mod part)
2013-06-13 01:59:13 +02:00
return key
end
end
2020-05-12 21:53:01 +02:00
if description_cache == nil then
-- cache stripped descriptions
description_cache = { }
for key , value in pairs ( minetest.registered_nodes ) do
local desc = strip_escapes ( value.description ) : gsub ( " \n .* " , " " , 1 ) : lower ( )
if desc ~= " " then
description_cache [ key ] = desc
end
end
end
for key , desc in pairs ( description_cache ) do
2017-08-31 20:38:38 +02:00
if desc == nodename then -- matches description
return key
end
2020-05-12 21:53:01 +02:00
end
for key , desc in pairs ( description_cache ) do
2019-12-19 16:58:24 +01:00
if desc == nodename .. " block " then
2017-08-31 20:38:38 +02:00
-- fuzzy description match (e.g. "Steel" == "Steel Block")
2013-06-13 01:59:13 +02:00
return key
end
end
2017-08-31 20:38:38 +02:00
local match = nil
2020-05-12 21:53:01 +02:00
for key , value in pairs ( description_cache ) do
if value : find ( nodename , 1 , true ) ~= nil then
2017-08-31 20:38:38 +02:00
if match ~= nil then
return nil
end
match = key -- substring description match (only if no ambiguities)
end
end
return match
2012-10-14 03:45:50 +02:00
end
2015-05-17 01:46:33 +02:00
-- 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 )
2013-07-31 06:02:37 +02:00
local dir = minetest.get_player_by_name ( name ) : get_look_dir ( )
2012-10-14 03:45:50 +02:00
local x , y , z = math.abs ( dir.x ) , math.abs ( dir.y ) , math.abs ( dir.z )
if x > y then
if x > z then
return " x " , dir.x > 0 and 1 or - 1
end
elseif y > z then
return " y " , dir.y > 0 and 1 or - 1
end
return " z " , dir.z > 0 and 1 or - 1
end
2015-06-01 23:08:43 +02:00
local function check_filename ( name )
return name : find ( " ^[%w%s%^&'@{}%[%],%$=!%-#%(%)%%%.%+~_]+$ " ) ~= nil
end
2023-06-07 11:41:10 +02:00
local function open_schematic ( name , param )
-- find the file in the world path
local testpaths = {
minetest.get_worldpath ( ) .. " /schems/ " .. param ,
minetest.get_worldpath ( ) .. " /schems/ " .. param .. " .we " ,
minetest.get_worldpath ( ) .. " /schems/ " .. param .. " .wem " ,
}
local file , err
for index , path in ipairs ( testpaths ) do
file , err = io.open ( path , " rb " )
if not err then
break
end
end
if err then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Could not open file \" @1 \" " , param ) )
2023-06-07 11:41:10 +02:00
return
end
local value = file : read ( " *a " )
file : close ( )
local version = worldedit.read_header ( value )
if version == nil or version == 0 then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Invalid file format! " ) )
2023-06-07 11:41:10 +02:00
return
elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Schematic was created with a newer version of WorldEdit. " ) )
2023-06-07 11:41:10 +02:00
return
end
return value
end
2015-05-17 01:46:33 +02:00
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " about " , {
privs = { } ,
2013-08-29 02:28:49 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Get information about the WorldEdit mod " ) ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " WorldEdit @1 " ..
2024-04-20 14:21:28 +02:00
" is available on this server. Type @2 to get a list of " ..
" commands, or find more information at @3 " ,
worldedit.version_string , minetest.colorize ( " #00ffff " , " //help " ) ,
2023-10-23 20:52:04 +02:00
" https://github.com/Uberi/Minetest-WorldEdit "
) )
2013-08-29 02:28:49 +02:00
end ,
} )
2024-04-20 20:36:07 +02:00
-- initially copied from builtin/chatcommands.lua
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " help " , {
2017-01-01 22:26:58 +01:00
privs = { } ,
params = " [all/<cmd>] " ,
2023-10-23 20:52:04 +02:00
description = S ( " Get help for WorldEdit commands " ) ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
return true , param
end ,
2017-01-01 22:26:58 +01:00
func = function ( name , param )
2024-04-21 22:43:36 +02:00
local function format_help_line ( cmd , def , follow_alias )
2019-12-19 14:12:45 +01:00
local msg = minetest.colorize ( " #00ffff " , " // " .. cmd )
2024-04-20 20:36:07 +02:00
if def.name ~= cmd then
msg = msg .. " : " .. S ( " alias to @1 " ,
minetest.colorize ( " #00ffff " , " // " .. def.name ) )
2024-04-21 22:43:36 +02:00
if follow_alias then
msg = msg .. " \n " .. format_help_line ( def.name , def )
end
2024-04-20 20:36:07 +02:00
else
if def.params and def.params ~= " " then
msg = msg .. " " .. def.params
end
if def.description and def.description ~= " " then
msg = msg .. " : " .. def.description
end
2017-01-01 22:26:58 +01:00
end
return msg
end
2024-04-21 22:43:36 +02:00
-- @param cmds list of {cmd, def}
local function sort_cmds ( cmds )
table.sort ( cmds , function ( c1 , c2 )
local cmd1 , cmd2 = c1 [ 1 ] , c2 [ 1 ]
local def1 , def2 = c1 [ 2 ] , c2 [ 2 ]
-- by category (this puts the empty category first)
if def1.category ~= def2.category then
return def1.category < def2.category
end
-- put aliases last
if ( cmd1 ~= def1.name ) ~= ( cmd2 ~= def2.name ) then
return cmd2 ~= def2.name
end
-- then by name
return c1 [ 1 ] < c2 [ 1 ]
end )
end
2017-01-01 22:26:58 +01:00
if not minetest.check_player_privs ( name , " worldedit " ) then
2023-10-23 20:52:04 +02:00
return false , S ( " You are not allowed to use any WorldEdit commands. " )
2017-01-01 22:26:58 +01:00
end
if param == " " then
2024-04-21 22:43:36 +02:00
local list = { }
2019-12-19 14:12:45 +01:00
for cmd , def in pairs ( worldedit.registered_commands ) do
if minetest.check_player_privs ( name , def.privs ) then
2024-04-21 22:43:36 +02:00
list [ # list + 1 ] = cmd
2017-01-01 22:26:58 +01:00
end
end
2024-04-21 22:43:36 +02:00
table.sort ( list )
2024-04-20 14:21:28 +02:00
local help = minetest.colorize ( " #00ffff " , " //help " )
2023-10-23 20:52:04 +02:00
return true , S ( " Available commands: @1@n "
2024-04-20 14:21:28 +02:00
.. " Use '@2' to get more information, "
.. " or '@3' to list everything. " ,
2024-04-21 22:43:36 +02:00
table.concat ( list , " " ) , help .. " <cmd> " , help .. " all " )
2017-01-01 22:26:58 +01:00
elseif param == " all " then
local cmds = { }
2019-12-19 14:12:45 +01:00
for cmd , def in pairs ( worldedit.registered_commands ) do
if minetest.check_player_privs ( name , def.privs ) then
2024-04-21 22:43:36 +02:00
cmds [ # cmds + 1 ] = { cmd , def }
end
end
sort_cmds ( cmds )
local list = { }
local last_cat = " "
for _ , e in ipairs ( cmds ) do
if e [ 2 ] . category ~= last_cat then
last_cat = e [ 2 ] . category
list [ # list + 1 ] = " ---- " .. last_cat
2017-01-01 22:26:58 +01:00
end
2024-04-21 22:43:36 +02:00
list [ # list + 1 ] = format_help_line ( e [ 1 ] , e [ 2 ] )
2017-01-01 22:26:58 +01:00
end
2024-04-21 22:43:36 +02:00
return true , S ( " Available commands:@n " ) .. table.concat ( list , " \n " )
2017-01-01 22:26:58 +01:00
else
2019-12-19 14:12:45 +01:00
local def = worldedit.registered_commands [ param ]
if not def then
2023-10-23 20:52:04 +02:00
return false , S ( " Command not available: " ) .. param
2019-12-19 14:12:45 +01:00
else
2024-04-21 22:43:36 +02:00
return true , format_help_line ( param , def , true )
2019-12-19 14:12:45 +01:00
end
2017-01-01 22:26:58 +01:00
end
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " inspect " , {
params = " [on/off/1/0/true/false/yes/no/enable/disable] " ,
2023-10-23 20:52:04 +02:00
description = S ( " Enable or disable node inspection " ) ,
2013-07-21 23:16:29 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
2013-12-21 00:41:13 +01:00
if param == " on " or param == " 1 " or param == " true " or param == " yes " or param == " enable " or param == " " then
2019-12-19 14:12:45 +01:00
return true , true
elseif param == " off " or param == " 0 " or param == " false " or param == " no " or param == " disable " then
return true , false
end
return false
end ,
func = function ( name , enable )
if enable then
2013-07-29 18:43:24 +02:00
worldedit.inspect [ name ] = true
2013-12-21 00:41:13 +01:00
local axis , sign = worldedit.player_axis ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S (
" inspector: inspection enabled for @1, currently facing the @2 axis " ,
name ,
axis .. ( sign > 0 and " + " or " - " )
) )
2019-12-19 14:12:45 +01:00
else
2013-07-29 18:43:24 +02:00
worldedit.inspect [ name ] = nil
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " inspector: inspection disabled " ) )
2013-07-21 23:16:29 +02:00
end
end ,
} )
2017-01-26 22:26:45 +01:00
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
2013-07-21 23:16:29 +02:00
minetest.register_on_punchnode ( function ( pos , node , puncher )
2013-07-29 18:43:24 +02:00
local name = puncher : get_player_name ( )
if worldedit.inspect [ name ] then
2017-01-03 17:11:38 +01:00
local axis , sign = worldedit.player_axis ( name )
2023-10-23 20:52:04 +02:00
local message = S (
2023-12-13 17:34:09 +01:00
" inspector: @1 at @2 (param1=@3, param2=@4, received light=@5) punched facing the @6 axis " ,
2023-10-23 20:52:04 +02:00
node.name ,
minetest.pos_to_string ( pos ) ,
node.param1 ,
node.param2 ,
get_node_rlight ( pos ) ,
axis .. ( sign > 0 and " + " or " - " )
)
2013-07-29 18:43:24 +02:00
worldedit.player_notify ( name , message )
2013-07-21 23:16:29 +02:00
end
end )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " reset " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Reset the region so that it is empty " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2012-10-14 03:45:50 +02:00
worldedit.pos1 [ name ] = nil
worldedit.pos2 [ name ] = nil
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2013-05-15 23:03:26 +02:00
worldedit.set_pos [ name ] = nil
2017-01-31 19:52:54 +01:00
--make sure the user does not try to confirm an operation after resetting pos:
reset_pending ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " region reset " ) )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " mark " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Show markers at the region positions " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " region marked " ) )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " unmark " , {
2013-05-15 23:03:26 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Hide markers if currently shown " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2013-05-15 23:03:26 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2013-05-15 23:03:26 +02:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
worldedit.pos1 [ name ] = nil
worldedit.pos2 [ name ] = nil
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2013-05-15 23:03:26 +02:00
worldedit.pos1 [ name ] = pos1
worldedit.pos2 [ name ] = pos2
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " region unmarked " ) )
2013-05-15 23:03:26 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " pos1 " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set WorldEdit region position @1 to the player's location " , 1 ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2019-06-16 20:30:56 +02:00
local pos = minetest.get_player_by_name ( name ) : get_pos ( )
2013-02-17 03:14:44 +01:00
pos.x , pos.y , pos.z = math.floor ( pos.x + 0.5 ) , math.floor ( pos.y + 0.5 ) , math.floor ( pos.z + 0.5 )
2012-10-14 03:45:50 +02:00
worldedit.pos1 [ name ] = pos
worldedit.mark_pos1 ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 1 , minetest.pos_to_string ( pos ) ) )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " pos2 " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set WorldEdit region position @1 to the player's location " , 2 ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
func = function ( name )
2019-06-16 20:30:56 +02:00
local pos = minetest.get_player_by_name ( name ) : get_pos ( )
2013-02-17 03:14:44 +01:00
pos.x , pos.y , pos.z = math.floor ( pos.x + 0.5 ) , math.floor ( pos.y + 0.5 ) , math.floor ( pos.z + 0.5 )
2012-10-14 03:45:50 +02:00
worldedit.pos2 [ name ] = pos
worldedit.mark_pos2 ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 2 , minetest.pos_to_string ( pos ) ) )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " p " , {
2012-10-19 03:28:58 +02:00
params = " set/set1/set2/get " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set WorldEdit region, WorldEdit position 1, or WorldEdit position 2 by punching nodes, or display the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
if param == " set " or param == " set1 " or param == " set2 " or param == " get " then
return true , param
end
2023-10-23 20:52:04 +02:00
return false , S ( " unknown subcommand: @1 " , param )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
func = function ( name , param )
if param == " set " then --set both WorldEdit positions
2012-10-19 03:28:58 +02:00
worldedit.set_pos [ name ] = " pos1 "
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " select positions by punching two nodes " ) )
2012-10-19 03:28:58 +02:00
elseif param == " set1 " then --set WorldEdit position 1
worldedit.set_pos [ name ] = " pos1only "
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " select position @1 by punching a node " , 1 ) )
2012-10-19 03:28:58 +02:00
elseif param == " set2 " then --set WorldEdit position 2
worldedit.set_pos [ name ] = " pos2 "
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " select position @1 by punching a node " , 2 ) )
2012-10-14 03:45:50 +02:00
elseif param == " get " then --display current WorldEdit positions
if worldedit.pos1 [ name ] ~= nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1: @2 " , 1 , minetest.pos_to_string ( worldedit.pos1 [ name ] ) ) )
2012-10-14 03:45:50 +02:00
else
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 not set " , 1 ) )
2012-10-14 03:45:50 +02:00
end
if worldedit.pos2 [ name ] ~= nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1: @2 " , 2 , minetest.pos_to_string ( worldedit.pos2 [ name ] ) ) )
2012-10-14 03:45:50 +02:00
else
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 not set " , 2 ) )
2012-10-14 03:45:50 +02:00
end
end
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " fixedpos " , {
2020-04-26 16:53:55 +02:00
params = " set1/set2 <x> <y> <z> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set a WorldEdit region position to the position at (<x>, <y>, <z>) " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2013-07-28 23:07:47 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
2013-07-28 23:07:47 +02:00
local found , _ , flag , x , y , z = param : find ( " ^(set[12])%s+([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2013-07-28 23:07:47 +02:00
end
2023-06-09 14:28:29 +02:00
return true , flag , vector.new ( tonumber ( x ) , tonumber ( y ) , tonumber ( z ) )
2019-12-19 14:12:45 +01:00
end ,
func = function ( name , flag , pos )
2013-07-28 23:07:47 +02:00
if flag == " set1 " then
worldedit.pos1 [ name ] = pos
worldedit.mark_pos1 ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 1 , minetest.pos_to_string ( pos ) ) )
2013-07-28 23:07:47 +02:00
else --flag == "set2"
worldedit.pos2 [ name ] = pos
worldedit.mark_pos2 ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 2 , minetest.pos_to_string ( pos ) ) )
2013-07-28 23:07:47 +02:00
end
end ,
} )
2012-10-14 03:45:50 +02:00
minetest.register_on_punchnode ( function ( pos , node , puncher )
local name = puncher : get_player_name ( )
if name ~= " " and worldedit.set_pos [ name ] ~= nil then --currently setting position
2012-10-19 03:28:58 +02:00
if worldedit.set_pos [ name ] == " pos1 " then --setting position 1
2012-10-14 03:45:50 +02:00
worldedit.pos1 [ name ] = pos
worldedit.mark_pos1 ( name )
2012-10-19 03:28:58 +02:00
worldedit.set_pos [ name ] = " pos2 " --set position 2 on the next invocation
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 1 , minetest.pos_to_string ( pos ) ) )
2012-10-19 03:28:58 +02:00
elseif worldedit.set_pos [ name ] == " pos1only " then --setting position 1 only
worldedit.pos1 [ name ] = pos
worldedit.mark_pos1 ( name )
2012-10-14 03:45:50 +02:00
worldedit.set_pos [ name ] = nil --finished setting positions
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 1 , minetest.pos_to_string ( pos ) ) )
2012-10-19 03:28:58 +02:00
elseif worldedit.set_pos [ name ] == " pos2 " then --setting position 2
2012-10-14 03:45:50 +02:00
worldedit.pos2 [ name ] = pos
worldedit.mark_pos2 ( name )
2012-10-19 03:28:58 +02:00
worldedit.set_pos [ name ] = nil --finished setting positions
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " position @1 set to @2 " , 2 , minetest.pos_to_string ( pos ) ) )
2013-06-22 08:24:54 +02:00
elseif worldedit.set_pos [ name ] == " prob " then --setting Minetest schematic node probabilities
worldedit.prob_pos [ name ] = pos
2023-03-22 21:26:53 +01:00
minetest.show_formspec ( name , " prob_val_enter " , " field[text;;] " )
2012-10-14 03:45:50 +02:00
end
end
end )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " volume " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Display the volume of the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Region operations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
func = function ( name )
2012-10-14 03:45:50 +02:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
local volume = worldedit.volume ( pos1 , pos2 )
2013-07-28 03:34:42 +02:00
local abs = math.abs
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S (
" current region has a volume of @1 nodes (@2*@3*@4) " ,
volume ,
abs ( pos2.x - pos1.x ) + 1 ,
abs ( pos2.y - pos1.y ) + 1 ,
abs ( pos2.z - pos1.z ) + 1
) )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " deleteblocks " , {
2015-02-08 01:38:05 +01:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Remove all MapBlocks (16x16x16) containing the selected area from the map " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2015-02-08 01:38:05 +01:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2015-02-08 01:38:05 +01:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
local success = minetest.delete_area ( pos1 , pos2 )
if success then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Area deleted. " ) )
2015-02-08 01:38:05 +01:00
else
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " There was an error during deletion of the area. " ) )
2015-02-08 01:38:05 +01:00
end
2019-12-19 14:12:45 +01:00
end ,
2015-02-08 01:38:05 +01:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " set " , {
2014-07-18 21:51:09 +02:00
params = " <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set the current WorldEdit region to <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2014-07-18 21:51:09 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2019-12-19 16:45:38 +01:00
local node = worldedit.normalize_nodename ( param )
2019-12-19 14:12:45 +01:00
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , node
end ,
nodes_needed = check_region ,
func = function ( name , node )
2014-07-18 21:51:09 +02:00
local count = worldedit.set ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes set " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2014-07-18 21:51:09 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " param2 " , {
2017-09-05 14:40:46 +02:00
params = " <param2> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set param2 of all nodes in the current WorldEdit region to <param2> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2017-09-05 14:40:46 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2017-09-05 14:40:46 +02:00
local param2 = tonumber ( param )
if not param2 then
2020-04-26 16:53:55 +02:00
return false
2017-09-05 14:40:46 +02:00
elseif param2 < 0 or param2 > 255 then
2023-10-23 20:52:04 +02:00
return false , S ( " Param2 is out of range (must be between 0 and 255 inclusive!) " )
2017-09-05 14:40:46 +02:00
end
2019-12-19 14:12:45 +01:00
return true , param2
end ,
nodes_needed = check_region ,
func = function ( name , param2 )
2017-09-05 14:40:46 +02:00
local count = worldedit.set_param2 ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , param2 )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes altered " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2017-09-05 14:40:46 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " mix " , {
2020-04-26 16:53:55 +02:00
params = " <node1> [count1] <node2> [count2] ... " ,
2023-10-23 20:52:04 +02:00
description = S ( " Fill the current WorldEdit region with a random mix of <node1>, ... " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2014-07-18 21:51:09 +02:00
local nodes = { }
2014-07-18 22:09:57 +02:00
for nodename in param : gmatch ( " [^%s]+ " ) do
2018-10-12 22:06:01 +02:00
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
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
2018-10-12 22:06:01 +02:00
nodes [ # nodes + 1 ] = node
end
2014-07-18 21:51:09 +02:00
end
2020-05-09 22:34:29 +02:00
if # nodes == 0 then
return false
end
2019-12-19 14:12:45 +01:00
return true , nodes
end ,
nodes_needed = check_region ,
func = function ( name , nodes )
2014-03-12 21:11:00 +01:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2014-07-01 01:13:44 +02:00
local count = worldedit.set ( pos1 , pos2 , nodes )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes set " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
local check_replace = function ( param )
2014-03-12 21:11:00 +01:00
local found , _ , searchnode , replacenode = param : find ( " ^([^%s]+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2014-03-12 21:11:00 +01:00
end
local newsearchnode = worldedit.normalize_nodename ( searchnode )
if not newsearchnode then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid search node name: @1 " , searchnode )
2014-03-12 21:11:00 +01:00
end
local newreplacenode = worldedit.normalize_nodename ( replacenode )
if not newreplacenode then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid replace node name: @1 " , replacenode )
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
return true , newsearchnode , newreplacenode
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " replace " , {
2012-10-14 03:45:50 +02:00
params = " <search node> <replace node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Replace all instances of <search node> with <replace node> in the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = check_replace ,
nodes_needed = check_region ,
func = function ( name , search_node , replace_node )
2014-10-30 03:47:08 +01:00
local count = worldedit.replace ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] ,
2019-12-19 14:12:45 +01:00
search_node , replace_node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes replaced " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " replaceinverse " , {
2013-01-13 00:29:57 +01:00
params = " <search node> <replace node> " ,
2023-10-30 16:53:12 +01:00
description = S ( " Replace all nodes other than <search node> with <replace node> in the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2013-01-13 00:20:41 +01:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = check_replace ,
nodes_needed = check_region ,
func = function ( name , search_node , replace_node )
2014-10-30 03:47:08 +01:00
local count = worldedit.replace ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] ,
2019-12-19 14:12:45 +01:00
search_node , replace_node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes replaced " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2013-01-13 00:20:41 +01:00
} )
2019-12-19 14:12:45 +01:00
local check_cube = function ( param )
2017-08-31 19:07:51 +02:00
local found , _ , w , h , l , nodename = param : find ( " ^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2017-08-31 19:07:51 +02:00
end
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
2019-12-19 16:45:38 +01:00
return true , tonumber ( w ) , tonumber ( h ) , tonumber ( l ) , node
2017-08-31 19:07:51 +02:00
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hollowcube " , {
2017-08-31 19:07:51 +02:00
params = " <width> <height> <length> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add a hollow cube with its ground level centered at WorldEdit position 1 with dimensions <width> x <height> x <length>, composed of <node>. " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2017-08-31 19:07:51 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_cube ,
nodes_needed = function ( name , w , h , l , node )
return w * h * l
end ,
func = function ( name , w , h , l , node )
local count = worldedit.cube ( worldedit.pos1 [ name ] , w , h , l , node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2017-08-31 19:07:51 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " cube " , {
2017-08-31 19:07:51 +02:00
params = " <width> <height> <length> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add a cube with its ground level centered at WorldEdit position 1 with dimensions <width> x <height> x <length>, composed of <node>. " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2017-08-31 19:07:51 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_cube ,
nodes_needed = function ( name , w , h , l , node )
return w * h * l
end ,
func = function ( name , w , h , l , node )
local count = worldedit.cube ( worldedit.pos1 [ name ] , w , h , l , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2017-08-31 19:07:51 +02:00
} )
2019-12-19 14:12:45 +01:00
local check_sphere = function ( param )
2014-03-12 21:11:00 +01:00
local found , _ , radius , nodename = param : find ( " ^(%d+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
2019-12-19 16:45:38 +01:00
return true , tonumber ( radius ) , node
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hollowsphere " , {
2012-10-14 03:45:50 +02:00
params = " <radius> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_sphere ,
nodes_needed = function ( name , radius , node )
2019-12-19 16:45:38 +01:00
return math.ceil ( ( 4 * math.pi * ( radius ^ 3 ) ) / 3 ) --volume of sphere
2019-12-19 14:12:45 +01:00
end ,
func = function ( name , radius , node )
local count = worldedit.sphere ( worldedit.pos1 [ name ] , radius , node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " sphere " , {
2012-10-14 03:45:50 +02:00
params = " <radius> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_sphere ,
nodes_needed = function ( name , radius , node )
2019-12-19 16:45:38 +01:00
return math.ceil ( ( 4 * math.pi * ( radius ^ 3 ) ) / 3 ) --volume of sphere
2019-12-19 14:12:45 +01:00
end ,
func = function ( name , radius , node )
local count = worldedit.sphere ( worldedit.pos1 [ name ] , radius , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
local check_dome = function ( param )
2014-03-12 21:11:00 +01:00
local found , _ , radius , nodename = param : find ( " ^(%d+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
2019-12-19 16:45:38 +01:00
return true , tonumber ( radius ) , node
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hollowdome " , {
2013-04-28 00:28:20 +02:00
params = " <radius> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2013-04-28 00:28:20 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_dome ,
nodes_needed = function ( name , radius , node )
2019-12-19 16:45:38 +01:00
return math.ceil ( ( 2 * math.pi * ( radius ^ 3 ) ) / 3 ) --volume of dome
2019-12-19 14:12:45 +01:00
end ,
func = function ( name , radius , node )
local count = worldedit.dome ( worldedit.pos1 [ name ] , radius , node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2013-04-28 00:28:20 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " dome " , {
2013-04-28 00:28:20 +02:00
params = " <radius> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2013-04-28 00:28:20 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_dome ,
nodes_needed = function ( name , radius , node )
2019-12-19 16:45:38 +01:00
return math.ceil ( ( 2 * math.pi * ( radius ^ 3 ) ) / 3 ) --volume of dome
2019-12-19 14:12:45 +01:00
end ,
func = function ( name , radius , node )
local count = worldedit.dome ( worldedit.pos1 [ name ] , radius , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2013-04-28 00:28:20 +02:00
} )
2019-12-19 14:12:45 +01:00
local check_cylinder = function ( param )
2012-10-19 03:10:30 +02:00
-- 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
2014-03-12 21:11:00 +01:00
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
return true , axis , tonumber ( length ) , tonumber ( radius1 ) , tonumber ( radius2 ) , node
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hollowcylinder " , {
2012-10-19 03:10:30 +02:00
params = " x/y/z/? <length> <radius1> [radius2] <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add hollow cylinder at WorldEdit position 1 along the given axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_cylinder ,
nodes_needed = function ( name , axis , length , radius1 , radius2 , node )
local radius = math.max ( radius1 , radius2 )
return math.ceil ( math.pi * ( radius ^ 2 ) * length )
end ,
func = function ( name , axis , length , radius1 , radius2 , node )
2012-10-14 03:45:50 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2012-10-14 03:45:50 +02:00
axis , sign = worldedit.player_axis ( name )
length = length * sign
end
2019-12-19 14:12:45 +01:00
local count = worldedit.cylinder ( worldedit.pos1 [ name ] , axis , length , radius1 , radius2 , node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " cylinder " , {
2012-10-19 03:10:30 +02:00
params = " x/y/z/? <length> <radius1> [radius2] <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add cylinder at WorldEdit position 1 along the given axis with length <length>, base radius <radius1> (and top radius [radius2]), composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_cylinder ,
nodes_needed = function ( name , axis , length , radius1 , radius2 , node )
local radius = math.max ( radius1 , radius2 )
return math.ceil ( math.pi * ( radius ^ 2 ) * length )
end ,
func = function ( name , axis , length , radius1 , radius2 , node )
2012-10-14 03:45:50 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2012-10-14 03:45:50 +02:00
axis , sign = worldedit.player_axis ( name )
length = length * sign
end
2019-12-19 14:12:45 +01:00
local count = worldedit.cylinder ( worldedit.pos1 [ name ] , axis , length , radius1 , radius2 , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
local check_pyramid = function ( param )
2015-09-13 11:31:47 +02:00
local found , _ , axis , height , nodename = param : find ( " ^([xyz%?])%s+([+-]?%d+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
end
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2015-09-13 11:31:47 +02:00
end
2019-12-19 14:12:45 +01:00
return true , axis , tonumber ( height ) , node
2015-09-13 11:31:47 +02:00
end
2021-01-17 16:35:58 +01:00
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hollowpyramid " , {
2013-07-29 18:43:24 +02:00
params = " x/y/z/? <height> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add hollow pyramid centered at WorldEdit position 1 along the given axis with height <height>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_pyramid ,
nodes_needed = function ( name , axis , height , node )
return math.ceil ( ( ( height * 2 + 1 ) ^ 2 ) * height / 3 )
end ,
func = function ( name , axis , height , node )
2013-07-29 18:43:24 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2013-07-29 18:43:24 +02:00
axis , sign = worldedit.player_axis ( name )
height = height * sign
end
2015-09-13 11:31:47 +02:00
local count = worldedit.pyramid ( worldedit.pos1 [ name ] , axis , height , node , true )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2015-09-13 11:31:47 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " pyramid " , {
2015-09-13 11:31:47 +02:00
params = " x/y/z/? <height> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add pyramid centered at WorldEdit position 1 along the given axis with height <height>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2015-09-13 11:31:47 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = check_pyramid ,
nodes_needed = function ( name , axis , height , node )
return math.ceil ( ( ( height * 2 + 1 ) ^ 2 ) * height / 3 )
end ,
func = function ( name , axis , height , node )
2015-09-13 11:31:47 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2015-09-13 11:31:47 +02:00
axis , sign = worldedit.player_axis ( name )
height = height * sign
2014-03-12 21:11:00 +01:00
end
2015-09-13 11:31:47 +02:00
local count = worldedit.pyramid ( worldedit.pos1 [ name ] , axis , height , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " spiral " , {
2013-08-01 04:15:08 +02:00
params = " <length> <height> <space> <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <space>, composed of <node> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Shapes " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = function ( param )
2014-03-12 21:11:00 +01:00
local found , _ , length , height , space , nodename = param : find ( " ^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2014-03-12 21:11:00 +01:00
end
2019-12-19 14:12:45 +01:00
local node = worldedit.normalize_nodename ( nodename )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , nodename )
2019-12-19 14:12:45 +01:00
end
return true , tonumber ( length ) , tonumber ( height ) , tonumber ( space ) , node
end ,
nodes_needed = function ( name , length , height , space , node )
return ( length + space ) * height -- TODO: this is not the upper bound
end ,
func = function ( name , length , height , space , node )
local count = worldedit.spiral ( worldedit.pos1 [ name ] , length , height , space , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes added " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " copy " , {
2012-10-14 03:45:50 +02:00
params = " x/y/z/? <amount> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Copy the current WorldEdit region along the given axis by <amount> nodes " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2012-10-14 03:45:50 +02:00
local found , _ , axis , amount = param : find ( " ^([xyz%?])%s+([+-]?%d+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2012-10-14 03:45:50 +02:00
end
2019-12-19 14:12:45 +01:00
return true , axis , tonumber ( amount )
end ,
nodes_needed = function ( name , axis , amount )
return check_region ( name ) * 2
end ,
func = function ( name , axis , amount )
2012-10-14 03:45:50 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2012-10-14 03:45:50 +02:00
axis , sign = worldedit.player_axis ( name )
amount = amount * sign
end
2014-07-18 21:51:09 +02:00
local count = worldedit.copy ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , axis , amount )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes copied " , count ) )
2014-03-12 21:11:00 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " move " , {
2012-10-14 03:45:50 +02:00
params = " x/y/z/? <amount> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Move the current WorldEdit region along the given axis by <amount> nodes " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2012-10-14 03:45:50 +02:00
local found , _ , axis , amount = param : find ( " ^([xyz%?])%s+([+-]?%d+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2012-10-14 03:45:50 +02:00
end
2019-12-19 14:12:45 +01:00
return true , axis , tonumber ( amount )
end ,
nodes_needed = function ( name , axis , amount )
return check_region ( name ) * 2
end ,
func = function ( name , axis , amount )
2012-10-14 03:45:50 +02:00
if axis == " ? " then
2017-09-12 14:58:26 +02:00
local sign
2012-10-14 03:45:50 +02:00
axis , sign = worldedit.player_axis ( name )
amount = amount * sign
end
2019-12-20 20:29:41 +01:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2013-07-31 06:02:37 +02:00
local count = worldedit.move ( pos1 , pos2 , axis , amount )
2012-10-14 03:45:50 +02:00
pos1 [ axis ] = pos1 [ axis ] + amount
pos2 [ axis ] = pos2 [ axis ] + amount
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes moved " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " stack " , {
2012-10-14 03:45:50 +02:00
params = " x/y/z/? <count> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Stack the current WorldEdit region along the given axis <count> times " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2014-03-10 22:57:49 +01:00
local found , _ , axis , repetitions = param : find ( " ^([xyz%?])%s+([+-]?%d+)$ " )
2019-12-19 14:12:45 +01:00
if found == nil then
return false
end
return true , axis , tonumber ( repetitions )
end ,
nodes_needed = function ( name , axis , repetitions )
return check_region ( name ) * math.abs ( repetitions )
end ,
func = function ( name , axis , repetitions )
2012-10-14 03:45:50 +02:00
if axis == " ? " then
2017-11-07 14:19:57 +01:00
local sign
2012-10-14 03:45:50 +02:00
axis , sign = worldedit.player_axis ( name )
2014-03-10 22:57:49 +01:00
repetitions = repetitions * sign
2012-10-14 03:45:50 +02:00
end
2019-09-08 22:13:35 +02:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
local count = worldedit.volume ( pos1 , pos2 ) * math.abs ( repetitions )
worldedit.stack ( pos1 , pos2 , axis , repetitions , function ( )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes stacked " , count ) )
2019-09-08 22:13:35 +02:00
end )
2012-10-14 03:45:50 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " stack2 " , {
2014-07-07 01:42:02 +02:00
params = " <count> <x> <y> <z> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Stack the current WorldEdit region <count> times by offset <x>, <y>, <z> " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2014-07-01 01:14:14 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2014-07-18 21:51:09 +02:00
local repetitions , incs = param : match ( " (%d+)%s*(.+) " )
if repetitions == nil then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid count: @1 " , param )
2014-07-18 21:51:09 +02:00
end
2014-12-16 07:44:22 +01:00
local x , y , z = incs : match ( " ([+-]?%d+) ([+-]?%d+) ([+-]?%d+) " )
2014-07-18 21:51:09 +02:00
if x == nil then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid increments: @1 " , param )
2014-07-18 21:51:09 +02:00
end
2014-07-01 01:14:14 +02:00
2023-06-09 14:28:29 +02:00
return true , tonumber ( repetitions ) , vector.new ( tonumber ( x ) , tonumber ( y ) , tonumber ( z ) )
2019-12-19 14:12:45 +01:00
end ,
nodes_needed = function ( name , repetitions , offset )
return check_region ( name ) * repetitions
end ,
func = function ( name , repetitions , offset )
2019-09-08 22:13:35 +02:00
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2014-07-18 21:51:09 +02:00
local count = worldedit.volume ( pos1 , pos2 ) * repetitions
2019-12-19 14:12:45 +01:00
worldedit.stack2 ( pos1 , pos2 , offset , repetitions , function ( )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes stacked " , count ) )
2019-09-08 22:13:35 +02:00
end )
end ,
2014-07-01 01:14:14 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " stretch " , {
2013-12-21 00:41:13 +01:00
params = " <stretchx> <stretchy> <stretchz> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Scale the current WorldEdit positions and region by a factor of <stretchx>, <stretchy>, <stretchz> along the X, Y, and Z axes, repectively, with position 1 as the origin " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2013-06-18 21:05:49 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2013-12-21 00:41:13 +01:00
local found , _ , stretchx , stretchy , stretchz = param : find ( " ^(%d+)%s+(%d+)%s+(%d+)$ " )
2019-12-19 14:12:45 +01:00
if found == nil then
return false
end
2013-12-21 00:41:13 +01:00
stretchx , stretchy , stretchz = tonumber ( stretchx ) , tonumber ( stretchy ) , tonumber ( stretchz )
2019-12-19 14:12:45 +01:00
if stretchx == 0 or stretchy == 0 or stretchz == 0 then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid scaling factors: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , stretchx , stretchy , stretchz
end ,
nodes_needed = function ( name , stretchx , stretchy , stretchz )
return check_region ( name ) * stretchx * stretchy * stretchz
end ,
func = function ( name , stretchx , stretchy , stretchz )
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2013-12-21 00:41:13 +01:00
local count , pos1 , pos2 = worldedit.stretch ( pos1 , pos2 , stretchx , stretchy , stretchz )
2013-06-18 21:05:49 +02:00
--reset markers to scaled positions
worldedit.pos1 [ name ] = pos1
worldedit.pos2 [ name ] = pos2
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2013-06-18 21:05:49 +02:00
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes stretched " , count ) )
2013-06-18 21:05:49 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " transpose " , {
2012-10-14 03:45:50 +02:00
params = " x/y/z/? x/y/z/? " ,
2023-10-23 20:52:04 +02:00
description = S ( " Transpose the current WorldEdit region along the given axes " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2012-10-14 03:45:50 +02:00
local found , _ , axis1 , axis2 = param : find ( " ^([xyz%?])%s+([xyz%?])$ " )
2019-12-19 14:12:45 +01:00
if found == nil then
return false
elseif axis1 == axis2 then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid usage: axes must be different " )
2019-12-19 14:12:45 +01:00
end
return true , axis1 , axis2
end ,
nodes_needed = check_region ,
func = function ( name , axis1 , axis2 )
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2014-03-12 21:11:00 +01:00
if axis1 == " ? " then axis1 = worldedit.player_axis ( name ) end
if axis2 == " ? " then axis2 = worldedit.player_axis ( name ) end
2013-07-31 06:02:37 +02:00
local count , pos1 , pos2 = worldedit.transpose ( pos1 , pos2 , axis1 , axis2 )
2012-12-12 23:17:56 +01:00
--reset markers to transposed positions
worldedit.pos1 [ name ] = pos1
worldedit.pos2 [ name ] = pos2
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2012-12-12 23:17:56 +01:00
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes transposed " , count ) )
2014-03-12 21:11:00 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " flip " , {
2012-10-14 03:45:50 +02:00
params = " x/y/z/? " ,
2023-10-23 20:52:04 +02:00
description = S ( " Flip the current WorldEdit region along the given axis " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
if param ~= " x " and param ~= " y " and param ~= " z " and param ~= " ? " then
return false
end
return true , param
end ,
nodes_needed = check_region ,
func = function ( name , param )
2014-03-12 21:11:00 +01:00
if param == " ? " then param = worldedit.player_axis ( name ) end
2014-07-18 21:51:09 +02:00
local count = worldedit.flip ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , param )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes flipped " , count ) )
2014-03-12 21:11:00 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " rotate " , {
2020-04-26 16:53:55 +02:00
params = " x/y/z/? <angle> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Rotate the current WorldEdit region around the given axis by angle <angle> (90 degree increment) " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2012-10-14 03:45:50 +02:00
local found , _ , axis , angle = param : find ( " ^([xyz%?])%s+([+-]?%d+)$ " )
2019-12-19 14:12:45 +01:00
if found == nil then
return false
end
angle = tonumber ( angle )
if angle % 90 ~= 0 or angle % 360 == 0 then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid usage: angle must be multiple of 90 " )
2019-12-19 14:12:45 +01:00
end
return true , axis , angle
end ,
nodes_needed = check_region ,
func = function ( name , axis , angle )
local pos1 , pos2 = worldedit.pos1 [ name ] , worldedit.pos2 [ name ]
2014-03-12 21:11:00 +01:00
if axis == " ? " then axis = worldedit.player_axis ( name ) end
2013-06-02 23:50:14 +02:00
local count , pos1 , pos2 = worldedit.rotate ( pos1 , pos2 , axis , angle )
2012-12-12 23:17:56 +01:00
--reset markers to rotated positions
worldedit.pos1 [ name ] = pos1
worldedit.pos2 [ name ] = pos2
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2012-12-12 23:17:56 +01:00
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes rotated " , count ) )
2014-03-12 21:11:00 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " orient " , {
2013-01-12 22:46:40 +01:00
params = " <angle> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment) " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Transformations " ) ,
2013-01-12 22:46:40 +01:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2013-01-12 22:46:40 +01:00
local found , _ , angle = param : find ( " ^([+-]?%d+)$ " )
if found == nil then
2019-12-19 14:12:45 +01:00
return false
2013-01-12 22:46:40 +01:00
end
2019-12-19 14:12:45 +01:00
angle = tonumber ( angle )
2013-01-12 22:46:40 +01:00
if angle % 90 ~= 0 then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid usage: angle must be multiple of 90 " )
2013-01-12 22:46:40 +01:00
end
2019-12-19 14:12:45 +01:00
return true , angle
end ,
nodes_needed = check_region ,
func = function ( name , angle )
local count = worldedit.orient ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , angle )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes oriented " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2013-01-12 22:46:40 +01:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " fixlight " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Fix the lighting in the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2014-07-18 21:51:09 +02:00
local count = worldedit.fixlight ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes updated " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " drain " , {
2016-03-23 22:09:55 +01:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Remove any fluid node within the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2016-03-23 22:09:55 +01:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2016-03-23 22:09:55 +01:00
-- TODO: make an API function for this
local count = 0
local pos1 , pos2 = worldedit.sort_pos ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2023-06-09 14:28:29 +02:00
local get_node , remove_node = minetest.get_node , minetest.remove_node
2016-03-23 22:09:55 +01:00
for x = pos1.x , pos2.x do
for y = pos1.y , pos2.y do
for z = pos1.z , pos2.z do
2023-06-09 14:28:29 +02:00
local p = vector.new ( x , y , z )
local n = get_node ( p ) . name
2016-03-23 22:09:55 +01:00
local d = minetest.registered_nodes [ n ]
2023-06-09 14:28:29 +02:00
if d ~= nil and ( d.drawtype == " liquid " or d.drawtype == " flowingliquid " ) then
remove_node ( p )
2016-03-23 22:09:55 +01:00
count = count + 1
end
end
end
end
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes updated " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2016-03-23 22:09:55 +01:00
} )
2020-04-07 03:05:30 +02:00
local clearcut_cache
local function clearcut ( pos1 , pos2 )
-- decide which nodes we consider plants
if clearcut_cache == nil then
clearcut_cache = { }
for name , def in pairs ( minetest.registered_nodes ) do
local groups = def.groups or { }
if (
-- the groups say so
groups.flower or groups.grass or groups.flora or groups.plant or
groups.leaves or groups.tree or groups.leafdecay or groups.sapling or
-- drawtype heuristic
( def.is_ground_content and def.buildable_to and
( def.sunlight_propagates or not def.walkable )
and def.drawtype == " plantlike " ) or
-- if it's flammable, it probably needs to go too
( def.is_ground_content and not def.walkable and groups.flammable )
) then
clearcut_cache [ name ] = true
end
end
end
local plants = clearcut_cache
local count = 0
local prev , any
2023-06-09 14:28:29 +02:00
local get_node , remove_node = minetest.get_node , minetest.remove_node
2020-04-07 03:05:30 +02:00
for x = pos1.x , pos2.x do
for z = pos1.z , pos2.z do
prev = false
any = false
-- first pass: remove floating nodes that would be left over
for y = pos1.y , pos2.y do
2023-06-09 14:28:29 +02:00
local pos = vector.new ( x , y , z )
local n = get_node ( pos ) . name
2020-04-07 03:05:30 +02:00
if plants [ n ] then
prev = true
any = true
elseif prev then
local def = minetest.registered_nodes [ n ] or { }
local groups = def.groups or { }
if groups.attached_node or ( def.buildable_to and groups.falling_node ) then
2023-06-09 14:28:29 +02:00
remove_node ( pos )
2020-04-07 03:05:30 +02:00
count = count + 1
else
prev = false
end
end
end
-- second pass: remove plants, top-to-bottom to avoid item drops
if any then
for y = pos2.y , pos1.y , - 1 do
2023-06-09 14:28:29 +02:00
local pos = vector.new ( x , y , z )
local n = get_node ( pos ) . name
2020-04-07 03:05:30 +02:00
if plants [ n ] then
2023-06-09 14:28:29 +02:00
remove_node ( pos )
2020-04-07 03:05:30 +02:00
count = count + 1
end
end
end
end
end
return count
end
worldedit.register_command ( " clearcut " , {
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Remove any plant, tree or foliage-like nodes in the selected region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2020-04-07 03:05:30 +02:00
privs = { worldedit = true } ,
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
local pos1 , pos2 = worldedit.sort_pos ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
local count = clearcut ( pos1 , pos2 )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes removed " , count ) )
2020-04-07 03:05:30 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " hide " , {
2012-12-12 23:17:56 +01:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Hide all nodes in the current WorldEdit region non-destructively " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-12-12 23:17:56 +01:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2014-07-18 21:51:09 +02:00
local count = worldedit.hide ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes hidden " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-12-12 23:17:56 +01:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " suppress " , {
2012-10-14 03:45:50 +02:00
params = " <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Suppress all <node> in the current WorldEdit region non-destructively " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
local node = worldedit.normalize_nodename ( param )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , node
end ,
nodes_needed = check_region ,
func = function ( name , node )
2014-07-18 21:51:09 +02:00
local count = worldedit.suppress ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes suppressed " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " highlight " , {
2012-10-14 03:45:50 +02:00
params = " <node> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Highlight <node> in the current WorldEdit region by hiding everything else non-destructively " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
local node = worldedit.normalize_nodename ( param )
if not node then
2023-10-23 20:52:04 +02:00
return false , S ( " invalid node name: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , node
end ,
nodes_needed = check_region ,
func = function ( name , node )
2014-07-18 21:51:09 +02:00
local count = worldedit.highlight ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , node )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes highlighted " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " restore " , {
2012-10-14 03:45:50 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Restores nodes hidden with WorldEdit in the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2014-07-18 21:51:09 +02:00
local count = worldedit.restore ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes restored " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-09-09 01:07:17 +02:00
local function detect_misaligned_schematic ( name , pos1 , pos2 )
2023-06-19 18:24:31 +02:00
pos1 = worldedit.sort_pos ( pos1 , pos2 )
2019-09-09 01:07:17 +02:00
-- Check that allocate/save can position the schematic correctly
-- The expected behaviour is that the (0,0,0) corner of the schematic stays
2024-04-21 22:43:36 +02:00
-- at pos1, this only works when the minimum position is actually present
2019-09-09 01:07:17 +02:00
-- in the schematic.
local node = minetest.get_node ( pos1 )
local have_node_at_origin = node.name ~= " air " and node.name ~= " ignore "
if not have_node_at_origin then
worldedit.player_notify ( name ,
2023-10-23 20:52:04 +02:00
S ( " Warning: The schematic contains excessive free space and WILL be " ..
2019-09-09 01:07:17 +02:00
" misaligned when allocated or loaded. To avoid this, shrink your " ..
2023-10-23 20:52:04 +02:00
" area to cover exactly the nodes to be saved. " )
2019-09-09 01:07:17 +02:00
)
end
end
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " save " , {
2012-10-14 03:45:50 +02:00
params = " <file> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Save the current WorldEdit region to \" (world folder)/schems/<file>.we \" " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
2012-10-14 03:45:50 +02:00
if param == " " then
2019-12-19 14:12:45 +01:00
return false
2012-10-14 03:45:50 +02:00
end
2015-06-01 23:08:43 +02:00
if not check_filename ( param ) then
2023-10-23 20:52:04 +02:00
return false , S ( " Disallowed file name: @1 " , param )
2013-12-21 00:41:13 +01:00
end
2019-12-19 14:12:45 +01:00
return true , param
end ,
nodes_needed = check_region ,
func = function ( name , param )
2015-05-17 01:46:33 +02:00
local result , count = worldedit.serialize ( worldedit.pos1 [ name ] ,
worldedit.pos2 [ name ] )
2019-09-09 01:07:17 +02:00
detect_misaligned_schematic ( name , worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2012-10-14 03:45:50 +02:00
local path = minetest.get_worldpath ( ) .. " /schems "
2015-05-17 01:46:33 +02:00
-- Create directory if it does not already exist
2019-07-17 02:06:29 +02:00
minetest.mkdir ( path )
2015-05-17 01:46:33 +02:00
2012-10-14 03:45:50 +02:00
local filename = path .. " / " .. param .. " .we "
local file , err = io.open ( filename , " wb " )
if err ~= nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Could not save file to \" @1 \" " , filename ) )
2012-10-14 03:45:50 +02:00
return
end
file : write ( result )
file : flush ( )
file : close ( )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes saved " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2012-10-14 03:45:50 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " allocate " , {
2012-10-21 23:49:43 +02:00
params = " <file> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Set the region defined by nodes from \" (world folder)/schems/<file>.we \" as the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2012-10-21 23:49:43 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = function ( param )
2012-10-21 23:49:43 +02:00
if param == " " then
2019-12-19 14:12:45 +01:00
return false
2012-10-21 23:49:43 +02:00
end
2015-06-01 23:08:43 +02:00
if not check_filename ( param ) then
2023-10-23 20:52:04 +02:00
return false , S ( " Disallowed file name: @1 " , param )
2013-12-21 00:41:13 +01:00
end
2019-12-19 14:12:45 +01:00
return true , param
end ,
func = function ( name , param )
local pos = worldedit.pos1 [ name ]
2012-10-21 23:49:43 +02:00
2023-06-07 11:41:10 +02:00
local value = open_schematic ( name , param )
if not value then
return false
2012-10-21 23:49:43 +02:00
end
2014-03-10 22:57:49 +01:00
local nodepos1 , nodepos2 , count = worldedit.allocate ( pos , value )
2019-07-17 01:20:40 +02:00
if not nodepos1 then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Schematic empty, nothing allocated " ) )
2023-06-07 11:41:10 +02:00
return false
2019-07-17 01:20:40 +02:00
end
2012-10-21 23:49:43 +02:00
worldedit.pos1 [ name ] = nodepos1
worldedit.pos2 [ name ] = nodepos2
2020-04-07 00:14:31 +02:00
worldedit.marker_update ( name )
2012-10-21 23:49:43 +02:00
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes allocated " , count ) )
2012-10-21 23:49:43 +02:00
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " load " , {
2012-10-14 03:45:50 +02:00
params = " <file> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Load nodes from \" (world folder)/schems/<file>[.we[m]] \" with position 1 of the current WorldEdit region as the origin " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2012-10-14 03:45:50 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = function ( param )
if param == " " then
return false
end
if not check_filename ( param ) then
2023-10-23 20:52:04 +02:00
return false , S ( " Disallowed file name: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , param
end ,
2012-10-14 03:45:50 +02:00
func = function ( name , param )
2019-12-19 14:12:45 +01:00
local pos = worldedit.pos1 [ name ]
2012-10-14 03:45:50 +02:00
2023-06-07 11:41:10 +02:00
local value = open_schematic ( name , param )
if not value then
return false
2012-10-14 03:45:50 +02:00
end
2013-04-28 19:08:09 +02:00
2014-03-10 22:57:49 +01:00
local count = worldedit.deserialize ( pos , value )
2023-06-07 11:41:10 +02:00
if count == nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Loading failed! " ) )
2023-06-07 11:41:10 +02:00
return false
end
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 nodes loaded " , count ) )
2012-10-14 03:45:50 +02:00
end ,
} )
2013-01-13 00:20:41 +01:00
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " lua " , {
2013-01-13 00:20:41 +01:00
params = " <code> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Executes <code> as a Lua chunk in the global namespace " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Code " ) ,
2013-01-14 22:14:54 +01:00
privs = { worldedit = true , server = true } ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
2024-04-20 20:00:41 +02:00
if param == " " then
return false
end
2019-12-19 14:12:45 +01:00
return true , param
end ,
2013-01-13 00:20:41 +01:00
func = function ( name , param )
2024-04-20 20:00:41 +02:00
-- shorthand like in the Lua interpreter
if param : sub ( 1 , 1 ) == " = " then
param = " return " .. param : sub ( 2 )
end
local err , ret = worldedit.lua ( param , name )
if err == nil then
minetest.log ( " action " , name .. " executed " .. param )
if ret ~= " nil " then
worldedit.player_notify ( name , " code successfully executed, returned " .. ret )
else
worldedit.player_notify ( name , " code successfully executed " )
end
2013-01-13 00:20:41 +01:00
else
2024-04-20 20:00:41 +02:00
minetest.log ( " action " , name .. " tried to execute " .. param )
worldedit.player_notify ( name , " code error: " .. err )
2013-01-13 00:20:41 +01:00
end
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " luatransform " , {
2013-01-13 00:20:41 +01:00
params = " <code> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Code " ) ,
2013-01-14 22:14:54 +01:00
privs = { worldedit = true , server = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
return true , param
end ,
nodes_needed = check_region ,
func = function ( name , param )
2014-07-18 21:51:09 +02:00
local err = worldedit.luatransform ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] , param )
2013-01-13 00:20:41 +01:00
if err then
2013-05-15 23:52:12 +02:00
worldedit.player_notify ( name , " code error: " .. err , false )
2017-05-21 17:31:59 +02:00
minetest.log ( " action " , name .. " tried to execute luatransform " .. param )
2013-01-13 00:20:41 +01:00
else
2013-05-15 23:52:12 +02:00
worldedit.player_notify ( name , " code successfully executed " , false )
2017-05-21 17:31:59 +02:00
minetest.log ( " action " , name .. " executed luatransform " .. param )
2013-01-13 00:20:41 +01:00
end
2019-12-19 14:12:45 +01:00
end ,
2013-04-28 19:08:09 +02:00
} )
2013-06-22 08:24:54 +02:00
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " mtschemcreate " , {
2013-07-29 18:43:24 +02:00
params = " <file> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Save the current WorldEdit region using the Minetest " ..
" Schematic format to \" (world folder)/schems/<filename>.mts \" " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2013-06-22 08:24:54 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
parse = function ( param )
if param == " " then
return false
2013-06-22 08:24:54 +02:00
end
2015-06-01 23:08:43 +02:00
if not check_filename ( param ) then
2023-10-23 20:52:04 +02:00
return false , S ( " Disallowed file name: @1 " , param )
2015-05-17 01:46:33 +02:00
end
2019-12-19 14:12:45 +01:00
return true , param
end ,
nodes_needed = check_region ,
func = function ( name , param )
2013-06-22 08:24:54 +02:00
local path = minetest.get_worldpath ( ) .. " /schems "
2015-05-17 01:46:33 +02:00
-- Create directory if it does not already exist
2019-07-17 02:06:29 +02:00
minetest.mkdir ( path )
2013-06-22 08:24:54 +02:00
2015-05-17 01:46:33 +02:00
local filename = path .. " / " .. param .. " .mts "
local ret = minetest.create_schematic ( worldedit.pos1 [ name ] ,
worldedit.pos2 [ name ] , worldedit.prob_list [ name ] ,
filename )
2013-06-22 08:24:54 +02:00
if ret == nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Failed to create Minetest schematic " ) )
2013-06-22 08:24:54 +02:00
else
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " Saved Minetest schematic to @1 " , param ) )
2013-06-22 08:24:54 +02:00
end
worldedit.prob_list [ name ] = { }
2019-12-19 14:12:45 +01:00
end ,
2013-06-22 08:24:54 +02:00
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " mtschemplace " , {
2013-07-29 18:43:24 +02:00
params = " <file> " ,
2023-10-23 20:52:04 +02:00
description = S ( " Load nodes from \" (world folder)/schems/<file>.mts \" with position 1 of the current WorldEdit region as the origin " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2013-06-22 08:24:54 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 1 ,
parse = function ( param )
2015-06-01 23:08:43 +02:00
if param == " " then
2019-12-19 14:12:45 +01:00
return false
2013-06-22 08:24:54 +02:00
end
2015-06-01 23:08:43 +02:00
if not check_filename ( param ) then
2023-10-23 20:52:04 +02:00
return false , S ( " Disallowed file name: @1 " , param )
2015-06-01 23:08:43 +02:00
end
2019-12-19 14:12:45 +01:00
return true , param
end ,
func = function ( name , param )
local pos = worldedit.pos1 [ name ]
2014-03-10 22:57:49 +01:00
2013-06-22 08:24:54 +02:00
local path = minetest.get_worldpath ( ) .. " /schems/ " .. param .. " .mts "
if minetest.place_schematic ( pos , path ) == nil then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " failed to place Minetest schematic " ) )
2013-06-22 08:24:54 +02:00
else
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " placed Minetest schematic @1 at @2 " , param , minetest.pos_to_string ( pos ) ) )
2013-06-22 08:24:54 +02:00
end
end ,
} )
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " mtschemprob " , {
2013-06-22 08:24:54 +02:00
params = " start/finish/get " ,
2023-10-23 20:52:04 +02:00
description = S ( " Begins node probability entry for Minetest schematics, gets the nodes that have probabilities set, or ends node probability entry " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Schematics " ) ,
2013-06-22 08:24:54 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
parse = function ( param )
if param ~= " start " and param ~= " finish " and param ~= " get " then
2023-10-23 20:52:04 +02:00
return false , S ( " unknown subcommand: @1 " , param )
2019-12-19 14:12:45 +01:00
end
return true , param
end ,
2013-06-22 08:24:54 +02:00
func = function ( name , param )
if param == " start " then --start probability setting
worldedit.set_pos [ name ] = " prob "
worldedit.prob_list [ name ] = { }
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " select Minetest schematic probability values by punching nodes " ) )
2013-06-22 08:24:54 +02:00
elseif param == " finish " then --finish probability setting
worldedit.set_pos [ name ] = nil
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " finished Minetest schematic probability selection " ) )
2013-06-22 08:24:54 +02:00
elseif param == " get " then --get all nodes that had probabilities set on them
local text = " "
local problist = worldedit.prob_list [ name ]
if problist == nil then
return
end
for k , v in pairs ( problist ) do
2016-08-05 16:41:37 +02:00
local prob = math.floor ( ( ( v.prob / 256 ) * 100 ) * 100 + 0.5 ) / 100
text = text .. minetest.pos_to_string ( v.pos ) .. " : " .. prob .. " % | "
2013-06-22 08:24:54 +02:00
end
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " currently set node probabilities: " ) )
2013-06-22 08:24:54 +02:00
worldedit.player_notify ( name , text )
end
end ,
} )
2016-08-05 16:41:37 +02:00
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2023-03-22 21:26:53 +01:00
if formname == " prob_val_enter " then
2016-08-05 16:41:37 +02:00
local name = player : get_player_name ( )
2023-03-22 21:26:53 +01:00
local problist = worldedit.prob_list [ name ]
if problist == nil then
return
end
local e = { pos = worldedit.prob_pos [ name ] , prob = tonumber ( fields.text ) }
if e.pos == nil or e.prob == nil or e.prob < 0 or e.prob > 256 then
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " invalid node probability given, not saved " ) )
2023-03-22 21:26:53 +01:00
return
end
problist [ # problist + 1 ] = e
2013-06-22 08:24:54 +02:00
end
2016-08-05 16:41:37 +02:00
end )
2013-07-29 18:43:24 +02:00
2019-12-19 14:12:45 +01:00
worldedit.register_command ( " clearobjects " , {
2013-07-29 18:43:24 +02:00
params = " " ,
2023-10-23 20:52:04 +02:00
description = S ( " Clears all objects within the WorldEdit region " ) ,
2024-04-21 22:43:36 +02:00
category = S ( " Node manipulation " ) , -- not really, but it doesn't fit anywhere else
2013-07-29 18:43:24 +02:00
privs = { worldedit = true } ,
2019-12-19 14:12:45 +01:00
require_pos = 2 ,
nodes_needed = check_region ,
func = function ( name )
2014-10-30 03:47:08 +01:00
local count = worldedit.clear_objects ( worldedit.pos1 [ name ] , worldedit.pos2 [ name ] )
2023-10-23 20:52:04 +02:00
worldedit.player_notify ( name , S ( " @1 objects cleared " , count ) )
2019-12-19 14:12:45 +01:00
end ,
2016-08-05 16:41:37 +02:00
} )