/
BukkitWorldScriptHelper.java
3570 lines (3023 loc) · 133 KB
/
BukkitWorldScriptHelper.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.scripts.containers.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.utilities.Conversion;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.ScoreboardHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Position;
import net.aufdemrand.denizencore.objects.aH.Argument;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.enchantment.*;
import org.bukkit.event.player.*;
import org.bukkit.event.hanging.*;
import org.bukkit.event.inventory.*;
import org.bukkit.event.vehicle.*;
import org.bukkit.event.weather.*;
import org.bukkit.event.world.*;
import org.bukkit.event.entity.*;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.util.BlockIterator;
public class BukkitWorldScriptHelper implements Listener {
public BukkitWorldScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
public static String doEvents(List<String> events, dNPC npc, dPlayer player, Map<String, dObject> context) {
return doEvents(events, npc, player, context, false);
}
public static String doEvents(List<String> events, dNPC npc, dPlayer player, Map<String, dObject> context, boolean useids) {
List<String> determ;
if (useids) {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context, true);
}
else {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context);
}
return determ.size() > 0 ? determ.get(0): "none";
}
/////////////////////
// CUSTOM EVENTS
/////////////////
// <--[event]
// @Events
// server start
//
// @Triggers when the server starts
//
// @Determine
// "CANCELLED" to save all plugins and cancel server startup.
//
// -->
public void serverStartEvent() {
// Start the 'timeEvent'
long ticks = Settings.worldScriptTimeEventFrequency().getTicks();
Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
timeEvent();
}
}, ticks, ticks);
// Fire the 'Server Start' event
String determination = doEvents(Arrays.asList("server start"),
null, null, null);
if (determination.toUpperCase().startsWith("CANCELLED"))
Bukkit.getServer().shutdown(); // TODO: WHY IS THIS AN OPTION?!
}
private final Map<String, Integer> current_time = new HashMap<String, Integer>();
// <--[event]
// @Events
// time changes (in <world>)
// <0-23>:00 in <world>
// time <0-23> in <world>
//
// @Triggers when the current time changes in a world (once per mine-hour).
// @Context
// <context.time> returns the current time.
// <context.world> returns the world.
//
// -->
public void timeEvent() {
for (World world : Bukkit.getWorlds()) {
int hour = Double.valueOf(world.getTime() / 1000).intValue();
hour = hour + 6;
// Get the hour
if (hour >= 24) hour = hour - 24;
dWorld currentWorld = new dWorld(world);
if (!current_time.containsKey(currentWorld.identifySimple())
|| current_time.get(currentWorld.identifySimple()) != hour) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("time", new Element(hour));
context.put("world", currentWorld);
doEvents(Arrays.asList
("time changes",
"time changes in " + currentWorld.identifySimple(),
String.valueOf(hour) + ":00 in " + currentWorld.identifySimple(),
"time " + String.valueOf(hour) + " in " + currentWorld.identifySimple()),
null, null, context, true);
current_time.put(currentWorld.identifySimple(), hour);
}
}
}
// <--[event]
// @Events
// player places hanging
// player places <hanging>
//
// @Triggers when a hanging entity (painting or itemframe) is placed.
// @Context
// <context.hanging> returns the dEntity of the hanging.
// <context.location> returns the dLocation of the block the hanging was placed on.
//
// @Determine
// "CANCELLED" to stop the hanging from being placed.
//
// -->
@EventHandler
public void hangingPlace(HangingPlaceEvent event) {
if (dEntity.isNPC(event.getPlayer()))
return;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity hanging = new dEntity(event.getEntity());
context.put("hanging", hanging);
context.put("location", new dLocation(event.getBlock().getLocation()));
String determination = doEvents(Arrays.asList
("player places hanging",
"player places " + hanging.identifyType()),
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
/////////////////////
// ENTITY EVENTS
/////////////////
// <--[event]
// @Events
// creeper powered (because <cause>)
//
// @Triggers when a creeper is struck by lightning and turned into a powered creeper.
// @Context
// <context.entity> returns the dEntity of the creeper.
// <context.lightning> returns the dEntity of the lightning.
// <context.cause> returns an Element of the cause for the creeper being powered.
//
// @Determine
// "CANCELLED" to stop the creeper from being powered.
//
// -->
@EventHandler
public void creeperPower(CreeperPowerEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
dEntity lightning = event.getLightning() == null ? null: new dEntity(event.getLightning());
String cause = event.getCause().name();
context.put("entity", entity);
if (lightning != null)
context.put("lightning", lightning);
context.put("cause", new Element(cause));
String determination = doEvents(Arrays.asList
("creeper powered",
"creeper powered because " + cause),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity combusts
// <entity> combusts
//
// @Triggers when an entity catches fire.
// @Context
// <context.entity> returns the entity that caught fire.
// <context.duration> returns the length of the burn.
//
// @Determine
// "CANCELLED" to stop the creeper from being powered.
//
// -->
// TODO: Smarten event
@EventHandler
public void onCombust(EntityCombustEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
Duration dura = new Duration((long)event.getDuration());
context.put("entity", entity);
context.put("duration", dura);
dPlayer player = null;
dNPC npc = null;
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String determination = doEvents(Arrays.asList
("entity combusts",
entity.identifySimple() + " combusts",
entity.identifyType() + " combusts"),
npc, player, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Duration.matches(determination))
event.setDuration(Duration.valueOf(determination).getTicksAsInt());
}
// <--[event]
// @Events
// entity changes block
// entity changes block (into <material>) (in <notable cuboid>)
// entity changes <material> (into <material>) (in <notable cuboid>)
// <entity> changes block (into <material>) (in <notable cuboid>)
// <entity> changes <material> (into <material>) (in <notable cuboid>)
//
// @Triggers when an entity changes the material of a block.
// @Context
// <context.entity> returns the dEntity that changed the block.
// <context.location> returns the dLocation of the changed block.
// <context.old_material> returns the old material of the block.
// <context.new_material> returns the new material of the block.
//
// @Determine
// "CANCELLED" to stop the entity from changing the block.
//
// -->
@EventHandler
public void entityChangeBlock(EntityChangeBlockEvent event) {
List<String> events = new ArrayList<String>();
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
dMaterial oldMaterial = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
dMaterial newMaterial = dMaterial.getMaterialFrom(event.getTo()); // Not able to get DATA here?
dLocation location = new dLocation(event.getBlock().getLocation());
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(location);
if (cuboids.size() > 0) {
events.add("entity changes block in notable cuboid");
events.add("entity changes " + oldMaterial.identifySimple() + " in notable cuboid");
events.add("entity changes block into " + newMaterial.identifySimple() + " in notable cuboid");
events.add("entity changes " + oldMaterial.identifySimple() + " into " + newMaterial.identifySimple()
+ " in notable cuboid");
events.add(entity.identifyType() + " changes block in notable cuboid");
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple() + " in notable cuboid");
events.add(entity.identifyType() + " changes block into " + newMaterial.identifySimple()
+ " in notable cuboid");
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple() + " into "
+ newMaterial.identifySimple() + " in notable cuboid");
}
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("entity changes block in " + cuboid.identifySimple());
events.add("entity changes " + oldMaterial.identifySimple() + " in " + cuboid.identifySimple());
events.add("entity changes block into " + newMaterial.identifySimple() + " in " + cuboid.identifySimple());
events.add("entity changes " + oldMaterial.identifySimple() + " into " + newMaterial.identifySimple()
+ " in " + cuboid.identifySimple());
events.add(entity.identifyType() + " changes block in " + cuboid.identifySimple());
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple() + " in "
+ cuboid.identifySimple());
events.add(entity.identifyType() + " changes block into " + newMaterial.identifySimple() + " in "
+ cuboid.identifySimple());
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple() + " into "
+ newMaterial.identifySimple() + " in " + cuboid.identifySimple());
cuboid_context.add(cuboid.identify());
}
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
context.put("entity", entity.getDenizenObject());
context.put("location", location);
context.put("old_material", oldMaterial);
context.put("new_material", newMaterial);
events.add("entity changes block");
events.add("entity changes " + oldMaterial.identifySimple());
events.add("entity changes block into " + newMaterial.identifySimple());
events.add("entity changes " + oldMaterial.identifySimple() + " into " + newMaterial.identifySimple());
events.add(entity.identifyType() + " changes block");
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple());
events.add(entity.identifyType() + " changes block into " + newMaterial.identifySimple());
events.add(entity.identifyType() + " changes " + oldMaterial.identifySimple() + " into "
+ newMaterial.identifySimple());
String determination = doEvents(events, null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity explodes
// <entity> explodes
//
// @Triggers when an entity explodes.
// @Context
// <context.blocks> returns a dList of blocks that the entity blew up.
// <context.entity> returns the dEntity that exploded.
// <context.location> returns the dLocation the entity blew up at.
// <contect.strength> returns an Element(Decimal) of the strength of the explosion.
//
// @Determine
// "CANCELLED" to stop the entity from exploding.
// dList(dLocation) to set a new lists of blocks that are to be affected by the explosion.
// Element(Decimal) to change the strength of the explosion.
//
// -->
@EventHandler
public void entityExplode(EntityExplodeEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
if (event.getEntity() == null) {
return; // Fix for other plugins doing weird stuff.
}
dEntity entity = new dEntity(event.getEntity());
context.put("entity", entity.getDenizenObject());
context.put("location", new dLocation(event.getLocation()));
context.put("strength", new Element(event.getYield()));
String blocks = "";
for (Block block : event.blockList()) {
blocks = blocks + new dLocation(block.getLocation()) + "|";
}
context.put("blocks", new dList(blocks));
String determination = doEvents(Arrays.asList
("entity explodes",
entity.identifyType() + " explodes",
entity.identifySimple() + " explodes"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (aH.matchesDouble(determination)) {
event.setYield(new Element(determination).asFloat());
}
else if (determination.length() > 0 && !determination.equalsIgnoreCase("none")) {
dList list = dList.valueOf(determination);
event.blockList().clear();
for (String loc: list) {
dLocation location = dLocation.valueOf(loc);
if (location == null)
dB.echoError("Invalid location '" + loc + "'");
else
event.blockList().add(location.getWorld().getBlockAt(location));
}
}
}
// <--[event]
// @Events
// potion splash
// <item> splashes
//
// @Triggers when a splash potion breaks open
// @Context
// <context.potion> returns a dItem of the potion that broke open.
// <context.entities> returns a dList of effected entities.
// <context.location> returns the dLocation the splash potion broke open at.
// <context.entity> returns a dEntity of the splash potion.
//
// @Determine
// "CANCELLED" to stop the potion effects.
//
// -->
@EventHandler
public void potionSplash(PotionSplashEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
dItem potion = new dItem(event.getEntity().getItem());
context.put("entity", entity);
context.put("location", new dLocation(entity.getLocation()));
context.put("potion", potion);
dList entities = new dList();
for (Entity e: event.getAffectedEntities()) {
entities.add(new dEntity(e).identify());
}
context.put("entities", entities);
String determination = doEvents(Arrays.asList
("potion splash",
potion.identifySimple() + " splashes"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity heals (because <cause>)
// <entity> heals (because <cause>)
//
// @Triggers when an entity heals.
// @Context
// <context.amount> returns the amount the entity healed.
// <context.entity> returns the dEntity that healed.
// <context.reason> returns the cause of the entity healing. Can be: REGEN, SATIATED, EATING, ENDER_CRYSTAL,
// MAGIC, MAGIC_REGEN, WITHER_SPAWN, WITHER, CUSTOM
//
// @Determine
// "CANCELLED" to stop the entity from healing.
// Element(Decimal) to set the amount of health the entity receives.
//
// -->
@EventHandler
public void entityRegainHealth(EntityRegainHealthEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
String reason = event.getRegainReason().name();
context.put("reason", new Element(event.getRegainReason().name()));
context.put("amount", new Element(event.getAmount()));
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String determination = doEvents(Arrays.asList
("entity heals",
"entity heals because " + reason,
entity.identifyType() + " heals",
entity.identifyType() + " heals because " + reason),
npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Double)) {
event.setAmount(aH.getDoubleFrom(determination));
}
}
// <--[event]
// @Events
// entity enters portal
// <entity> enters portal
//
// @Triggers when an entity enters a portal.
// @Context
// <context.entity> returns the dEntity.
// <context.location> returns the dLocation of the portal block touched by the entity.
//
// -->
@EventHandler // TODO: This fires very rapidly. Smart event?
public void entityPortalEnter(EntityPortalEnterEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("location", new dLocation(event.getLocation()));
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
doEvents(Arrays.asList
("entity enters portal",
entity.identifyType() + " enters portal"),
npc, player, context, true);
}
// <--[event]
// @Events
// entity exits portal
// <entity> exits portal
//
// @Triggers when an entity exits a portal.
// @Context
// <context.entity> returns the dEntity.
// <context.location> returns the dLocation of the portal block touched by the entity.
//
// -->
@EventHandler
public void entityPortalExit(EntityPortalExitEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("location", new dLocation(event.getTo()));
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
doEvents(Arrays.asList
("entity exits portal",
entity.identifyType() + " exits portal"),
npc, player, context, true);
}
// <--[event]
// @Events
// player uses portal
//
// @Triggers when a player enters a portal.
// @Context
// <context.from> returns the location teleported from.
// <context.to> returns the location teleported to.
// @Determine
// "CANCELLED" to stop the teleport.
// dLocation to change the destination.
// -->
@EventHandler
public void playerPortalEnter(PlayerPortalEvent event) {
if (event.getPlayer() == null || event.getFrom() == null ||
event.getTo() == null) {
// If some other plugin is messing with this event... just back off.
return;
}
if (dEntity.isNPC(event.getPlayer()))
return;
dPlayer player = dEntity.getPlayerFrom(event.getPlayer());
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("from", new dLocation(event.getFrom()));
context.put("to", new dLocation(event.getTo()));
String determination = doEvents(Arrays.asList
("player uses portal"), null, player, context);
if (determination.toUpperCase().startsWith("CANCELLED")) {
event.setCancelled(true);
}
else if (dLocation.matches(determination)) {
event.setTo(dLocation.valueOf(determination));
}
}
// <--[event]
// @Events
// entity shoots bow
// <entity> shoots bow
// entity shoots <item>
// <entity> shoots <item>
//
// @Triggers when an entity shoots something out of a bow.
// @Context
// <context.entity> returns the dEntity that shot the bow.
// <context.projectile> returns a dEntity of the projectile.
// <context.bow> returns the bow item used to shoot.
// <context.force> returns the force of the shot.
//
// @Determine
// "CANCELLED" to stop the entity from shooting the bow.
// dList(dEntity) to change the projectile(s) being shot.
//
// -->
@EventHandler
public void entityShootBow(EntityShootBowEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dPlayer player = null;
dNPC npc = null;
dItem bow = new dItem(event.getBow());
dEntity projectile = new dEntity(event.getProjectile());
dEntity entity = new dEntity(event.getEntity());
context.put("bow", bow);
context.put("projectile", projectile);
context.put("entity", entity.getDenizenObject());
context.put("force", new Element(event.getForce() * 3));
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String determination = doEvents(Arrays.asList
("entity shoots bow",
"entity shoots " + bow.identifySimple(),
entity.identifyType() + " shoots bow",
entity.identifyType() + " shoots " + bow.identifySimple()),
npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED")) {
event.setCancelled(true);
}
// Don't use event.setProjectile() because it doesn't work
else if (Argument.valueOf(determination).matchesArgumentList(dEntity.class)) {
event.setCancelled(true);
// Get the list of entities
Object list = dList.valueOf(determination).filter(dEntity.class);
@SuppressWarnings("unchecked")
List<dEntity> newProjectiles = (List<dEntity>) list;
// Go through all the entities, spawning/teleporting them
for (dEntity newProjectile : newProjectiles) {
newProjectile.spawnAt(entity.getEyeLocation()
.add(entity.getEyeLocation().getDirection()));
// Set the entity as the shooter of the projectile,
// where applicable
if (newProjectile.isProjectile()) {
newProjectile.setShooter(entity);
}
}
// Mount the projectiles on top of each other
Position.mount(Conversion.convertEntities(newProjectiles));
// Get the last entity on the list, i.e. the one at the bottom
// if there are many mounted on top of each other
Entity lastProjectile = newProjectiles.get
(newProjectiles.size() - 1).getBukkitEntity();
// Give it the same velocity as the arrow that would
// have been shot by the bow
// Note: No, I can't explain why this has to be multiplied by three, it just does.
lastProjectile.setVelocity(event.getEntity().getLocation()
.getDirection().multiply(event.getForce() * 3));
}
}
// <--[event]
// @Events
// entity tamed
// <entity> tamed
// player tames entity
// player tames <entity>
//
// @Triggers when an entity is tamed.
// @Context
// <context.entity> returns a dEntity of the tamed entity.
//
// @Determine
// "CANCELLED" to stop the entity from being tamed.
//
// -->
@EventHandler
public void entityTame(EntityTameEvent event) {
dPlayer player = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("entity", entity);
List<String> events = new ArrayList<String>();
events.add("entity tamed");
events.add(entity.identifyType() + " tamed");
if (event.getOwner() instanceof Player) {
player = dEntity.getPlayerFrom((Player) event.getOwner());
events.add("player tames entity");
events.add("player tames " + entity.identifyType());
}
String determination = doEvents(events, null, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity targets (<entity>)
// entity targets (<entity>) because <cause>
// <entity> targets (<entity>)
// <entity> targets (<entity>) because <cause>
//
// @Triggers when an entity targets a new entity.
// @Context
// <context.entity> returns the targeting entity.
// <context.reason> returns the reason the entity changed targets.
// <context.target> returns the targeted entity.
//
// @Determine
// "CANCELLED" to stop the entity from being targeted.
// dEntity to make the entity target a different entity instead.
//
// -->
@EventHandler
public void entityTarget(EntityTargetEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
final dEntity entity = new dEntity(event.getEntity());
String reason = event.getReason().name();
context.put("entity", entity);
context.put("reason", new Element(reason));
List<String> events = new ArrayList<String>();
events.add("entity targets");
events.add("entity targets because " + reason);
events.add(entity.identifyType() + " targets");
events.add(entity.identifyType() + " targets because " + reason);
if (event.getTarget() != null) {
dEntity target = new dEntity(event.getTarget());
context.put("target", target.getDenizenObject());
if (target.isCitizensNPC()) { npc = target.getDenizenNPC(); }
else if (target.isPlayer()) player = target.getDenizenPlayer();
events.add("entity targets entity");
events.add("entity targets entity because " + reason);
events.add("entity targets " + target.identifyType());
events.add("entity targets " + target.identifyType() + " because " + reason);
events.add(entity.identifyType() + " targets entity");
events.add(entity.identifyType() + " targets entity because " + reason);
events.add(entity.identifyType() + " targets " + target.identifyType());
events.add(entity.identifyType() + " targets " + target.identifyType() + " because " + reason);
}
String determination = doEvents(events, npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
// If the determination matches a dEntity, change the event's target
// using a scheduled task (otherwise, the target will not be changed)
//
// Note: this does not work with all monster types
else if (dEntity.matches(determination)) {
final dEntity newTarget = dEntity.valueOf(determination);
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
if (newTarget.isValid() && newTarget.isLivingEntity()) {
entity.target(newTarget.getLivingEntity());
}
}
}, 1);
}
}
// <--[event]
// @Events
// entity unleashed (because <reason>)
// <entity> unleashed (because <reason>)
//
// @Triggers when an entity is unleashed.
// @Context
// <context.entity> returns the dEntity.
// <context.reason> returns an Element of the reason for the unleashing.
// @Note
// Reasons include DISTANCE, HOLDER_GONE, PLAYER_UNLEASH, and UNKNOWN
//
// -->
@EventHandler
public void entityUnleash(EntityUnleashEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
String reason = event.getReason().name();
context.put("entity", entity.getDenizenObject());
context.put("reason", new Element(reason));
doEvents(Arrays.asList
("entity unleashed",
"entity unleashed because " + reason,
entity.identifyType() + " unleashed",
entity.identifyType() + " unleashed because " + reason),
null, null, context, true);
}
// <--[event]
// @Events
// entity explosion primes
// <entity> explosion primes
//
// @Triggers when an entity decides to explode.
// @Context
// <context.entity> returns the dEntity.
// <context.radius> returns an Element of the explosion's radius.
// <context.fire> returns an Element with a value of "true" if the explosion will create fire and "false" otherwise.
//
// @Determine
// "CANCELLED" to stop the entity from deciding to explode.
// TODO: Set radius, set fire-state (mulitple-determinations)
//
// -->
@EventHandler
public void explosionPrimeEvent(ExplosionPrimeEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Entity entity = event.getEntity();
context.put("entity", new dEntity(entity));
context.put("radius", new Element(event.getRadius()));
context.put("fire", new Element(event.getFire()));
String determination = doEvents(Arrays.asList
("entity explosion primes",
entity.getType().name() + " explosion primes"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity changes food level
// <entity> changes food level
//
// @Triggers when an entity's food level changes.
// @Context
// <context.entity> returns the dEntity.
// <context.food> returns an Element(Number) of the entity's new food level.
//
// @Determine
// "CANCELLED" to stop the entity's food level from changing.
// Element(Decimal) to set the entity's new food level.
//
// -->
@EventHandler
public void foodLevelChange(FoodLevelChangeEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("food", new Element(event.getFoodLevel()));
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String determination = doEvents(Arrays.asList
("entity changes food level",
entity.identifyType() + " changes food level"),
npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Integer)) {
event.setFoodLevel(aH.getIntegerFrom(determination));
}
}
// <--[event]
// @Events
// horse jumps
// (<color>) (<type>) jumps
//
// @Triggers when a horse jumps.
// @Context
// <context.entity> returns the dEntity of the horse.
// <context.color> returns an Element of the horse's color.
// <context.variant> returns an Element of the horse's variant.
// <context.power> returns an Element(Decimal) of the jump's power.
//
// @Determine
// "CANCELLED" to stop the horse from jumping.
// Element(Decimal) to set the power of the jump.
//
// -->
@EventHandler
public void horseJump(HorseJumpEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
String variant = event.getEntity().getVariant().name();
String color = event.getEntity().getColor().name();
dEntity entity = new dEntity(event.getEntity());
context.put("variant", new Element(variant));
context.put("color", new Element(color));
context.put("power", new Element(event.getPower()));
context.put("entity", entity);
String determination = doEvents(Arrays.asList
("horse jumps",
variant + " jumps",
color + " jumps",
color + " " + variant + " jumps"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Float)) {
event.setPower(aH.getFloatFrom(determination));
}
}
// <--[event]
// @Events
// item despawns
// <item> despawns
// <material> despawns
//
// @Triggers when an item entity despawns.
// @Context
// <context.item> returns the dItem of the entity.
// <context.entity> returns the dEntity.
//
// @Determine
// "CANCELLED" to stop the item entity from despawning.
//
// -->
@EventHandler
public void itemDespawn(ItemDespawnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getEntity().getItemStack());
context.put("item", item);
context.put("entity", new dEntity(event.getEntity()));
List<String> events = new ArrayList<String>();
events.add("item despawns");
events.add(item.identifySimple() + " despawns");
events.add(item.identifyMaterial() + " despawns");
String determination = doEvents(events, null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]