14
14
15
15
dofile (minetest .get_modpath (" worldedit_commands" ) .. " /mark.lua" )
16
16
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
+
17
99
worldedit .player_notify = function (name , message )
18
100
minetest .chat_send_player (name , " WorldEdit -!- " .. message , false )
19
101
end
20
102
21
103
-- determines whether `nodename` is a valid node name, returning a boolean
22
104
worldedit .normalize_nodename = function (nodename )
105
+ if nodename == " " then return nil end
23
106
local fullname = ItemStack ({name = nodename }):get_name () -- resolve aliases of node names to full names
24
107
if minetest .registered_nodes [fullname ] or fullname == " air" then -- directly found node name or alias of nodename
25
108
return fullname
@@ -52,6 +135,8 @@ worldedit.player_axis = function(name)
52
135
return " z" , dir .z > 0 and 1 or - 1
53
136
end
54
137
138
+
139
+
55
140
minetest .register_chatcommand (" /about" , {
56
141
params = " " ,
57
142
description = " Get information about the mod" ,
@@ -247,7 +332,7 @@ minetest.register_chatcommand("/volume", {
247
332
local pos1 , pos2 = worldedit .pos1 [name ], worldedit .pos2 [name ]
248
333
if pos1 == nil or pos2 == nil then
249
334
worldedit .player_notify (name , " no region selected" )
250
- return
335
+ return nil
251
336
end
252
337
253
338
local volume = worldedit .volume (pos1 , pos2 )
@@ -263,35 +348,20 @@ minetest.register_chatcommand("/set", {
263
348
params = " <node>" ,
264
349
description = " Set the current WorldEdit region to <node>" ,
265
350
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
278
354
279
355
local count = worldedit .set (pos1 , pos2 , node )
280
356
worldedit .player_notify (name , count .. " nodes set" )
281
- end ,
357
+ end ) ,
282
358
})
283
359
284
360
minetest .register_chatcommand (" /replace" , {
285
361
params = " <search node> <replace node>" ,
286
362
description = " Replace all instances of <search node> with <replace node> in the current WorldEdit region" ,
287
363
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 )
295
365
local found , _ , searchnode , replacenode = param :find (" ^([^%s]+)%s+(.+)$" )
296
366
if found == nil then
297
367
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -310,20 +380,14 @@ minetest.register_chatcommand("/replace", {
310
380
311
381
local count = worldedit .replace (pos1 , pos2 , newsearchnode , newreplacenode )
312
382
worldedit .player_notify (name , count .. " nodes replaced" )
313
- end ,
383
+ end ) ,
314
384
})
315
385
316
386
minetest .register_chatcommand (" /replaceinverse" , {
317
387
params = " <search node> <replace node>" ,
318
388
description = " Replace all nodes other than <search node> with <replace node> in the current WorldEdit region" ,
319
389
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 )
327
391
local found , _ , searchnode , replacenode = param :find (" ^([^%s]+)%s+(.+)$" )
328
392
if found == nil then
329
393
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -342,30 +406,24 @@ minetest.register_chatcommand("/replaceinverse", {
342
406
343
407
local count = worldedit .replaceinverse (pos1 , pos2 , searchnode , replacenode )
344
408
worldedit .player_notify (name , count .. " nodes replaced" )
345
- end ,
409
+ end ) ,
346
410
})
347
411
348
412
minetest .register_chatcommand (" /hollowsphere" , {
349
413
params = " <radius> <node>" ,
350
414
description = " Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>" ,
351
415
privs = {worldedit = true },
352
416
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
358
419
359
420
local found , _ , radius , nodename = param :find (" ^(%d+)%s+(.+)$" )
360
421
if found == nil then
361
422
worldedit .player_notify (name , " invalid usage: " .. param )
362
423
return
363
424
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
369
427
370
428
local count = worldedit .hollow_sphere (pos , tonumber (radius ), node )
371
429
worldedit .player_notify (name , count .. " nodes added" )
@@ -377,22 +435,16 @@ minetest.register_chatcommand("/sphere", {
377
435
description = " Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>" ,
378
436
privs = {worldedit = true },
379
437
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
385
440
386
441
local found , _ , radius , nodename = param :find (" ^(%d+)%s+(.+)$" )
387
442
if found == nil then
388
443
worldedit .player_notify (name , " invalid usage: " .. param )
389
444
return
390
445
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
396
448
397
449
local count = worldedit .sphere (pos , tonumber (radius ), node )
398
450
worldedit .player_notify (name , count .. " nodes added" )
@@ -404,22 +456,16 @@ minetest.register_chatcommand("/hollowdome", {
404
456
description = " Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node>" ,
405
457
privs = {worldedit = true },
406
458
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
412
461
413
462
local found , _ , radius , nodename = param :find (" ^([+-]?%d+)%s+(.+)$" )
414
463
if found == nil then
415
464
worldedit .player_notify (name , " invalid usage: " .. param )
416
465
return
417
466
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
423
469
424
470
local count = worldedit .hollow_dome (pos , tonumber (radius ), node )
425
471
worldedit .player_notify (name , count .. " nodes added" )
@@ -431,22 +477,16 @@ minetest.register_chatcommand("/dome", {
431
477
description = " Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node>" ,
432
478
privs = {worldedit = true },
433
479
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
439
482
440
483
local found , _ , radius , nodename = param :find (" ^([+-]?%d+)%s+(.+)$" )
441
484
if found == nil then
442
485
worldedit .player_notify (name , " invalid usage: " .. param )
443
486
return
444
487
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
450
490
451
491
local count = worldedit .dome (pos , tonumber (radius ), node )
452
492
worldedit .player_notify (name , count .. " nodes added" )
@@ -458,11 +498,8 @@ minetest.register_chatcommand("/hollowcylinder", {
458
498
description = " Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>" ,
459
499
privs = {worldedit = true },
460
500
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
466
503
467
504
local found , _ , axis , length , radius , nodename = param :find (" ^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$" )
468
505
if found == nil then
@@ -474,11 +511,8 @@ minetest.register_chatcommand("/hollowcylinder", {
474
511
axis , sign = worldedit .player_axis (name )
475
512
length = length * sign
476
513
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
482
516
483
517
local count = worldedit .hollow_cylinder (pos , axis , length , radius , node )
484
518
worldedit .player_notify (name , count .. " nodes added" )
@@ -490,11 +524,8 @@ minetest.register_chatcommand("/cylinder", {
490
524
description = " Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>" ,
491
525
privs = {worldedit = true },
492
526
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
498
529
499
530
local found , _ , axis , length , radius , nodename = param :find (" ^([xyz%?])%s+([+-]?%d+)%s+(%d+)%s+(.+)$" )
500
531
if found == nil then
@@ -506,11 +537,8 @@ minetest.register_chatcommand("/cylinder", {
506
537
axis , sign = worldedit .player_axis (name )
507
538
length = length * sign
508
539
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
514
542
515
543
local count = worldedit .cylinder (pos , axis , length , radius , node )
516
544
worldedit .player_notify (name , count .. " nodes added" )
@@ -522,11 +550,8 @@ minetest.register_chatcommand("/pyramid", {
522
550
description = " Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>" ,
523
551
privs = {worldedit = true },
524
552
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
530
555
531
556
local found , _ , axis , height , nodename = param :find (" ^([xyz%?])%s+([+-]?%d+)%s+(.+)$" )
532
557
if found == nil then
@@ -538,11 +563,8 @@ minetest.register_chatcommand("/pyramid", {
538
563
axis , sign = worldedit .player_axis (name )
539
564
height = height * sign
540
565
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
546
568
547
569
local count = worldedit .pyramid (pos , axis , height , node )
548
570
worldedit .player_notify (name , count .. " nodes added" )
@@ -554,22 +576,16 @@ minetest.register_chatcommand("/spiral", {
554
576
description = " Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <space>, composed of <node>" ,
555
577
privs = {worldedit = true },
556
578
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
562
581
563
582
local found , _ , length , height , space , nodename = param :find (" ^(%d+)%s+(%d+)%s+(%d+)%s+(.+)$" )
564
583
if found == nil then
565
584
worldedit .player_notify (name , " invalid usage: " .. param )
566
585
return
567
586
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
573
589
574
590
local count = worldedit .spiral (pos , tonumber (length ), tonumber (height ), tonumber (space ), node )
575
591
worldedit .player_notify (name , count .. " nodes added" )
@@ -580,13 +596,7 @@ minetest.register_chatcommand("/copy", {
580
596
params = " x/y/z/? <amount>" ,
581
597
description = " Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes" ,
582
598
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 )
590
600
local found , _ , axis , amount = param :find (" ^([xyz%?])%s+([+-]?%d+)$" )
591
601
if found == nil then
592
602
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -600,20 +610,14 @@ minetest.register_chatcommand("/copy", {
600
610
601
611
local count = worldedit .copy (pos1 , pos2 , axis , amount )
602
612
worldedit .player_notify (name , count .. " nodes copied" )
603
- end ,
613
+ end ) ,
604
614
})
605
615
606
616
minetest .register_chatcommand (" /move" , {
607
617
params = " x/y/z/? <amount>" ,
608
618
description = " Move the current WorldEdit region along the x/y/z/? axis by <amount> nodes" ,
609
619
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 )
617
621
local found , _ , axis , amount = param :find (" ^([xyz%?])%s+([+-]?%d+)$" )
618
622
if found == nil then
619
623
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -632,55 +636,49 @@ minetest.register_chatcommand("/move", {
632
636
worldedit .mark_pos1 (name )
633
637
worldedit .mark_pos2 (name )
634
638
worldedit .player_notify (name , count .. " nodes moved" )
635
- end ,
639
+ end ) ,
636
640
})
637
641
638
642
minetest .register_chatcommand (" /stack" , {
639
643
params = " x/y/z/? <count>" ,
640
644
description = " Stack the current WorldEdit region along the x/y/z/? axis <count> times" ,
641
645
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+)$" )
650
648
if found == nil then
651
649
worldedit .player_notify (name , " invalid usage: " .. param )
652
650
return
653
651
end
654
- count = tonumber (count )
652
+ repetitions = tonumber (repetitions )
655
653
if axis == " ?" then
656
654
axis , sign = worldedit .player_axis (name )
657
- count = count * sign
655
+ repetitions = repetitions * sign
658
656
end
659
657
660
- local count = worldedit .stack (pos1 , pos2 , axis , count )
658
+ local count = worldedit .stack (pos1 , pos2 , axis , repetitions )
661
659
worldedit .player_notify (name , count .. " nodes stacked" )
662
660
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 ),
663
667
})
664
668
665
669
minetest .register_chatcommand (" /stretch" , {
666
670
params = " <stretchx> <stretchy> <stretchz>" ,
667
671
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" ,
668
672
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 )
676
674
local found , _ , stretchx , stretchy , stretchz = param :find (" ^(%d+)%s+(%d+)%s+(%d+)$" )
677
675
if found == nil then
678
676
worldedit .player_notify (name , " invalid usage: " .. param )
679
677
return
680
678
end
681
679
stretchx , stretchy , stretchz = tonumber (stretchx ), tonumber (stretchy ), tonumber (stretchz )
682
680
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 )
684
682
end
685
683
686
684
local count , pos1 , pos2 = worldedit .stretch (pos1 , pos2 , stretchx , stretchy , stretchz )
@@ -693,19 +691,19 @@ minetest.register_chatcommand("/stretch", {
693
691
694
692
worldedit .player_notify (name , count .. " nodes stretched" )
695
693
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 ),
696
700
})
697
701
698
702
minetest .register_chatcommand (" /transpose" , {
699
703
params = " x/y/z/? x/y/z/?" ,
700
704
description = " Transpose the current WorldEdit region along the x/y/z/? and x/y/z/? axes" ,
701
705
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 )
709
707
local found , _ , axis1 , axis2 = param :find (" ^([xyz%?])%s+([xyz%?])$" )
710
708
if found == nil then
711
709
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -731,20 +729,14 @@ minetest.register_chatcommand("/transpose", {
731
729
worldedit .mark_pos2 (name )
732
730
733
731
worldedit .player_notify (name , count .. " nodes transposed" )
734
- end ,
732
+ end ) ,
735
733
})
736
734
737
735
minetest .register_chatcommand (" /flip" , {
738
736
params = " x/y/z/?" ,
739
737
description = " Flip the current WorldEdit region along the x/y/z/? axis" ,
740
738
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 )
748
740
if param == " ?" then
749
741
param = worldedit .player_axis (name )
750
742
end
@@ -755,20 +747,14 @@ minetest.register_chatcommand("/flip", {
755
747
756
748
local count = worldedit .flip (pos1 , pos2 , param )
757
749
worldedit .player_notify (name , count .. " nodes flipped" )
758
- end ,
750
+ end ) ,
759
751
})
760
752
761
753
minetest .register_chatcommand (" /rotate" , {
762
754
params = " <axis> <angle>" ,
763
755
description = " Rotate the current WorldEdit region around the axis <axis> by angle <angle> (90 degree increment)" ,
764
756
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 )
772
758
local found , _ , axis , angle = param :find (" ^([xyz%?])%s+([+-]?%d+)$" )
773
759
if found == nil then
774
760
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -791,20 +777,14 @@ minetest.register_chatcommand("/rotate", {
791
777
worldedit .mark_pos2 (name )
792
778
793
779
worldedit .player_notify (name , count .. " nodes rotated" )
794
- end ,
780
+ end ) ,
795
781
})
796
782
797
783
minetest .register_chatcommand (" /orient" , {
798
784
params = " <angle>" ,
799
785
description = " Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment)" ,
800
786
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 )
808
788
local found , _ , angle = param :find (" ^([+-]?%d+)$" )
809
789
if found == nil then
810
790
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -818,112 +798,70 @@ minetest.register_chatcommand("/orient", {
818
798
local count = worldedit .orient (pos1 , pos2 , angle )
819
799
820
800
worldedit .player_notify (name , count .. " nodes oriented" )
821
- end ,
801
+ end ) ,
822
802
})
823
803
824
804
minetest .register_chatcommand (" /fixlight" , {
825
805
params = " " ,
826
806
description = " Fix the lighting in the current WorldEdit region" ,
827
807
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 )
835
809
local count = worldedit .fixlight (pos1 , pos2 )
836
810
worldedit .player_notify (name , count .. " nodes updated" )
837
- end ,
811
+ end ) ,
838
812
})
839
813
840
814
minetest .register_chatcommand (" /hide" , {
841
815
params = " " ,
842
816
description = " Hide all nodes in the current WorldEdit region non-destructively" ,
843
817
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 )
851
819
local count = worldedit .hide (pos1 , pos2 )
852
820
worldedit .player_notify (name , count .. " nodes hidden" )
853
- end ,
821
+ end ) ,
854
822
})
855
823
856
824
minetest .register_chatcommand (" /suppress" , {
857
825
params = " <node>" ,
858
826
description = " Suppress all <node> in the current WorldEdit region non-destructively" ,
859
827
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
872
831
873
832
local count = worldedit .suppress (pos1 , pos2 , node )
874
833
worldedit .player_notify (name , count .. " nodes suppressed" )
875
- end ,
834
+ end ) ,
876
835
})
877
836
878
837
minetest .register_chatcommand (" /highlight" , {
879
838
params = " <node>" ,
880
839
description = " Highlight <node> in the current WorldEdit region by hiding everything else non-destructively" ,
881
840
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
894
844
895
845
local count = worldedit .highlight (pos1 , pos2 , node )
896
846
worldedit .player_notify (name , count .. " nodes highlighted" )
897
- end ,
847
+ end ) ,
898
848
})
899
849
900
850
minetest .register_chatcommand (" /restore" , {
901
851
params = " " ,
902
852
description = " Restores nodes hidden with WorldEdit in the current WorldEdit region" ,
903
853
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 )
911
855
local count = worldedit .restore (pos1 , pos2 )
912
856
worldedit .player_notify (name , count .. " nodes restored" )
913
- end ,
857
+ end ) ,
914
858
})
915
859
916
860
minetest .register_chatcommand (" /save" , {
917
861
params = " <file>" ,
918
862
description = " Save the current WorldEdit region to \" (world folder)/schems/<file>.we\" " ,
919
863
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 )
927
865
if param == " " then
928
866
worldedit .player_notify (name , " invalid usage: " .. param )
929
867
return
@@ -937,6 +875,7 @@ minetest.register_chatcommand("/save", {
937
875
938
876
local path = minetest .get_worldpath () .. " /schems"
939
877
local filename = path .. " /" .. param .. " .we"
878
+ filename = filename :gsub (" \" " , " \\\" " ):gsub (" \\ " , " \\\\ " ) -- escape any nasty characters
940
879
os.execute (" mkdir \" " .. path .. " \" " ) -- create directory if it does not already exist
941
880
local file , err = io.open (filename , " wb" )
942
881
if err ~= nil then
@@ -948,19 +887,16 @@ minetest.register_chatcommand("/save", {
948
887
file :close ()
949
888
950
889
worldedit .player_notify (name , count .. " nodes saved" )
951
- end ,
890
+ end ) ,
952
891
})
953
892
954
893
minetest .register_chatcommand (" /allocate" , {
955
894
params = " <file>" ,
956
895
description = " Set the region defined by nodes from \" (world folder)/schems/<file>.we\" as the current WorldEdit region" ,
957
896
privs = {worldedit = true },
958
897
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
964
900
965
901
if param == " " then
966
902
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -984,7 +920,7 @@ minetest.register_chatcommand("/allocate", {
984
920
worldedit .player_notify (name , " invalid file: file is invalid or created with newer version of WorldEdit" )
985
921
return
986
922
end
987
- local nodepos1 , nodepos2 , count = worldedit .allocate (pos1 , value )
923
+ local nodepos1 , nodepos2 , count = worldedit .allocate (pos , value )
988
924
989
925
worldedit .pos1 [name ] = nodepos1
990
926
worldedit .mark_pos1 (name )
@@ -1000,11 +936,8 @@ minetest.register_chatcommand("/load", {
1000
936
description = " Load nodes from \" (world folder)/schems/<file>[.we[m]]\" with position 1 of the current WorldEdit region as the origin" ,
1001
937
privs = {worldedit = true },
1002
938
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
1008
941
1009
942
if param == " " then
1010
943
worldedit .player_notify (name , " invalid usage: " .. param )
@@ -1040,7 +973,7 @@ minetest.register_chatcommand("/load", {
1040
973
return
1041
974
end
1042
975
1043
- local count = worldedit .deserialize (pos1 , value )
976
+ local count = worldedit .deserialize (pos , value )
1044
977
1045
978
worldedit .player_notify (name , count .. " nodes loaded" )
1046
979
end ,
@@ -1053,8 +986,7 @@ minetest.register_chatcommand("/lua", {
1053
986
func = function (name , param )
1054
987
local admin = minetest .setting_get (" name" )
1055
988
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" )
1058
990
return
1059
991
end
1060
992
local err = worldedit .lua (param )
@@ -1070,16 +1002,10 @@ minetest.register_chatcommand("/luatransform", {
1070
1002
params = " <code>" ,
1071
1003
description = " Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region" ,
1072
1004
privs = {worldedit = true , server = true },
1073
- func = function (name , param )
1005
+ func = safe_region ( function (name , param , pos1 , pos2 )
1074
1006
local admin = minetest .setting_get (" name" )
1075
1007
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" )
1083
1009
return
1084
1010
end
1085
1011
@@ -1089,59 +1015,52 @@ minetest.register_chatcommand("/luatransform", {
1089
1015
else
1090
1016
worldedit .player_notify (name , " code successfully executed" , false )
1091
1017
end
1092
- end ,
1018
+ end ) ,
1093
1019
})
1094
1020
1095
- if minetest .place_schematic then
1096
1021
minetest .register_chatcommand (" /mtschemcreate" , {
1097
1022
params = " <file>" ,
1098
1023
description = " Save the current WorldEdit region using the Minetest Schematic format to \" (world folder)/schems/<filename>.mts\" " ,
1099
1024
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 )
1106
1026
if param == nil then
1107
1027
worldedit .player_notify (name , " No filename specified" )
1108
1028
return
1109
1029
end
1110
1030
1111
1031
local path = minetest .get_worldpath () .. " /schems"
1112
1032
local filename = path .. " /" .. param .. " .mts"
1033
+ filename = filename :gsub (" \" " , " \\\" " ):gsub (" \\ " , " \\\\ " ) -- escape any nasty characters
1113
1034
os.execute (" mkdir \" " .. path .. " \" " ) -- create directory if it does not already exist
1114
1035
1115
1036
local ret = minetest .create_schematic (pos1 , pos2 , worldedit .prob_list [name ], filename )
1116
1037
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 )
1118
1039
else
1119
- worldedit .player_notify (name , " Saved Minetest schematic to " .. param , false )
1040
+ worldedit .player_notify (name , " saved Minetest schematic to " .. param , false )
1120
1041
end
1121
1042
worldedit .prob_list [name ] = {}
1122
- end ,
1043
+ end ) ,
1123
1044
})
1124
1045
1125
1046
minetest .register_chatcommand (" /mtschemplace" , {
1126
1047
params = " <file>" ,
1127
1048
description = " Load nodes from \" (world folder)/schems/<file>.mts\" with position 1 of the current WorldEdit region as the origin" ,
1128
1049
privs = {worldedit = true },
1129
1050
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
1135
1051
if param == nil then
1136
- worldedit .player_notify (name , " No filename specified" )
1052
+ worldedit .player_notify (name , " no filename specified" )
1137
1053
return
1138
1054
end
1139
1055
1056
+ local pos = get_position (name )
1057
+ if pos == nil then return end
1058
+
1140
1059
local path = minetest .get_worldpath () .. " /schems/" .. param .. " .mts"
1141
1060
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 )
1143
1062
else
1144
- worldedit .player_notify (name , " Placed Minetest schematic " .. param ..
1063
+ worldedit .player_notify (name , " placed Minetest schematic " .. param ..
1145
1064
" at " .. minetest .pos_to_string (pos ), false )
1146
1065
end
1147
1066
end ,
@@ -1169,7 +1088,7 @@ minetest.register_chatcommand("/mtschemprob", {
1169
1088
local prob = math.floor (((v [" prob" ] / 256 ) * 100 ) * 100 + 0.5 ) / 100
1170
1089
text = text .. minetest .pos_to_string (v [" pos" ]) .. " : " .. prob .. " % | "
1171
1090
end
1172
- worldedit .player_notify (name , " Currently set node probabilities:" )
1091
+ worldedit .player_notify (name , " currently set node probabilities:" )
1173
1092
worldedit .player_notify (name , text )
1174
1093
else
1175
1094
worldedit .player_notify (name , " unknown subcommand: " .. param )
@@ -1187,20 +1106,13 @@ minetest.register_on_player_receive_fields(
1187
1106
end
1188
1107
end
1189
1108
)
1190
- end
1191
1109
1192
1110
minetest .register_chatcommand (" /clearobjects" , {
1193
1111
params = " " ,
1194
1112
description = " Clears all objects within the WorldEdit region" ,
1195
1113
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 )
1203
1115
local count = worldedit .clearobjects (pos1 , pos2 )
1204
1116
worldedit .player_notify (name , count .. " objects cleared" )
1205
- end ,
1117
+ end ) ,
1206
1118
})
0 commit comments