1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
minetest.register_privilege("worldedit", "Can use WorldEdit commands")
worldedit = {}
worldedit.set_pos = {}
worldedit.pos1 = {}
worldedit.pos2 = {}
dofile(minetest.get_modpath("worldedit") .. "/functions.lua")
dofile(minetest.get_modpath("worldedit") .. "/mark.lua")
--determines whether `nodename` is a valid node name, returning a boolean
worldedit.node_is_valid = function(temp_pos, nodename)
local originalnode = minetest.env:get_node(temp_pos) --save the original node to restore later
minetest.env:add_node(temp_pos, {name=nodename}) --attempt to add the node
local value = minetest.env:get_node(temp_pos).name --obtain the name of the newly added node
if value == nodename or value == "default:" .. nodename then --successfully added node
minetest.env:add_node(temp_pos, originalnode) --restore the original node
return true --node is valid
end
return false --node is not valid
end
minetest.register_chatcommand("/reset", {
params = "",
description = "Reset the region so that it is empty",
privs = {worldedit=true},
func = function(name, param)
worldedit.pos1[name] = nil
worldedit.pos2[name] = nil
worldedit.mark_pos1(name)
worldedit.mark_pos2(name)
minetest.chat_send_player(name, "WorldEdit region reset")
end,
})
minetest.register_chatcommand("/mark", {
params = "",
description = "Show markers at the region positions",
privs = {worldedit=true},
func = function(name, param)
worldedit.mark_pos1(name)
worldedit.mark_pos2(name)
minetest.chat_send_player(name, "WorldEdit region marked")
end,
})
minetest.register_chatcommand("/pos1", {
params = "",
description = "Set WorldEdit region position 1 to the player's location",
privs = {worldedit=true},
func = function(name, param)
local pos = minetest.env:get_player_by_name(name):getpos()
pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
worldedit.pos1[name] = pos
worldedit.mark_pos1(name)
minetest.chat_send_player(name, "WorldEdit position 1 set to " .. minetest.pos_to_string(pos))
end,
})
minetest.register_chatcommand("/pos2", {
params = "",
description = "Set WorldEdit region position 2 to the player's location",
privs = {worldedit=true},
func = function(name, param)
local pos = minetest.env:get_player_by_name(name):getpos()
pos.x, pos.y, pos.z = math.floor(pos.x), math.floor(pos.y), math.floor(pos.z)
worldedit.pos2[name] = pos
worldedit.mark_pos2(name)
minetest.chat_send_player(name, "WorldEdit position 2 set to " .. minetest.pos_to_string(pos))
end,
})
minetest.register_chatcommand("/p", {
params = "set/get",
description = "Set WorldEdit region by punching two nodes, or display the current WorldEdit region",
privs = {worldedit=true},
func = function(name, param)
if param == "set" then --set both WorldEdit positions
worldedit.set_pos[name] = 1
minetest.chat_send_player(name, "Select positions by punching two nodes")
elseif param == "get" then --display current WorldEdit positions
if worldedit.pos1[name] ~= nil then
minetest.chat_send_player(name, "WorldEdit position 1: " .. minetest.pos_to_string(worldedit.pos1[name]))
else
minetest.chat_send_player(name, "WorldEdit position 1 not set")
end
if worldedit.pos2[name] ~= nil then
minetest.chat_send_player(name, "WorldEdit position 2: " .. minetest.pos_to_string(worldedit.pos2[name]))
else
minetest.chat_send_player(name, "WorldEdit position 2 not set")
end
else
minetest.chat_send_player(name, "Unknown subcommand: " .. param)
end
end,
})
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
if worldedit.set_pos[name] == 1 then --setting position 1
worldedit.set_pos[name] = 2 --set position 2 on the next invocation
worldedit.pos1[name] = pos
worldedit.mark_pos1(name)
minetest.chat_send_player(name, "WorldEdit region position 1 set to " .. minetest.pos_to_string(pos))
else --setting position 2
worldedit.set_pos[name] = nil --finished setting positions
worldedit.pos2[name] = pos
worldedit.mark_pos2(name)
minetest.chat_send_player(name, "WorldEdit region position 2 set to " .. minetest.pos_to_string(pos))
end
end
end)
minetest.register_chatcommand("/volume", {
params = "",
description = "Display the volume of the current WorldEdit region",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local volume = worldedit.volume(pos1, pos2)
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 .. ")")
end,
})
minetest.register_chatcommand("/set", {
params = "<node>",
description = "Set the current WorldEdit region to <node>",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
if param == "" or not worldedit.node_is_valid(pos1, param) then
minetest.chat_send_player(name, "Invalid node name: " .. param)
return
end
local count = worldedit.set(pos1, pos2, param)
minetest.chat_send_player(name, count .. " nodes set")
end,
})
minetest.register_chatcommand("/replace", {
params = "<search node> <replace node>",
description = "Replace all instances of <search node> with <place node> in the current WorldEdit region",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+([^%s]+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
if not worldedit.node_is_valid(pos1, searchnode) then
minetest.chat_send_player(name, "Invalid search node name: " .. searchnode)
return
end
if not worldedit.node_is_valid(pos1, replacenode) then
minetest.chat_send_player(name, "Invalid replace node name: " .. replacenode)
return
end
local count = worldedit.replace(pos1, pos2, searchnode, replacenode)
minetest.chat_send_player(name, count .. " nodes replaced")
end,
})
minetest.register_chatcommand("/copy", {
params = "x/y/z <amount>",
description = "Copy the current WorldEdit region along the x/y/z axis by <amount> nodes",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local count = worldedit.copy(pos1, pos2, axis, tonumber(amount))
minetest.chat_send_player(name, count .. " nodes copied")
end,
})
minetest.register_chatcommand("/move", {
params = "x/y/z <amount>",
description = "Move the current WorldEdit region along the x/y/z axis by <amount> nodes",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, axis, amount = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local count = worldedit.move(pos1, pos2, axis, tonumber(amount))
minetest.chat_send_player(name, count .. " nodes moved")
end,
})
minetest.register_chatcommand("/stack", {
params = "x/y/z <count>",
description = "Stack the current WorldEdit region along the x/y/z axis <count> times",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, axis, count = param:find("^([xyz])%s+([+-]?%d+)$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local count = worldedit.stack(pos1, pos2, axis, tonumber(count))
minetest.chat_send_player(name, count .. " nodes stacked")
end,
})
minetest.register_chatcommand("/transpose", {
params = "x/y/z x/y/z",
description = "Transpose the current WorldEdit region along the x/y/z and x/y/z axes",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local found, _, axis1, axis2 = param:find("^([xyz])%s+([xyz])$")
if found == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
if axis1 == axis2 then
minetest.chat_send_player(name, "Invalid usage: axes are the same")
return
end
local count = worldedit.transpose(pos1, pos2, axis1, axis2)
minetest.chat_send_player(name, count .. " nodes transposed")
end,
})
minetest.register_chatcommand("/flip", {
params = "x/y/z",
description = "Flip the current WorldEdit region along the x/y/z axis",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
if param ~= "x" and param ~= "y" and param ~= "z" then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local count = worldedit.flip(pos1, pos2, param)
minetest.chat_send_player(name, count .. " nodes flipped")
end,
})
minetest.register_chatcommand("/rotate", {
params = "<angle>",
description = "Rotate the current WorldEdit region around the y axis by angle <angle> (90 degree increment)",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
angle = tonumber(param)
if angle == nil then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
if angle % 90 ~= 0 then
minetest.chat_send_player(name, "Invalid usage: angle must be multiple of 90")
return
end
local count = worldedit.rotate(pos1, pos2, angle)
minetest.chat_send_player(name, count .. " nodes rotated")
end,
})
minetest.register_chatcommand("/dig", {
params = "",
description = "Dig the current WorldEdit region",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
local count = worldedit.dig(pos1, pos2)
minetest.chat_send_player(name, count .. " nodes dug")
end,
})
minetest.register_chatcommand("/save", {
params = "<file>",
description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.we\"",
privs = {worldedit=true},
func = function(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
if param == "" then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local result, count = worldedit.serialize(pos1, pos2)
local path = minetest.get_worldpath() .. "/schems"
local filename = path .. "/" .. param .. ".we"
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
local file, err = io.open(filename, "wb")
if err ~= nil then
minetest.chat_send_player(name, "Could not save file to \"" .. filename .. "\"")
return
end
file:write(result)
file:flush()
file:close()
minetest.chat_send_player(name, count .. " nodes saved")
end,
})
minetest.register_chatcommand("/load", {
params = "<file>",
description = "Load nodes from \"(world folder)/schems/<file>.we\" with position 1 of the current WorldEdit region as the origin",
privs = {worldedit=true},
func = function(name, param)
local pos1 = worldedit.pos1[name]
if pos1 == nil then
minetest.chat_send_player(name, "No WorldEdit region selected")
return
end
if param == "" then
minetest.chat_send_player(name, "Invalid usage: " .. param)
return
end
local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"
local file, err = io.open(filename, "rb")
if err ~= nil then
minetest.chat_send_player(name, "Could not open file \"" .. filename .. "\"")
return
end
local value = file:read("*a")
file:close()
local count
if value:find("{") then --old WorldEdit format
count = worldedit.deserialize_old(pos1, value)
else --new WorldEdit format
count = worldedit.deserialize(pos1, value)
end
minetest.chat_send_player(name, count .. " nodes loaded")
end,
})
|