-
Notifications
You must be signed in to change notification settings - Fork 8
/
CpeCommands.cs
2484 lines (2295 loc) · 121 KB
/
CpeCommands.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
// ProCraft Copyright 2014-2016 Joseph Beauvais <123DMWM@gmail.com>
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace fCraft {
/// <summary> Most commands for CPE extensions are here. </summary>
static class CpeCommands {
internal static void Init() {
CommandManager.RegisterCommand(CdAFKModel);
CommandManager.RegisterCommand(CdChangeModel);
CommandManager.RegisterCommand(CdChangeSkin);
CommandManager.RegisterCommand(Cdclickdistance);
CommandManager.RegisterCommand(CdCustomColors);
CommandManager.RegisterCommand(CdEntity);
CommandManager.RegisterCommand(CdEnv);
CommandManager.RegisterCommand(CdEnvPreset);
CommandManager.RegisterCommand(CdGlobalBlock);
CommandManager.RegisterCommand(CdLevelBlock);
CommandManager.RegisterCommand(CdHackControl);
CommandManager.RegisterCommand(CdListClients);
CommandManager.RegisterCommand(CdMRD);
CommandManager.RegisterCommand(Cdtex);
CommandManager.RegisterCommand(CdtextHotKey);
CommandManager.RegisterCommand(CdZoneShow);
CommandManager.RegisterCommand(CdEntityRot);
}
internal static string[] validEntities = { "chibi", "chicken", "creeper", "giant", "humanoid",
"human", "pig", "sheep", "skeleton", "spider", "zombie"
};
/// <summary> Ensures that the hex color has the correct length (3 or 6 characters)
/// and character set (alphanumeric chars allowed). </summary>
public static bool IsValidHex(string hex) {
if (hex == null) throw new ArgumentNullException("hex");
if (hex.StartsWith("#")) hex = hex.Remove(0, 1);
if (hex.Length != 3 && hex.Length != 6) return false;
for (int i = 0; i < hex.Length; i++) {
char ch = hex[i];
if (ch < '0' || ch > '9'
&& ch < 'A' || ch > 'F'
&& ch < 'a' || ch > 'f') return false;
}
return true;
}
static Player FindPlayer(Player player, CommandReader cmd) {
string name = cmd.Next();
if (name == null) name = player.Name;
return Server.FindPlayerOrPrintMatches(player, name, SearchOptions.IncludeSelf);
}
#region AddEntity
static readonly CommandDescriptor CdEntity = new CommandDescriptor {
Name = "Entity",
Aliases = new[] { "AddEntity", "AddEnt", "Ent" },
Permissions = new[] { Permission.BringAll },
Category = CommandCategory.CPE | CommandCategory.World | CommandCategory.New,
Usage = "/ent <create / remove / removeAll / model / list / bring / skin>",
Help = "Commands for manipulating entities. For help and usage for the individual options, use /help ent <option>.",
HelpSections = new Dictionary<string, string>{
{ "create", "&H/Ent create <entity name> <model> <skin>&N&S" +
"Creates a new entity with the given name. Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name." },
{ "remove", "&H/Ent remove <entity name> <world>&N&S" +
"Removes the given entity." },
{ "removeall", "&H/Ent removeAll&N&S" +
"Removes all entities from the world."},
{ "model", "&H/Ent model <entity name> <model>&N&S" +
"Changes the model of an entity to the given model. Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name."},
{ "list", "&H/Ent list <world>&N&S" +
"Prints out a list of all the entites on the server."},
{ "bring", "&H/Ent bring <entity name>&N&S" +
"Brings the given entity to you."},
{ "skin", "&H/Ent skin <entity name> <skin url or name>&N&S" +
"Changes the skin of a bot."}
},
Handler = EntityHandler,
};
private static void EntityHandler(Player player, CommandReader cmd) {
string option = cmd.Next();
if (string.IsNullOrEmpty(option)) {
CdEntity.PrintUsage(player);
return;
}
if (option.CaselessEquals("reload") && player.Info.Rank == RankManager.HighestRank) {
Entity.ReloadAll();
player.Message("Reloaded Entities from file");
return;
}
if (option.CaselessEquals("list")) {
string search = cmd.Next() ?? player.World.Name;
World world = WorldManager.FindWorldOrPrintMatches(player, search);
if (world != null) {
player.Message("Entities on &f{0}&S: ", world.Name);
player.Message(" &F" + Entity.AllIn(world).JoinToString("&S, &F", n => n.Name));
}
return;
}
if (option.CaselessEquals("removeall")) {
string search = cmd.Next() ?? player.World.Name;
World world = WorldManager.FindWorldOrPrintMatches(player, search);
if (cmd.IsConfirmed) {
Entity.RemoveAllIn(world);
player.Message("All entities on &f{0}&S removed.", player.World.Name);
} else {
player.Confirm(cmd, "This will remove all the entites on {0}, are you sure?", player.World.Name);
}
return;
}
//finally away from the special cases
string entityName = cmd.Next();
if (string.IsNullOrEmpty(entityName)) {
CdEntity.PrintUsage(player);
return;
}
Entity entity = new Entity();
if (option != "create" && option != "add") {
entity = Entity.Find(player.World, entityName);
if (entity == null) {
player.Message(
"Could not find {0}! Please make sure you spelled the entities name correctly. To view all the entities, type /ent list.",
entityName);
return;
}
}
switch (option.ToLower()) {
case "create":
case "add":
string requestedModel = "humanoid";
if (cmd.HasNext) {
requestedModel = cmd.Next().ToLower();
requestedModel = ParseModel(player, requestedModel);
if (requestedModel == null) {
player.Message(
"That wasn't a valid entity model! Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name.");
return;
}
}
//if a botname has already been chosen, ask player for a new name
if (Entity.Exists(player.World, entityName)) {
player.Message("An entity with that name already exists! To view all entities, type /ent list.");
return;
}
string skinString1 = (cmd.Next() ?? entityName);
if (skinString1 != null) skinString1 = ParseSkin(skinString1);
sbyte newEntityId = Entity.NextFreeID(player.World.Name);
if (newEntityId == Packet.SelfId) {
player.Message("No more free ids left! Remove an entity first.");
} else {
entity = Entity.Create(entityName, skinString1, requestedModel, player.World, player.Position, newEntityId);
player.Message("Successfully created entity {0}&S with id:{1} and skin {2}.", entity.Name, entity.ID, entity.Skin, entity.Name);
}
break;
case "remove":
player.Message("{0} was removed from {1}", entity.Name, player.World.Name);
Entity.Remove(entity);
break;
case "model":
if (cmd.HasNext) {
string model = cmd.Next().ToLower();
if (string.IsNullOrEmpty(model)) {
player.Message(
"Usage is /Ent model <bot> <model>. Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name.");
break;
}
model = ParseModel(player, model);
if (model == null) {
player.Message(
"That wasn't a valid entity model! Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name.");
break;
}
player.Message("Changed entity model to {0}.", model);
entity.ChangeModel(model);
} else {
player.Message(
"Usage is /Ent model <bot> <model>. Valid models are chibi, chicken, creeper, giant, human, pig, sheep, skeleton, spider, zombie, or any block ID/Name.");
}
break;
case "bring":
entity.TeleportTo(player.Position);
break;
case "tp":
case "teleport":
World targetWorld = entity.WorldIn();
if (targetWorld == player.World) {
if (player.World != null) {
player.LastWorld = player.World;
player.LastPosition = player.Position;
}
player.TeleportTo(entity.GetPos());
} else {
if (targetWorld.Name.StartsWith("PW_") &&
!targetWorld.AccessSecurity.ExceptionList.Included.Contains(player.Info)) {
player.Message(
"You cannot join due to that Bot being in a personal world that you cannot access.");
break;
}
switch (targetWorld.AccessSecurity.CheckDetailed(player.Info)) {
case SecurityCheckResult.Allowed:
case SecurityCheckResult.WhiteListed:
if (player.Info.Rank.Name == "Banned") {
player.Message("&CYou can not change worlds while banned.");
player.Message("Cannot teleport to {0}&S.", entity.Name,
targetWorld.ClassyName, targetWorld.AccessSecurity.MinRank.ClassyName);
break;
}
if (targetWorld.IsFull) {
player.Message("Cannot teleport to {0}&S because world {1}&S is full.",
entity.Name, targetWorld.ClassyName);
player.Message("Cannot teleport to {0}&S.", entity.Name,
targetWorld.ClassyName, targetWorld.AccessSecurity.MinRank.ClassyName);
break;
}
player.StopSpectating();
player.JoinWorld(targetWorld, WorldChangeReason.Tp, entity.GetPos());
break;
case SecurityCheckResult.BlackListed:
player.Message("Cannot teleport to {0}&S because you are blacklisted on world {1}",
entity.Name, targetWorld.ClassyName);
break;
case SecurityCheckResult.RankTooLow:
if (player.Info.Rank.Name == "Banned") {
player.Message("&CYou can not change worlds while banned.");
player.Message("Cannot teleport to {0}&S.", entity.Name,
targetWorld.ClassyName, targetWorld.AccessSecurity.MinRank.ClassyName);
break;
}
if (targetWorld.IsFull) {
if (targetWorld.IsFull) {
player.Message("Cannot teleport to {0}&S because world {1}&S is full.",
entity.Name, targetWorld.ClassyName);
player.Message("Cannot teleport to {0}&S.", entity.Name,
targetWorld.ClassyName, targetWorld.AccessSecurity.MinRank.ClassyName);
break;
}
player.StopSpectating();
player.JoinWorld(targetWorld, WorldChangeReason.Tp, entity.GetPos());
break;
}
player.Message("Cannot teleport to {0}&S because world {1}&S requires {2}+&S to join.",
entity.Name, targetWorld.ClassyName,
targetWorld.AccessSecurity.MinRank.ClassyName);
break;
}
}
break;
case "skin":
string skinString3 = cmd.Next();
if (string.IsNullOrEmpty(skinString3)) {
player.Message("Please specify a skin URL/Name.");
break;
} else {
skinString3 = ParseSkin(skinString3);
}
player.Message("Changed entity skin to {0}.", skinString3 ?? entity.Name);
entity.ChangeSkin(skinString3);
break;
default:
CdEntity.PrintUsage(player);
break;
}
}
#endregion
#region ChangeModel
static readonly CommandDescriptor CdChangeModel = new CommandDescriptor {
Name = "Model",
Aliases = new[] { "ChangeModel", "cm" },
Category = CommandCategory.CPE | CommandCategory.Moderation | CommandCategory.New,
Permissions = new[] { Permission.ReadStaffChat },
Usage = "/Model [Player] [Model]",
IsConsoleSafe = true,
Help = "Change the Model or Skin of [Player]!&N" +
"Valid models: &S[Any Block Name or ID#], Chibi, Chicken, Creeper, Giant, Humanoid, Pig, Sheep, Skeleton, Spider, Zombie",
Handler = ModelHandler
};
private static void ModelHandler(Player player, CommandReader cmd) {
SetModel(player, cmd, "", p => p.Info.Model, (p, value) => p.Info.Model = value);
}
static readonly CommandDescriptor CdAFKModel = new CommandDescriptor {
Name = "AFKModel",
Category = CommandCategory.New,
Permissions = new[] { Permission.Chat },
Usage = "/AFKModel [Player] [Model]",
IsConsoleSafe = true,
Help = "Changes the model of a player when they are AFK.&N" +
"Valid models: &S [Any Block Name or ID#], Chibi, Chicken, Creeper, Giant, Humanoid, Pig, Sheep, Skeleton, Spider, Zombie!",
Handler = AFKModelHandler
};
private static void AFKModelHandler(Player player, CommandReader cmd) {
SetModel(player, cmd, "AFK ",
p => p.AFKModel,
(p, value) => p.AFKModel = value);
}
static void SetModel(Player player, CommandReader cmd, string prefix,
Func<Player, string> getter, Action<Player, string> setter) {
Player target = FindPlayer(player, cmd);
if (target == null) return;
if (!player.IsStaff && target != player) {
Rank staffRank = RankManager.GetMinRankWithAnyPermission(Permission.ReadStaffChat);
if (staffRank != null) {
player.Message("You must be {0}&S+ to change another player's {1}Model",
staffRank.ClassyName, prefix);
} else {
player.Message("No ranks have the ReadStaffChat permission," +
"so no one can change other player's {0}Model, yell at the owner.", prefix);
}
return;
}
if (target.Info.Rank.Index < player.Info.Rank.Index) {
player.Message("Cannot change the {0}Model of someone higher rank than you.", prefix); return;
}
string model = cmd.Next();
if (string.IsNullOrEmpty(model)) {
player.Message("Current {2}Model for {0}: &f{1}", target.Name, getter(target), prefix);
return;
}
model = ParseModel(player, model);
if (model == null) {
player.Message("Model not valid, see &H/Help {0}Model&S.", prefix.TrimEnd());
return;
}
if (getter(target).CaselessEquals(model)) {
player.Message("&f{0}&S's {0}model is already set to &f{1}", target.Name, model, prefix);
return;
}
target.Message("&f{0}&SChanged your {3}model from &f{1} &Sto &f{2}",
(target == player ? "" : player.Name + " "), getter(target), model, prefix);
if (target != player) {
player.Message("Changed {3}model of &f{0} &Sfrom &f{1} &Sto &f{2}",
target.Name, getter(target), model, prefix);
}
target.oldMob = target.Info.Model;
target.oldafkMob = target.afkMob;
setter(target, model);
}
internal static string ParseModel(Player player, string model) {
model = model ?? "humanoid";
float scale = 0.0f;
string scalestr = "";
int sepIndex = model.IndexOf('|');
if (sepIndex >= 0) {
scalestr = model.Substring(sepIndex + 1);
model = model.Substring(0, sepIndex);
}
if (float.TryParse(scalestr, out scale)) {
if (scale < 0.25f) scale = 0.25f;
if (scale > 3f) scale = 3f;
}
byte blockId;
Block block;
if (byte.TryParse(model, out blockId)) {
} else if (Map.GetBlockByName(model, false, out block)) {
model = ((byte)block).ToString();
}
return scale == 0 ? model : model + "|" + scale;
}
static readonly CommandDescriptor CdChangeSkin = new CommandDescriptor {
Name = "Skin",
Aliases = new[] { "ChageSkin", "chs" },
Category = CommandCategory.New | CommandCategory.Moderation,
Permissions = new[] { Permission.EditPlayerDB },
Usage = "/Skin [Player] [SkinName]",
IsConsoleSafe = true,
Help = "Change the Skin of [Player]!",
Handler = SkinHandler
};
private static void SkinHandler(Player player, CommandReader cmd) {
if (!cmd.HasNext) { CdChangeSkin.PrintUsage(player); return; }
Player target = FindPlayer(player, cmd);
if (target == null) return;
string skin = cmd.Next();
if (skin == null) { CdChangeSkin.PrintUsage(player); return; }
skin = ParseSkin(skin);
if (target.Info.skinName == skin) {
player.Message("&f{0}&S's skin is already set to &f{1}", target.Name, skin);
return;
}
target.Message("&f{0}&Shanged your skin from &f{1} &Sto &f{2}",
(target == player ? "&SC" : player.Name + " &Sc"), target.oldskinName, skin);
if (player != target) {
player.Message("Changed skin of &f{0} &Sto &f{1}", target.Name, skin);
}
target.oldskinName = target.Info.skinName;
target.Info.skinName = skin;
}
static string ParseSkin(string skin) {
if (skin.StartsWith("--")) {
return "http://minecraft.net/skin/" + skin.Substring(2) + ".png";
}
if (skin.StartsWith("-+")) {
return "http://skins.minecraft.net/MinecraftSkins/" + skin.Substring(2) + ".png";
}
if (skin.StartsWith("++")) {
return "http://i.imgur.com/" + skin.Substring(2) + ".png";
}
return skin;
}
#endregion
#region ClickDistance
static readonly CommandDescriptor Cdclickdistance = new CommandDescriptor {
Name = "ReachDistance",
Aliases = new[] { "Reach", "rd" },
Permissions = new[] { Permission.DrawAdvanced },
IsConsoleSafe = true,
Category = CommandCategory.CPE | CommandCategory.World | CommandCategory.New,
Help = "Changes player reach distance. Every 32 is one block. Default: 160",
Usage = "/reach [Player] [distance or reset]",
Handler = ClickDistanceHandler
};
static void ClickDistanceHandler(Player player, CommandReader cmd) {
Player target = FindPlayer(player, cmd);
if (target == null) return;
if (!player.IsStaff && target != player) {
Rank staffRank = RankManager.GetMinRankWithAnyPermission(Permission.ReadStaffChat);
if (staffRank != null) {
player.Message("You must be {0}&S+ to change another players reach distance", staffRank.ClassyName);
} else {
player.Message("No ranks have the ReadStaffChat permission so no one can change other players reachdistance, yell at the owner.");
}
return;
}
if (target.Info.Rank.Index < player.Info.Rank.Index) {
player.Message("Cannot change the Reach Distance of someone higher rank than you.");
return;
}
string rawDist = cmd.Next();
if (string.IsNullOrEmpty(rawDist)) {
if (target == player) {
player.Message("Your current ReachDistance: {0} blocks [Units: {1}]", player.Info.ReachDistance / 32, player.Info.ReachDistance);
} else {
player.Message("Current ReachDistance for {2}: {0} blocks [Units: {1}]", target.Info.ReachDistance / 32, target.Info.ReachDistance, target.Name);
}
return;
}
short distance;
if (!short.TryParse(rawDist, out distance)) {
if (rawDist != "reset") {
player.Message("Please try something inbetween 0 and 32767");
return;
} else {
distance = 160;
}
}
if (distance < 0 || distance > 32767) {
player.Message("Reach distance must be between 0 and 32767");
return;
}
if (distance == target.Info.ReachDistance) {
if (player != target) {
player.Message("{0}'s reach distance is already set to {1}", target.ClassyName, target.Info.ReachDistance);
} else {
player.Message("Your reach distance is already set to {0}", target.Info.ReachDistance);
}
return;
}
if (player != target) {
if (target.Supports(CpeExt.ClickDistance)) {
target.Message("{0} set your reach distance from {1} to {2} blocks [Units: {3}]",
player.Name, target.Info.ReachDistance / 32, distance / 32, distance);
player.Message("Set reach distance for {0} from {1} to {2} blocks [Units: {3}]",
target.Name, target.Info.ReachDistance / 32, distance / 32, distance);
target.Info.ReachDistance = distance;
target.Send(Packet.MakeSetClickDistance(distance));
} else {
player.Message("This player does not support ReachDistance packet");
}
} else {
if (player.Supports(CpeExt.ClickDistance)) {
player.Message("Set own reach distance from {0} to {1} blocks [Units: {2}]",
player.Info.ReachDistance / 32, distance / 32, distance);
player.Info.ReachDistance = distance;
player.Send(Packet.MakeSetClickDistance(distance));
} else {
player.Message("You don't support ReachDistance packet");
}
}
}
#endregion
#region CustomColors
static readonly CommandDescriptor CdCustomColors = new CommandDescriptor {
Name = "CustomColors",
Aliases = new[] { "ccols" },
Category = CommandCategory.CPE | CommandCategory.Chat | CommandCategory.New,
Permissions = new[] { Permission.Chat },
Usage = "/ccols [type] [args]",
IsConsoleSafe = true,
Help = "&SModifies the custom colors, or prints information about them.&N" +
"&STypes are: add, free, list, remove&N" +
"&SSee &H/help ccols <type>&S for details about each type.",
HelpSections = new Dictionary<string, string>{
{ "add", "&H/ccols add [code] [name] [fallback] [hex]&N" +
"&Scode is in ASCII. You cannot replace the standard color codes.&N" +
"&Sfallback is a standard color code, shown to non-supporting clients.&N" },
{ "free", "&H/ccols free&N" +
"&SPrints a list of free/unused available color codes." },
{ "list", "&H/ccols list [offset]&N" +
"&SPrints a list of the codes, names, and fallback codes of the custom colors. " },
{ "remove", "&H/ccols remove [code]&N" +
"&SRemoves the custom color which has the given color code." }
},
Handler = CustomColorsHandler,
};
static void CustomColorsHandler(Player p, CommandReader cmd) {
string type = cmd.Next();
if (type == null) { CdCustomColors.PrintUsage(p); return; }
type = type.ToLower();
if (type == "add" || type == "create" || type == "new") {
AddCustomColorsHandler(p, cmd);
} else if (type == "delete" || type == "remove") {
RemoveCustomColorsHandler(p, cmd);
} else if (type == "list") {
ListCustomColorsHandler(p, cmd);
} else if (type == "free" || type == "available") {
FreeCustomColorsHandler(p);
} else {
CdCustomColors.PrintUsage(p);
}
}
static void AddCustomColorsHandler(Player p, CommandReader cmd) {
if (cmd.CountRemaining < 4) { p.Message("Usage: &H/ccols add [code] [name] [fallback] [hex]"); return; }
if (!p.Can(Permission.DefineCustomBlocks)) {
p.MessageNoAccess(Permission.DefineCustomBlocks); return;
}
string rawCode = cmd.Next();
if (rawCode.Length > 1) {
p.Message("Color code must only be one character long."); return;
}
char code = rawCode[0];
if (Color.IsStandardColorCode(code)) {
p.Message(code + " is a standard color code, and thus cannot be replaced."); return;
}
if (code <= ' ' || code > '~' || code == '%' || code == '&' || code == '\\' || code == '"' || code == '@') {
p.Message(code + " must be a standard ASCII character.");
p.Message("It also cannot be a space, percentage, ampersand, backslash, at symbol, or double quotes.");
return;
}
if (Color.IsColorCode(code)) {
p.Message("There is already a custom or server defined color with the code " + code +
", you must either use a different code or use \"&H/ccols remove " + code + "&S\"");
return;
}
string name = cmd.Next();
if (Color.Parse(name) != null) {
p.Message("There is already an existing standard or " +
"custom color with the name \"" + name + "\"."); return;
}
string rawFallback = cmd.Next();
if (rawFallback.Length > 1) {
p.Message("Fallback color code must only be one character long."); return;
}
char fallback = rawFallback[0];
if (!Color.IsStandardColorCode(fallback)) {
p.Message(fallback + " must be a standard color code."); return;
}
string hex = cmd.Next();
if (!IsValidHex(hex)) {
p.Message("\"#{0}\" is not a valid hex color.", hex.Replace("#", "")); return;
}
CustomColor col = Color.ParseHex(hex);
col.Code = code; col.Fallback = fallback; col.Name = name;
Color.AddExtColor(col);
p.Message("Successfully added a custom color. &{0} %{0} {1}", col.Code, col.Name.ToLower().UppercaseFirst());
}
static void RemoveCustomColorsHandler(Player p, CommandReader cmd) {
string fullCode = cmd.Next();
if (fullCode == null) { p.Message("Usage: &H/ccols remove [code]"); return; }
if (!p.Can(Permission.DefineCustomBlocks)) {
p.MessageNoAccess(Permission.DefineCustomBlocks);
return;
}
char code = fullCode[0];
if (Color.IsStandardColorCode(code)) {
p.Message(code + " is a standard color, and thus cannot be removed."); return;
}
if ((int)code >= 256 || Color.ExtColors[code].Undefined) {
p.Message("There is no custom color with the code " + code + ".");
p.Message("Use \"&H/ccols list\" &Sto see a list of custom colors.");
return;
}
Color.RemoveExtColor(code);
p.Message("Successfully removed a custom color. {0}", code);
}
static void ListCustomColorsHandler(Player p, CommandReader cmd) {
int offset = 0, index = 0, count = 0;
cmd.NextInt(out offset);
CustomColor[] cols = Color.ExtColors;
for (int i = 0; i < cols.Length; i++) {
CustomColor col = cols[i];
if (col.Undefined) continue;
if (index >= offset) {
count++;
const string format = "{0} - %{1} displays as &{1}{2}&S, and falls back to {3}.";
p.Message(format, col.Name, col.Code, Hex(col), col.Fallback);
if (count >= 8) {
const string helpFormat = "To see the next set of custom colors, type &H/ccols list {0}";
p.Message(helpFormat, offset + 8);
return;
}
}
index++;
}
}
static void FreeCustomColorsHandler(Player p) {
StringBuilder codes = new StringBuilder();
for (char c = '!'; c <= '~'; c++) {
if (IsValidCode(c)) codes.Append(c).Append(' ');
}
p.Message("Free codes: &f" + codes.ToString());
}
static bool IsValidCode(char code) {
if (code == '%' || code == '&' || code == '\\' || code == '"' || code == '@')
return false;
return !Color.IsColorCode(code);
}
static string Hex(CustomColor c) {
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
#endregion
#region Env
static readonly CommandDescriptor CdEnv = new CommandDescriptor {
Name = "Env",
Category = CommandCategory.CPE | CommandCategory.World | CommandCategory.New,
Permissions = new[] { Permission.ManageWorlds },
Help = "Prints or changes environmental variables for a world.&N" +
"Variables are: border, clouds, edge, fog, level, cloudsheight, shadow, sky, sunlight, " +
"texture, weather, maxfog, cloudspeed, weatherspeed, weatherfade.&N" +
"See &H/Help env <Variable>&S for details about a variable.&N" +
"Type &H/Env <WorldName> normal&S to reset everything.",
HelpSections = new Dictionary<string, string>{
{ "normal", "&H/Env <WorldName> normal&N&S" +
"Resets all environment settings to their defaults for the given world." },
{ "clouds", "&H/Env <WorldName> clouds <Color>&N&S" +
"Sets color of the clouds." +
"&NUse \"normal\" instead of color to reset." },
{ "fog", "&H/Env <WorldName> fog <Color>&N&S" +
"Sets color of the fog. Sky color blends with fog color in the distance. " +
"&NUse \"normal\" instead of color to reset." },
{ "shadow", "&H/Env <WorldName> shadow <Color>&N&S" +
"Sets color of the shadowed areas. " +
"&NUse \"normal\" instead of color to reset." },
{ "sunlight", "&H/Env <WorldName> sunlight <Color>&N&S" +
"Sets color of the lighted areas. " +
"&NUse \"normal\" instead of color to reset." },
{ "sky", "&H/Env <WorldName> sky <Color>&N&S" +
"Sets color of the sky. Sky color blends with fog color in the distance. " +
"&NUse \"normal\" instead of color to reset." },
{ "level", "&H/Env <WorldName> level <#>&N&S" +
"Sets height of the map edges/water level, in terms of blocks from the bottom of the map. " +
"&NUse \"normal\" instead of a number to reset to default (middle of the map)." },
{ "sideoffset", "&H/Env <WorldName> sideoffset <#>&N&S" +
"Sets height of the map sides/bedrock level, in terms of offset vertically from map edges height. " +
"&NUse \"normal\" instead of a number to reset to default (-2)." },
{ "cloudsheight","&H/Env <WorldName> cloudsheight <#>&N&S" +
"Sets height of the clouds, in terms of blocks from the bottom of the map. " +
"&NUse \"normal\" instead of a number to reset to default (map height + 2)." },
{ "edge", "&H/Env <WorldName> edge <BlockType>&N&S" +
"Changes the type of block that's visible beyond the map boundaries. "+
"&NUse \"normal\" instead of a number to reset to default (water)." },
{ "border", "&H/Env <WorldName> border <BlockType>&N&S" +
"Changes the type of block that's visible on sides the map boundaries. "+
"&NUse \"normal\" instead of a number to reset to default (bedrock)." },
{ "texture", "&H/Env <WorldName> texture <Texture .PNG Url>&N&S" +
"Changes the texture for all visible blocks on a map. "+
"&NUse \"normal\" instead of a web link to reset to default (" + Server.DefaultTerrain + ")" },
{ "weather", "&H/Env <WorldName> weather <0,1,2/sun,rain,snow>&N&S" +
"Changes the weather on a specified map. "+
"&NUse \"normal\" instead to use default (0/sun)" },
{ "maxfog", "&H/Env <WorldName> maxfog <#>&N&S" +
"Sets the maximum distance clients can see around them. " +
"&NUse \"normal\" instead of a number to reset to default (0)." },
{ "cloudspeed", "&H/Env <WorldName> cloudspeed <#>&N&S" +
"Sets how fast clouds travel across the sky, relative to normal speed. Can be negative." +
"&NUse \"normal\" instead of a number to reset to default (0)." },
{ "weatherspeed","&H/Env <WorldName> weatherspeed <#>&N&S" +
"Sets how fast rain/snow falls, relative to normal speed. Can be negative." +
"&NUse \"normal\" instead of a number to reset to default (0)." },
{ "weatherfade","&H/Env <WorldName> weatherfade <#>&N&S" +
"Sets how quickly rain/snow fades, relative to normal rate." +
"&NUse \"normal\" instead of a number to reset to default (0)." }
},
Usage = "/Env <WorldName> <Variable>",
IsConsoleSafe = true,
Handler = EnvHandler
};
static void EnvHandler(Player player, CommandReader cmd) {
string arg1 = cmd.Next(), arg2 = cmd.Next(), arg3 = cmd.Next();
World world;
// Print own world info when just /env
if (arg1 == null) {
world = player.World;
if (world == null) { player.Message("When used from console, /Env requires a world name."); return; }
ShowEnvSettings(player, world);
return;
}
// Print that world's info when /env worldname
if (arg2 == null) {
world = WorldManager.FindWorldOrPrintMatches(player, arg1);
if (world == null) return;
ShowEnvSettings(player, world);
return;
}
// Reset env when /env worldname normal
if (arg2.CaselessEquals("normal")) {
world = WorldManager.FindWorldOrPrintMatches(player, arg1);
if (world == null) return;
if (cmd.IsConfirmed) {
ResetEnv(player, world);
} else {
Logger.Log(LogType.UserActivity,
"Env: Asked {0} to confirm resetting enviroment settings for world {1}",
player.Name, world.Name);
player.Confirm(cmd, "Reset enviroment settings for world {0}&S?", world.ClassyName);
}
return;
}
// Handle either /env var value (using current world), or /env world var value
if (arg3 == null && player.World == null) {
player.Message("When used from console, /Env requires a world name."); return;
}
world = arg3 == null ? player.World : WorldManager.FindWorldOrPrintMatches(player, arg1);
if (world == null) return;
string variable = arg3 == null ? arg1 : arg2;
string value = arg3 == null ? arg2 : arg3;
if (value.StartsWith("#"))
value = value.Remove(0, 1);
switch (variable.ToLower()) {
case "fog":
SetEnvColor(player, world, value, "fog color", EnvVariable.FogColor, ref world.FogColor);
break;
case "cloud":
case "clouds":
SetEnvColor(player, world, value, "cloud color", EnvVariable.CloudColor, ref world.CloudColor);
break;
case "sky":
SetEnvColor(player, world, value, "sky color", EnvVariable.SkyColor, ref world.SkyColor);
break;
case "dark":
case "shadow":
SetEnvColor(player, world, value, "shadow color", EnvVariable.Shadow, ref world.ShadowColor);
break;
case "sun":
case "light":
case "sunlight":
SetEnvColor(player, world, value, "sunlight color", EnvVariable.Sunlight, ref world.LightColor);
break;
case "level":
case "edgelevel":
case "waterlevel":
SetEnvAppearanceShort(player, world, value, EnvProp.EdgeLevel,
"water level", 0, ref world.EdgeLevel);
break;
case "cloudheight":
case "cloudsheight":
SetEnvAppearanceShort(player, world, value, EnvProp.CloudsLevel,
"clouds height", 0, ref world.CloudsHeight);
break;
case "sideoffset":
case "sidesoffset":
case "bedrockoffset":
SetEnvAppearanceShort(player, world, value, EnvProp.SidesOffset,
"bedrock offset", -2, ref world.SidesOffset);
break;
case "fogdist":
case "maxfog":
case "maxdist":
SetEnvAppearanceShort(player, world, value, EnvProp.MaxFog,
"max fog distance", 0, ref world.MaxFogDistance);
break;
case "weatherfade":
SetEnvAppearanceFloat(player, world, value, EnvProp.WeatherFade, "weather fade rate",
0, 255, 128, 128, ref world.WeatherFade);
break;
case "weatherspeed":
SetEnvAppearanceFloat(player, world, value, EnvProp.WeatherSpeed, "weather speed",
-32767, 32767, 256, 256, ref world.WeatherSpeed);
break;
case "cloudspeed":
case "cloudsspeed":
SetEnvAppearanceFloat(player, world, value, EnvProp.CloudsSpeed, "clouds speed",
-32767, 32767, 256, 256, ref world.CloudsSpeed);
break;
case "horizon":
case "edge":
case "water":
SetEnvAppearanceBlock(player, world, value, EnvProp.EdgeBlock,
"water block", Block.StillWater, ref world.HorizonBlock);
break;
case "side":
case "border":
case "bedrock":
SetEnvAppearanceBlock(player, world, value, EnvProp.SidesBlock,
"bedrock block", Block.Admincrete, ref world.EdgeBlock);
break;
case "tex":
case "terrain":
case "texture":
FilterURL(ref value);
if (value.CaselessEquals("default")) {
player.Message("Reset texture for {0}&S to {1}", world.ClassyName, Server.DefaultTerrain);
value = "Default";
} else if (!value.EndsWith(".png") && !value.EndsWith(".zip")) {
player.Message("Env Texture: Invalid image type. Please use a \".png\" or \".zip\"", world.ClassyName);
return;
} else if (!(value.CaselessStarts("http://") || value.CaselessStarts("https://"))) {
player.Message("Env Texture: Invalid URL. Please use a \"http://\" or \"https://\" type url.", world.ClassyName);
return;
} else {
player.Message("Set texture for {0}&S to {1}", world.ClassyName, value);
}
world.Texture = value;
foreach (Player p in world.Players) {
if (p.Supports(CpeExt.EnvMapAspect))
p.Send(Packet.MakeEnvSetMapUrl(world.GetTexture(), p.HasCP437));
else if (p.Supports(CpeExt.EnvMapAppearance) || p.Supports(CpeExt.EnvMapAppearance2))
p.SendEnvSettings();
}
break;
case "weather":
byte weather = 0;
if (value.CaselessEquals("normal")) {
player.Message("Reset weather for {0}&S to normal(0) ", world.ClassyName);
world.Weather = 0;
} else {
if (!byte.TryParse(value, out weather)) {
if (value.CaselessEquals("sun")) {
weather = 0;
} else if (value.CaselessEquals("rain")) {
weather = 1;
} else if (value.CaselessEquals("snow")) {
weather = 2;
}
}
if (weather < 0 || weather > 2) {
player.Message("Please use a valid integer(0,1,2) or string(sun,rain,snow)");
return;
}
world.Weather = weather;
player.Message("&aSet weather for {0}&a to {1} ({2}&a)", world.ClassyName, weather, weather == 0 ? "&SSun" : (weather == 1 ? "&1Rain" : "&fSnow"));
}
foreach (Player p in world.Players) {
if (p.Supports(CpeExt.EnvWeatherType)) {
p.Send(Packet.SetWeather(world.Weather));
}
}
break;
default:
CdEnv.PrintUsage(player);
return;
}
WorldManager.SaveWorldList();
}
static void FilterURL(ref string url) {
// a lot of people try linking to the dropbox page instead of directly to file, so we auto correct them
if (url.StartsWith("http://www.dropbox")) {
url = "http://dl.dropbox" + url.Substring("http://www.dropbox".Length);
url = url.Replace("?dl=0", "");
} else if (url.StartsWith("https://www.dropbox")) {
url = "https://dl.dropbox" + url.Substring("https://www.dropbox".Length);
url = url.Replace("?dl=0", "");
}
}
static void ShowEnvSettings(Player player, World world) {
player.Message("Environment settings for world {0}&S:", world.ClassyName);
player.Message(" Cloud: {0} Fog: {1} Sky: {2}",
world.CloudColor == null ? "normal" : '#' + world.CloudColor,
world.FogColor == null ? "normal" : '#' + world.FogColor,
world.SkyColor == null ? "normal" : '#' + world.SkyColor);
player.Message(" Shadow: {0} Sunlight: {1} Edge level: {2}",
world.ShadowColor == null ? "normal" : '#' + world.ShadowColor,
world.LightColor == null ? "normal" : '#' + world.LightColor,
world.GetEdgeLevel() + " blocks");
player.Message(" Clouds height: {0} Max fog distance: {1}",
world.GetCloudsHeight() + " blocks",
world.MaxFogDistance <= 0 ? "(no limit)" : world.MaxFogDistance.ToString());
player.Message(" Cloud speed: {0} Weather speed: {1}",
(world.CloudsSpeed / 256f).ToString("F2") + "%",
(world.WeatherSpeed / 256f).ToString("F2") + "%");
player.Message(" Weather fade rate: {0}",
(world.WeatherFade / 128f).ToString("F2") + "%");
player.Message(" Water block: {1} Bedrock block: {0}",
world.EdgeBlock, world.HorizonBlock);
player.Message(" Texture: {0}", world.GetTexture());
if (!player.IsUsingWoM) {
player.Message(" You need ClassiCube or ClassicalSharp client to see the changes.");
}
}
static void ResetEnv(Player player, World world) {
world.FogColor = null;
world.CloudColor = null;
world.SkyColor = null;
world.ShadowColor = null;
world.LightColor = null;
world.EdgeLevel = -1;
world.SidesOffset = -2;
world.CloudsHeight = short.MinValue;
world.MaxFogDistance = 0;
world.EdgeBlock = (byte)Block.Admincrete;
world.HorizonBlock = (byte)Block.Water;
world.Texture = "Default";
world.WeatherSpeed = 256;
world.CloudsSpeed = 256;
world.WeatherFade = 128;
Logger.Log(LogType.UserActivity,
"Env: {0} {1} reset environment settings for world {2}",
player.Info.Rank.Name, player.Name, world.Name);
player.Message("Enviroment settings for world {0} &Swere reset.", world.ClassyName);
WorldManager.SaveWorldList();
foreach (Player p in world.Players) {
if (p.Supports(CpeExt.EnvMapAppearance) || p.Supports(CpeExt.EnvMapAppearance2)
|| p.Supports(CpeExt.EnvMapAspect))
p.SendEnvSettings();
}
}
static void SetEnvColor(Player player, World world, string value,