Skip to content

Commit 0dddffa

Browse files
committedMar 10, 2014
Add warning for really huge regions.
1 parent b8b8db4 commit 0dddffa

File tree

1 file changed

+198
-286
lines changed

1 file changed

+198
-286
lines changed
 

Diff for: ‎worldedit_commands/init.lua

+198-286
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,95 @@ end
1414

1515
dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua")
1616

17+
local get_position = function(name)
18+
local pos1 = worldedit.pos1[name]
19+
if pos1 == nil then
20+
worldedit.player_notify(name, "no position 1 selected")
21+
end
22+
return pos1
23+
end
24+
25+
local get_node = function(name, nodename)
26+
local node = worldedit.normalize_nodename(nodename)
27+
if not node then
28+
worldedit.player_notify(name, "invalid node name: " .. nodename)
29+
return nil
30+
end
31+
return node
32+
end
33+
34+
35+
--`callback` is a callback to run when the user confirms
36+
--`nodes_needed` is a function accepting `param`, `pos1`, and `pos2` to calculate the number of nodes needed
37+
local safe_region
38+
do --safe region wrapper function
39+
local safe_region_callback
40+
local safe_region_name
41+
local safe_region_param
42+
safe_region = function(callback, nodes_needed)
43+
nodes_needed = nodes_needed or worldedit.volume
44+
return function(name, param)
45+
--obtain positions
46+
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
47+
if pos1 == nil or pos2 == nil then
48+
worldedit.player_notify(name, "no region selected")
49+
return nil
50+
end
51+
52+
--check volume
53+
local count = nodes_needed(pos1, pos2, name, param)
54+
if not count or count < 10000 then
55+
return callback(name, param, pos1, pos2)
56+
end
57+
58+
--save callback to call later
59+
safe_region_callback, safe_region_name, safe_region_param = callback, name, param
60+
worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel")
61+
end
62+
end
63+
64+
minetest.register_chatcommand("/y", {
65+
params = "",
66+
description = "Confirm a pending operation",
67+
func = function()
68+
local callback, name, param = safe_region_callback, safe_region_name, safe_region_param
69+
if not callback then
70+
worldedit.player_notify(name, "no operation pending")
71+
return
72+
end
73+
74+
--obtain positions
75+
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
76+
if pos1 == nil or pos2 == nil then
77+
worldedit.player_notify(name, "no region selected")
78+
return
79+
end
80+
81+
safe_region_callback, safe_region_name, safe_region_param = nil, nil, nil --reset pending operation
82+
callback(name, param, pos1, pos2)
83+
end,
84+
})
85+
86+
minetest.register_chatcommand("/n", {
87+
params = "",
88+
description = "Confirm a pending operation",
89+
func = function()
90+
if not safe_region_callback then
91+
worldedit.player_notify(name, "no operation pending")
92+
return
93+
end
94+
safe_region_callback, safe_region_name, safe_region_param = nil, nil, nil
95+
end,
96+
})
97+
end
98+
1799
worldedit.player_notify = function(name, message)
18100
minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)
19101
end
20102

21103
--determines whether `nodename` is a valid node name, returning a boolean
22104
worldedit.normalize_nodename = function(nodename)
105+
if nodename == "" then return nil end
23106
local fullname = ItemStack({name=nodename}):get_name() --resolve aliases of node names to full names
24107
if minetest.registered_nodes[fullname] or fullname == "air" then --directly found node name or alias of nodename
25108
return fullname
@@ -52,6 +135,8 @@ worldedit.player_axis = function(name)
52135
return "z", dir.z > 0 and 1 or -1
53136
end
54137

138+
139+
55140
minetest.register_chatcommand("/about", {
56141
params = "",
57142
description = "Get information about the mod",
@@ -247,7 +332,7 @@ minetest.register_chatcommand("/volume", {
247332
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
248333
if pos1 == nil or pos2 == nil then
249334
worldedit.player_notify(name, "no region selected")
250-
return
335+
return nil
251336
end
252337

253338
local volume = worldedit.volume(pos1, pos2)
@@ -263,35 +348,20 @@ minetest.register_chatcommand("/set", {
263348
params = "<node>",
264349
description = "Set the current WorldEdit region to <node>",
265350
privs = {worldedit=true},
266-
func = function(name, param)
267-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
268-
if pos1 == nil or pos2 == nil then
269-
worldedit.player_notify(name, "no region selected")
270-
return
271-
end
272-
273-
local node = worldedit.normalize_nodename(param)
274-
if param == "" or not node then
275-
worldedit.player_notify(name, "invalid node name: " .. param)
276-
return
277-
end
351+
func = safe_region(function(name, param, pos1, pos2)
352+
local node = get_node(name, param)
353+
if not node then return end
278354

279355
local count = worldedit.set(pos1, pos2, node)
280356
worldedit.player_notify(name, count .. " nodes set")
281-
end,
357+
end),
282358
})
283359

284360
minetest.register_chatcommand("/replace", {
285361
params = "<search node> <replace node>",
286362
description = "Replace all instances of <search node> with <replace node> in the current WorldEdit region",
287363
privs = {worldedit=true},
288-
func = function(name, param)
289-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
290-
if pos1 == nil or pos2 == nil then
291-
worldedit.player_notify(name, "no region selected")
292-
return
293-
end
294-
364+
func = safe_region(function(name, param, pos1, pos2)
295365
local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$")
296366
if found == nil then
297367
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -310,20 +380,14 @@ minetest.register_chatcommand("/replace", {
310380

311381
local count = worldedit.replace(pos1, pos2, newsearchnode, newreplacenode)
312382
worldedit.player_notify(name, count .. " nodes replaced")
313-
end,
383+
end),
314384
})
315385

316386
minetest.register_chatcommand("/replaceinverse", {
317387
params = "<search node> <replace node>",
318388
description = "Replace all nodes other than <search node> with <replace node> in the current WorldEdit region",
319389
privs = {worldedit=true},
320-
func = function(name, param)
321-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
322-
if pos1 == nil or pos2 == nil then
323-
worldedit.player_notify(name, "no region selected")
324-
return
325-
end
326-
390+
func = safe_region(function(name, param, pos1, pos2)
327391
local found, _, searchnode, replacenode = param:find("^([^%s]+)%s+(.+)$")
328392
if found == nil then
329393
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -342,30 +406,24 @@ minetest.register_chatcommand("/replaceinverse", {
342406

343407
local count = worldedit.replaceinverse(pos1, pos2, searchnode, replacenode)
344408
worldedit.player_notify(name, count .. " nodes replaced")
345-
end,
409+
end),
346410
})
347411

348412
minetest.register_chatcommand("/hollowsphere", {
349413
params = "<radius> <node>",
350414
description = "Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",
351415
privs = {worldedit=true},
352416
func = function(name, param)
353-
local pos = worldedit.pos1[name]
354-
if pos == nil then
355-
worldedit.player_notify(name, "no region selected")
356-
return
357-
end
417+
local pos = get_position(name)
418+
if pos == nil then return end
358419

359420
local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
360421
if found == nil then
361422
worldedit.player_notify(name, "invalid usage: " .. param)
362423
return
363424
end
364-
local node = worldedit.normalize_nodename(nodename)
365-
if not node then
366-
worldedit.player_notify(name, "invalid node name: " .. nodename)
367-
return
368-
end
425+
local node = get_node(name, nodename)
426+
if not node then return end
369427

370428
local count = worldedit.hollow_sphere(pos, tonumber(radius), node)
371429
worldedit.player_notify(name, count .. " nodes added")
@@ -377,22 +435,16 @@ minetest.register_chatcommand("/sphere", {
377435
description = "Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>",
378436
privs = {worldedit=true},
379437
func = function(name, param)
380-
local pos = worldedit.pos1[name]
381-
if pos == nil then
382-
worldedit.player_notify(name, "no region selected")
383-
return
384-
end
438+
local pos = get_position(name)
439+
if pos == nil then return end
385440

386441
local found, _, radius, nodename = param:find("^(%d+)%s+(.+)$")
387442
if found == nil then
388443
worldedit.player_notify(name, "invalid usage: " .. param)
389444
return
390445
end
391-
local node = worldedit.normalize_nodename(nodename)
392-
if not node then
393-
worldedit.player_notify(name, "invalid node name: " .. nodename)
394-
return
395-
end
446+
local node = get_node(name, nodename)
447+
if not node then return end
396448

397449
local count = worldedit.sphere(pos, tonumber(radius), node)
398450
worldedit.player_notify(name, count .. " nodes added")
@@ -404,22 +456,16 @@ minetest.register_chatcommand("/hollowdome", {
404456
description = "Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",
405457
privs = {worldedit=true},
406458
func = function(name, param)
407-
local pos = worldedit.pos1[name]
408-
if pos == nil then
409-
worldedit.player_notify(name, "no region selected")
410-
return
411-
end
459+
local pos = get_position(name)
460+
if pos == nil then return end
412461

413462
local found, _, radius, nodename = param:find("^([+-]?%d+)%s+(.+)$")
414463
if found == nil then
415464
worldedit.player_notify(name, "invalid usage: " .. param)
416465
return
417466
end
418-
local node = worldedit.normalize_nodename(nodename)
419-
if not node then
420-
worldedit.player_notify(name, "invalid node name: " .. nodename)
421-
return
422-
end
467+
local node = get_node(name, nodename)
468+
if not node then return end
423469

424470
local count = worldedit.hollow_dome(pos, tonumber(radius), node)
425471
worldedit.player_notify(name, count .. " nodes added")
@@ -431,22 +477,16 @@ minetest.register_chatcommand("/dome", {
431477
description = "Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node>",
432478
privs = {worldedit=true},
433479
func = function(name, param)
434-
local pos = worldedit.pos1[name]
435-
if pos == nil then
436-
worldedit.player_notify(name, "no region selected")
437-
return
438-
end
480+
local pos = get_position(name)
481+
if pos == nil then return end
439482

440483
local found, _, radius, nodename = param:find("^([+-]?%d+)%s+(.+)$")
441484
if found == nil then
442485
worldedit.player_notify(name, "invalid usage: " .. param)
443486
return
444487
end
445-
local node = worldedit.normalize_nodename(nodename)
446-
if not node then
447-
worldedit.player_notify(name, "invalid node name: " .. nodename)
448-
return
449-
end
488+
local node = get_node(name, nodename)
489+
if not node then return end
450490

451491
local count = worldedit.dome(pos, tonumber(radius), node)
452492
worldedit.player_notify(name, count .. " nodes added")
@@ -458,11 +498,8 @@ minetest.register_chatcommand("/hollowcylinder", {
458498
description = "Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",
459499
privs = {worldedit=true},
460500
func = function(name, param)
461-
local pos = worldedit.pos1[name]
462-
if pos == nil then
463-
worldedit.player_notify(name, "no region selected")
464-
return
465-
end
501+
local pos = get_position(name)
502+
if pos == nil then return end
466503

467504
local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
468505
if found == nil then
@@ -474,11 +511,8 @@ minetest.register_chatcommand("/hollowcylinder", {
474511
axis, sign = worldedit.player_axis(name)
475512
length = length * sign
476513
end
477-
local node = worldedit.normalize_nodename(nodename)
478-
if not node then
479-
worldedit.player_notify(name, "invalid node name: " .. nodename)
480-
return
481-
end
514+
local node = get_node(name, nodename)
515+
if not node then return end
482516

483517
local count = worldedit.hollow_cylinder(pos, axis, length, radius, node)
484518
worldedit.player_notify(name, count .. " nodes added")
@@ -490,11 +524,8 @@ minetest.register_chatcommand("/cylinder", {
490524
description = "Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>",
491525
privs = {worldedit=true},
492526
func = function(name, param)
493-
local pos = worldedit.pos1[name]
494-
if pos == nil then
495-
worldedit.player_notify(name, "no region selected")
496-
return
497-
end
527+
local pos = get_position(name)
528+
if pos == nil then return end
498529

499530
local found, _, axis, length, radius, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$")
500531
if found == nil then
@@ -506,11 +537,8 @@ minetest.register_chatcommand("/cylinder", {
506537
axis, sign = worldedit.player_axis(name)
507538
length = length * sign
508539
end
509-
local node = worldedit.normalize_nodename(nodename)
510-
if not node then
511-
worldedit.player_notify(name, "invalid node name: " .. nodename)
512-
return
513-
end
540+
local node = get_node(name, nodename)
541+
if not node then return end
514542

515543
local count = worldedit.cylinder(pos, axis, length, radius, node)
516544
worldedit.player_notify(name, count .. " nodes added")
@@ -522,11 +550,8 @@ minetest.register_chatcommand("/pyramid", {
522550
description = "Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>",
523551
privs = {worldedit=true},
524552
func = function(name, param)
525-
local pos = worldedit.pos1[name]
526-
if pos == nil then
527-
worldedit.player_notify(name, "no region selected")
528-
return
529-
end
553+
local pos = get_position(name)
554+
if pos == nil then return end
530555

531556
local found, _, axis, height, nodename = param:find("^([xyz%?])%s+([+-]?%d+)%s+(.+)$")
532557
if found == nil then
@@ -538,11 +563,8 @@ minetest.register_chatcommand("/pyramid", {
538563
axis, sign = worldedit.player_axis(name)
539564
height = height * sign
540565
end
541-
local node = worldedit.normalize_nodename(nodename)
542-
if not node then
543-
worldedit.player_notify(name, "invalid node name: " .. nodename)
544-
return
545-
end
566+
local node = get_node(name, nodename)
567+
if not node then return end
546568

547569
local count = worldedit.pyramid(pos, axis, height, node)
548570
worldedit.player_notify(name, count .. " nodes added")
@@ -554,22 +576,16 @@ minetest.register_chatcommand("/spiral", {
554576
description = "Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <space>, composed of <node>",
555577
privs = {worldedit=true},
556578
func = function(name, param)
557-
local pos = worldedit.pos1[name]
558-
if pos == nil then
559-
worldedit.player_notify(name, "no region selected")
560-
return
561-
end
579+
local pos = get_position(name)
580+
if pos == nil then return end
562581

563582
local found, _, length, height, space, nodename = param:find("^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$")
564583
if found == nil then
565584
worldedit.player_notify(name, "invalid usage: " .. param)
566585
return
567586
end
568-
local node = worldedit.normalize_nodename(nodename)
569-
if not node then
570-
worldedit.player_notify(name, "invalid node name: " .. nodename)
571-
return
572-
end
587+
local node = get_node(name, nodename)
588+
if not node then return end
573589

574590
local count = worldedit.spiral(pos, tonumber(length), tonumber(height), tonumber(space), node)
575591
worldedit.player_notify(name, count .. " nodes added")
@@ -580,13 +596,7 @@ minetest.register_chatcommand("/copy", {
580596
params = "x/y/z/? <amount>",
581597
description = "Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes",
582598
privs = {worldedit=true},
583-
func = function(name, param)
584-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
585-
if pos1 == nil or pos2 == nil then
586-
worldedit.player_notify(name, "no region selected")
587-
return
588-
end
589-
599+
func = safe_region(function(name, param, pos1, pos2)
590600
local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")
591601
if found == nil then
592602
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -600,20 +610,14 @@ minetest.register_chatcommand("/copy", {
600610

601611
local count = worldedit.copy(pos1, pos2, axis, amount)
602612
worldedit.player_notify(name, count .. " nodes copied")
603-
end,
613+
end),
604614
})
605615

606616
minetest.register_chatcommand("/move", {
607617
params = "x/y/z/? <amount>",
608618
description = "Move the current WorldEdit region along the x/y/z/? axis by <amount> nodes",
609619
privs = {worldedit=true},
610-
func = function(name, param)
611-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
612-
if pos1 == nil or pos2 == nil then
613-
worldedit.player_notify(name, "no region selected")
614-
return
615-
end
616-
620+
func = safe_region(function(name, param, pos1, pos2)
617621
local found, _, axis, amount = param:find("^([xyz%?])%s+([+-]?%d+)$")
618622
if found == nil then
619623
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -632,55 +636,49 @@ minetest.register_chatcommand("/move", {
632636
worldedit.mark_pos1(name)
633637
worldedit.mark_pos2(name)
634638
worldedit.player_notify(name, count .. " nodes moved")
635-
end,
639+
end),
636640
})
637641

638642
minetest.register_chatcommand("/stack", {
639643
params = "x/y/z/? <count>",
640644
description = "Stack the current WorldEdit region along the x/y/z/? axis <count> times",
641645
privs = {worldedit=true},
642-
func = function(name, param)
643-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
644-
if pos1 == nil or pos2 == nil then
645-
worldedit.player_notify(name, "no region selected")
646-
return
647-
end
648-
649-
local found, _, axis, count = param:find("^([xyz%?])%s+([+-]?%d+)$")
646+
func = safe_region(function(name, param, pos1, pos2)
647+
local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")
650648
if found == nil then
651649
worldedit.player_notify(name, "invalid usage: " .. param)
652650
return
653651
end
654-
count = tonumber(count)
652+
repetitions = tonumber(repetitions)
655653
if axis == "?" then
656654
axis, sign = worldedit.player_axis(name)
657-
count = count * sign
655+
repetitions = repetitions * sign
658656
end
659657

660-
local count = worldedit.stack(pos1, pos2, axis, count)
658+
local count = worldedit.stack(pos1, pos2, axis, repetitions)
661659
worldedit.player_notify(name, count .. " nodes stacked")
662660
end,
661+
function(pos1, pos2, name, param)
662+
local found, _, axis, repetitions = param:find("^([xyz%?])%s+([+-]?%d+)$")
663+
if found then
664+
return tonumber(repetitions) * worldedit.volume(pos1, pos2)
665+
end
666+
end),
663667
})
664668

665669
minetest.register_chatcommand("/stretch", {
666670
params = "<stretchx> <stretchy> <stretchz>",
667671
description = "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",
668672
privs = {worldedit=true},
669-
func = function(name, param)
670-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
671-
if pos1 == nil or pos2 == nil then
672-
worldedit.player_notify(name, "no region selected")
673-
return
674-
end
675-
673+
func = safe_region(function(name, param, pos1, pos2)
676674
local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")
677675
if found == nil then
678676
worldedit.player_notify(name, "invalid usage: " .. param)
679677
return
680678
end
681679
stretchx, stretchy, stretchz = tonumber(stretchx), tonumber(stretchy), tonumber(stretchz)
682680
if stretchx == 0 or stretchy == 0 or stretchz == 0 then
683-
worldedit.player_notify(name, "invalid scaling factor: " .. param)
681+
worldedit.player_notify(name, "invalid scaling factors: " .. param)
684682
end
685683

686684
local count, pos1, pos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)
@@ -693,19 +691,19 @@ minetest.register_chatcommand("/stretch", {
693691

694692
worldedit.player_notify(name, count .. " nodes stretched")
695693
end,
694+
function(pos1, pos2, name, param)
695+
local found, _, stretchx, stretchy, stretchz = param:find("^(%d+)%s+(%d+)%s+(%d+)$")
696+
if found then
697+
return tonumber(stretchx) * tonumber(stretchy) * tonumber(stretchz) * worldedit.volume(pos1, pos2)
698+
end
699+
end),
696700
})
697701

698702
minetest.register_chatcommand("/transpose", {
699703
params = "x/y/z/? x/y/z/?",
700704
description = "Transpose the current WorldEdit region along the x/y/z/? and x/y/z/? axes",
701705
privs = {worldedit=true},
702-
func = function(name, param)
703-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
704-
if pos1 == nil or pos2 == nil then
705-
worldedit.player_notify(name, "no region selected")
706-
return
707-
end
708-
706+
func = safe_region(function(name, param, pos1, pos2)
709707
local found, _, axis1, axis2 = param:find("^([xyz%?])%s+([xyz%?])$")
710708
if found == nil then
711709
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -731,20 +729,14 @@ minetest.register_chatcommand("/transpose", {
731729
worldedit.mark_pos2(name)
732730

733731
worldedit.player_notify(name, count .. " nodes transposed")
734-
end,
732+
end),
735733
})
736734

737735
minetest.register_chatcommand("/flip", {
738736
params = "x/y/z/?",
739737
description = "Flip the current WorldEdit region along the x/y/z/? axis",
740738
privs = {worldedit=true},
741-
func = function(name, param)
742-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
743-
if pos1 == nil or pos2 == nil then
744-
worldedit.player_notify(name, "no region selected")
745-
return
746-
end
747-
739+
func = safe_region(function(name, param, pos1, pos2)
748740
if param == "?" then
749741
param = worldedit.player_axis(name)
750742
end
@@ -755,20 +747,14 @@ minetest.register_chatcommand("/flip", {
755747

756748
local count = worldedit.flip(pos1, pos2, param)
757749
worldedit.player_notify(name, count .. " nodes flipped")
758-
end,
750+
end),
759751
})
760752

761753
minetest.register_chatcommand("/rotate", {
762754
params = "<axis> <angle>",
763755
description = "Rotate the current WorldEdit region around the axis <axis> by angle <angle> (90 degree increment)",
764756
privs = {worldedit=true},
765-
func = function(name, param)
766-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
767-
if pos1 == nil or pos2 == nil then
768-
worldedit.player_notify(name, "no region selected")
769-
return
770-
end
771-
757+
func = safe_region(function(name, param, pos1, pos2)
772758
local found, _, axis, angle = param:find("^([xyz%?])%s+([+-]?%d+)$")
773759
if found == nil then
774760
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -791,20 +777,14 @@ minetest.register_chatcommand("/rotate", {
791777
worldedit.mark_pos2(name)
792778

793779
worldedit.player_notify(name, count .. " nodes rotated")
794-
end,
780+
end),
795781
})
796782

797783
minetest.register_chatcommand("/orient", {
798784
params = "<angle>",
799785
description = "Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment)",
800786
privs = {worldedit=true},
801-
func = function(name, param)
802-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
803-
if pos1 == nil or pos2 == nil then
804-
worldedit.player_notify(name, "no region selected")
805-
return
806-
end
807-
787+
func = safe_region(function(name, param, pos1, pos2)
808788
local found, _, angle = param:find("^([+-]?%d+)$")
809789
if found == nil then
810790
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -818,112 +798,70 @@ minetest.register_chatcommand("/orient", {
818798
local count = worldedit.orient(pos1, pos2, angle)
819799

820800
worldedit.player_notify(name, count .. " nodes oriented")
821-
end,
801+
end),
822802
})
823803

824804
minetest.register_chatcommand("/fixlight", {
825805
params = "",
826806
description = "Fix the lighting in the current WorldEdit region",
827807
privs = {worldedit=true},
828-
func = function(name, param)
829-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
830-
if pos1 == nil or pos2 == nil then
831-
worldedit.player_notify(name, "no region selected")
832-
return
833-
end
834-
808+
func = safe_region(function(name, param, pos1, pos2)
835809
local count = worldedit.fixlight(pos1, pos2)
836810
worldedit.player_notify(name, count .. " nodes updated")
837-
end,
811+
end),
838812
})
839813

840814
minetest.register_chatcommand("/hide", {
841815
params = "",
842816
description = "Hide all nodes in the current WorldEdit region non-destructively",
843817
privs = {worldedit=true},
844-
func = function(name, param)
845-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
846-
if pos1 == nil or pos2 == nil then
847-
worldedit.player_notify(name, "no region selected")
848-
return
849-
end
850-
818+
func = safe_region(function(name, param, pos1, pos2)
851819
local count = worldedit.hide(pos1, pos2)
852820
worldedit.player_notify(name, count .. " nodes hidden")
853-
end,
821+
end),
854822
})
855823

856824
minetest.register_chatcommand("/suppress", {
857825
params = "<node>",
858826
description = "Suppress all <node> in the current WorldEdit region non-destructively",
859827
privs = {worldedit=true},
860-
func = function(name, param)
861-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
862-
if pos1 == nil or pos2 == nil then
863-
worldedit.player_notify(name, "no region selected")
864-
return
865-
end
866-
867-
local node = worldedit.normalize_nodename(param)
868-
if param == "" or not node then
869-
worldedit.player_notify(name, "invalid node name: " .. param)
870-
return
871-
end
828+
func = safe_region(function(name, param, pos1, pos2)
829+
local node = get_node(name, param)
830+
if not node then return end
872831

873832
local count = worldedit.suppress(pos1, pos2, node)
874833
worldedit.player_notify(name, count .. " nodes suppressed")
875-
end,
834+
end),
876835
})
877836

878837
minetest.register_chatcommand("/highlight", {
879838
params = "<node>",
880839
description = "Highlight <node> in the current WorldEdit region by hiding everything else non-destructively",
881840
privs = {worldedit=true},
882-
func = function(name, param)
883-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
884-
if pos1 == nil or pos2 == nil then
885-
worldedit.player_notify(name, "no region selected")
886-
return
887-
end
888-
889-
local node = worldedit.normalize_nodename(param)
890-
if param == "" or not node then
891-
worldedit.player_notify(name, "invalid node name: " .. param)
892-
return
893-
end
841+
func = safe_region(function(name, param, pos1, pos2)
842+
local node = get_node(name, param)
843+
if not node then return end
894844

895845
local count = worldedit.highlight(pos1, pos2, node)
896846
worldedit.player_notify(name, count .. " nodes highlighted")
897-
end,
847+
end),
898848
})
899849

900850
minetest.register_chatcommand("/restore", {
901851
params = "",
902852
description = "Restores nodes hidden with WorldEdit in the current WorldEdit region",
903853
privs = {worldedit=true},
904-
func = function(name, param)
905-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
906-
if pos1 == nil or pos2 == nil then
907-
worldedit.player_notify(name, "no region selected")
908-
return
909-
end
910-
854+
func = safe_region(function(name, param, pos1, pos2)
911855
local count = worldedit.restore(pos1, pos2)
912856
worldedit.player_notify(name, count .. " nodes restored")
913-
end,
857+
end),
914858
})
915859

916860
minetest.register_chatcommand("/save", {
917861
params = "<file>",
918862
description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.we\"",
919863
privs = {worldedit=true},
920-
func = function(name, param)
921-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
922-
if pos1 == nil or pos2 == nil then
923-
worldedit.player_notify(name, "no region selected")
924-
return
925-
end
926-
864+
func = safe_region(function(name, param, pos1, pos2)
927865
if param == "" then
928866
worldedit.player_notify(name, "invalid usage: " .. param)
929867
return
@@ -937,6 +875,7 @@ minetest.register_chatcommand("/save", {
937875

938876
local path = minetest.get_worldpath() .. "/schems"
939877
local filename = path .. "/" .. param .. ".we"
878+
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters
940879
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
941880
local file, err = io.open(filename, "wb")
942881
if err ~= nil then
@@ -948,19 +887,16 @@ minetest.register_chatcommand("/save", {
948887
file:close()
949888

950889
worldedit.player_notify(name, count .. " nodes saved")
951-
end,
890+
end),
952891
})
953892

954893
minetest.register_chatcommand("/allocate", {
955894
params = "<file>",
956895
description = "Set the region defined by nodes from \"(world folder)/schems/<file>.we\" as the current WorldEdit region",
957896
privs = {worldedit=true},
958897
func = function(name, param)
959-
local pos1 = worldedit.pos1[name]
960-
if pos1 == nil then
961-
worldedit.player_notify(name, "no region selected")
962-
return
963-
end
898+
local pos = get_position(name)
899+
if pos == nil then return end
964900

965901
if param == "" then
966902
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -984,7 +920,7 @@ minetest.register_chatcommand("/allocate", {
984920
worldedit.player_notify(name, "invalid file: file is invalid or created with newer version of WorldEdit")
985921
return
986922
end
987-
local nodepos1, nodepos2, count = worldedit.allocate(pos1, value)
923+
local nodepos1, nodepos2, count = worldedit.allocate(pos, value)
988924

989925
worldedit.pos1[name] = nodepos1
990926
worldedit.mark_pos1(name)
@@ -1000,11 +936,8 @@ minetest.register_chatcommand("/load", {
1000936
description = "Load nodes from \"(world folder)/schems/<file>[.we[m]]\" with position 1 of the current WorldEdit region as the origin",
1001937
privs = {worldedit=true},
1002938
func = function(name, param)
1003-
local pos1 = worldedit.pos1[name]
1004-
if pos1 == nil then
1005-
worldedit.player_notify(name, "no region selected")
1006-
return
1007-
end
939+
local pos = get_position(name)
940+
if pos == nil then return end
1008941

1009942
if param == "" then
1010943
worldedit.player_notify(name, "invalid usage: " .. param)
@@ -1040,7 +973,7 @@ minetest.register_chatcommand("/load", {
1040973
return
1041974
end
1042975

1043-
local count = worldedit.deserialize(pos1, value)
976+
local count = worldedit.deserialize(pos, value)
1044977

1045978
worldedit.player_notify(name, count .. " nodes loaded")
1046979
end,
@@ -1053,8 +986,7 @@ minetest.register_chatcommand("/lua", {
1053986
func = function(name, param)
1054987
local admin = minetest.setting_get("name")
1055988
if not admin or not name == admin then
1056-
worldedit.player_notify(name, "This command can only"
1057-
.." be run by the server administrator")
989+
worldedit.player_notify(name, "this command can only be run by the server administrator")
1058990
return
1059991
end
1060992
local err = worldedit.lua(param)
@@ -1070,16 +1002,10 @@ minetest.register_chatcommand("/luatransform", {
10701002
params = "<code>",
10711003
description = "Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region",
10721004
privs = {worldedit=true, server=true},
1073-
func = function(name, param)
1005+
func = safe_region(function(name, param, pos1, pos2)
10741006
local admin = minetest.setting_get("name")
10751007
if not admin or not name == admin then
1076-
worldedit.player_notify(name, "This command can only"
1077-
.." be run by the server administrator")
1078-
return
1079-
end
1080-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
1081-
if pos1 == nil or pos2 == nil then
1082-
worldedit.player_notify(name, "no region selected")
1008+
worldedit.player_notify(name, "this command can only be run by the server administrator")
10831009
return
10841010
end
10851011

@@ -1089,59 +1015,52 @@ minetest.register_chatcommand("/luatransform", {
10891015
else
10901016
worldedit.player_notify(name, "code successfully executed", false)
10911017
end
1092-
end,
1018+
end),
10931019
})
10941020

1095-
if minetest.place_schematic then
10961021
minetest.register_chatcommand("/mtschemcreate", {
10971022
params = "<file>",
10981023
description = "Save the current WorldEdit region using the Minetest Schematic format to \"(world folder)/schems/<filename>.mts\"",
10991024
privs = {worldedit=true},
1100-
func = function(name, param)
1101-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
1102-
if pos1 == nil or pos2 == nil then
1103-
worldedit.player_notify(name, "No region selected")
1104-
return
1105-
end
1025+
func = safe_region(function(name, param, pos1, pos2)
11061026
if param == nil then
11071027
worldedit.player_notify(name, "No filename specified")
11081028
return
11091029
end
11101030

11111031
local path = minetest.get_worldpath() .. "/schems"
11121032
local filename = path .. "/" .. param .. ".mts"
1033+
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters
11131034
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
11141035

11151036
local ret = minetest.create_schematic(pos1, pos2, worldedit.prob_list[name], filename)
11161037
if ret == nil then
1117-
worldedit.player_notify(name, "Failed to create Minetest schematic", false)
1038+
worldedit.player_notify(name, "failed to create Minetest schematic", false)
11181039
else
1119-
worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)
1040+
worldedit.player_notify(name, "saved Minetest schematic to " .. param, false)
11201041
end
11211042
worldedit.prob_list[name] = {}
1122-
end,
1043+
end),
11231044
})
11241045

11251046
minetest.register_chatcommand("/mtschemplace", {
11261047
params = "<file>",
11271048
description = "Load nodes from \"(world folder)/schems/<file>.mts\" with position 1 of the current WorldEdit region as the origin",
11281049
privs = {worldedit=true},
11291050
func = function(name, param)
1130-
local pos = worldedit.pos1[name]
1131-
if pos == nil then
1132-
worldedit.player_notify(name, "No position selected")
1133-
return
1134-
end
11351051
if param == nil then
1136-
worldedit.player_notify(name, "No filename specified")
1052+
worldedit.player_notify(name, "no filename specified")
11371053
return
11381054
end
11391055

1056+
local pos = get_position(name)
1057+
if pos == nil then return end
1058+
11401059
local path = minetest.get_worldpath() .. "/schems/" .. param .. ".mts"
11411060
if minetest.place_schematic(pos, path) == nil then
1142-
worldedit.player_notify(name, "Failed to place Minetest schematic", false)
1061+
worldedit.player_notify(name, "failed to place Minetest schematic", false)
11431062
else
1144-
worldedit.player_notify(name, "Placed Minetest schematic " .. param ..
1063+
worldedit.player_notify(name, "placed Minetest schematic " .. param ..
11451064
" at " .. minetest.pos_to_string(pos), false)
11461065
end
11471066
end,
@@ -1169,7 +1088,7 @@ minetest.register_chatcommand("/mtschemprob", {
11691088
local prob = math.floor(((v["prob"] / 256) * 100) * 100 + 0.5) / 100
11701089
text = text .. minetest.pos_to_string(v["pos"]) .. ": " .. prob .. "% | "
11711090
end
1172-
worldedit.player_notify(name, "Currently set node probabilities:")
1091+
worldedit.player_notify(name, "currently set node probabilities:")
11731092
worldedit.player_notify(name, text)
11741093
else
11751094
worldedit.player_notify(name, "unknown subcommand: " .. param)
@@ -1187,20 +1106,13 @@ minetest.register_on_player_receive_fields(
11871106
end
11881107
end
11891108
)
1190-
end
11911109

11921110
minetest.register_chatcommand("/clearobjects", {
11931111
params = "",
11941112
description = "Clears all objects within the WorldEdit region",
11951113
privs = {worldedit=true},
1196-
func = function(name, param)
1197-
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
1198-
if pos1 == nil or pos2 == nil then
1199-
worldedit.player_notify(name, "no region selected")
1200-
return
1201-
end
1202-
1114+
func = safe_region(function(name, param, pos1, pos2)
12031115
local count = worldedit.clearobjects(pos1, pos2)
12041116
worldedit.player_notify(name, count .. " objects cleared")
1205-
end,
1117+
end),
12061118
})

0 commit comments

Comments
 (0)
Please sign in to comment.