/
BuildingCommands.cs
2845 lines (2502 loc) · 121 KB
/
BuildingCommands.cs
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Part of fCraft | Copyright 2009-2015 Matvei Stefarov <me@matvei.org> | BSD-3 | See LICENSE.txt //Copyright (c) 2011-2013 Jon Baker, Glenn Marien and Lao Tszy <Jonty800@gmail.com> //Copyright (c) <2012-2014> <LeChosenOne, DingusBungus> | ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
using System;
using System.IO;
using System.Linq;
using fCraft.Drawing;
using fCraft.MapConversion;
using System.Collections.Generic;
using JetBrains.Annotations;
using RandomMaze;
using System.Drawing;
namespace fCraft {
/// <summary> Commands for placing specific blocks (solid, water, grass),
/// and switching block placement modes (paint, bind). </summary>
static class BuildingCommands {
public static int MaxUndoCount = 2000000;
public const int MaxCalculationExceptions = 100;
const string GeneralDrawingHelp = " Use &H/Cancel&S to cancel selection mode. " +
"Use &H/Undo&S to stop and undo the last command.";
internal static void Init() {
CommandManager.RegisterCommand( CdBind );
CommandManager.RegisterCommand( CdGrass );
CommandManager.RegisterCommand( CdSolid );
CommandManager.RegisterCommand( CdLava );
CommandManager.RegisterCommand( CdWater );
CommandManager.RegisterCommand( CdPaint );
CommandManager.RegisterCommand( CdTree );
CommandManager.RegisterCommand( CdCancel );
CommandManager.RegisterCommand( CdMark );
CommandManager.RegisterCommand( CdMarkAll );
CommandManager.RegisterCommand( CdDoNotMark );
CommandManager.RegisterCommand( CdUndo );
CommandManager.RegisterCommand( CdRedo );
CommandManager.RegisterCommand( CdReplace );
CommandManager.RegisterCommand( CdReplaceNot );
CommandManager.RegisterCommand( CdReplaceBrush );
CommandManager.RegisterCommand( CdReplaceNotBrush );
CdReplace.Help += GeneralDrawingHelp;
CdReplaceNot.Help += GeneralDrawingHelp;
CdReplaceBrush.Help += GeneralDrawingHelp;
CdReplaceNotBrush.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdCopySlot );
CommandManager.RegisterCommand( CdCopy );
CommandManager.RegisterCommand( CdCut );
CommandManager.RegisterCommand( CdPaste );
CommandManager.RegisterCommand( CdPasteNot );
CommandManager.RegisterCommand( CdPasteX );
CommandManager.RegisterCommand( CdPasteNotX );
CommandManager.RegisterCommand( CdMirror );
CommandManager.RegisterCommand( CdRotate );
CdCut.Help += GeneralDrawingHelp;
CdPaste.Help += GeneralDrawingHelp;
CdPasteNot.Help += GeneralDrawingHelp;
CdPasteX.Help += GeneralDrawingHelp;
CdPasteNotX.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdRestore );
CdRestore.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdCuboid );
CommandManager.RegisterCommand( CdCuboidWireframe );
CommandManager.RegisterCommand( CdCuboidHollow );
CdCuboid.Help += GeneralDrawingHelp;
CdCuboidHollow.Help += GeneralDrawingHelp;
CdCuboidWireframe.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdEllipsoid );
CommandManager.RegisterCommand( CdEllipsoidHollow );
CdEllipsoid.Help += GeneralDrawingHelp;
CdEllipsoidHollow.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdLine );
CommandManager.RegisterCommand(CdSnap);
CdSnap.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdTriangle );
CommandManager.RegisterCommand( CdTriangleWireframe );
CdLine.Help += GeneralDrawingHelp;
CdTriangle.Help += GeneralDrawingHelp;
CdTriangleWireframe.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdSphere );
CommandManager.RegisterCommand( CdSphereHollow );
CommandManager.RegisterCommand( CdTorus );
CdSphere.Help += GeneralDrawingHelp;
CdSphereHollow.Help += GeneralDrawingHelp;
CdTorus.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdFill2D );
CommandManager.RegisterCommand( CdFill3D );
CdFill2D.Help += GeneralDrawingHelp;
CdFill3D.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdUndoArea );
CommandManager.RegisterCommand( CdUndoPlayer );
CommandManager.RegisterCommand( CdHighlight );
CommandManager.RegisterCommand( CdUndoAreaNot );
CommandManager.RegisterCommand( CdUndoPlayerNot );
CdUndoArea.Help += GeneralDrawingHelp;
CdUndoAreaNot.Help += GeneralDrawingHelp;
CommandManager.RegisterCommand( CdStatic );
CommandManager.RegisterCommand( CdWalls );
CommandManager.RegisterCommand( CdPlace );
CommandManager.RegisterCommand( CdDisPlace );
CommandManager.RegisterCommand( CdCenter );
CommandManager.RegisterCommand( CdMazeCuboid );
CommandManager.RegisterCommand( CdWrite );
CommandManager.RegisterCommand( CdDraw2D );
CommandManager.RegisterCommand( CdSetFont );
CommandManager.RegisterCommand( CdDrawImage );
CommandManager.RegisterCommand( CdPlane );
CommandManager.RegisterCommand( CdPlaneW );
CommandManager.RegisterCommand( CdOverlay );
CommandManager.RegisterCommand(CdReplaceAll);
CommandManager.RegisterCommand(CdSnake);
}
#region Cuboid
static readonly CommandDescriptor CdCuboid = new CommandDescriptor {
Name = "Cuboid",
Aliases = new[] { "blb", "c", "z" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Fills a rectangular area (cuboid) with blocks.",
Handler = CuboidHandler
};
static void CuboidHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new CuboidDrawOperation( player ) );
}
static readonly CommandDescriptor CdCuboidWireframe = new CommandDescriptor {
Name = "CuboidW",
Aliases = new[] { "cubw", "cw", "bfb" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a wireframe box (a frame) around the selected rectangular area.",
Handler = CuboidWireframeHandler
};
static void CuboidWireframeHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new CuboidWireframeDrawOperation( player ) );
}
static readonly CommandDescriptor CdCuboidHollow = new CommandDescriptor {
Name = "CuboidH",
Aliases = new[] { "cubh", "ch", "h", "bhb" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Surrounds the selected rectangular area with a box of blocks. " +
"Unless two blocks are specified, leaves the inside untouched.",
Handler = CuboidHollowHandler
};
static readonly CommandDescriptor CdOverlay = new CommandDescriptor
{
Name = "Overlay",
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Covers blocks that are underneath air.",
Handler = overlayHandler
};
static void overlayHandler(Player player, CommandReader cmd)
{
DrawOperationBegin(player, cmd, new OverlayDrawOperation(player));
}
static void CuboidHollowHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new CuboidHollowDrawOperation( player ) );
}
#endregion
#region plane
private static readonly CommandDescriptor CdPlane = new CommandDescriptor
{
Name = "Plane",
Aliases = new[] { "Quad" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a plane between three points.",
Handler = PlaneHandler
};
private static void PlaneHandler(Player player, CommandReader cmd)
{
DrawOperationBegin(player, cmd, new PlaneDrawOperation(player));
}
private static readonly CommandDescriptor CdPlaneW = new CommandDescriptor
{
Name = "PlaneW",
Aliases = new[] { "QuadW" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a wireframe plane between four points.",
Handler = PlaneWHandler
};
private static void PlaneWHandler(Player player, CommandReader cmd)
{
DrawOperationBegin(player, cmd, new PlaneWireframeDrawOperation(player));
}
#endregion
#region Ellipsoid
static readonly CommandDescriptor CdEllipsoid = new CommandDescriptor {
Name = "Ellipsoid",
Aliases = new[] { "e" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Fills an ellipsoid-shaped area (elongated sphere) with blocks.",
Handler = EllipsoidHandler
};
static void EllipsoidHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new EllipsoidDrawOperation( player ) );
}
static readonly CommandDescriptor CdEllipsoidHollow = new CommandDescriptor {
Name = "EllipsoidH",
Aliases = new[] { "eh" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Surrounds the selected an ellipsoid-shaped area (elongated sphere) with a shell of blocks.",
Handler = EllipsoidHollowHandler
};
static void EllipsoidHollowHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new EllipsoidHollowDrawOperation( player ) );
}
#endregion
#region sphere
static readonly CommandDescriptor CdSphere = new CommandDescriptor {
Name = "Sphere",
Aliases = new[] { "sp", "spheroid" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw, Permission.DrawAdvanced },
RepeatableSelection = true,
Help = "Fills a spherical area with blocks. " +
"The first mark denotes the CENTER of the sphere, and " +
"distance to the second mark denotes the radius.",
Handler = SphereHandler
};
static void SphereHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new SphereDrawOperation( player ) );
}
static readonly CommandDescriptor CdSphereHollow = new CommandDescriptor {
Name = "SphereH",
Aliases = new[] { "sph", "hsphere" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw, Permission.DrawAdvanced },
RepeatableSelection = true,
Help = "Surrounds a spherical area with a shell of blocks. " +
"The first mark denotes the CENTER of the sphere, and " +
"distance to the second mark denotes the radius.",
Handler = SphereHollowHandler
};
static void SphereHollowHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new SphereHollowDrawOperation( player ) );
}
#endregion
#region line
static readonly CommandDescriptor CdLine = new CommandDescriptor {
Name = "Line",
Aliases = new[] { "ln" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a continuous line between two points with blocks. " +
"Marks do not have to be aligned.",
Handler = LineHandler
};
static void LineHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new LineDrawOperation( player ) );
}
#endregion
#region Snap
static readonly CommandDescriptor CdSnap = new CommandDescriptor
{
Name = "Snap",
Aliases = new[] { "LineSnap" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a line along the axis or at 45degrees thereof." +
"Second mark automatically snaps into position.",
Handler = SnapHandler
};
static void SnapHandler(Player player, CommandReader cmd)
{
DrawOperationBegin(player, cmd, new SnapDrawOperation(player));
}
#endregion
#region triangle
static readonly CommandDescriptor CdTriangleWireframe = new CommandDescriptor {
Name = "TriangleW",
Aliases = new[] { "tw" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws lines between three points, to form a triangle.",
Handler = TriangleWireframeHandler
};
static void TriangleWireframeHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new TriangleWireframeDrawOperation( player ) );
}
static readonly CommandDescriptor CdTriangle = new CommandDescriptor {
Name = "Triangle",
Aliases = new[] { "t" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a triangle between three points.",
Handler = TriangleHandler
};
static void TriangleHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new TriangleDrawOperation( player ) );
}
#endregion
#region torus
static readonly CommandDescriptor CdTorus = new CommandDescriptor {
Name = "Torus",
Aliases = new[] { "donut", "bagel" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw, Permission.DrawAdvanced },
RepeatableSelection = true,
Help = "Draws a horizontally-oriented torus. The first mark denotes the CENTER of the torus, horizontal " +
"distance to the second mark denotes the ring radius, and the vertical distance to the second mark denotes the " +
"tube radius",
Handler = TorusHandler
};
static void TorusHandler( Player player, CommandReader cmd ) {
DrawOperationBegin( player, cmd, new TorusDrawOperation( player ) );
}
#endregion
#region drawoperation
static void DrawOperationBegin( Player player, CommandReader cmd, DrawOperation op ) {
// try to create instance of player's currently selected brush
// all command parameters are passed to the brush
IBrush brush = player.ConfigureBrush(cmd);
// MakeInstance returns null if there were problems with syntax, abort
if( brush == null ) return;
op.Brush = brush;
player.SelectionStart( op.ExpectedMarks, DrawOperationCallback, op, Permission.Draw );
player.Message( "{0}: Click or &H/Mark&S {1} blocks.",
op.Description, op.ExpectedMarks );
}
static void DrawOperationCallback( Player player, Vector3I[] marks, object tag ) {
DrawOperation op = (DrawOperation)tag;
if( !op.Prepare( marks ) ) return;
if( !player.Info.ClassicubeVerified ) {
player.Message("As you had an older minecraft.net account, you must have an admin verify your " +
"new classicube.net account actually is you with /verify before you can use drawing commands.");
op.Cancel();
return;
}
if( !player.CanDraw( op.BlocksTotalEstimate ) ) {
player.Message( "You are only allowed to run draw commands that affect up to {0} blocks. This one would affect {1} blocks.",
player.Info.Rank.DrawLimit,
op.Bounds.Volume );
op.Cancel();
return;
}
player.Message( "{0}: Processing ~{1} blocks.",
op.Description, op.BlocksTotalEstimate );
op.Begin();
}
#endregion
#region SetFont
static CommandDescriptor CdSetFont = new CommandDescriptor()
{
Name = "SetFont",
Aliases = new[] { "FontSet", "Font", "Sf" },
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new Permission[] { Permission.Draw },
IsConsoleSafe = false,
Help = "Sets the properties for /Write, such as: font and size",
Handler = SetFontHandler,
Usage = "/SetFont < Font | Size | Reset | Style> <Variable>"
};
static void SetFontHandler(Player player, CommandReader cmd) {
string arg = cmd.Next();
if (arg == null) {
CdSetFont.PrintUsage(player); return;
}
if (arg.CaselessEquals("reset")) {
player.font = new Font("Times New Roman", 20, FontStyle.Regular);
player.Message("SetFont: Font reverted back to default ({0} size {1})",
player.font.FontFamily.Name, player.font.Size);
} else if (arg.CaselessEquals("font")) {
HandleFont(player, cmd);
} else if (arg.CaselessEquals("size")) {
int size = -1;
if (cmd.NextInt(out size)) {
if (size < 5) {
player.Message("&WIncorrect font size ({0}): Size needs to be at least 5" +
"(which is ideal for minecraft font, not the others)", size);
return;
}
player.Message("SetFont: Size changed from {0} to {1} ({2})", player.font.Size, size, player.font.FontFamily.Name);
player.font = new System.Drawing.Font(player.font.FontFamily, size);
} else {
player.Message("&WInvalid size, use /SetFont Size FontSize. Example: /SetFont Size 14");
}
} else if (arg.CaselessEquals("style")) {
string style = cmd.Next();
if (style == null) {
CdSetFont.PrintUsage(player);
} else if (style.CaselessEquals("italic")) {
player.font = new System.Drawing.Font(player.font, FontStyle.Italic);
player.Message("SetFont: Style set to Italic ({0})", player.font.FontFamily.Name);
} else if (style.CaselessEquals("bold")) {
player.font = new System.Drawing.Font(player.font, FontStyle.Bold);
player.Message("SetFont: Style set to Bold ({0})", player.font.FontFamily.Name);
} else if (style.CaselessEquals("regular")) {
player.font = new System.Drawing.Font(player.font, FontStyle.Regular);
player.Message("SetFont: Style set to Regular ({0})", player.font.FontFamily.Name);
}
} else {
CdSetFont.PrintUsage(player);
}
}
static void HandleFont(Player player, CommandReader cmd) {
string name = cmd.NextAll();
if (!Directory.Exists(Paths.FontsPath)) {
Directory.CreateDirectory(Paths.FontsPath);
player.Message("There are no fonts available for this server. " +
"Font is set to default: {0}", player.font.FontFamily.Name);
return;
}
string[] available = GetAvailableFonts();
if (available == null) {
player.Message("No fonts have been found."); return;
}
if (String.IsNullOrEmpty(name)) {
player.Message("{0} fonts Available: {1}",
available.Length, available.JoinToString());
return;
}
string match = null;
for (int i = 0; i < available.Length; i++) {
string font = available[i];
if (font == null) continue;
if (!font.CaselessStarts(name)) continue;
if (font.CaselessEquals(name)) {
match = font; break;
} else if (match == null) {
match = font;
} else {
var matches = available.Where(f => f.CaselessStarts(name));
player.Message("Multiple font files matched \"{0}\": {1}",
name, matches.JoinToString());
return;
}
}
if (match != null) {
player.Message("Your font has changed to \"{0}\":", match);
string path = System.IO.Path.Combine(Paths.FontsPath, match + ".ttf");
player.font = new System.Drawing.Font(player.LoadFontFamily(path), player.font.Size);
} else {
player.Message("No fonts found for \"{0}\". Available fonts: {1}",
name, available.JoinToString());
}
}
#endregion
#region Draw2d
static readonly CommandDescriptor CdDraw2D = new CommandDescriptor
{
Name = "Draw2D",
Aliases = new[] { "D2d" },
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new Permission[] { Permission.DrawAdvanced },
RepeatableSelection = true,
Help = "/Draw2D, then select a shape (Polygon, spiral, star). You can then choose a size in blocks " +
" for the shape before selecting two points." +
"Example: /Draw2d Polygon 50. Polygon and triangle can be used with any number of points " +
"exceeding 3, which should follow the 'Size' argument",
Usage = "/Draw2D <Shape> <Size> <Points> <Fill: true/false>",
Handler = Draw2DHandler,
};
static void Draw2DHandler(Player player, CommandReader cmd)
{
string Shape = cmd.Next();
if (Shape == null)
{
CdDraw2D.PrintUsage(player);
return;
}
switch (Shape.ToLower())
{
case "polygon":
case "star":
case "spiral":
break;
default:
CdDraw2D.PrintUsage(player);
return;
}
int radius = 0;
int Points = 0;
if (!cmd.NextInt(out radius))
{
radius = 20;
}
if (!cmd.NextInt(out Points))
{
Points = 5;
}
bool fill = true;
if (cmd.HasNext)
{
if (!bool.TryParse(cmd.Next(), out fill))
{
fill = true;
}
}
Draw2DData tag = new Draw2DData() { Shape = Shape, Points = Points, Radius = radius, Fill = fill };
player.Message("Draw2D({0}): Click 2 blocks or use &H/Mark&S to set direction.", Shape);
player.SelectionStart(2, Draw2DCallback, tag, Permission.DrawAdvanced);
}
struct Draw2DData
{
public int Radius;
public int Points;
public string Shape;
public bool Fill;
}
static void Draw2DCallback(Player player, Vector3I[] marks, object tag)
{
Block block = new Block();
Draw2DData data = (Draw2DData)tag;
int radius = data.Radius;
int Points = data.Points;
bool fill = data.Fill;
string Shape = data.Shape;
if (player.LastUsedBlockType == Block.None)
{
block = Block.Stone;
}
else
{
block = player.LastUsedBlockType;
}
Direction direction = DirectionFinder.GetDirection(marks);
try
{
ShapesLib lib = new ShapesLib(block, marks, player, radius, direction);
switch (Shape.ToLower())
{
case "polygon":
lib.DrawRegularPolygon(Points, 18, fill);
break;
case "star":
lib.DrawStar(Points, radius, fill);
break;
case "spiral":
lib.DrawSpiral();
break;
default:
player.Message("&WUnknown shape");
CdDraw2D.PrintUsage(player);
lib = null;
return;
}
if (lib.blockCount > 0)
{
player.Message("/Draw2D: Drawing {0} with a size of '{1}' using {2} blocks of {3}",
Shape, radius, lib.blockCount, Map.GetBlockName(player.World, block));
}
else
{
player.Message("&WNo direction was set");
}
lib = null; //get lost
}
catch (Exception e)
{
player.Message(e.Message);
}
}
#endregion
#region write
static readonly CommandDescriptor CdWrite = new CommandDescriptor
{
Name = "Write",
Aliases = new[] { "Text", "Wt" },
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new Permission[] { Permission.DrawAdvanced },
RepeatableSelection = true,
IsConsoleSafe = false,
Help = "/Write Sentence, then click 2 blocks. The first is the starting point, the second is the direction",
Usage = "/Write Sentence",
Handler = WriteHandler,
};
static void WriteHandler(Player player, CommandReader cmd) {
string sentence = cmd.NextAll();
if (String.IsNullOrEmpty(sentence)) {
CdWrite.PrintUsage(player);
return;
}
player.Message("Write: Click 2 blocks or use &H/Mark&S to set direction.");
player.SelectionStart(2, WriteCallback, sentence, Permission.DrawAdvanced);
}
static void WriteCallback(Player player, Vector3I[] marks, object tag) {
Block block = player.LastUsedBlockType;
if (block == Block.None) block = Block.Stone;
Direction direction = DirectionFinder.GetDirection(marks);
string sentence = (string)tag;
try {
FontHandler render = new FontHandler(block, marks, player, direction);
render.CreateGraphicsAndDraw(sentence);
if (render.blockCount > 0) {
player.Message("/Write (Size {0}, {1}: Writing '{2}' using {3} blocks of {4}",
player.font.Size, player.font.FontFamily.Name, sentence, render.blockCount,
Map.GetBlockName(player.World, block));
} else {
player.Message("&WNo direction was set");
}
} catch (Exception e) {
player.Message(e.Message);
Logger.Log(LogType.Error, "WriteCommand: " + e);
}
}
static string[] GetAvailableFonts() {
if (!Directory.Exists(Paths.FontsPath)) return null;
string[] files = Directory.GetFiles(Paths.FontsPath, "*.ttf", SearchOption.TopDirectoryOnly)
.Select(name => System.IO.Path.GetFileNameWithoutExtension(name))
.Where(name => !String.IsNullOrEmpty(name))
.ToArray();
return files.Length == 0 ? null : files;
}
#endregion
#region Tree
static readonly CommandDescriptor CdTree = new CommandDescriptor
{
Name = "Tree",
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Usage = "/Tree Shape Height",
Help = "Plants a tree of given shape and height. Available shapes: Normal, Bamboo and Palm. Cone, Round, Rainforest, Mangrove",
Handler = TreeHandler
};
static void TreeHandler(Player player, CommandReader cmd)
{
string shapeName = cmd.Next();
int height;
Forester.TreeShape shape;
// that's one ugly if statement... does the job though.
if (shapeName == null ||
!cmd.NextInt(out height) ||
!EnumUtil.TryParse(shapeName, out shape, true) ||
shape == Forester.TreeShape.Stickly ||
shape == Forester.TreeShape.Procedural)
{
CdTree.PrintUsage(player);
player.Message("Available shapes: Normal, Bamboo, Palm, Cone, Round, Rainforest, Mangrove.");
return;
}
if (height < 6 || height > 1024)
{
player.Message("Tree height must be 6 blocks or above");
return;
}
int volume = (int)Math.Pow(height, 3);
if (!player.CanDraw(volume))
{
player.Message(String.Format("You are only allowed to run commands that affect up to {0} blocks. This one would affect {1} blocks.",
player.Info.Rank.DrawLimit, volume));
return;
}
Map map = player.World.Map;
ForesterArgs args = new ForesterArgs
{
Height = height - 1,
Operation = Forester.ForesterOperation.Add,
Map = map,
Shape = shape,
TreeCount = 1,
RootButtresses = false,
Roots = Forester.RootMode.None,
Rand = new Random()
};
player.SelectionStart(1, TreeCallback, args, CdTree.Permissions);
player.Message("Tree: Place a block or type /Mark to use your location.");
}
static void TreeCallback(Player player, Vector3I[] marks, object tag)
{
ForesterArgs args = (ForesterArgs)tag;
int blocksPlaced = 0, blocksDenied = 0;
UndoState undoState = player.DrawBegin(null);
args.BlockPlacing +=
(sender, e) =>
DrawOneBlock(player, player.World.Map, e.Block, new Vector3I(e.Coords.X, e.Coords.Y, e.Coords.Z),
BlockChangeContext.Drawn,
ref blocksPlaced, ref blocksDenied, undoState);
Forester.SexyPlant(args, marks[0]);
DrawingFinished(player, "/Tree: Planted", blocksPlaced, blocksDenied);
}
#endregion
#region walls
private static readonly CommandDescriptor CdWalls = new CommandDescriptor
{
Name = "Walls",
IsConsoleSafe = false,
RepeatableSelection = true,
Category = CommandCategory.New | CommandCategory.Building,
IsHidden = false,
Permissions = new[] { Permission.Draw },
Help = "Fills a rectangular area of walls",
Handler = WallsHandler
};
private static void WallsHandler(Player player, CommandReader cmd)
{
DrawOperationBegin(player, cmd, new WallsDrawOperation(player));
}
#endregion
#region displace
private static readonly CommandDescriptor CdDisPlace = new CommandDescriptor
{
Name = "DPlace",
Aliases = new[] { "distanceplace", "displace", "dp" },
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new[] { Permission.Draw },
Usage = "/DPlace [block] [distance away]",
Help = "Places a block a certain distance away from where you are, in the direction you are looking",
Handler = DisPlace
};
private static void DisPlace(Player player, CommandReader cmd) {
if (cmd.CountRemaining < 2) {
CdDisPlace.PrintUsage(player);
return;
}
Block block;
if (!cmd.NextBlock(player, false, out block)) return;
int length;
if (!cmd.NextInt(out length)) return;
byte yaw = player.Position.R, pitch = player.Position.L;
Vector3I pos = player.Position.ToBlockCoordsRaw(); pos.Y--;
Vector3I dir = Vector3I.FlatDirection(yaw, pitch);
pos += dir * length;
pos.X = Math.Min(player.WorldMap.Width - 1, Math.Max(0, pos.X));
pos.Y = Math.Min(player.WorldMap.Length - 1, Math.Max(0, pos.Y));
pos.Z = Math.Min(player.WorldMap.Height - 1, Math.Max(0, pos.Z));
if (player.CanPlace(player.World.Map, pos, block, BlockChangeContext.Drawn) != CanPlaceResult.Allowed) {
player.Message("&WYou are not allowed to build here");
return;
}
Player.RaisePlayerPlacedBlockEvent(player, player.WorldMap, pos, player.WorldMap.GetBlock(pos),
block, BlockChangeContext.Drawn);
BlockUpdate blockUpdate = new BlockUpdate(null, pos, block);
player.World.Map.QueueUpdate(blockUpdate);
player.Message("Block placed at {0} ({1} blocks away from you)", pos, length);
}
#endregion
#region Place
private static readonly CommandDescriptor CdPlace = new CommandDescriptor {
Name = "Place",
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new[] { Permission.Draw },
Usage = "/Place [x] [y] [z] and/or [block]",
Help = "Places a block at specified XYZ or directly below your feet.",
IsConsoleSafe = true,
Handler = PlaceHandler
};
private static void PlaceHandler(Player player, CommandReader cmd) {
bool isConsole = (player == Player.Console);
if (isConsole && cmd.Count < 6) {
player.Message("When used by console /Place requires a world name.");
player.Message("/Place [x] [y] [z] [block] [world]");
return;
}
Block block = Block.Stone;
if (!isConsole && player.LastUsedBlockType != Block.None)
block = player.LastUsedBlockType;
Vector3I coords;
int x, y, z;
if (cmd.NextInt(out x) && cmd.NextInt(out y) && cmd.NextInt(out z)) {
if (cmd.HasNext && !cmd.NextBlock(player, false, out block)) return;
coords = new Vector3I(x, y, z);
} else if (isConsole) {
player.Message("Invalid coordinates!");
return;
} else {
cmd.Rewind();
if (cmd.HasNext && !cmd.NextBlock(player, false, out block)) return;
coords = new Vector3I(player.Position.BlockX, player.Position.BlockY, (player.Position.Z - 64) / 32);
}
World world;
if (player == Player.Console) {
string worldName = cmd.Next();
if (string.IsNullOrEmpty(worldName)) {
player.Message("Console must specify a world!");
}
world = WorldManager.FindWorldOrPrintMatches(player, worldName);
if (world == null)
return;
} else {
world = player.World;
}
bool unLoad = false;
if (!world.IsLoaded) {
world.LoadMap();
unLoad = true;
}
coords.X = Math.Min(world.map.Width - 1, Math.Max(0, coords.X));
coords.Y = Math.Min(world.map.Length - 1, Math.Max(0, coords.Y));
coords.Z = Math.Min(world.map.Height - 1, Math.Max(0, coords.Z));
if (player == Player.Console) {
BlockUpdate blockUpdate = new BlockUpdate(player, coords, block);
player.Info.ProcessBlockPlaced((byte)block);
world.map.QueueUpdate(blockUpdate);
Player.RaisePlayerPlacedBlockEvent(player, world.map, coords, block, world.map.GetBlock(coords), BlockChangeContext.Manual);
} else {
player.SendNow(Packet.MakeSetBlock(coords, block));
player.PlaceBlockWithEvents(coords, ClickAction.Build, block);
}
if (!isConsole)
player.Message("{0} placed at {1}", Map.GetBlockName(player.World, block), coords);
if (unLoad)
world.UnloadMap(true);
}
#endregion
#region center
private static readonly CommandDescriptor CdCenter = new CommandDescriptor
{
Name = "Center",
Aliases = new[] { "Centre" },
Category = CommandCategory.New | CommandCategory.Building,
Permissions = new[] { Permission.Build },
RepeatableSelection = true,
Usage = "/Center",
Help = "Places a block at the center for a chosen cuboided area",
UsableByFrozenPlayers = false,
Handler = CenterHandler
};
private static void CenterHandler(Player player, CommandReader cmd) {
player.SelectionStart(2, CenterCallback, null, CdCenter.Permissions);
player.Message("Center: Place a block or type /Mark to use your location.");
}
private static void CenterCallback(Player player, Vector3I[] marks, object tag) {
if (player.LastUsedBlockType == Block.None) {
player.Message("&WCannot deduce desired block. Click a block or type out the block name.");
return;
}
BoundingBox bounds = new BoundingBox(marks[0], marks[1]);
int blocksDrawn = 0, blocksSkipped = 0;
UndoState undoState = player.DrawBegin(null);
World playerWorld = player.World;
if (playerWorld == null)
PlayerOpException.ThrowNoWorld(player);
Vector3I[] coords = new Vector3I[8];
int count = 0, cenX = bounds.XCentre, cenY = bounds.YCentre, cenZ = bounds.ZCentre;
if ((bounds.Width % 2) == 0) {
coords[count] = new Vector3I(cenX + 1, cenY, cenZ); count++;
}
if ((bounds.Width % 2) == 0 && (bounds.Length % 2) == 0) {
coords[count] = new Vector3I(cenX + 1, cenY + 1, cenZ); count++;
}
if ((bounds.Width % 2) == 0 && (bounds.Height % 2) == 0) {
coords[count] = new Vector3I(cenX + 1, cenY, cenZ + 1); count++;
}
if ((bounds.Width % 2) == 0 && (bounds.Length % 2) == 0 && (bounds.Height % 2) == 0) {
coords[count] = new Vector3I(cenX + 1, cenY + 1, cenZ + 1); count++;
}
if ((bounds.Length % 2) == 0) {
coords[count] = new Vector3I(cenX, cenY + 1, cenZ); count++;
}
if ((bounds.Length % 2) == 0 && (bounds.Height % 2) == 0) {
coords[count] = new Vector3I(cenX, cenY + 1, cenZ + 1); count++;
}
if ((bounds.Height % 2) == 0) {
coords[count] = new Vector3I(cenX, cenY, cenZ + 1); count++;
}
coords[count] = new Vector3I(cenX, cenY, cenZ); count++;
for (int i = 0; i < count; i++) {
DrawOneBlock(player, player.World.Map, player.LastUsedBlockType, coords[i],
BlockChangeContext.Drawn, ref blocksDrawn, ref blocksSkipped, undoState);
}
DrawingFinished(player, "Placed", blocksDrawn, blocksSkipped);
}
#endregion
#region Fill
static readonly CommandDescriptor CdFill2D = new CommandDescriptor
{
Name = "Fill2D",
Aliases = new[] { "f2d" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw, Permission.DrawAdvanced },
RepeatableSelection = true,
Help = "Fills a continuous area with blocks, in 2D. " +
"Takes just 1 mark, and replaces blocks of the same type as the block you clicked. " +
"Works similar to \"Paint Bucket\" tool in Photoshop. " +
"Direction of effect is determined by where the player is looking.",
Handler = Fill2DHandler
};
static void Fill2DHandler(Player player, CommandReader cmd)
{
Fill2DDrawOperation op = new Fill2DDrawOperation(player);
IBrush brush = player.ConfigureBrush(cmd);
if (brush == null) return;
op.Brush = brush;
player.SelectionStart(1, Fill2DCallback, op, Permission.Draw);
player.Message("{0}: Click a block to start filling.", op.Description);
}
static void Fill2DCallback(Player player, Vector3I[] marks, object tag)
{
DrawOperation op = (DrawOperation)tag;
if (!op.Prepare(marks)) return;
if (player.WorldMap.GetBlock(marks[0]) == Block.Air)
{
Logger.Log(LogType.UserActivity,
"Fill2D: Asked {0} to confirm replacing air on world {1}",
player.Name,
// ReSharper disable PossibleNullReferenceException
player.World.Name);
// ReSharper restore PossibleNullReferenceException
player.Confirm(Fill2DConfirmCallback, op, "{0}: Replace air?", op.Description);
}
else
{
Fill2DConfirmCallback(player, op, false);
}
}
static void Fill2DConfirmCallback(Player player, object tag, bool fromConsole)
{
Fill2DDrawOperation op = (Fill2DDrawOperation)tag;
int maxDim = Math.Max(op.Bounds.Width, Math.Max(op.Bounds.Length, op.Bounds.Height));
int otherDim = op.Bounds.Volume / maxDim;
player.Message("{0}: Filling in a {1}x{2} area...",
op.Description, maxDim, otherDim);
op.Begin();
}