1 minetest.register_privilege("worldedit", "Can use WorldEdit commands")
\r
5 worldedit.set_pos = {}
\r
10 dofile(minetest.get_modpath("worldedit") .. "/functions.lua")
\r
11 dofile(minetest.get_modpath("worldedit") .. "/mark.lua")
\r
13 --determines whether `nodename` is a valid node name, returning a boolean
\r
14 worldedit.node_is_valid = function(temp_pos, nodename)
\r
15 local originalnode = minetest.env:get_node(temp_pos) --save the original node to restore later
\r
16 minetest.env:add_node(temp_pos, {name=nodename}) --attempt to add the node
\r
17 local value = minetest.env:get_node(temp_pos).name --obtain the name of the newly added node
\r
18 if value == nodename or value == "default:" .. nodename then --successfully added node
\r
19 minetest.env:add_node(temp_pos, originalnode) --restore the original node
\r
20 return true --node is valid
\r
22 return false --node is not valid
\r
25 minetest.register_chatcommand("/reset", {
\r
27 description = "Reset the region so that it is empty",
\r
28 privs = {worldedit=true},
\r
29 func = function(name, param)
\r
30 worldedit.pos1[name] = nil
\r
31 worldedit.pos2[name] = nil
\r
32 worldedit.mark_pos1(name)
\r
33 worldedit.mark_pos2(name)
\r
34 minetest.chat_send_player(name, "WorldEdit region reset")
\r
38 minetest.register_chatcommand("/mark", {
\r
40 description = "Show markers at the region positions",
\r
41 privs = {worldedit=true},
\r
42 func = function(name, param)
\r
43 worldedit.mark_pos1(name)
\r
44 worldedit.mark_pos2(name)
\r
45 minetest.chat_send_player(name, "WorldEdit region marked")
\r
49 minetest.register_chatcommand("/pos1", {
\r
51 description = "Set WorldEdit region position 1 to the player's location",
\r
52 privs = {worldedit=true},
\r
53 func = function(name, param)
\r
54 local pos = minetest.env:get_player_by_name(name):getpos()
\r
55 pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
\r
56 worldedit.pos1[name] = pos
\r
57 worldedit.mark_pos1(name)
\r
58 minetest.chat_send_player(name, "WorldEdit position 1 set to " .. minetest.pos_to_string(pos))
\r
62 minetest.register_chatcommand("/pos2", {
\r
64 description = "Set WorldEdit region position 2 to the player's location",
\r
65 privs = {worldedit=true},
\r
66 func = function(name, param)
\r
67 local pos = minetest.env:get_player_by_name(name):getpos()
\r
68 pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
\r
69 worldedit.pos2[name] = pos
\r
70 worldedit.mark_pos2(name)
\r
71 minetest.chat_send_player(name, "WorldEdit position 2 set to " .. minetest.pos_to_string(pos))
\r
75 minetest.register_chatcommand("/p", {
\r
77 description = "Set WorldEdit region by punching two nodes, or display the current WorldEdit region",
\r
78 privs = {worldedit=true},
\r
79 func = function(name, param)
\r
80 if param == "set" then --set both WorldEdit positions
\r
81 worldedit.set_pos[name] = 1
\r
82 minetest.chat_send_player(name, "Select positions by punching two nodes")
\r
83 elseif param == "get" then --display current WorldEdit positions
\r
84 if worldedit.pos1[name] ~= nil then
\r
85 minetest.chat_send_player(name, "WorldEdit position 1: " .. minetest.pos_to_string(worldedit.pos1[name]))
\r
87 minetest.chat_send_player(name, "WorldEdit position 1 not set")
\r
89 if worldedit.pos2[name] ~= nil then
\r
90 minetest.chat_send_player(name, "WorldEdit position 2: " .. minetest.pos_to_string(worldedit.pos2[name]))
\r
92 minetest.chat_send_player(name, "WorldEdit position 2 not set")
\r
95 minetest.chat_send_player(name, "Unknown subcommand: " .. param)
\r
100 minetest.register_on_punchnode(function(pos, node, puncher)
\r
101 local name = puncher:get_player_name()
\r
102 if name ~= "" and worldedit.set_pos[name] ~= nil then --currently setting position
\r
103 if worldedit.set_pos[name] == 1 then --setting position 1
\r
104 worldedit.set_pos[name] = 2 --set position 2 on the next invocation
\r
105 worldedit.pos1[name] = pos
\r
106 worldedit.mark_pos1(name)
\r
107 minetest.chat_send_player(name, "WorldEdit region position 1 set to " .. minetest.pos_to_string(pos))
\r
108 else --setting position 2
\r
109 worldedit.set_pos[name] = nil --finished setting positions
\r
110 worldedit.pos2[name] = pos
\r
111 worldedit.mark_pos2(name)
\r
112 minetest.chat_send_player(name, "WorldEdit region position 2 set to " .. minetest.pos_to_string(pos))
\r
117 minetest.register_chatcommand("/volume", {
\r
119 description = "Display the volume of the current WorldEdit region",
\r
120 privs = {worldedit=true},
\r
121 func = function(name, param)
\r
122 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
123 if pos1 == nil or pos2 == nil then
\r
124 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
128 local volume = worldedit.volume(pos1, pos2)
\r
129 minetest.chat_send_player(name, "Current WorldEdit region has a volume of " .. volume .. " nodes (" .. pos2.x - pos1.x .. "*" .. pos2.y - pos1.y .. "*" .. pos2.z - pos1.z .. ")")
\r
133 minetest.register_chatcommand("/set", {
\r
135 description = "Set the current WorldEdit region to <node>",
\r
136 privs = {worldedit=true},
\r
137 func = function(name, param)
\r
138 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
139 if pos1 == nil or pos2 == nil then
\r
140 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
144 if param == "" or not worldedit.node_is_valid(pos1, param) then
\r
145 minetest.chat_send_player(name, "Invalid node name: " .. param)
\r
149 local count = worldedit.set(pos1, pos2, param)
\r
150 minetest.chat_send_player(name, count .. " nodes set")
\r
154 minetest.register_chatcommand("/replace", {
\r
155 params = "<search node> <replace node>",
\r
156 description = "Replace all instances of <search node> with <place node> in the current WorldEdit region",
\r
157 privs = {worldedit=true},
\r
158 func = function(name, param)
\r
159 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
160 if pos1 == nil or pos2 == nil then
\r
161 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
165 local found, _, searchnode, replacenode = param:find("([^%s]+)%s+([^%s]+)")
\r
166 if found == nil then
\r
167 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
170 if not worldedit.node_is_valid(pos1, searchnode) then
\r
171 minetest.chat_send_player(name, "Invalid search node name: " .. searchnode)
\r
174 if not worldedit.node_is_valid(pos1, replacenode) then
\r
175 minetest.chat_send_player(name, "Invalid replace node name: " .. replacenode)
\r
179 local count = worldedit.replace(pos1, pos2, searchnode, replacenode)
\r
180 minetest.chat_send_player(name, count .. " nodes replaced")
\r
184 minetest.register_chatcommand("/copy", {
\r
185 params = "x/y/z <amount>",
\r
186 description = "Copy the current WorldEdit region along the x/y/z axis by <amount> nodes",
\r
187 privs = {worldedit=true},
\r
188 func = function(name, param)
\r
189 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
190 if pos1 == nil or pos2 == nil then
\r
191 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
195 local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")
\r
196 if found == nil then
\r
197 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
201 local count = worldedit.copy(pos1, pos2, axis, tonumber(amount))
\r
202 minetest.chat_send_player(name, count .. " nodes copied")
\r
206 minetest.register_chatcommand("/move", {
\r
207 params = "x/y/z <amount>",
\r
208 description = "Move the current WorldEdit region along the x/y/z axis by <amount> nodes",
\r
209 privs = {worldedit=true},
\r
210 func = function(name, param)
\r
211 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
212 if pos1 == nil or pos2 == nil then
\r
213 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
217 local found, _, axis, amount = param:find("([xyz])%s+([+-]?%d+)")
\r
218 if found == nil then
\r
219 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
223 local count = worldedit.move(pos1, pos2, axis, tonumber(amount))
\r
224 minetest.chat_send_player(name, count .. " nodes moved")
\r
228 minetest.register_chatcommand("/stack", {
\r
229 params = "x/y/z <count>",
\r
230 description = "Stack the current WorldEdit region along the x/y/z axis <count> times",
\r
231 privs = {worldedit=true},
\r
232 func = function(name, param)
\r
233 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
234 if pos1 == nil or pos2 == nil then
\r
235 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
239 local found, _, axis, count = param:find("([xyz])%s+([+-]?%d+)")
\r
240 if found == nil then
\r
241 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
245 local count = worldedit.stack(pos1, pos2, axis, tonumber(count))
\r
246 minetest.chat_send_player(name, count .. " nodes stacked")
\r
250 minetest.register_chatcommand("/dig", {
\r
252 description = "Dig the current WorldEdit region",
\r
253 privs = {worldedit=true},
\r
254 func = function(name, param)
\r
255 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
256 if pos1 == nil or pos2 == nil then
\r
257 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
261 local count = worldedit.dig(pos1, pos2)
\r
262 minetest.chat_send_player(name, count .. " nodes dug")
\r
266 minetest.register_chatcommand("/save", {
\r
268 description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.we\"",
\r
269 privs = {worldedit=true},
\r
270 func = function(name, param)
\r
271 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
\r
272 if pos1 == nil or pos2 == nil then
\r
273 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
277 if param == "" then
\r
278 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
282 local result, count = worldedit.serialize(pos1, pos2)
\r
284 local path = minetest.get_worldpath() .. "/schems"
\r
285 local filename = path .. "/" .. param .. ".we"
\r
286 os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
\r
287 local file, err = io.open(filename, "wb")
\r
289 minetest.chat_send_player(name, "Could not save file to \"" .. filename .. "\"")
\r
296 minetest.chat_send_player(name, count .. " nodes saved")
\r
300 minetest.register_chatcommand("/load", {
\r
302 description = "Load nodes from \"(world folder)/schems/<file>.we\" with position 1 of the current WorldEdit region as the origin",
\r
303 privs = {worldedit=true},
\r
304 func = function(name, param)
\r
305 local pos1 = worldedit.pos1[name]
\r
306 if pos1 == nil then
\r
307 minetest.chat_send_player(name, "No WorldEdit region selected")
\r
311 if param == "" then
\r
312 minetest.chat_send_player(name, "Invalid usage: " .. param)
\r
316 local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"
\r
317 local file, err = io.open(filename, "rb")
\r
319 minetest.chat_send_player(name, "Could not open file \"" .. filename .. "\"")
\r
322 local value = file:read("*a")
\r
326 if value:find("{") then --old WorldEdit format
\r
327 count = worldedit.deserialize_old(pos1, value)
\r
328 else --new WorldEdit format
\r
329 count = worldedit.deserialize(pos1, value)
\r
332 minetest.chat_send_player(name, count .. " nodes loaded")
\r