forked from anegostudios/vscreativemod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldEdit.cs
1337 lines (1025 loc) · 48.3 KB
/
WorldEdit.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.Datastructures;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
using Vintagestory.API.Util;
namespace Vintagestory.ServerMods.WorldEdit
{
public partial class WorldEdit : ModSystem
{
public ICoreServerAPI sapi;
WorldEditClientHandler clientHandler;
IServerNetworkChannel serverChannel;
Dictionary<string, WorldEditWorkspace> workspaces = new Dictionary<string, WorldEditWorkspace>();
// Unpretty, but too lazy to type it over and over again ;-)
IServerPlayer fromPlayer;
int groupId;
WorldEditWorkspace workspace;
public bool serverOverloadProtection = true;
string exportFolderPath;
public bool ReplaceMetaBlocks
{
get
{
object val = null;
sapi.ObjectCache.TryGetValue("donotResolveImports", out val);
if (val is bool)
{
return !(bool)val;
}
return true;
}
}
public override void StartPre(ICoreAPI api)
{
ToolRegistry.RegisterDefaultTools();
}
public override void StartClientSide(ICoreClientAPI capi)
{
clientHandler = new WorldEditClientHandler(capi);
}
public override void StartServerSide(ICoreServerAPI sapi)
{
this.sapi = sapi;
exportFolderPath = sapi.GetOrCreateDataPath("WorldEdit");
sapi.Permissions.RegisterPrivilege("worldedit", "Ability to use world edit tools");
sapi.RegisterCommand("we", "World edit tools", "[ms|me|mc|mex|clear|mdelete|mfill|imp|impr|blu|brs|brm|ers|range|tool|on|off|undo|redo|sovp|block|...]", CmdEditServer, "worldedit");
sapi.Event.PlayerJoin += OnPlayerJoin;
sapi.Event.PlayerSwitchGameMode += OnSwitchedGameMode;
sapi.Event.BreakBlock += OnBreakBlock;
sapi.Event.DidPlaceBlock += OnDidBuildBlock;
sapi.Event.SaveGameLoaded += OnLoad;
sapi.Event.GameWorldSave += OnSave;
serverChannel =
sapi.Network.RegisterChannel("worldedit")
.RegisterMessageType(typeof(RequestWorkSpacePacket))
.RegisterMessageType(typeof(WorldEditWorkspace))
.RegisterMessageType(typeof(ChangePlayerModePacket))
.RegisterMessageType(typeof(CopyToClipboardPacket))
.RegisterMessageType(typeof(SchematicJsonPacket))
.SetMessageHandler<RequestWorkSpacePacket>(OnRequestWorkSpaceMessage)
.SetMessageHandler<ChangePlayerModePacket>(OnChangePlayerModeMessage)
.SetMessageHandler<SchematicJsonPacket>(OnReceivedSchematic)
;
}
private void OnReceivedSchematic(IServerPlayer fromPlayer, SchematicJsonPacket networkMessage)
{
WorldEditWorkspace workspace = GetOrCreateWorkSpace(fromPlayer);
if (workspace.ToolsEnabled && workspace.ToolInstance is ImportTool)
{
ImportTool impTool = workspace.ToolInstance as ImportTool;
string error = null;
BlockSchematic schematic = BlockSchematic.LoadFromString(networkMessage.JsonCode, ref error);
if (error == null)
{
this.fromPlayer = fromPlayer;
impTool.SetBlockDatas(this, new BlockSchematic[] { schematic });
fromPlayer.SendMessage(GlobalConstants.CurrentChatGroup, Lang.Get("Ok, schematic loaded into clipboard."), EnumChatType.CommandSuccess);
}
else
{
fromPlayer.SendMessage(GlobalConstants.CurrentChatGroup, Lang.Get("Error loading schematic: {0}", error), EnumChatType.CommandError);
}
}
}
private void OnChangePlayerModeMessage(IPlayer fromPlayer, ChangePlayerModePacket plrmode)
{
IServerPlayer plr = fromPlayer as IServerPlayer;
bool freeMoveAllowed = plr.HasPrivilege(Privilege.freemove);
bool gameModeAllowed = plr.HasPrivilege(Privilege.gamemode);
bool pickRangeAllowed = plr.HasPrivilege(Privilege.pickingrange);
if (plrmode.axisLock != null)
{
fromPlayer.WorldData.FreeMovePlaneLock = (EnumFreeMovAxisLock)plrmode.axisLock;
}
if (plrmode.pickingRange != null && pickRangeAllowed)
{
fromPlayer.WorldData.PickingRange = (float)plrmode.pickingRange;
}
if (plrmode.fly != null)
{
fromPlayer.WorldData.FreeMove = (bool)plrmode.fly && freeMoveAllowed;
}
if (plrmode.noclip != null)
{
fromPlayer.WorldData.NoClip = (bool)plrmode.noclip && freeMoveAllowed;
}
}
private void OnRequestWorkSpaceMessage(IPlayer fromPlayer, RequestWorkSpacePacket networkMessage)
{
SendPlayerWorkSpace(fromPlayer.PlayerUID);
}
public WorldEditWorkspace GetWorkSpace(string playerUid)
{
WorldEditWorkspace space = null;
workspaces.TryGetValue(playerUid, out space);
return space;
}
public void SendPlayerWorkSpace(string playerUID)
{
serverChannel.SendPacket(workspaces[playerUID], (IServerPlayer)sapi.World.PlayerByUid(playerUID));
}
public void RegisterTool(string toolname, Type tool)
{
ToolRegistry.RegisterToolType(toolname, tool);
}
private void OnSwitchedGameMode(IServerPlayer player)
{
if (player.WorldData.CurrentGameMode != EnumGameMode.Creative)
{
WorldEditWorkspace workspace = GetOrCreateWorkSpace(player);
workspace.ToolsEnabled = false;
workspace.StartMarker = null;
workspace.EndMarker = null;
fromPlayer = player;
ResendBlockHighlights();
}
}
private void OnPlayerJoin(IPlayer player)
{
fromPlayer = player as IServerPlayer;
WorldEditWorkspace workspace = GetOrCreateWorkSpace(player);
IBlockAccessorRevertable revertableBlockAccess = sapi.World.GetBlockAccessorRevertable(true, true);
// Initialize all tools once to build up the workspace for the client gui tool options
foreach (var val in ToolRegistry.ToolTypes)
{
ToolRegistry.InstanceFromType(val.Key, workspace, revertableBlockAccess);
}
if (workspace.ToolsEnabled)
{
if (workspace.ToolInstance != null)
{
EnumHighlightBlocksMode mode = EnumHighlightBlocksMode.CenteredToSelectedBlock;
if (workspace.ToolOffsetMode == EnumToolOffsetMode.Attach) mode = EnumHighlightBlocksMode.AttachedToSelectedBlock;
sapi.World.HighlightBlocks(player, (int)EnumHighlightSlot.Brush, workspace.ToolInstance.GetBlockHighlights(this), workspace.ToolInstance.GetBlockHighlightColors(this), mode);
}
}
else
{
HighlightSelectedArea(workspace, player);
}
}
private void OnSave()
{
using (MemoryStream ms = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(ms);
writer.Write(workspaces.Count);
foreach (WorldEditWorkspace workspace in workspaces.Values)
{
workspace.ToBytes(writer);
}
sapi.WorldManager.SaveGame.StoreData("worldeditworkspaces", ms.ToArray());
}
}
private void OnLoad()
{
byte[] data = sapi.WorldManager.SaveGame.GetData("worldeditworkspaces");
if (data == null || data.Length == 0) return;
try
{
using (MemoryStream ms = new MemoryStream(data))
{
BinaryReader reader = new BinaryReader(ms);
int count = reader.ReadInt32();
while (count-- > 0)
{
IBlockAccessorRevertable revertableBlockAccess = sapi.World.GetBlockAccessorRevertable(true, true);
WorldEditWorkspace workspace = new WorldEditWorkspace(sapi.World, revertableBlockAccess);
workspace.FromBytes(reader);
if (workspace.PlayerUID == null)
{
continue;
}
workspaces[workspace.PlayerUID] = workspace;
}
}
}
catch (Exception)
{
sapi.Server.LogEvent("Exception thrown when trying to load worldedit workspaces. Will ignore.");
}
}
public bool CanUseWorldEdit(IPlayer player, bool showError = false)
{
if (player.WorldData.CurrentGameMode != EnumGameMode.Creative)
{
if (showError) Bad("Only available in creative mode.");
return false;
}
if (!((IServerPlayer)player).HasPrivilege("worldedit"))
{
if (showError) Bad("No privilege to use");
return false;
}
return true;
}
WorldEditWorkspace GetOrCreateWorkSpace(IPlayer player)
{
string playeruid = player.PlayerUID;
if (workspaces.ContainsKey(playeruid))
{
return workspaces[playeruid];
}
else
{
IBlockAccessorRevertable revertableBlockAccess = sapi.World.GetBlockAccessorRevertable(true, true);
workspaces[playeruid] = new WorldEditWorkspace(sapi.World, revertableBlockAccess);
workspaces[playeruid].PlayerUID = playeruid;
return workspaces[playeruid];
}
}
private void CmdEditServer(IServerPlayer player, int groupId, CmdArgs args)
{
this.fromPlayer = player;
this.groupId = groupId;
if (!CanUseWorldEdit(player, true)) return;
this.workspace = GetOrCreateWorkSpace(player);
BlockPos centerPos = player.Entity.Pos.AsBlockPos;
IPlayerInventoryManager plrInv = player.InventoryManager;
ItemStack stack = plrInv.ActiveHotbarSlot.Itemstack;
if (args.Length == 0)
{
Bad("No arguments supplied. Check the wiki for help, or did you mean to type .we?");
return;
}
string cmd = args.PopWord();
if ((cmd == "tr" || cmd == "tsx" || cmd == "tsy" || cmd == "tsz") && args.Length > 0)
{
double val = 0;
if (double.TryParse(args[0], NumberStyles.Any, GlobalConstants.DefaultCultureInfo, out val))
{
if (val > 50 && serverOverloadProtection)
{
Bad("Operation rejected. Server overload protection is on. Might kill the server or client to use such a large brush (max is 50).");
SendPlayerWorkSpace(fromPlayer.PlayerUID);
return;
}
}
}
switch (cmd)
{
case "impr":
int angle = 90;
if (args.Length > 0)
{
if (!int.TryParse(args[0], NumberStyles.Any, GlobalConstants.DefaultCultureInfo, out angle))
{
Bad("Invalid Angle (not a number)");
break;
}
}
if (angle < 0) angle += 360;
if (angle != 0 && angle != 90 && angle != 180 && angle != 270)
{
Bad("Invalid Angle, allowed values are -270, -180, -90, 0, 90, 180 and 270");
break;
}
workspace.ImportAngle = angle;
Good("Ok, set rotation to " + angle + " degrees");
break;
case "impflip":
workspace.ImportFlipped = !workspace.ImportFlipped;
Good("Ok, import data flip " + (workspace.ImportFlipped ? "on" : "off"));
break;
case "mcopy":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Please mark start and end position");
break;
}
workspace.clipboardBlockData = CopyArea(workspace.StartMarker, workspace.EndMarker);
Good(Lang.Get("{0} blocks and {1} entities copied", workspace.clipboardBlockData.BlockIds.Count, workspace.clipboardBlockData.EntitiesUnpacked.Count));
break;
case "mposcopy":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Please mark start and end position");
break;
}
BlockPos s = workspace.StartMarker;
BlockPos e = workspace.EndMarker;
serverChannel.SendPacket(new CopyToClipboardPacket() { Text = string.Format("/we mark {0} {1} {2} {3} {4} {5}", s.X, s.Y, s.Z, e.X, e.Y, e.Z) }, player);
break;
case "mpaste":
if (workspace.clipboardBlockData == null)
{
Bad("No copied block data to paste");
break;
}
PasteBlockData(workspace.clipboardBlockData, workspace.StartMarker, EnumOrigin.StartPos);
Good(workspace.clipboardBlockData.BlockIds.Count + " blocks pasted");
break;
case "cpinfo":
if (workspace.clipboardBlockData == null)
{
Bad("No schematic in the clipboard");
break;
}
workspace.clipboardBlockData.Init(workspace.revertableBlockAccess);
workspace.clipboardBlockData.LoadMetaInformationAndValidate(workspace.revertableBlockAccess, workspace.world, "(from clipboard)");
string sides = "";
for (int i = 0; i < workspace.clipboardBlockData.PathwayStarts.Length; i++)
{
if (sides.Length > 0) sides += ",";
sides += workspace.clipboardBlockData.PathwaySides[i].Code + " (" + workspace.clipboardBlockData.PathwayOffsets[i].Length + " blocks)";
}
if (sides.Length > 0) sides = "Found " + workspace.clipboardBlockData.PathwayStarts.Length + " pathways: " + sides;
Good("{0} blocks in clipboard. {1}", workspace.clipboardBlockData.BlockIds.Count, sides);
break;
case "block":
if (stack == null || stack.Class == EnumItemClass.Item)
{
Bad("Please put the desired block in your active hotbar slot");
return;
}
sapi.World.BlockAccessor.SetBlock(stack.Id, centerPos.DownCopy());
Good("Block placed");
break;
case "relight":
workspace.DoRelight = (bool)args.PopBool(true);
workspace.revertableBlockAccess.Relight = workspace.DoRelight;
Good("Block relighting now " + ((workspace.DoRelight) ? "on" : "off"));
break;
case "sovp":
if (args.Length > 0)
{
if (!fromPlayer.HasPrivilege(Privilege.controlserver))
{
Bad("controlserver privilege required to change server overload protection flag");
break;
}
serverOverloadProtection = (bool)args.PopBool(true);
}
Good("Server overload protection " + (serverOverloadProtection ? "on" : "off"));
break;
case "undo":
HandleHistoryChange(args, false);
break;
case "redo":
HandleHistoryChange(args, true);
break;
case "on":
Good("World edit tools now enabled");
workspace.ToolsEnabled = true;
ResendBlockHighlights();
break;
case "off":
Good("World edit tools now disabled");
workspace.ToolsEnabled = false;
ResendBlockHighlights();
break;
case "rebuildrainmap":
if (!player.HasPrivilege(Privilege.controlserver))
{
Bad("You lack the controlserver privilege to rebuild the rain map.");
return;
}
Good("Ok, rebuilding rain map on all loaded chunks, this may take some time and lag the server");
int rebuilt = RebuildRainMap();
Good("Done, rebuilding {0} map chunks", rebuilt);
break;
case "t":
string toolname = null;
string suppliedToolname = args.PopAll();
if (suppliedToolname.Length > 0)
{
int toolId;
if (int.TryParse(suppliedToolname, NumberStyles.Any, GlobalConstants.DefaultCultureInfo, out toolId))
{
if (toolId < 0)
{
Good("World edit tools now disabled");
workspace.ToolsEnabled = false;
ResendBlockHighlights();
return;
}
toolname = ToolRegistry.ToolTypes.GetKeyAtIndex(toolId);
}
else
{
foreach (string name in ToolRegistry.ToolTypes.Keys)
{
if (name.ToLowerInvariant().StartsWith(suppliedToolname.ToLowerInvariant()))
{
toolname = name;
break;
}
}
}
}
if (toolname == null)
{
Bad("No such tool '" + suppliedToolname + "' registered");
break;
}
workspace.SetTool(toolname);
Good(toolname + " tool selected");
workspace.ToolsEnabled = true;
ResendBlockHighlights();
SendPlayerWorkSpace(fromPlayer.PlayerUID);
break;
case "tom":
EnumToolOffsetMode mode = EnumToolOffsetMode.Center;
try
{
int index = 0;
if (args.Length > 0)
{
index = args[0].ToInt(0);
}
mode = (EnumToolOffsetMode)index;
}
catch (Exception) { }
workspace.ToolOffsetMode = mode;
Good("Set tool offset mode " + mode);
ResendBlockHighlights();
break;
case "range":
float pickingrange = GlobalConstants.DefaultPickingRange;
if (args.Length > 0)
{
pickingrange = args[0].ToFloat(GlobalConstants.DefaultPickingRange);
}
fromPlayer.WorldData.PickingRange = pickingrange;
fromPlayer.BroadcastPlayerData();
Good("Picking range " + pickingrange + " set");
break;
case "mex":
case "mexc":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Please mark start and end position");
break;
}
if (args.Length < 1)
{
Bad("Please provide a filename");
break;
}
ExportArea(args[0], workspace.StartMarker, workspace.EndMarker, cmd == "mexc" ? fromPlayer : null);
if (args.Length > 1 && args[1] == "c")
{
BlockPos st = workspace.StartMarker;
BlockPos en = workspace.EndMarker;
serverChannel.SendPacket(new CopyToClipboardPacket() { Text = string.Format("/we mark {0} {1} {2} {3} {4} {5}\n/we mex {6}", st.X, st.Y, st.Z, en.X, en.Y, en.Z, args[0]) }, player);
}
break;
case "mre":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Please mark start and end position");
break;
}
Good("Relighting marked area, this may lag the server for a while...");
sapi.WorldManager.FullRelight(workspace.StartMarker, workspace.EndMarker);
Good("Ok, relighting complete");
break;
case "mgencode":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Please mark start and end position");
break;
}
if (player.CurrentBlockSelection == null)
{
Bad("Please look at a block as well");
break;
}
GenMarkedMultiblockCode(player);
break;
case "imp":
if (workspace.StartMarker == null)
{
Bad("Please mark a start position");
break;
}
if (args.Length < 1)
{
Bad("Please provide a filename");
break;
}
EnumOrigin origin = EnumOrigin.StartPos;
if (args.Length > 1)
{
try
{
origin = (EnumOrigin)Enum.Parse(typeof(EnumOrigin), args[1]);
}
catch (Exception)
{
}
}
ImportArea(args[0], workspace.StartMarker, origin);
break;
case "impres":
if (args.Length == 0)
{
Good("Import item/block resolving currently " + (!sapi.ObjectCache.ContainsKey("donotResolveImports") || (bool)sapi.ObjectCache["donotResolveImports"] == false ? "on" : "off"));
}
else
{
bool doreplace = (bool)args.PopBool(ReplaceMetaBlocks);
sapi.ObjectCache["donotResolveImports"] = !doreplace;
Good("Import item/block resolving now globally " + (doreplace ? "on" : "off"));
}
break;
case "blu":
BlockLineup(centerPos, args);
Good("Block lineup created");
break;
// Mark start
case "ms":
SetStartPos(centerPos);
break;
// Mark end
case "me":
SetEndPos(centerPos);
break;
case "mark":
workspace.StartMarker = args.PopVec3i(null)?.AsBlockPos;
workspace.EndMarker = args.PopVec3i(null)?.AsBlockPos;
Good("Start and end position marked");
EnsureInsideMap(workspace.EndMarker);
HighlightSelectedArea(workspace, fromPlayer);
break;
case "gn":
ModifyMarker(BlockFacing.NORTH, args);
break;
case "ge":
ModifyMarker(BlockFacing.EAST, args);
break;
case "gs":
ModifyMarker(BlockFacing.SOUTH, args);
break;
case "gw":
ModifyMarker(BlockFacing.WEST, args);
break;
case "gu":
ModifyMarker(BlockFacing.UP, args);
break;
case "gd":
ModifyMarker(BlockFacing.DOWN, args);
break;
case "mr":
HandleRotateCommand(args.PopInt(), args.PopWord());
break;
case "mmirn":
HandleMirrorCommand(BlockFacing.NORTH, args);
break;
case "mmire":
HandleMirrorCommand(BlockFacing.EAST, args);
break;
case "mmirs":
HandleMirrorCommand(BlockFacing.SOUTH, args);
break;
case "mmirw":
HandleMirrorCommand(BlockFacing.WEST, args);
break;
case "mmiru":
HandleMirrorCommand(BlockFacing.UP, args);
break;
case "mmird":
HandleMirrorCommand(BlockFacing.DOWN, args);
break;
case "mrepn":
HandleRepeatCommand(BlockFacing.NORTH, args);
break;
case "mrepe":
HandleRepeatCommand(BlockFacing.EAST, args);
break;
case "mreps":
HandleRepeatCommand(BlockFacing.SOUTH, args);
break;
case "mrepw":
HandleRepeatCommand(BlockFacing.WEST, args);
break;
case "mrepu":
HandleRepeatCommand(BlockFacing.UP, args);
break;
case "mrepd":
HandleRepeatCommand(BlockFacing.DOWN, args);
break;
case "mmu":
HandleMoveCommand(BlockFacing.UP, args);
break;
case "mmd":
HandleMoveCommand(BlockFacing.DOWN, args);
break;
case "mmn":
HandleMoveCommand(BlockFacing.NORTH, args);
break;
case "mme":
HandleMoveCommand(BlockFacing.EAST, args);
break;
case "mms":
HandleMoveCommand(BlockFacing.SOUTH, args);
break;
case "mmw":
HandleMoveCommand(BlockFacing.WEST, args);
break;
case "mmby":
HandleMoveCommand(null, args);
break;
case "smu":
HandleShiftCommand(BlockFacing.UP, args);
break;
case "smd":
HandleShiftCommand(BlockFacing.DOWN, args);
break;
case "smn":
HandleShiftCommand(BlockFacing.NORTH, args);
break;
case "sme":
HandleShiftCommand(BlockFacing.EAST, args);
break;
case "sms":
HandleShiftCommand(BlockFacing.SOUTH, args);
break;
case "smw":
HandleShiftCommand(BlockFacing.WEST, args);
break;
case "smby":
HandleShiftCommand(null, args);
break;
// Marked clear
case "mc":
case "clear":
workspace.StartMarker = null;
workspace.EndMarker = null;
Good("Marked positions cleared");
ResendBlockHighlights();
break;
// Marked info
case "minfo":
int sizeX = Math.Abs(workspace.StartMarker.X - workspace.EndMarker.X);
int sizeY = Math.Abs(workspace.StartMarker.Y - workspace.EndMarker.Y);
int sizeZ = Math.Abs(workspace.StartMarker.Z - workspace.EndMarker.Z);
Good(string.Format("Marked area is a cuboid of size {0}x{1}x{2} or a total of {3:n0} blocks", sizeX, sizeY, sizeZ, ((long)sizeX * sizeY * sizeZ)));
break;
// Fill marked
case "mfill":
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Start marker or end marker not set");
return;
}
if (stack == null || stack.Class == EnumItemClass.Item)
{
Bad("Please put the desired block in your active hotbar slot");
return;
}
int filled = FillArea(stack, workspace.StartMarker, workspace.EndMarker);
Good(filled + " marked blocks placed");
break;
case "mclear":
{
Bad("No such function, did you mean mdelete?");
return;
}
// Clear marked
case "mdelete":
{
if (workspace.StartMarker == null || workspace.EndMarker == null)
{
Bad("Start marker or end marker not set");
return;
}
int cleared = FillArea(null, workspace.StartMarker, workspace.EndMarker);
Good(cleared + " marked blocks removed");
}
break;
// Clear area
case "delete":
{
if (args.Length < 1)
{
Bad("Missing size param");
return;
}
int size = 0;
if (!int.TryParse(args[0], NumberStyles.Any, GlobalConstants.DefaultCultureInfo, out size))
{
Bad("Invalide size param");
return;
}
int height = 20;
if (args.Length > 1)
{
int.TryParse(args[1], NumberStyles.Any, GlobalConstants.DefaultCultureInfo, out height);
}
int cleared = FillArea(null, centerPos.AddCopy(-size, 0, -size), centerPos.AddCopy(size, height, size));
Good(cleared + " Blocks removed");
}
break;
default:
args.PushSingle(cmd);
if (workspace.ToolInstance == null || !workspace.ToolInstance.OnWorldEditCommand(this, args))
{
Bad("No such function " + cmd + ". Maybe wrong tool selected?");
}
break;
}
}
private void GenMarkedMultiblockCode(IServerPlayer player)
{
BlockPos centerPos = player.CurrentBlockSelection.Position;
OrderedDictionary<int, int> blocks = new OrderedDictionary<int, int>();
List<Vec4i> offsets = new List<Vec4i>();
MultiblockStructure ms = new MultiblockStructure();
sapi.World.BlockAccessor.WalkBlocks(workspace.StartMarker, workspace.EndMarker, (block, pos) =>
{
if (block.Id == 0) return;
int blockNum = ms.GetOrCreateBlockNumber(block);
BlockOffsetAndNumber offset = new BlockOffsetAndNumber(pos.X - centerPos.X, pos.Y - centerPos.Y, pos.Z - centerPos.Z, blockNum);
ms.Offsets.Add(offset);
}, true);
StringBuilder sb = new StringBuilder();
sb.AppendLine("multiblockStructure: {");
sb.AppendLine("\tblockNumbers: {");
foreach (var val in ms.BlockNumbers)
{
sb.AppendLine(string.Format("\t\t\"{0}\": {1},", val.Key.ToShortString(), val.Value));
}
sb.AppendLine("\t},");
sb.AppendLine("\toffsets: [");
foreach (var val in ms.Offsets)
{
sb.AppendLine(string.Format("\t\t{{ x: {0}, y: {1}, z: {2}, w: {3} }},", val.X, val.Y, val.Z, val.W));
}
sb.AppendLine("\t]");
sb.AppendLine("}");
sapi.World.Logger.Notification("Multiblockstructure centered around {0}:\n{1}", centerPos, sb.ToString());
Good("Json code written to server-main.txt");
}
public void SetStartPos(BlockPos pos)
{
workspace.StartMarker = pos;
Good("Start position " + workspace.StartMarker + " marked");
EnsureInsideMap(workspace.StartMarker);
HighlightSelectedArea(workspace, fromPlayer);
}