/
WorldScriptHelper.java
4735 lines (3952 loc) · 172 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.events.ScriptReloadEvent;
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.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 org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
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.server.*;
import org.bukkit.event.vehicle.*;
import org.bukkit.event.weather.*;
import org.bukkit.event.world.*;
import org.bukkit.event.entity.*;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockIterator;
@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());
}
//////////////////
// MAPS
///////////
// Store the UUIDs of dying entities along with the dEntities of their killers,
// to bring <context.damager> from "on entity killed" to "on entity dies"
public static Map<UUID, dEntity> entityKillers = new HashMap<UUID, dEntity>();
//////////////////
// PERFORMANCE
///////////
// Map for keeping the names of events
public static Map<String, List<WorldScriptContainer>> events = new HashMap<String, List<WorldScriptContainer>>();
// Builds a Map of 'world events' and scripts containing said world events.
@EventHandler
public void scanWorldEvents(ScriptReloadEvent event) {
// Loop through each world script
for (WorldScriptContainer script : world_scripts.values()) {
if (script == null) continue;
// ...and through each event inside the script.
if (script.contains("EVENTS"))
for (String eventName : script.getConfigurationSection("EVENTS").getKeys(false)) {
List<WorldScriptContainer> list;
if (events.containsKey(eventName))
list = events.get(eventName);
else
list = new ArrayList<WorldScriptContainer>();
list.add(script);
events.put(eventName, list);
}
}
// dB.echoApproval("Built events map: " + events);
}
public static List<String> trimEvents(List<String> event) {
List<String> parsed = new ArrayList<String>();
for (String e : event)
if (events.containsKey("ON " + e.toUpperCase()))
parsed.add(e);
return parsed;
}
// Strip object identifiers from world event names and add the results
// to the original list of world event names without any duplicates
public static List<String> addAlternates(List<String> events) {
Set<String> newEvents = new HashSet<String>();
for (String event : events) {
if (event.matches(".*?[a-z]{1,2}@[\\w ]+")) {
newEvents.add(event.replaceAll("[a-z]{1,2}@", ""));
}
}
List<String> finalEvents = new ArrayList<String>();
finalEvents.addAll(events);
finalEvents.addAll(newEvents);
return finalEvents;
}
/////////////////////
// EVENT HANDLER
/////////////////
public static String doEvents(List<String> eventNames, dNPC npc, Player player, Map<String, dObject> context, boolean usesIdentifiers) {
// If a list of events uses identifiers, also add those events to the list
// with their identifiers stripped
return doEvents(usesIdentifiers ? addAlternates(eventNames)
: eventNames,
npc, player, context);
}
public static String doEvents(List<String> eventNames, dNPC npc, Player player, Map<String, dObject> context) {
String determination = "none";
if (dB.showEventsFiring) dB.echoApproval("Fired world events for '" + eventNames.toString() + '\'');
for (String eventName : eventNames) {
if (events.containsKey("ON " + eventName.toUpperCase()))
for (WorldScriptContainer script : events.get("ON " + eventName.toUpperCase())) {
if (script == null) 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(script, "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(script, 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 in <notable cuboid>
// player breaks <material>
// player breaks <material> in <notable cuboid>
// player breaks block with <item>
// player breaks <material> with <item>
// player breaks <material> with <item> in <notable cuboid>
// player breaks block with <material>
// player breaks <material> with <material>
// player breaks <material> with <material> in <notable cuboid>
//
// @Triggers when a player breaks a block.
// @Context
// <context.location> returns the dLocation the block was broken at.
// <context.material> returns the dMaterial of the block that was broken.
// <context.cuboids> returns a dList of notable cuboids surrounding the block 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();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
dItem item = new dItem(event.getPlayer().getItemInHand());
// Add events
List<String> events = new ArrayList<String>();
events.add("player breaks block");
events.add("player breaks " + material.identify());
events.add("player breaks block with " + item.identify());
events.add("player breaks " + material.identify() +
" with " + item.identify());
events.add("player breaks block with " + item.identifyMaterial());
events.add("player breaks " + material.identify() +
" with " + item.identifyMaterial());
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getBlock().getLocation());
if (cuboids.size() > 0) {
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("player breaks block in " + cuboid.identify());
events.add("player breaks " + material.identify() + " in " + cuboid.identify());
events.add("player breaks " + material.identify() + " with " + item.identify() + " in " + cuboid.identify());
events.add("player breaks " + material.identify() + " with " + item.identifyMaterial() + " in " + cuboid.identify());
events.add("player breaks block in cuboid");
events.add("player breaks " + material.identify() + " in cuboid");
events.add("player breaks " + material.identify() + " with " + item.identify() + " in cuboid");
events.add("player breaks " + material.identify() + " with " + item.identifyMaterial() + " in cuboid");
cuboid_context.add(cuboid.identify());
}
// Add in cuboids context, if inside a cuboid
context.put("cuboids", cuboid_context);
}
// Trim events not used
events = trimEvents(events);
// Don't continue if there are no events to run
if (events.size() == 0) return;
// Add in more context
context.put("location", new dLocation(block.getLocation()));
context.put("material", material);
// Do events, get the determination
String determination = doEvents(events, null, event.getPlayer(), context, true);
if (determination.toUpperCase().startsWith("CANCELLED")) {
// Straight up cancel the event
event.setCancelled(true);
} else if (determination.toUpperCase().startsWith("NOTHING")) {
// Make nothing drop, usually used as "drop:nothing"
event.setCancelled(true);
block.setType(Material.AIR);
} else if (Argument.valueOf(determination).matchesArgumentList(dItem.class)) {
// Get a dList of dItems to drop
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> returns the dLocation the block was burned at.
// <context.material> returns the dMaterial 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()));
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
String determination = doEvents(Arrays.asList
("block burns",
material.identify() + " burns"),
null, null, context, true);
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> returns the dLocation of the block the player is trying to build on.
// <context.old_material> returns the dMaterial of the block the player is trying to build on.
// <context.new_material> returns the dMaterial 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 = dMaterial.getMaterialFrom(event.getBlock().getType());
dMaterial newMaterial = dMaterial.getMaterialFrom(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, true);
if (determination.toUpperCase().startsWith("BUILDABLE"))
event.setBuildable(true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setBuildable(false);
}
// <--[event]
// @Events
// player damages block
// player damages <material>
// player damages block in <notable cuboid>
// player damages <material> in <notable cuboid>
//
// @Triggers when a block is damaged by a player.
// @Context
// <context.location> returns the dLocation the block that was damaged.
// <context.material> returns the dMaterial of the block that was damaged.
// <context.cuboids> returns a dList of notable cuboids which the damaged block is contained.
//
// @Determine
// "CANCELLED" to stop the block from being damaged.
// "INSTABREAK" to make the block get broken instantly.
//
// -->
@EventHandler
public void blockDamage(BlockDamageEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
List<String> events = new ArrayList<String>();
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getBlock().getLocation());
if (cuboids.size() > 0) {
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("player damages block in " + cuboid.identify());
events.add("player damages " + material.identify() + " in " + cuboid.identify());
cuboid_context.add(cuboid.identify());
}
// Add in cuboids context, if inside a cuboid
context.put("cuboids", cuboid_context);
}
events.add("player damages block");
events.add("player damages " + material.identify());
// Trim events not used
events = trimEvents(events);
// Return if no events left to parse
if (events.size() == 0) return;
// Add in add'l context
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("material", material);
String determination = doEvents(events,
null, event.getPlayer(), context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
if (determination.toUpperCase().startsWith("INSTABREAK"))
event.setInstaBreak(true);
}
// <--[event]
// @Events
// block dispenses item
// block dispenses <item>
// <block> dispenses item
// <block> dispenses <item>
//
// @Triggers when a block dispenses an item.
// @Context
// <context.location> returns the dLocation of the dispenser.
// <context.item> returns the dItem of the item being dispensed.
//
// @Determine
// "CANCELLED" to stop the block from dispensing.
// Element(Double) to set the power with which the item is shot.
//
// -->
@EventHandler
public void blockDispense(BlockDispenseEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getItem());
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("item", item);
String determination = doEvents(Arrays.asList
("block dispenses item",
"block dispenses " + item.identify(),
material.identify() + " dispenses item",
material.identify() + " dispenses " + item.identify()),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Double)) {
event.setVelocity(event.getVelocity().normalize()
.multiply(aH.getDoubleFrom(determination)));
}
}
// <--[event]
// @Events
// block fades
// <block> fades
//
// @Triggers when a block fades, melts or disappears based on world conditions.
// @Context
// <context.location> returns the dLocation the block faded at.
// <context.material> returns the dMaterial of the block that faded.
//
// @Determine
// "CANCELLED" to stop the block from fading.
//
// -->
@EventHandler
public void blockFade(BlockFadeEvent 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
("block fades",
material.identify() + " fades"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block forms
// <block> forms
//
// @Triggers when a block is formed based on world conditions,
// e.g. when snow forms in a snow storm or ice forms
// in a snowy biome
// @Context
// <context.location> returns the dLocation the block.
// <context.material> returns the dMaterial of the block.
//
// @Determine
// "CANCELLED" to stop the block from forming.
//
// -->
@EventHandler
public void blockForm(BlockFormEvent 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
("block forms",
material.identify() + " forms"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// liquid spreads
// <liquid block> spreads
//
// @Triggers when a liquid block spreads.
// @Context
// <context.destination> returns the dLocation the block spread to.
// <context.location> returns the dLocation the block spread from.
// <context.material> returns the dMaterial 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>();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("destination", new dLocation(event.getToBlock().getLocation()));
context.put("material", material);
String determination = doEvents(Arrays.asList
("liquid spreads",
material.identify() + " spreads"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block grows
// <block> grows
//
// @Triggers when a block grows naturally in the world,
// e.g. when wheat, sugar canes, cactuses,
// watermelons or pumpkins grow
// @Context
// <context.location> returns the dLocation the block.
// <context.material> returns the dMaterial of the block.
//
// @Determine
// "CANCELLED" to stop the block from growing.
//
// -->
@EventHandler
public void blockGrow(BlockGrowEvent 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
("block grows",
material.identify() + " grows"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block ignites
// <block> ignites
//
// @Triggers when a block is set on fire.
// @Context
// <context.location> returns the dLocation the block was set on fire at.
// <context.material> returns the dMaterial 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>();
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
("block ignites",
material.identify() + " ignites"),
null, event.getPlayer(), context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// piston extends
// <block> extends
//
// @Triggers when a piston extends.
// @Context
// <context.location> returns the dLocation of the piston.
// <context.material> returns the dMaterial of the piston.
// <context.length> returns the number of blocks that will be moved by the piston.
//
// @Determine
// "CANCELLED" to stop the piston from extending.
//
// -->
@EventHandler
public void blockPistonExtend(BlockPistonExtendEvent 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);
context.put("length", new Element(event.getLength()));
String determination = doEvents(Arrays.asList
("piston extends",
material.identify() + " extends"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// piston retracts
// <block> retracts
//
// @Triggers when a piston retracts.
// @Context
// <context.location> returns the dLocation of the piston.
// <context.retract_location> returns the new dLocation of the block that
// will be moved by the piston if it is sticky.
// <context.material> returns the dMaterial of the piston.
//
// @Determine
// "CANCELLED" to stop the piston from retracting.
//
// -->
@EventHandler
public void blockPistonRetract(BlockPistonRetractEvent 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("retract_location", new dLocation(event.getRetractLocation()));
context.put("material", material);
String determination = doEvents(Arrays.asList
("piston retracts",
material.identify() + " retracts"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// player places block
// player places <block>
// player places block in <notable cuboid>
// player places <block> in <notable cuboid>
//
// @Triggers when a player places a block.
// @Context
// <context.location> returns the dLocation of the block that was placed.
// <context.material> returns the dMaterial 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>();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
List<String> events = new ArrayList<String>();
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getBlock().getLocation());
if (cuboids.size() > 0) {
dList cuboid_context = new dList();
for (dCuboid cuboid : cuboids) {
events.add("player places block in " + cuboid.identify());
events.add("player places " + material.identify() + " in " + cuboid.identify());
cuboid_context.add(cuboid.identify());
}
// Add in cuboids context, if inside a cuboid
context.put("cuboids", cuboid_context);
}
events.add("player places block");
events.add("player places " + material.identify());
// Trim events not used
events = trimEvents(events);
// Return if no events left to parse
if (events.size() == 0) return;
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("material", material);
String determination = doEvents(events,
null, event.getPlayer(), context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// block spreads
// <block> spreads
//
// @Triggers when a block spreads based on world conditions,
// e.g. when fire spreads, when mushrooms spread
// @Context
// <context.location> returns the dLocation the block.
// <context.material> returns the dMaterial of the block.
//
// @Determine
// "CANCELLED" to stop the block from growing.
//
// -->
@EventHandler
public void blockSpread(BlockSpreadEvent 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
("block spreads",
material.identify() + " spreads"),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// brewing stand brews
//
// @Triggers when a brewing stand brews a potion.
// @Context
// <context.location> returns the dLocation of the brewing stand.
// <context.inventory> returns the dInventory of the brewing stand's contents.
//
// @Determine
// "CANCELLED" to stop the brewing stand from brewing.
//
// -->
@EventHandler
public void brew(BrewEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("inventory", new dInventory(event.getContents()));
String determination = doEvents(Arrays.asList
("brewing stand brews"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity forms block
// entity forms <block>
// <entity> forms block
// <entity> forms <block>
//
// @Triggers when a block is formed by an entity,
// e.g. when a snowman forms snow
// @Context
// <context.location> returns the dLocation the block.
// <context.material> returns the dMaterial of the block.
// <context.entity> returns the dEntity that formed the block.
//
// @Determine
// "CANCELLED" to stop the block from forming.
//
// -->
@EventHandler
public void entityBlockForm(EntityBlockFormEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
dEntity entity = new dEntity(event.getEntity());
context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("material", material);
context.put("entity", entity.getDenizenObject());
String determination = doEvents(Arrays.asList
("entity forms block",
"entity forms " + material.identify(),
entity.identifyType() + " forms block",
entity.identifyType() + " forms " + material.identify()),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[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(Integer) 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.identify()),
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(Integer) to set the amount of experience the player will get.
//
// -->
@EventHandler
public void furnaceExtract(FurnaceExtractEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial itemMaterial = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
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.identify() + " from furnace",
"player takes " + item.identifyMaterial() + " from furnace"),
null, 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.identify(),
"furnace smelts item into " + result.identify(),
"furnace smelts " + source.identify() + " into " + result.identify()),
null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (dItem.matches(determination)) {
event.setResult(dItem.valueOf(determination).getItemStack());
}