/
CommandHandler.java
1080 lines (951 loc) · 52 KB
/
CommandHandler.java
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
package net.aufdemrand.denizen;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.*;
import net.aufdemrand.denizen.listeners.AbstractListener;
import net.aufdemrand.denizen.npc.traits.*;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptHelper;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.WorldScriptHelper;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.ScriptRepo;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.command.Command;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.json.JSONException;
public class CommandHandler {
private final Citizens plugin;
public CommandHandler(Citizens plugin) {
this.plugin = plugin;
}
// <--[language]
// @name /npc pushable command
// @description
// The '/npc pushable' command controls a NPCs Pushable Trait. When a NPC is 'pushable', the NPC
// will move out of the way when colliding with another LivingEntity.
//
// Pushable NPCs have 3 different settings available: Toggled, Returnable, and Delay.
//
// When an NPCs Pushable Trait is toggled off, it will not function. Entities which
// collide may occupy the same space. To toggle pushable on or off, use the Bukkit command:
// /npc pushable -t
//
// Setting the NPC as 'returnable' will automatically navigate the NPC back to
// its original location after a specified delay. If not returnable, NPCs will retain
// their position after being moved.
// /npc pushable -r
//
// To change the delay of a returnable NPC, use the following Bukkit Command,
// specifying the number of seconds in which the delay should be.
// /npc pushable --delay #
// It is possible to use multiple arguments at once. For example:
// /npc pushable -t -r --delay 10
//
// Note: If allowed to move in undesirable areas, the NPC may be un-returnable
// if the navigator cancels navigation due to being stuck. Care should be taken
// to ensure a safe area around the NPC.
//
// See also: 'pushable trait'
// -->
@Command(
aliases = { "npc" }, usage = "pushable -t (-r) (--delay #)", desc = "Makes a NPC pushable.",
flags = "rt", modifiers = { "pushable", "push" }, min = 1, max = 2, permission = "denizen.npc.pushable")
@Requirements(selected = true, ownership = true)
public void pushable(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(PushableTrait.class)) npc.addTrait(PushableTrait.class);
PushableTrait trait = npc.getTrait(PushableTrait.class);
if (args.hasFlag('r') && !args.hasFlag('t')) {
trait.setReturnable(!trait.isReturnable());
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + (trait.isReturnable() ? " will " : " will not ") + "return when being pushed"
+ (!trait.isReturnable() || trait.isPushable() ? "." : ", but is currently not pushable."));
return;
} else if (args.hasValueFlag("delay") && !args.hasFlag('t')) {
if (args.getFlag("delay").matches("\\d+") && args.getFlagInteger("delay") > 0) {
trait.setDelay(Integer.valueOf(args.getFlag("delay")));
trait.setReturnable(true);
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + " will return after '" + args.getFlag("delay") + "' seconds"
+ (trait.isPushable() ? "." : ", but is currently not pushable."));
return;
} else {
Messaging.send(sender, ChatColor.RED + "Delay must be a valid number of seconds!");
return;
}
} else if (args.hasFlag('t') && !args.hasValueFlag("delay") && !args.hasFlag('r')) {
trait.toggle();
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + (trait.isPushable() ? " is" : " is not") + " currently pushable" +
(trait.isReturnable() && trait.isPushable() ? " and will return when pushed after '" + trait.getDelay() + "' seconds." : "."));
return;
} else if (args.hasFlag('t')) {
trait.toggle();
if (args.hasFlag('r')) trait.setReturnable(true);
if (args.hasValueFlag("delay") && args.getFlag("delay").matches("\\d+") && args.getFlagInteger("delay") > 0)
trait.setDelay(args.getFlagInteger("delay"));
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + (trait.isPushable() ? " is" : " is not") + " currently pushable" +
(trait.isReturnable() && trait.isPushable() ? " and will return when pushed after '" + trait.getDelay() + "' seconds." : "."));
return;
} else if (args.length() > 2) {
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '-t' to toggle pushable state. <b>Example: /npc pushable -t");
Messaging.send(sender, "<f>To have the NPC return when pushed, use '-r'.");
Messaging.send(sender, "<f>Change the return delay with '--delay #'.");
Messaging.send(sender, "");
}
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + (trait.isPushable() ? " is" : " is not") + " currently pushable" +
(trait.isReturnable() ? " and will return when pushed after " + trait.getDelay() + " seconds." : "."));
}
// <--[language]
// @name /npc constant command
// @description
// The /npc constants command configures a NPC's constants. Uses Denizen's ConstantTrait to keep track of
// NPC-specific constants. This provides seamless integration with an assignment script's 'Default Constants' in
// which string variables can be stored and retrieved with the use of 'replaceable tags', or API. Constants set at
// the NPC level override any constants from the NPC's assignment script.
//
// Constants may be used in several ways: Setting, Removing, and Viewing
//
// To set a constant, all that is required is a name and value. Use the Bukkit command in the
// following manner: (Note the use of quotes on multi world values)
// /npc constant --set constant_name --value 'multi word value'
//
// Removing a constant from an NPC only requires a name. Note: It is not possible to remove a
// constant set by the NPCs Assignment Script, except by modifying the script itself.
// /npc constant --remove constant_name
//
// Viewing constants is easy, just use '/npc constant #', specifying a page number. Constants which
// have been overridden by the NPC are formatted with a strike-through to indicate this case.
//
// To reference a constant value, use the replaceable tag to get the NPCs 'constant' attribute. For example:
// <npc.constant[constant_name]>. Constants may also have other tags in their value, which will be replaced
// whenever the constant is used, allowing the use of dynamic information.
// -->
@Command(
aliases = { "npc" }, usage = "constant --set|remove name --value constant value",
desc = "Views/adds/removes NPC string constants.", flags = "r", modifiers = { "constants", "constant", "cons" },
min = 1, max = 3, permission = "denizen.npc.constants")
@Requirements(selected = true, ownership = true)
public void constants(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(ConstantsTrait.class)) npc.addTrait(ConstantsTrait.class);
ConstantsTrait trait = npc.getTrait(ConstantsTrait.class);
if (args.hasValueFlag("set")) {
if (!args.hasValueFlag("value")) throw new CommandException("--SET requires use of the '--VALUE \"constant value\"' argument.");
trait.setConstant(args.getFlag("set"), args.getFlag("value"));
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + " has added constant '" + args.getFlag("set") + "'.");
return;
} else if (args.hasValueFlag("remove")) {
trait.removeConstant(args.getFlag("remove"));
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + " has removed constant '" + args.getFlag("remove") + "'.");
return;
} else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '--set name' to add/set a new NPC-specific constant.");
Messaging.send(sender, "<f>Must also specify '--value \"constant value\"'.");
Messaging.send(sender, "<b>Example: /npc constant --set constant_1 --value \"test value\"");
Messaging.send(sender, "<f>Remove NPC-specific constants with '--remove name'");
Messaging.send(sender, "<f>Note: Constants set will override any specified in an");
Messaging.send(sender, "<f>assignment. Constants specified in assignments cannot be");
Messaging.send(sender, "<f>removed with this command.");
Messaging.send(sender, "");
return;
}
trait.describe(sender, args.getInteger(1, 1));
}
/*
* ASSIGNMENT
*/
@Command(
aliases = { "npc" }, usage = "assignment --set assignment_name (-r)",
desc = "Controls the assignment for an NPC.", flags = "r", modifiers = { "assignment", "assign" },
min = 1, max = 3, permission = "denizen.npc.assign")
@Requirements(selected = true, ownership = true)
public void assignment(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(AssignmentTrait.class)) npc.addTrait(AssignmentTrait.class);
Player player = null;
if (sender instanceof Player) player = (Player) sender;
AssignmentTrait trait = npc.getTrait(AssignmentTrait.class);
if (args.hasValueFlag("set")) {
String script = args.getFlag("set").replace("\"", "");
if (trait.setAssignment(script, dPlayer.mirrorBukkitPlayer(player)))
if (trait.hasAssignment())
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s assignment is now: '" + trait.getAssignment().getName() + "'.");
else Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s assignment was not able to be set.");
else if (ScriptRegistry.containsScript(script))
Messaging.send(sender, ChatColor.RED + "A script with that name exists, but it is not an assignment script!");
else
Messaging.send(sender, ChatColor.RED + "Invalid assignment! Has the script sucessfully loaded, or has it been mispelled?");
return;
} else if (args.hasFlag('r')) {
trait.removeAssignment(dPlayer.mirrorBukkitPlayer(player));
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s assignment has been removed.");
return;
} else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '--set name' to set an assignment script to this NPC.");
Messaging.send(sender, "<b>Example: /npc assignment --set \"Magic Shop\"");
Messaging.send(sender, "<f>Remove an assignment with '-r'.");
Messaging.send(sender, "<f>Note: Assigning a script will fire an 'On Assignment:' action.");
Messaging.send(sender, "");
return;
}
trait.describe(sender, args.getInteger(1, 1));
}
/*
* TRIGGER
*/
@Command(
aliases = { "npc" }, usage = "trigger [trigger name] [(--cooldown [seconds])|(--radius [radius])|(-t)]",
desc = "Controls the various triggers for an NPC.", flags = "t", modifiers = { "trigger", "tr" },
min = 1, max = 3, permission = "denizen.npc.trigger")
@Requirements(selected = true, ownership = true)
public void trigger(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(TriggerTrait.class)) npc.addTrait(TriggerTrait.class);
TriggerTrait trait = npc.getTrait(TriggerTrait.class);
if ((args.hasValueFlag("name") || (args.argsLength() > 1 && (args.getJoinedStrings(1) != null) && !args.getString(1).matches("\\d+")))) {
// Get the name of the trigger
String triggerName;
if (args.hasValueFlag("name")) triggerName = args.getFlag("name");
else triggerName = args.getJoinedStrings(1);
// Check to make sure trigger exists
if (DenizenAPI.getCurrentInstance().getTriggerRegistry().get(triggerName) == null) {
Messaging.send(sender, ChatColor.RED + "'" + triggerName.toUpperCase() + "' trigger does not exist.");
Messaging.send(sender, "<f>Usage: /npc trigger [trigger_name] [(--cooldown #)|(--radius #)|(-t)]");
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '--name trigger_name' to specify a specific trigger, and '-t' to toggle.");
Messaging.send(sender, "<b>Example: /npc trigger --name damage -t");
Messaging.send(sender, "<f>You may also use '--cooldown #' to specify a new cooldown time, and '--radius #' to specify a specific radius, when applicable.");
Messaging.send(sender, "");
return;
}
// If toggling
if (args.hasFlag('t')) trait.toggleTrigger(triggerName);
// If setting cooldown
if (args.hasValueFlag("cooldown"))
trait.setLocalCooldown(triggerName, args.getFlagDouble("cooldown"));
// If specifying radius
if (args.hasValueFlag("radius")) {
trait.setLocalRadius(triggerName, args.getFlagInteger("radius"));
Messaging.send(sender, ChatColor.YELLOW + triggerName.toUpperCase() + " trigger radius now " + args.getFlag("radius") + ".");
}
// Show current status of the trigger
Messaging.send(sender, ChatColor.YELLOW + triggerName.toUpperCase() + " trigger " + (trait.isEnabled(triggerName) ? "is" : "is not") + " currently enabled" +
(trait.isEnabled(triggerName) ? " with a cooldown of '" + trait.getCooldownDuration(triggerName) + "' seconds." : "."));
return;
}
trait.describe(sender, args.getInteger(1, 1));
}
/*
* NICKNAME
*/
@Command(
aliases = { "npc" }, usage = "nickname [--set nickname]",
desc = "Gives the NPC a nickname, used with a Denizen-compatible Speech Engine.", modifiers = { "nickname", "nick", "ni" },
min = 1, max = 3, permission = "denizen.npc.nickname")
@Requirements(selected = true, ownership = true)
public void nickname(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(NicknameTrait.class)) npc.addTrait(NicknameTrait.class);
NicknameTrait trait = npc.getTrait(NicknameTrait.class);
if (args.hasValueFlag("set")) {
trait.setNickname(args.getFlag("set"));
Messaging.send(sender, ChatColor.GREEN + "Nickname set.");
return;
} else if (args.hasFlag('r')) {
trait.setNickname("");
Messaging.send(sender, ChatColor.YELLOW + "Nickname removed.");
return;
}
if (trait.hasNickname())
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s nickname is '" + trait.getNickname() + "'.");
else Messaging.send(sender, ChatColor.YELLOW + npc.getName() + " does not have a nickname!");
}
/*
* NAMEPLATE / NAMETAG
*/
@Command(
aliases = { "npc" }, usage = "nameplate [--color color]",
desc = "Sets the namepalte color of the NPC.", modifiers = { "nameplate", "nametag", "np", "nt" },
min = 1, max = 3, permission = "denizen.npc.nameplate")
@Requirements(selected = true, ownership = true)
public void nameplate(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(NameplateTrait.class)) npc.addTrait(NameplateTrait.class);
NameplateTrait trait = npc.getTrait(NameplateTrait.class);
if (args.hasValueFlag("color")) {
String colorString = args.getFlag("color").toUpperCase();
ChatColor color = null;
try {
color = ChatColor.valueOf(colorString);
} catch( Exception e) {}
if(color != null) {
trait.setColor(color);
Messaging.send(sender, ChatColor.GREEN + "Nameplate color set.");
} else {
Messaging.send(sender, ChatColor.RED + "No color with name '" + colorString + "' found!");
}
return;
} else if( args.hasFlag('r') ) {
trait.setColor(null);
Messaging.send(sender, ChatColor.GREEN + "Nameplate color reset.");
}
if (trait.hasColor())
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s nameplate color is " + trait.getColor() + trait.getColor().name() + ".");
else Messaging.send(sender, ChatColor.YELLOW + npc.getName() + " does not have a nameplate color!");
}
/*
* Sit
*/
@Command(
aliases = { "npc" }, usage = "sit (--location x,y,z,world) (--anchor anchor_name) (-c)",
desc = "Makes the NPC sit.", flags = "c", modifiers = { "sit" },
min = 1, max = 3, permission = "denizen.npc.sit")
@Requirements(selected = true, ownership = true)
public void sitting(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (npc.getBukkitEntity().getType() != EntityType.PLAYER) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " needs to be a Player type NPC to sit!");
return;
}
if (!npc.hasTrait(SittingTrait.class)) npc.addTrait(SittingTrait.class);
SittingTrait trait = npc.getTrait(SittingTrait.class);
if (trait.isSitting()) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already sitting!");
return;
}
if (args.hasFlag('c')) {
trait.sit(args.getSenderTargetBlockLocation());
} else if (args.hasValueFlag("location")) {
String[] argsArray = args.getFlag("location").split(",");
if (argsArray.length != 4) {
Messaging.send(sender, ChatColor.RED + "Usage: /npc sit --location x,y,z,world");
return;
}
trait.sit(aH.getLocationFrom("location:" + argsArray[0] + "," + argsArray[1] + "," + argsArray[2] + "," + argsArray[3]));
} else if (args.hasValueFlag("anchor")) {
DenizenAPI.getCurrentInstance().tagManager();
trait.sit(aH.getLocationFrom(TagManager.tag(null, DenizenAPI.getCurrentInstance().getNPCRegistry().getDenizen(npc),("location:<anchor:" + args.getFlag("anchor") + ">"), false)));
} else {
trait.sit();
}
}
/*
* Stand
*/
@Command(
aliases = { "npc" }, usage = "stand",
desc = "Makes the NPC stand.", modifiers = { "stand" },
min = 1, max = 3, permission = "denizen.npc.stand")
@Requirements(selected = true, ownership = true)
public void standing(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (npc.hasTrait(SittingTrait.class)) {
SittingTrait trait = npc.getTrait(SittingTrait.class);
if (!trait.isSitting()) {
npc.removeTrait(SittingTrait.class);
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already standing!");
return;
}
trait.stand();
npc.removeTrait(SittingTrait.class);
} else if (npc.hasTrait(SneakingTrait.class)) {
SneakingTrait trait = npc.getTrait(SneakingTrait.class);
if (!trait.isSneaking()) {
npc.removeTrait(SittingTrait.class);
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already standing!");
return;
}
trait.stand();
npc.removeTrait(SneakingTrait.class);
}
}
/*
* Sleep
*/
@Command(
aliases = { "npc" }, usage = "sleep (--location x,y,z,world) (--anchor anchor_name)",
desc = "Makes the NPC sleep.", modifiers = { "sleep" },
min = 1, max = 3, permission = "denizen.npc.sleep")
@Requirements(selected = true, ownership = true)
public void sleeping(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(SleepingTrait.class)) npc.addTrait(SleepingTrait.class);
SleepingTrait trait = npc.getTrait(SleepingTrait.class);
if (trait.isSleeping()) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already sleeping!");
return;
}
if (args.hasValueFlag("location")) {
String[] argsArray = args.getFlag("location").split(",");
if (argsArray.length != 4) {
Messaging.send(sender, ChatColor.RED + "Usage: /npc sit --location x,y,z,world");
return;
}
trait.toSleep(aH.getLocationFrom("location:" + argsArray[0] + "," + argsArray[1] + "," + argsArray[2] + "," + argsArray[3]));
} else if (args.hasValueFlag("anchor")) {
DenizenAPI.getCurrentInstance().tagManager();
trait.toSleep(aH.getLocationFrom(TagManager.tag(null, DenizenAPI.getCurrentInstance().getNPCRegistry().getDenizen(npc),("location:<anchor:" + args.getFlag("anchor") + ">"), false)));
} else {
trait.toSleep();
}
}
/*
* Wakeup
*/
@Command(
aliases = { "npc" }, usage = "wakeup",
desc = "Makes the NPC wake up.", modifiers = { "wakeup" },
min = 1, max = 3, permission = "denizen.npc.sleep")
@Requirements(selected = true, ownership = true)
public void wakingup(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(SleepingTrait.class)) npc.addTrait(SleepingTrait.class);
SleepingTrait trait = npc.getTrait(SleepingTrait.class);
if (!trait.isSleeping()) {
npc.removeTrait(SleepingTrait.class);
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already awake!");
return;
}
trait.wakeUp();
npc.removeTrait(SleepingTrait.class);
}
/*
* Fish
*/
@Command(
aliases = { "npc" }, usage = "fish (--location x,y,z,world) (--anchor anchor_name) (-c)",
desc = "Makes the NPC fish, casting at the given location.", flags = "c, f", modifiers = { "fish" },
min = 1, max = 3, permission = "denizen.npc.fish")
@Requirements(selected = true, ownership = true)
public void startFishing(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(FishingTrait.class)) npc.addTrait(FishingTrait.class);
FishingTrait trait = npc.getTrait(FishingTrait.class);
if (trait.isFishing()) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already fishing!");
return;
}
if (args.hasFlag('c')) {
trait.startFishing(args.getSenderTargetBlockLocation());
}
if (args.hasFlag('f')) {
trait.setCatchFish(true);
}
if (args.hasValueFlag("percent")) {
trait.setCatchPercent(args.getFlagInteger("percent"));
}
if (args.hasValueFlag("location")) {
String[] argsArray = args.getFlag("location").split(",");
if (argsArray.length != 4) {
Messaging.send(sender, ChatColor.RED + "Usage: /npc fish --location x,y,z,world");
return;
}
trait.startFishing(aH.getLocationFrom("location:" + argsArray[0] + "," + argsArray[1] + "," + argsArray[2] + "," + argsArray[3]));
} else if (args.hasValueFlag("anchor")) {
DenizenAPI.getCurrentInstance().tagManager();
trait.startFishing(aH.getLocationFrom(TagManager.tag(null, DenizenAPI.getCurrentInstance().getNPCRegistry().getDenizen(npc),("location:<anchor:" + args.getFlag("anchor") + ">"), false)));
} else {
trait.startFishing();
}
}
/*
* Stopfishing
*/
@Command(
aliases = { "npc" }, usage = "stopfishing",
desc = "Makes the NPC stop fishing.", modifiers = { "stopfishing" },
min = 1, max = 3, permission = "denizen.npc.fish")
@Requirements(selected = true, ownership = true)
public void stopFishing(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(FishingTrait.class)) npc.addTrait(FishingTrait.class);
FishingTrait trait = npc.getTrait(FishingTrait.class);
if (!trait.isFishing()) {
npc.removeTrait(FishingTrait.class);
Messaging.send(sender, ChatColor.RED + npc.getName() + " isnt fishing!");
return;
}
trait.stopFishing();
npc.removeTrait(FishingTrait.class);
}
/*
* Effect
*/
@Command(
aliases = { "npc" }, usage = "effect (--play effect_name) (--delay #) (-d)",
desc = "Sets the NPC particle effect.", modifiers = { "effect" }, flags = "d",
min = 1, max = 3, permission = "denizen.npc.effect")
@Requirements(selected = true, ownership = true)
public void playEffect(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(ParticlesTrait.class)) npc.addTrait(ParticlesTrait.class);
ParticlesTrait trait = npc.getTrait(ParticlesTrait.class);
if (args.hasValueFlag("delay")) {
trait.setWait(args.getFlagInteger("delay"));
}
if (args.hasFlag('d')) {
trait.setDense(true);
}
if (args.hasValueFlag("play")) {
String name = args.getFlag("play");
if (!npc.hasTrait(ParticlesTrait.class)) npc.addTrait(ParticlesTrait.class);
if (name.equalsIgnoreCase("none")) {
trait.setEffect("NONE");
} else if (name.equalsIgnoreCase("flame")) {
trait.setEffect("FLAME");
} else if (name.equalsIgnoreCase("ender")) {
trait.setEffect("ENDER");
} else if (name.equalsIgnoreCase("smoke")) {
trait.setEffect("SMOKE");
} else if (name.equalsIgnoreCase("potbreak")) {
trait.setEffect("POTBREAK");
} else if (name.equalsIgnoreCase("potion")) {
trait.setEffect("POTION");
} else if (name.equalsIgnoreCase("heart")) {
trait.setEffect("HEART");
} else if (name.equalsIgnoreCase("explosion")) {
trait.setEffect("EXPLOSION");
} else Messaging.send(sender, ChatColor.RED + "Not a valid effect name!");
} else Messaging.send(sender, ChatColor.RED + "Please specify an effect name!");
}
/*
* Sneak
*/
@Command(
aliases = { "npc" }, usage = "sneak",
desc = "Makes the NPC crouch.", flags = "", modifiers = { "sneak", "crouch" },
min = 1, max = 3, permission = "denizen.npc.sneak")
@Requirements(selected = true, ownership = true)
public void sneaking(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (npc.getBukkitEntity().getType() != EntityType.PLAYER) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " needs to be a Player type NPC to sneak!");
return;
}
if (!npc.hasTrait(SneakingTrait.class)) npc.addTrait(SneakingTrait.class);
SneakingTrait trait = npc.getTrait(SneakingTrait.class);
if (trait.isSneaking()) {
Messaging.send(sender, ChatColor.RED + npc.getName() + " is already sneaking!");
} else {
trait.sneak();
}
}
/*
* HEALTH
*/
@Command(
aliases = { "npc" }, usage = "health --set # (-r)",
desc = "Sets the max health for an NPC.", modifiers = { "health", "he", "hp" },
min = 1, max = 3, permission = "denizen.npc.health")
@Requirements(selected = true, ownership = true)
public void health(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!npc.hasTrait(HealthTrait.class)) npc.addTrait(HealthTrait.class);
HealthTrait trait = npc.getTrait(HealthTrait.class);
boolean showMore = true;
if (args.hasValueFlag("max")) {
trait.setMaxhealth(args.getFlagInteger("max"));
trait.setHealth();
Messaging.send(sender, ChatColor.GREEN + npc.getName() + "'s health maximum is now " + trait.getMaxhealth() + ".");
showMore = false;
} if (args.hasValueFlag("set")) {
trait.setHealth(args.getFlagInteger("set"));
} if (args.hasValueFlag("respawndelay")) {
trait.setRespawnDelay(args.getFlag("respawndelay"));
Messaging.send(sender, ChatColor.GREEN + npc.getName() + "'s respawn delay now " + trait.getRespawnDelay()
+ (trait.isRespawnable() ? "." : ", but is not currently auto-respawnable upon death."));
showMore = false;
} if (args.hasValueFlag("respawnlocation")) {
trait.setRespawnLocation(args.getFlag("respawnlocation"));
Messaging.send(sender, ChatColor.GREEN + npc.getName() + "'s respawn location now " + trait.getRespawnLocationAsString()
+ (trait.isRespawnable() ? "." : ", but is not currently auto-respawnable upon death."));
showMore = false;
} if (args.hasFlag('s')) {
trait.setRespawnable(!trait.isRespawnable());
Messaging.send(sender, ChatColor.GREEN + npc.getName() + (trait.isRespawnable()
? " will now auto-respawn on death after " + trait.getRespawnDelay() + " seconds."
: " will no longer auto-respawn on death."));
showMore = false;
} if (args.hasFlag('a')) {
trait.animateOnDeath(!trait.animatesOnDeath());
Messaging.send(sender, ChatColor.GREEN + npc.getName() + (trait.animatesOnDeath()
? " will now animate on death."
: " will no longer animate on death."));
showMore = false;
} else if (args.hasFlag('r')) {
trait.setHealth();
Messaging.send(sender, ChatColor.GREEN + npc.getName() + "'s health reset to " + trait.getMaxhealth() + ".");
showMore = false;
}
if (showMore)
Messaging.send(sender, ChatColor.YELLOW + npc.getName() + "'s health is '" + trait.getHealth() + "/" + trait.getMaxhealth() + "'.");
}
/*
* Chatbot
*/
@Command(
aliases = { "npc" }, usage = "chatbot",
desc = "Turns the NPC into a chatbot.", flags = "", modifiers = { "chatbot", "bot" },
min = 1, max = 2, permission = "denizen.npc.chatbot")
@Requirements(selected = true, ownership = true)
public void chatbot(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!Depends.hasProgramAB) {
Messaging.send(sender, ChatColor.RED + "The Chatbot Trait " +
" cannot be used if Program AB is missing from Denizen/lib");
return;
}
if (!npc.hasTrait(ChatbotTrait.class)) {
npc.addTrait(ChatbotTrait.class);
Messaging.send(sender, ChatColor.BLUE + npc.getName() +
" has become a chatbot!");
}
ChatbotTrait trait = npc.getTrait(ChatbotTrait.class);
if (args.argsLength() > 1) {
trait.setBot(args.getString(1));
Messaging.send(sender, ChatColor.BLUE + npc.getName() +
" is now the chatbot " + args.getString(1));
}
}
/*
* DENIZEN SUBMIT
*/
@Command(
aliases = { "denizen" }, usage = "submit",
desc = "Submits recorded logs triggered by /denizen debug -r", modifiers = { "submit" },
min = 1, max = 3, permission = "denizen.submit")
public void submit(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (!dB.record) {
Messaging.send(sender, ChatColor.RED + "Use /denizen debug -r to record debug information to be submitted");
return;
}
dB.record = false;
try {
URL url = new URL("http://mcmonkey4eva.dyndns.org/paste");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setConnectTimeout(10000); // Max 10 seconds - don't crash a server if the pastebin is down!
uc.connect();
uc.getOutputStream().write(
("postid=pastetext&pastetype=log"
+ "&response=micro&pastetitle=Denizen+Debug+Logs+From+" + URLEncoder.encode(Bukkit.getServer().getMotd().replace(ChatColor.COLOR_CHAR, (char)0x01))
+ "&pastecontents=" + dB.Recording)
.getBytes("UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
Messaging.send(sender, ChatColor.GREEN + "Successfully submitted to http://mcmonkey4eva.dyndns.org" + in.readLine());
in.close();
dB.Recording = "";
}
catch (Exception e) {
if (dB.showStackTraces) {
e.printStackTrace();
}
dB.Recording = "";
Messaging.send(sender, ChatColor.RED + "Error while submitting.");
}
}
/*
* DENIZEN DEBUG
*/
@Command(
aliases = { "denizen" }, usage = "debug",
desc = "Toggles debug mode for Denizen.", modifiers = { "debug", "de", "db" },
min = 1, max = 3, permission = "denizen.debug", flags = "scebr")
public void debug(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if (args.hasFlag('s')) {
if (!dB.debugMode) dB.toggle();
dB.showStackTraces = !dB.showStackTraces;
} else if (args.hasFlag('c')) {
if (!dB.debugMode) dB.toggle();
dB.showColor = !dB.showColor;
} else if (args.hasFlag('e')) {
if (!dB.debugMode) dB.toggle();
dB.showEventsFiring = !dB.showEventsFiring;
} else if (args.hasFlag('b')) {
if (!dB.debugMode) dB.toggle();
dB.showScriptBuilder = !dB.showScriptBuilder;
} else if (args.hasFlag('r')) {
dB.record = !dB.record;
dB.Recording = "";
} else dB.toggle();
Messaging.send(sender, ChatColor.YELLOW + "Denizen debugger is " + (dB.debugMode ?
((dB.showStackTraces) ? "enabled and showing stack-traces." : "enabled.") : "disabled.") + (dB.record ? " (Recording Active)": ""));
}
/*
* DENIZEN HELP
*/
@Command(
aliases = { "denizen" }, usage = "help",
desc = "Links to the Denizen handbook.", modifiers = { "help"},
min = 1, max = 3, permission = "denizen.basic")
public void help(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Messaging.send(sender, "§cFor help about Denizen, please read the Denizen Handbook here: §bhttp://goo.gl/4CSK8");
}
/*
* DENIZEN VERSION
*/
@Command(
aliases = { "denizen" }, usage = "version",
desc = "Shows the currently loaded version of Denizen.", modifiers = { "version"},
min = 1, max = 3, permission = "denizen.basic")
public void version(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Messaging.send(sender, ChatColor.YELLOW + " _/_ _ ._ _ _ ");
Messaging.send(sender, ChatColor.YELLOW + "(/(-/ )/ /_(-/ ) " + ChatColor.GRAY + " scriptable NPCs");
Messaging.send(sender, "");
Messaging.send(sender, ChatColor.GRAY + "by: " + ChatColor.WHITE + "aufdemrand");
Messaging.send(sender, ChatColor.GRAY + "version: "+ ChatColor.WHITE + Denizen.versionTag);
}
/*
* DENIZEN SAVE
*/
@Command(
aliases = { "denizen" }, usage = "save",
desc = "Saves the current state of Denizen/saves.yml.", modifiers = { "save" },
min = 1, max = 3, permission = "denizen.basic", flags = "s")
public void save(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
DenizenAPI.getCurrentInstance().saveSaves();
Messaging.send(sender, ChatColor.GREEN + "Denizen/saves.yml saved to disk from memory.");
}
/*
* DENIZEN LISTENER
*/
@Command(
aliases = { "denizen" }, usage = "listener (--player) --id listener_id --report|cancel|finish",
desc = "Checks/cancels/finishes listeners in progress.", modifiers = { "listener" },
min = 1, max = 3, permission = "denizen.basic", flags = "s")
public void listener(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Denizen denizen = ((Denizen) plugin.getServer().getPluginManager().getPlugin("Denizen"));
dPlayer player = null;
if (sender instanceof Player) player = dPlayer.mirrorBukkitPlayer((Player) sender);
if (args.hasValueFlag("player"))
player = dPlayer.valueOf(args.getFlag("player"));
if (player == null) throw new CommandException("Specified player not online or not found!");
Map<String,AbstractListener> listeners = denizen.getListenerRegistry().getListenersFor(player);
if (listeners == null || listeners.isEmpty()) {
Messaging.send(sender, ChatColor.GREEN + player.getName() + " has no active listeners.");
return;
}
if (args.hasValueFlag("report")) {
for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values())
if (quest.getListenerId().equalsIgnoreCase(args.getFlag("report")))
Messaging.send(sender, quest.report());
return;
} else if (args.hasValueFlag("cancel")) {
for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values())
if (quest.getListenerId().equalsIgnoreCase(args.getFlag("cancel"))) {
Messaging.send(sender, ChatColor.GREEN + "Cancelling '" + quest.getListenerId() + "' for " + player.getName() + ".");
quest.cancel();
}
return;
} else if (args.hasValueFlag("finish")) {
for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values())
if (quest.getListenerId().equalsIgnoreCase(args.getFlag("finish"))) {
Messaging.send(sender, ChatColor.GREEN + "Force-finishing '" + quest.getListenerId() + "' for " + player.getName() + ".");
quest.finish();
}
return;
} else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
Messaging.send(sender, "");
Messaging.send(sender, "<f>Use '--report|cancel|finish id' to modify/view a specific quest listener.");
Messaging.send(sender, "<b>Example: /npc listener --report \"Journey 1\"");
Messaging.send(sender, "");
return;
}
Paginator paginator = new Paginator();
paginator.header("Active quest listeners for " + player.getName() + ":");
paginator.addLine("<e>Key: <a>Type <b>ID");
if (listeners == null || listeners.isEmpty())
paginator.addLine("None.");
else for (AbstractListener quest : listeners.values())
paginator.addLine("<a>" + quest.getListenerType() + " <b>" + quest.getListenerId());
paginator.sendPage(sender, args.getInteger(1, 1));
}
/*
* DENIZEN RELOAD
*/
@Command ( aliases = { "denizen" }, usage = "reload (saves|config|scripts) (-a)",
desc = "Reloads various Denizen YML files from disk to memory.", modifiers = { "reload" },
min = 1, max = 3, permission = "denizen.basic", flags = "a" )
public void reload(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
Denizen denizen = (Denizen) plugin.getServer().getPluginManager().getPlugin("Denizen");
// Get reload type
if (args.hasFlag('a')) {
denizen.reloadSaves();
denizen.reloadConfig();
ScriptHelper.resetHadAnError();
ScriptHelper.reloadScripts();
Messaging.send(sender, ChatColor.GREEN + "Denizen/saves.yml, Denizen/config.yml, and Denizen/scripts/... reloaded from disk to memory.");
if (ScriptHelper.getHadAnError()) {
Messaging.send(sender, ChatColor.RED + "There was an error loading your scripts, check the console for details!");
}
List<String> events = new ArrayList<String>();
Map<String, dObject> context = new HashMap<String, dObject>();
events.add("reload scripts");
context.put("all", Element.TRUE);
context.put("sender", new Element(sender.getName()));
context.put("haderror", new Element(ScriptHelper.getHadAnError()));
WorldScriptHelper.doEvents(events, null, (sender instanceof Player) ? ((Player)sender) : null, context);
return;
}
// Reload a specific item
if (args.length() > 2) {
if (args.getString(1).equalsIgnoreCase("saves")) {
denizen.reloadSaves();
Messaging.send(sender, ChatColor.GREEN + "Denizen/saves.yml reloaded from disk to memory.");
return;
} else if (args.getString(1).equalsIgnoreCase("config")) {
denizen.reloadConfig();
Messaging.send(sender, ChatColor.GREEN + "Denizen/config.yml reloaded from disk to memory.");
return;
} else if (args.getString(1).equalsIgnoreCase("scripts")) {
ScriptHelper.resetHadAnError();
ScriptHelper.reloadScripts();
Messaging.send(sender, ChatColor.GREEN + "Denizen/scripts/... reloaded from disk to memory.");
if (ScriptHelper.getHadAnError()) {
Messaging.send(sender, ChatColor.RED + "There was an error loading your scripts, check the console for details!");
}
List<String> events = new ArrayList<String>();
Map<String, dObject> context = new HashMap<String, dObject>();
events.add("reload scripts");
context.put("all", Element.FALSE);
context.put("haderror", new Element(ScriptHelper.getHadAnError()));
context.put("sender", new Element(sender.getName()));
WorldScriptHelper.doEvents(events, null, (sender instanceof Player) ? ((Player)sender) : null, context);
return;
}
}
Messaging.send(sender, "");
Messaging.send(sender, "<f>Specify which parts to reload. Valid options are: SAVES, CONFIG, SCRIPTS");
Messaging.send(sender, "<b>Example: /denizen reload scripts");
Messaging.send(sender, "<f>Use '-a' to reload all parts.");
Messaging.send(sender, "");
}
/*
* DENIZEN SCRIPTS
*/
@Command(
aliases = { "denizen" }, usage = "scripts (--type assignment|task|activity|interact) (--filter string)",
desc = "Lists currently loaded dScripts.", modifiers = { "scripts" },
min = 1, max = 4, permission = "denizen.basic")
public void scripts(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
// Fill arguments
String type = null; if (args.hasValueFlag("type")) type = args.getFlag("type");
String filter = null; if (args.hasValueFlag("filter")) filter = args.getFlag("filter");
// Get script names from the scripts.yml in memory
Set<String> scripts = ScriptRegistry._getScriptNames();
// New Paginator to display script names
Paginator paginator = new Paginator().header("Scripts");
paginator.addLine("<e>Key: <a>Type <b>Name");
// Add scripts to Paginator
for (String script : scripts) {
ScriptContainer scriptContainer = ScriptRegistry.getScriptContainer(script);
// If a --type has been specified...
if (type != null) {
if (scriptContainer.getContainerType().equalsIgnoreCase(type))
if (filter != null) {
if (script.contains(filter.toUpperCase()))
paginator.addLine("<a>" + scriptContainer.getContainerType().substring(0, 4) + " <b>" + script);
}
else paginator.addLine("<a>" + scriptContainer.getContainerType().substring(0, 4) + " <b>" + script);
// If a --filter has been specified...
} else if (filter != null) {
if (script.contains(filter.toUpperCase()))
paginator.addLine("<a>" + scriptContainer.getContainerType().substring(0, 4) + " <b>" + script);
} else paginator.addLine("<a>" + scriptContainer.getContainerType().substring(0, 4) + " <b>" + script);
}
// Send the contents of the Paginator to the Player (or Console)
if (!paginator.sendPage(sender, args.getInteger(1, 1)))
throw new CommandException(Messages.COMMAND_PAGE_MISSING);
}
/*
* CITIZENS SCRIPT REPO
*/
@Command(
aliases = {"denizen"}, usage = "repo (info|search|load)",
desc = "Repo commands.", modifiers = {"info", "search", "load"},
min = 1, permission = "denizen.repo")
public void repo(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
if(args.argsLength()>1 && args.getString(2).equalsIgnoreCase("info")){
if(args.argsLength()==3){
try{
ScriptRepo.info(sender, args.getString(2));
}catch (JSONException e){