-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
WorldScriptHelper.java
2332 lines (1882 loc) · 87 KB
/
WorldScriptHelper.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 net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.aH.Argument;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.Conversion;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Position;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.entity.*;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLevelChangeEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.event.vehicle.VehicleDestroyEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import org.bukkit.event.weather.LightningStrikeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
@SuppressWarnings("deprecation")
public class WorldScriptHelper implements Listener {
public static Map<String, WorldScriptContainer> world_scripts = new ConcurrentHashMap<String, WorldScriptContainer>(8, 0.9f, 1);
public WorldScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
/////////////////////
// EVENT HANDLER
/////////////////
public static String doEvents(List<String> eventNames, dNPC npc, Player player, Map<String, dObject> context) {
String determination = "none";
if (dB.showEventsFiring) dB.log("Fired for '" + eventNames.toString() + "'");
for (WorldScriptContainer script : world_scripts.values()) {
if (script == null) continue;
for (String eventName : eventNames) {
if (!script.contains("EVENTS.ON " + eventName.toUpperCase())) continue;
// Fetch script from Event
//
// Note: a "new dPlayer(null)" will not be null itself,
// so keep a ternary operator here
List<ScriptEntry> entries = script.getEntries
(player != null ? new dPlayer(player) : null,
npc, "events.on " + eventName);
if (entries.isEmpty()) continue;
dB.report("Event",
aH.debugObj("Type", "On " + eventName)
+ script.getAsScriptArg().debug()
+ (npc != null ? aH.debugObj("NPC", npc.toString()) : "")
+ (player != null ? aH.debugObj("Player", player.getName()) : "")
+ (context != null ? aH.debugObj("Context", context.toString()) : ""));
dB.echoDebug(dB.DebugElement.Header, "Building event 'On " + eventName.toUpperCase() + "' for " + script.getName());
// Create new ID -- this is what we will look for when determining an outcome
long id = DetermineCommand.getNewId();
// Add the reqId to each of the entries for the determine command (this may be slightly outdated, add to TODO)
ScriptBuilder.addObjectToEntries(entries, "ReqId", id);
// Add entries and context to the queue
ScriptQueue queue = InstantQueue.getQueue(null).addEntries(entries);
if (context != null) {
for (Map.Entry<String, dObject> entry : context.entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
}
// Start the queue!
queue.start();
// Check the determination
if (DetermineCommand.hasOutcome(id))
determination = DetermineCommand.getOutcome(id);
}
}
return determination;
}
/////////////////////
// BLOCK EVENTS
/////////////////
// <--[event]
// @Events
// player breaks block
// player breaks <block>
// player breaks block with <item>
// player breaks <block> with <item>
//
// @Triggers when a player breaks a block.
// @Context
// <context.location> will return the location the block was broken at.
// <context.type> will return the material of the block that was broken.
//
// @Determine
// "CANCELLED" to stop the block from breaking.
// "NOTHING" to make the block drop no items.
// dList(dItem) to make the block drop a specified list of items.
//
// -->
@EventHandler
public void blockBreak(BlockBreakEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Block block = event.getBlock();
String blockType = block.getType().name();
context.put("location", new dLocation(block.getLocation()));
context.put("type", new Element(blockType));
dItem item = new dItem(event.getPlayer().getItemInHand());
List<String> events = new ArrayList<String>();
events.add("player breaks block");
events.add("player breaks " + blockType);
events.add("player breaks block with " + item.identify());
events.add("player breaks " + blockType + " with " + item.identify());
if (!item.identify().equals(item.identify().split(":")[0])) {
events.add("player breaks block with " +
item.identify().split(":")[0]);
events.add("player breaks " + blockType + " with " +
item.identify().split(":")[0]);
}
if (item.isItemscript()) {
events.add("player breaks block with itemscript "
+ item.getMaterial());
events.add("player breaks " + blockType + " with itemscript "
+ item.getMaterial());
}
String determination = doEvents(events, null, event.getPlayer(), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
// Make nothing drop, usually used as "drop:nothing"
else if (determination.toUpperCase().startsWith("NOTHING")) {
event.setCancelled(true);
block.setType(Material.AIR);
}
// Get a dList of dItems to drop
else if (Argument.valueOf(determination).matchesArgumentList(dItem.class)) {
// Cancel the event
event.setCancelled(true);
block.setType(Material.AIR);
// Get the list of items
Object list = dList.valueOf(determination).filter(dItem.class);
@SuppressWarnings("unchecked")
List<dItem> newItems = (List<dItem>) list;
for (dItem newItem : newItems) {
block.getWorld().dropItemNaturally(block.getLocation(),
newItem.getItemStack()); // Drop each item
}
}
}
// <--[event]
// @Events
// block burns
// <block> burns
//
// @Triggers when a block is destroyed by fire.
// @Context
// <context.location> will return the location the block was burned at.
// <context.type> will return the material of the block that was burned.
//
// @Determine
// "CANCELLED" to stop the block from being destroyed.
//
// -->
@EventHandler
public void blockBurn(BlockBurnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
String determination = doEvents(Arrays.asList
("block burns",
event.getBlock().getType().name() + " burns"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block being built
// block being built on <material>
// <material> being built
// <material> being built on <material>
//
// @Triggers when an attempt is made to build a block on another block. Not necessarily caused by players.
// @Context
// <context.location> will return the location of the block the player is trying to build on.
// <context.old_material> will return the material of the block the player is trying to build on.
// <context.new_material> will return the material of the block the player is trying to build.
//
// @Determine
// "BUILDABLE" to allow the building.
// "CANCELLED" to cancel the building.
//
// -->
@EventHandler
public void blockCanBuild(BlockCanBuildEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial oldMaterial = new dMaterial(event.getBlock().getType());
dMaterial newMaterial = new dMaterial(event.getMaterial());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("old_material", oldMaterial);
context.put("new_material", newMaterial);
String determination = doEvents(Arrays.asList
("block being built",
"block being built on " + oldMaterial.identify(),
newMaterial.identify() + " being built",
newMaterial.identify() + " being built on " +
oldMaterial.identify()),
null, null, context);
if (determination.toUpperCase().startsWith("BUILDABLE"))
event.setBuildable(true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setBuildable(false);
}
// <--[event]
// @Events
// block ignites
// <block> ignites
//
// @Triggers when a block is set on fire.
// @Context
// <context.location> will return the location the block was set on fire at.
// <context.type> will return the material of the block that was set on fire.
//
// @Determine
// "CANCELLED" to stop the block from being ignited.
//
// -->
@EventHandler
public void blockIgnite(BlockIgniteEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
String determination = doEvents(Arrays.asList
("block ignites",
event.getBlock().getType().name() + " ignites"),
null, event.getPlayer(), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block moves
// <block> moves
//
// @Triggers when a block moves.
// @Context
// <context.location> will return the location the block moved to.
// <context.type> will return the material of the block that moved.
//
// @Determine
// "CANCELLED" to stop the block from being moved.
//
// -->
@EventHandler
public void blockPhysics(BlockPhysicsEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
String determination = doEvents(Arrays.asList
("block moves",
event.getBlock().getType().name() + " moves"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// player places block
// player places <block>
//
// @Triggers when a player places a block.
// @Context
// <context.location> will return the location the block that was placed.
// <context.type> will return the material of the block that was placed.
//
// @Determine
// "CANCELLED" to stop the block from being placed.
//
// -->
@EventHandler
public void blockPlace(BlockPlaceEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
String determination = doEvents(Arrays.asList
("player places block",
"player places " + event.getBlock().getType().name()),
null, event.getPlayer(), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block powered
// <block> powered
// block unpowered
// <block> unpowered
//
// @Triggers when a block is (un)powered.
// @Context
// <context.location> will return the location of the block that was (un)powered.
// <context.type> will return the material of the block that was (un)powered.
//
// @Determine
// "CANCELLED" to stop the block from being (un)powered.
//
// -->
@EventHandler
public void blockRedstone(BlockRedstoneEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
List<String> events = new ArrayList<String>();
if (event.getNewCurrent() > 0) {
events.add("block powered");
events.add(event.getBlock().getType().name() + " powered");
}
else {
events.add("block unpowered");
events.add(event.getBlock().getType().name() + " unpowered");
}
String determination = doEvents(events, null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setNewCurrent(event.getOldCurrent());
}
// <--[event]
// @Events
// block spreads
// <block> spreads
//
// @Triggers when a liquid block spreads.
// @Context
// <context.destination> will return the location the block spread to.
// <context.location> will return the location the block spread from.
// <context.type> will return the material of the block that spread.
//
// @Determine
// "CANCELLED" to stop the block from spreading.
//
// -->
@EventHandler
public void blockFromTo(BlockFromToEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("type", new Element(event.getBlock().getType().name()));
context.put("destination", new dLocation(event.getToBlock().getLocation()));
String determination = doEvents(Arrays.asList
("block spreads",
event.getBlock().getType().name() + " spreads"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// player changes sign
// player changes wall_sign
// player changes sign_post
//
// @Triggers when a player changes a sign.
// @Context
// <context.location> will return the location of the sign.
// <context.new> will return the new sign text as a dList.
// <context.old> will return the old sign text as a dList.
//
// @Determine
// "CANCELLED" to stop the sign from being changed.
//
// -->
@EventHandler
public void signChange(final SignChangeEvent event) {
final Map<String, dObject> context = new HashMap<String, dObject>();
final Player player = event.getPlayer();
final Block block = event.getBlock();
Sign sign = (Sign) block.getState();
context.put("old", new dList(Arrays.asList(sign.getLines())));
context.put("location", new dLocation(block.getLocation()));
context.put("new", new dList(Arrays.asList(event.getLines())));
String determination = doEvents(Arrays.asList
("player changes sign",
"player changes " + block.getType().name()),
null, player, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
/////////////////////
// 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'
Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
timeEvent();
}
}, Settings.WorldScriptTimeEventFrequency().getTicks(), Settings.WorldScriptTimeEventFrequency().getTicks());
// Fire the 'Server Start' event
String determination = doEvents(Arrays.asList("server start"),
null, null, null);
if (determination.toUpperCase().startsWith("CANCELLED"))
Bukkit.getServer().shutdown();
}
private 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 a block is set on fire.
// @Context
// <context.time> will return the current time.
// <context.world> will return 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;
if (!current_time.containsKey(world.getName())
|| current_time.get(world.getName()) != hour) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("time", new Element(hour));
context.put("world", new dWorld(world));
doEvents(Arrays.asList("time changes in " + world.getName(),
String.valueOf(hour) + ":00 in " + world.getName(),
"time " + String.valueOf(hour) + " in " + world.getName()),
null, null, context);
current_time.put(world.getName(), hour);
}
}
}
/////////////////////
// HANGING EVENTS
/////////////////
// <--[event]
// @Events
// hanging breaks
// hanging breaks because <cause>
// <entity> breaks
// <entity> breaks because <cause>
//
// @Triggers when a hanging block is broken.
// @Context
// <context.cause> will return the cause of the block breaking.
// <context.entity> will return the entity that broke the hanging block, if any.
// <context.hanging> will return the hanging block as a dEntity.
//
// @Determine
// "CANCELLED" to stop the hanging block from being broken.
//
// -->
@EventHandler
public void hangingBreak(HangingBreakEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Player player = null;
dNPC npc = null;
String hangingType = event.getEntity().getType().name();
String cause = event.getCause().name();
context.put("hanging", new dEntity(event.getEntity()));
context.put("cause", new Element(cause));
List<String> events = new ArrayList<String>();
events.add("hanging breaks");
events.add("hanging breaks because " + cause);
events.add(hangingType + " breaks");
events.add(hangingType +
" breaks because " + cause);
if (event instanceof HangingBreakByEntityEvent) {
// <--[event]
// @Events
// <entity> breaks hanging
// <entity> breaks hanging because <cause>
// <entity> breaks <hanging>
// <entity> breaks <hanging> because <cause>
//
// @Triggers when a hanging block is broken by an entity.
// @Context
// <context.cause> will return the cause of the block breaking.
// <context.entity> will return the entity that broke the hanging block.
// <context.hanging> will return the hanging block as a dEntity.
//
// @Determine
// "CANCELLED" to stop the hanging block from being broken.
//
// -->
HangingBreakByEntityEvent subEvent = (HangingBreakByEntityEvent) event;
Entity entity = subEvent.getRemover();
String entityType = entity.getType().name();
if (CitizensAPI.getNPCRegistry().isNPC(entity)) {
npc = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(entity));
context.put("entity", npc);
entityType = "npc";
}
else if (entity instanceof Player) {
player = (Player) entity;
context.put("entity", new dPlayer((Player) entity));
}
else {
context.put("entity", new dEntity(entity));
}
events.add("entity breaks hanging");
events.add("entity breaks hanging because " + cause);
events.add("entity breaks " + hangingType);
events.add("entity breaks " + hangingType + " because " + cause);
events.add(entityType + " breaks hanging");
events.add(entityType + " breaks hanging because " + cause);
events.add(entityType + " breaks " + hangingType);
events.add(entityType + " breaks " + hangingType + " because " + cause);
}
String determination = doEvents(events, npc, player, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
/////////////////////
// ENTITY EVENTS
/////////////////
// <--[event]
// @Events
// entity spawns
// entity spawns because <cause>
// <entity> spawns
// <entity> spawns because <cause>
//
// @Triggers when an entity spawns.
// @Context
// <context.entity> will return the entity that spawned.
// <context.reason> will return the reason the entity spawned.
//
// @Determine
// "CANCELLED" to stop the entity from spawning.
//
// -->
@EventHandler
public void creatureSpawn(CreatureSpawnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Entity entity = event.getEntity();
context.put("entity", new dEntity(entity));
context.put("reason", new Element(event.getSpawnReason().name()));
String determination = doEvents(Arrays.asList
("entity spawns",
"entity spawns because " + event.getSpawnReason().name(),
entity.getType().name() + " spawns",
entity.getType().name() + " spawns because " +
event.getSpawnReason().name()),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity combusts
// <entity> combusts
//
// @Triggers when an entity combusts.
// @Context
// <context.duration> will return how long the entity takes to combust.
// <context.entity> will return the entity that combusted.
//
// @Determine
// "CANCELLED" to stop the entity from combusting.
//
// -->
@EventHandler
public void entityCombust(EntityCombustEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Entity entity = event.getEntity();
context.put("entity", new dEntity(entity));
context.put("duration", new Duration((long) event.getDuration()));
String determination = doEvents(Arrays.asList
("entity combusts",
entity.getType().name() + " combusts"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity damaged
// entity damaged by <cause>
// <entity> damaged
// <entity> damaged by <cause>
//
// @Triggers when an entity is damaged.
// @Context
// <context.cause> will return the reason the entity was damaged.
// <context.damage> will return the amount of damage dealt.
// <context.entity> will return the entity that was damaged.
//
// @Determine
// "CANCELLED" to stop the entity from being damaged.
// Element(Number) to set the amount of damage the entity receives.
//
// -->
@EventHandler
public void entityDamage(EntityDamageEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
boolean isFatal = false;
dEntity entity = new dEntity(event.getEntity());
String entityType = entity.getEntityType().name();
String cause = event.getCause().name();
String determination;
dPlayer player = null;
dNPC npc = null;
if (entity.isNPC()) {
npc = new dNPC(entity.getNPC());
context.put("entity", npc);
entityType = "npc";
}
else if (entity.isPlayer()) {
player = new dPlayer(entity.getPlayer());
context.put("entity", player);
}
else {
context.put("entity", entity);
}
context.put("damage", new Element(event.getDamage()));
context.put("cause", new Element(event.getCause().name()));
if (entity.getLivingEntity() != null) {
if (event.getDamage() >= entity.getLivingEntity().getHealth()) {
isFatal = true;
}
}
List<String> events = new ArrayList<String>();
events.add("entity damaged");
events.add("entity damaged by " + cause);
events.add(entityType + " damaged");
events.add(entityType + " damaged by " + cause);
if (isFatal) {
// <--[event]
// @Events
// entity killed
// entity killed by <cause>
// <entity> killed
// <entity> killed by <cause>
//
// @Triggers when an entity is killed.
// @Context
// <context.cause> will return the reason the entity was killed.
// <context.entity> will return the entity that was killed.
// <context.damage> will return the amount of damage dealt.
// <context.shooter> will return the shooter of the entity, if any.
//
// @Determine
// "CANCELLED" to stop the entity from being killed.
// Element(Number) to set the amount of damage the entity receives, instead of dying.
//
// -->
events.add("entity killed");
events.add("entity killed by " + cause);
events.add(entityType + " killed");
events.add(entityType + " killed by " + cause);
}
if (event instanceof EntityDamageByEntityEvent) {
// <--[event]
// @Events
// entity damages entity
// entity damages <entity>
// <entity> damages entity
// <entity> damages <entity>
//
// @Triggers when an entity damages another entity.
// @Context
// <context.cause> will return the reason the entity was damaged.
// <context.entity> will return the entity that was damaged.
// <context.damage> will return the amount of damage dealt.
// <context.damager> will return the entity damaging the other entity.
// <context.shooter> will return the shooter of the entity, if any.
//
// @Determine
// "CANCELLED" to stop the entity from being damaged.
// Element(Number) to set the amount of damage the entity receives.
//
// -->
EntityDamageByEntityEvent subEvent = (EntityDamageByEntityEvent) event;
// Have a different set of player and NPC contexts for events
// like "player damages player" from the one we have for
// "player damaged by player"
dPlayer subPlayer = null;
dNPC subNPC = null;
dEntity damager = new dEntity(subEvent.getDamager());
String damagerType = damager.getEntityType().name();
if (damager.isNPC()) {
subNPC = new dNPC(damager.getNPC());
context.put("damager", subNPC);
damagerType = "npc";
// If we had no NPC in our regular context, use this one
if (npc == null) npc = subNPC;
}
else if (damager.isPlayer()) {
subPlayer = new dPlayer(damager.getPlayer());
context.put("damager", subPlayer);
// If we had no player in our regular context, use this one
if (player == null) player = subPlayer;
}
else {
context.put("damager", damager);
if (damager.getBukkitEntity() instanceof Projectile) {
if (((Projectile) damager.getBukkitEntity()).getShooter() != null) {
dEntity shooter = new dEntity(((Projectile) damager.getBukkitEntity()).getShooter());
if (shooter.isNPC()) {
context.put("shooter", new dNPC(shooter.getNPC()));
}
else if (shooter.isPlayer()) {
context.put("shooter", new dPlayer(shooter.getPlayer()));
}
else {
context.put("shooter", shooter);
}
}
else {
context.put("shooter", new Element("null"));
}
}
}
events.add("entity damaged by entity");
events.add("entity damaged by " + damagerType);
events.add(entityType + " damaged by entity");
events.add(entityType + " damaged by " + damagerType);
// Have a new list of events for the subContextPlayer
// and subContextNPC
List<String> subEvents = new ArrayList<String>();
subEvents.add("entity damages entity");
subEvents.add("entity damages " + entityType);
subEvents.add(damagerType + " damages entity");
subEvents.add(damagerType + " damages " + entityType);
if (isFatal) {
events.add("entity killed by entity");
events.add("entity killed by " + damagerType);
events.add(entityType + " killed by entity");
events.add(entityType + " killed by " + damagerType);
// <--[event]
// @Events
// entity kills entity
// entity kills <entity>
// <entity> kills entity
// <entity> kills <entity>
//
// @Triggers when an entity kills another entity.
// @Context
// <context.cause> will return the reason the entity was killed.
// <context.entity> will return the entity that was killed.
// <context.damager> will return the entity killing the other entity.
// <context.shooter> will return the shooter of the entity, if any.
//
// @Determine
// "CANCELLED" to stop the entity from being killed.
// Element(Number) to set the amount of damage the entity receives, instead of dying.
//
// -->
subEvents.add("entity kills entity");
subEvents.add("entity kills " + entityType);
subEvents.add(damagerType + " kills entity");
subEvents.add(damagerType + " kills " + entityType);
}
determination = doEvents(subEvents, (subNPC != null ? subNPC : null), (subPlayer != null?subPlayer.getPlayerEntity():null), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Double)) {
event.setDamage(aH.getDoubleFrom(determination));
}
}
determination = doEvents(events, (npc != null ? npc : null),
(player != null && player.isOnline() ? player.getPlayerEntity() : null), context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Double)) {
event.setDamage(aH.getDoubleFrom(determination));
}
}
// <--[event]
// @Events
// projectile hits block
// <projectile> hits block
//
// @Triggers when a projectile hits a block.
// @Context