/
BukkitWorldScriptHelper.java
3917 lines (3313 loc) · 146 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.denizencore.tags.core.EscapeTags;
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.block.Sign;
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";
}
/////////////////////
// BLOCK EVENTS
/////////////////
// <--[event]
// @Events
// furnace burns item
// furnace burns <item>
//
// @Triggers when a furnace burns an item used as fuel.
// @Context
// <context.location> returns the dLocation of the furnace.
// <context.item> returns the dItem burnt.
//
// @Determine
// "CANCELLED" to stop the furnace from burning the item.
// Element(Number) to set the burn time for this fuel.
//
// -->
@EventHandler
public void furnaceBurn(FurnaceBurnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getFuel());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("item", item);
String determination = doEvents(Arrays.asList
("furnace burns item",
"furnace burns " + item.identifySimple()),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Integer)) {
event.setBurnTime(aH.getIntegerFrom(determination));
}
}
// <--[event]
// @Events
// player takes item from furnace
// player takes <item> from furnace
// player takes <material> from furnace
//
// @Triggers when a player takes an item from a furnace.
// @Context
// <context.location> returns the dLocation of the furnace.
// <context.item> returns the dItem taken out of the furnace.
//
// @Determine
// Element(Number) to set the amount of experience the player will get.
//
// -->
@EventHandler
public void furnaceExtract(FurnaceExtractEvent event) {
if (dEntity.isNPC(event.getPlayer()))
return;
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial itemMaterial = dMaterial.getMaterialFrom(event.getItemType());
dItem item = new dItem(itemMaterial, event.getItemAmount());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("item", item);
String determination = doEvents(Arrays.asList
("player takes item from furnace",
"player takes " + item.identifySimple() + " from furnace",
"player takes " + item.identifyMaterial() + " from furnace"),
null, dEntity.getPlayerFrom(event.getPlayer()), context, true);
if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Integer)) {
event.setExpToDrop(aH.getIntegerFrom(determination));
}
}
// <--[event]
// @Events
// furnace smelts item (into <item>)
// furnace smelts <item> (into <item>)
//
// @Triggers when a furnace smelts an item.
// @Context
// <context.location> returns the dLocation of the furnace.
// <context.source_item> returns the dItem that is being smelted.
// <context.result_item> returns the dItem that is the result of the smelting.
//
// @Determine
// "CANCELLED" to stop the furnace from smelting the item.
// dItem to set the item that is the result of the smelting.
//
// -->
@EventHandler
public void furnaceSmelt(FurnaceSmeltEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem source = new dItem(event.getSource());
dItem result = new dItem(event.getResult());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("source_item", source);
context.put("result_item", result);
String determination = doEvents(Arrays.asList
("furnace smelts item",
"furnace smelts " + source.identifySimple(),
"furnace smelts item into " + result.identifySimple(),
"furnace smelts " + source.identifySimple() + " into " + result.identifySimple()),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (dItem.matches(determination)) {
event.setResult(dItem.valueOf(determination).getItemStack());
}
}
// <--[event]
// @Events
// leaves decay
// <block> decay
//
// @Triggers when leaves decay.
// @Context
// <context.location> returns the dLocation of the leaves.
// <context.material> returns the dMaterial of the leaves.
//
// @Determine
// "CANCELLED" to stop the leaves from decaying.
//
// -->
@EventHandler
public void leavesDecay(LeavesDecayEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("material", material);
String determination = doEvents(Arrays.asList
("leaves decay",
material.identifySimple() + " decay"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// player changes sign
// player changes sign in <notable cuboid>
// player changes <material>
// player changes <material> in <notable cuboid>
//
// @Triggers when a player changes a sign.
// @Context
// <context.location> returns the dLocation of the sign.
// <context.new> returns the new sign text as a dList.
// <context.old> returns the old sign text as a dList.
// <context.material> returns the dMaterial of the sign.
// <context.cuboids> returns a dList of notable cuboids surrounding the sign.
//
// @Determine
// "CANCELLED" to stop the sign from being changed.
// dList to change the lines (Uses escaping, see <@link language Property Escaping>)
//
// -->
@EventHandler
public void signChange(final SignChangeEvent event) {
if (dEntity.isNPC(event.getPlayer()))
return;
Block block = event.getBlock();
if (block == null || !(block.getState() instanceof Sign)) {
return; // Fix error induced by dark magic.
}
List<String> events = new ArrayList<String>();
Map<String, dObject> context = new HashMap<String, dObject>();
dPlayer player = dEntity.getPlayerFrom(event.getPlayer());
Sign sign = (Sign) block.getState();
dLocation location = new dLocation(block.getLocation());
dMaterial material = dMaterial.getMaterialFrom(block.getType(), block.getData());
context.put("old", new dList(Arrays.asList(sign.getLines())));
context.put("new", new dList(Arrays.asList(event.getLines())));
dList old_escaped = new dList();
for (String str: sign.getLines()) {
old_escaped.add(EscapeTags.Escape(str));
}
context.put("old_escaped", old_escaped); // Deprecated
dList new_escaped = new dList();
for (String str: event.getLines()) {
new_escaped.add(EscapeTags.Escape(str)); // Deprecated
}
context.put("new_escaped", new_escaped);
context.put("location", location);
context.put("material", material);
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(location);
if (cuboids.size() > 0) {
events.add("player changes sign in notable cuboid");
events.add("player changes " + material.identifySimple() + " in notable cuboid");
}
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("player changes sign in " + cuboid.identifySimple());
events.add("player changes " + material.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);
events.add("player changes sign");
events.add("player changes " + material.identifySimple());
String determination = doEvents(events, null, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (determination.length() > 0 && !determination.equalsIgnoreCase("none")) {
dList lines = dList.valueOf(determination);
for (int i = 0; i < 4 && i < lines.size(); i++) {
event.setLine(i, EscapeTags.unEscape(lines.get(i)));
}
}
}
/////////////////////
// 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);
}
}
}
/////////////////////
// HANGING EVENTS
/////////////////
// <--[event]
// @Events
// hanging breaks
// hanging breaks because <cause>
// <hanging> breaks
// <hanging> breaks because <cause>
//
// @Triggers when a hanging entity (painting or itemframe) is broken.
// @Context
// <context.cause> returns the cause of the entity breaking.
// <context.entity> returns the dEntity that broke the hanging entity, if any.
// <context.hanging> returns the dEntity of the hanging.
//
// @Determine
// "CANCELLED" to stop the hanging from being broken.
//
// -->
@EventHandler
public void hangingBreak(HangingBreakEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity hanging = new dEntity(event.getEntity());
String cause = event.getCause().name();
List<String> events = new ArrayList<String>();
events.add("hanging breaks");
events.add("hanging breaks because " + cause);
events.add(hanging.identifyType() + " breaks");
events.add(hanging.identifyType() +
" breaks because " + cause);
if (event instanceof HangingBreakByEntityEvent) {
// <--[event]
// @Events
// <entity> breaks hanging
// <entity> breaks hanging because <cause>
// <entity> breaks <hanging> in <notable cuboid>
// <entity> breaks <hanging> because
// <entity> breaks <hanging> because <cause>
//
// @Triggers when a hanging entity is broken by an entity.
// @Context
// <context.cause> returns the cause of the entity breaking.
// <context.entity> returns the dEntity that broke the hanging entity.
// <context.hanging> returns the hanging entity as a dEntity.
//
// @Determine
// "CANCELLED" to stop the hanging entity from being broken.
//
// -->
HangingBreakByEntityEvent subEvent = (HangingBreakByEntityEvent) event;
dEntity entity = new dEntity(subEvent.getRemover());
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getEntity().getLocation());
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add(entity.identifyType() + " breaks " + hanging.identifyType() + " in " + cuboid.identifySimple());
cuboid_context.add(cuboid.identifySimple());
}
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
events.add("entity breaks hanging");
events.add("entity breaks hanging because " + cause);
events.add("entity breaks " + hanging.identifyType());
events.add("entity breaks " + hanging.identifyType() + " because " + cause);
events.add(entity.identifyType() + " breaks hanging");
events.add(entity.identifyType() + " breaks hanging because " + cause);
events.add(entity.identifyType() + " breaks " + hanging.identifyType());
events.add(entity.identifyType() + " breaks " + hanging.identifyType() + " because " + cause);
}
// Add context
context.put("hanging", hanging);
context.put("cause", new Element(cause));
String determination = doEvents(events, npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[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)) {