/
BukkitWorldScriptHelper.java
2906 lines (2464 loc) · 107 KB
/
BukkitWorldScriptHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.aufdemrand.denizen.scripts.containers.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.ScoreboardHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.aH.Argument;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.enchantment.*;
import org.bukkit.event.player.*;
import org.bukkit.event.inventory.*;
import org.bukkit.event.vehicle.*;
import org.bukkit.event.weather.*;
import org.bukkit.event.world.*;
import org.bukkit.event.entity.*;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.util.BlockIterator;
public class BukkitWorldScriptHelper implements Listener {
public BukkitWorldScriptHelper() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
public static String doEvents(List<String> events, dNPC npc, dPlayer player, Map<String, dObject> context) {
return doEvents(events, npc, player, context, false);
}
public static String doEvents(List<String> events, dNPC npc, dPlayer player, Map<String, dObject> context, boolean useids) {
List<String> determ;
if (useids) {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context, true);
}
else {
determ = OldEventManager.doEvents(events, new BukkitScriptEntryData(player, npc), context);
}
return determ.size() > 0 ? determ.get(0): "none";
}
/////////////////////
// CUSTOM EVENTS
/////////////////
// <--[event]
// @Events
// server start
//
// @Triggers when the server starts
//
// @Determine
// "CANCELLED" to save all plugins and cancel server startup.
//
// -->
public void serverStartEvent() {
// Start the 'timeEvent'
long ticks = Settings.worldScriptTimeEventFrequency().getTicks();
Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
timeEvent();
}
}, ticks, ticks);
// Fire the 'Server Start' event
String determination = doEvents(Arrays.asList("server start"),
null, null, null);
if (determination.toUpperCase().startsWith("CANCELLED"))
Bukkit.getServer().shutdown(); // TODO: WHY IS THIS AN OPTION?!
}
private final Map<String, Integer> current_time = new HashMap<String, Integer>();
// <--[event]
// @Events
// time changes (in <world>)
// <0-23>:00 in <world>
// time <0-23> in <world>
//
// @Triggers when the current time changes in a world (once per mine-hour).
// @Context
// <context.time> returns the current time.
// <context.world> returns the world.
//
// -->
public void timeEvent() {
for (World world : Bukkit.getWorlds()) {
int hour = Double.valueOf(world.getTime() / 1000).intValue();
hour = hour + 6;
// Get the hour
if (hour >= 24) hour = hour - 24;
dWorld currentWorld = new dWorld(world);
if (!current_time.containsKey(currentWorld.identifySimple())
|| current_time.get(currentWorld.identifySimple()) != hour) {
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("time", new Element(hour));
context.put("world", currentWorld);
doEvents(Arrays.asList
("time changes",
"time changes in " + currentWorld.identifySimple(),
String.valueOf(hour) + ":00 in " + currentWorld.identifySimple(),
"time " + String.valueOf(hour) + " in " + currentWorld.identifySimple()),
null, null, context, true);
current_time.put(currentWorld.identifySimple(), hour);
}
}
}
/////////////////////
// Additional EVENTS
/////////////////
// <--[event]
// @Events
// entity unleashed (because <reason>)
// <entity> unleashed (because <reason>)
//
// @Triggers when an entity is unleashed.
// @Context
// <context.entity> returns the dEntity.
// <context.reason> returns an Element of the reason for the unleashing.
// @Note
// Reasons include DISTANCE, HOLDER_GONE, PLAYER_UNLEASH, and UNKNOWN
//
// -->
@EventHandler
public void entityUnleash(EntityUnleashEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
String reason = event.getReason().name();
context.put("entity", entity.getDenizenObject());
context.put("reason", new Element(reason));
doEvents(Arrays.asList
("entity unleashed",
"entity unleashed because " + reason,
entity.identifyType() + " unleashed",
entity.identifyType() + " unleashed because " + reason),
null, null, context, true);
}
// <--[event]
// @Events
// entity explosion primes
// <entity> explosion primes
//
// @Triggers when an entity decides to explode.
// @Context
// <context.entity> returns the dEntity.
// <context.radius> returns an Element of the explosion's radius.
// <context.fire> returns an Element with a value of "true" if the explosion will create fire and "false" otherwise.
//
// @Determine
// "CANCELLED" to stop the entity from deciding to explode.
// TODO: Set radius, set fire-state (mulitple-determinations)
//
// -->
@EventHandler
public void explosionPrimeEvent(ExplosionPrimeEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
Entity entity = event.getEntity();
context.put("entity", new dEntity(entity));
context.put("radius", new Element(event.getRadius()));
context.put("fire", new Element(event.getFire()));
String determination = doEvents(Arrays.asList
("entity explosion primes",
entity.getType().name() + " explosion primes"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// entity changes food level
// <entity> changes food level
//
// @Triggers when an entity's food level changes.
// @Context
// <context.entity> returns the dEntity.
// <context.food> returns an Element(Number) of the entity's new food level.
//
// @Determine
// "CANCELLED" to stop the entity's food level from changing.
// Element(Decimal) to set the entity's new food level.
//
// -->
@EventHandler
public void foodLevelChange(FoodLevelChangeEvent event) {
dPlayer player = null;
dNPC npc = null;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("food", new Element(event.getFoodLevel()));
context.put("entity", entity.getDenizenObject());
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String determination = doEvents(Arrays.asList
("entity changes food level",
entity.identifyType() + " changes food level"),
npc, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Integer)) {
event.setFoodLevel(aH.getIntegerFrom(determination));
}
}
// <--[event]
// @Events
// horse jumps
// (<color>) (<type>) jumps
//
// @Triggers when a horse jumps.
// @Context
// <context.entity> returns the dEntity of the horse.
// <context.color> returns an Element of the horse's color.
// <context.variant> returns an Element of the horse's variant.
// <context.power> returns an Element(Decimal) of the jump's power.
//
// @Determine
// "CANCELLED" to stop the horse from jumping.
// Element(Decimal) to set the power of the jump.
//
// -->
@EventHandler
public void horseJump(HorseJumpEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
String variant = event.getEntity().getVariant().name();
String color = event.getEntity().getColor().name();
dEntity entity = new dEntity(event.getEntity());
context.put("variant", new Element(variant));
context.put("color", new Element(color));
context.put("power", new Element(event.getPower()));
context.put("entity", entity);
String determination = doEvents(Arrays.asList
("horse jumps",
variant + " jumps",
color + " jumps",
color + " " + variant + " jumps"),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Float)) {
event.setPower(aH.getFloatFrom(determination));
}
}
// <--[event]
// @Events
// item despawns
// <item> despawns
// <material> despawns
//
// @Triggers when an item entity despawns.
// @Context
// <context.item> returns the dItem of the entity.
// <context.entity> returns the dEntity.
//
// @Determine
// "CANCELLED" to stop the item entity from despawning.
//
// -->
@EventHandler
public void itemDespawn(ItemDespawnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getEntity().getItemStack());
context.put("item", item);
context.put("entity", new dEntity(event.getEntity()));
List<String> events = new ArrayList<String>();
events.add("item despawns");
events.add(item.identifySimple() + " despawns");
events.add(item.identifyMaterial() + " despawns");
String determination = doEvents(events, null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// item spawns
// <item> spawns
// <material> spawns
//
// @Triggers when an item entity spawns.
// @Context
// <context.item> returns the dItem of the entity.
// <context.entity> returns the dEntity.
// <context.location> returns the location of the entity to be spawned.
//
// @Determine
// "CANCELLED" to stop the item entity from spawning.
//
// -->
@EventHandler
public void itemSpawn(ItemSpawnEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = new dItem(event.getEntity().getItemStack());
context.put("item", item);
context.put("entity", new dEntity(event.getEntity()));
context.put("location", new dLocation(event.getLocation()));
List<String> events = new ArrayList<String>();
events.add("item spawns");
events.add(item.identifySimple() + " spawns");
events.add(item.identifyMaterial() + " spawns");
String determination = doEvents(events, null, null, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// pig zapped
//
// @Triggers when a pig is zapped by lightning and turned into a pig zombie.
// @Context
// <context.pig> returns the dEntity of the pig.
// <context.pig_zombie> returns the dEntity of the pig zombie.
// <context.lightning> returns the dEntity of the lightning.
//
// @Determine
// "CANCELLED" to stop the pig from being zapped.
//
// -->
@EventHandler
public void pigZap(PigZapEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity pig = new dEntity(event.getEntity());
dEntity pigZombie = new dEntity(event.getPigZombie());
dEntity lightning = new dEntity(event.getLightning());
context.put("pig", pig);
context.put("pig_zombie", pigZombie);
context.put("lightning", lightning);
String determination = doEvents(Arrays.asList
("pig zapped"), null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// projectile hits block
// projectile hits <material>
// <projectile> hits block
// <projectile> hits <material>
//
// @Triggers when a projectile hits a block.
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
@EventHandler
public void projectileHit(ProjectileHitEvent event) {
dPlayer player = null;
dNPC npc = null;
if (event.getEntity() == null)
return;
dEntity projectile = new dEntity(event.getEntity());
if (projectile.getLocation() == null)
return; // No, I can't explain how or why this would ever happen... nonetheless, it appears it does happen sometimes.
if (Double.isNaN(projectile.getLocation().getDirection().normalize().getX()))
return; // I can't explain this one either. It also chooses to happen whenever it pleases.
Block block = null;
try {
BlockIterator bi = new BlockIterator(projectile.getLocation().getWorld(),
projectile.getLocation().toVector(), projectile.getLocation().getDirection().normalize(), 0, 4);
while(bi.hasNext()) {
block = bi.next();
if(block.getTypeId() != 0) {
break;
}
}
}
catch (IllegalStateException ex) {
// This happens because it can. Also not explainable whatsoever.
// As this error happens on no fault of the user, display no error message... just cancel the event.
return;
}
if (block == null)
return;
dEntity shooter = projectile.getShooter();
dMaterial material = dMaterial.getMaterialFrom(block.getType(), block.getData());
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("projectile", projectile);
context.put("location", new dLocation(block.getLocation()));
List<String> events = new ArrayList<String>();
events.add("projectile hits block");
events.add("projectile hits " + material.identifySimple());
events.add(projectile.identifyType() + " hits block");
events.add(projectile.identifyType() + " hits " + material.identifySimple());
if (shooter != null) {
context.put("shooter", shooter.getDenizenObject());
// <--[event]
// @Events
// entity shoots block
// entity shoots <material> (with <projectile>)
// <entity> shoots block
// <entity> shoots <material> (with <projectile>)
//
// @Triggers when a projectile shot by an entity hits a block.
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
if (shooter.isCitizensNPC()) { npc = shooter.getDenizenNPC(); }
else if (shooter.isPlayer()) { player = shooter.getDenizenPlayer(); }
events.add("entity shoots block");
events.add("entity shoots block with " + projectile.identifyType());
events.add("entity shoots " + material.identifySimple() + " with " + projectile.identifyType());
events.add("entity shoots " + material.identifySimple());
events.add(shooter.identifyType() + " shoots block");
events.add(shooter.identifyType() + " shoots block with " + projectile.identifyType());
events.add(shooter.identifyType() + " shoots " + material.identifySimple() + " with " + projectile.identifyType());
events.add(shooter.identifyType() + " shoots " + material.identifySimple());
}
doEvents(events, npc, player, context, true);
}
// <--[event]
// @Events
// projectile launched
// <entity> launched
//
// @Triggers when a projectile is launched.
// @Context
// <context.entity> returns the projectile.
//
// @Determine
// "CANCELLED" to stop it from being launched.
//
// -->
@EventHandler
public void projectileLaunch(ProjectileLaunchEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
List<String> events = new ArrayList<String>();
dEntity projectile = new dEntity(event.getEntity());
context.put("entity", projectile);
events.add("projectile launched");
events.add(projectile.identifySimple() + " launched");
events.add(projectile.identifyType() + " launched");
String Determination = doEvents(events, null, null, context, true);
if (Determination.equalsIgnoreCase("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// player dyes sheep (<color>)
// sheep dyed (<color>)
//
// @Triggers when a sheep is dyed by a player.
// @Context
// <context.entity> returns the dEntity of the sheep.
// <context.color> returns an Element of the color the sheep is being dyed.
//
// @Determine
// "CANCELLED" to stop it from being dyed.
// Element(String) that matches DyeColor to dye it a different color.
//
// -->
@EventHandler
public void sheepDyeWool(SheepDyeWoolEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
String color = event.getColor().name();
context.put("entity", entity);
context.put("color", new Element(color));
String determination = doEvents(Arrays.asList
("player dyes sheep",
"player dyes sheep " + color,
"sheep dyed",
"sheep dyed " + color), null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (determination.equals(determination.toUpperCase())) {
try {
event.setColor(DyeColor.valueOf(determination));
}
catch (IllegalArgumentException e) {
dB.echoError("Unknown dye color " + determination);
}
}
}
// <--[event]
// @Events
// sheep regrows wool
//
// @Triggers when a sheep regrows wool.
// @Context
// <context.entity> returns the dEntity of the sheep.
//
// @Determine
// "CANCELLED" to stop it from regrowing wool.
//
// -->
@EventHandler
public void sheepRegrowWool(SheepRegrowWoolEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
context.put("entity", entity);
String determination = doEvents(Arrays.asList
("sheep regrows wool"), null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
// <--[event]
// @Events
// slime splits (into <#>)
//
// @Triggers when a slime splits into smaller slimes.
// @Context
// <context.entity> returns the dEntity of the slime.
// <context.count> returns an Element(Number) of the number of smaller slimes it will split into.
//
// @Determine
// "CANCELLED" to stop it from splitting.
// Element(Number) to set the number of smaller slimes it will split into.
//
// -->
@EventHandler
public void slimeSplit(SlimeSplitEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getEntity());
int count = event.getCount();
context.put("entity", entity);
context.put("count", new Element(count));
String determination = doEvents(Arrays.asList
("slime splits",
"slime splits into " + count),
null, null, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination)
.matchesPrimitive(aH.PrimitiveType.Integer)) {
event.setCount(aH.getIntegerFrom(determination));
}
}
/////////////////////
// INVENTORY EVENTS
/////////////////
// <--[event]
// @Events
// item crafted
// <item> crafted
// <material> crafted
//
// @Triggers when an item's recipe is correctly formed.
// @Context
// <context.inventory> returns the dInventory of the crafting inventory.
// <context.item> returns the dItem to be crafted.
// <context.recipe> returns a dList of dItems in the recipe.
//
// @Determine
// "CANCELLED" to stop the item from being crafted.
// dItem to change the item that is crafted.
//
// -->
@EventHandler
public void craftItemEvent(PrepareItemCraftEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
List<String> events = new ArrayList<String>();
events.add("item crafted");
CraftingInventory inventory = event.getInventory();
context.put("inventory", new dInventory(inventory));
Recipe recipe = event.getRecipe();
dItem result = recipe.getResult() != null ? new dItem(recipe.getResult()) : null;
if (result != null) {
context.put("item", result);
events.add(result.identifySimple() + " crafted");
events.add(result.identifyMaterial() + " crafted");
}
dList recipeList = new dList();
for (ItemStack item : inventory.getMatrix()) {
if (item != null)
recipeList.add(new dItem(item).identify());
else
recipeList.add(new dItem(Material.AIR).identify());
}
context.put("recipe", recipeList);
Player player = (Player) event.getView().getPlayer();
String determination = doEvents(events, null, dEntity.getPlayerFrom(player), context);
if (determination.toUpperCase().startsWith("CANCELLED")) {
inventory.setResult(null);
player.updateInventory();
}
else if (dItem.matches(determination)) {
inventory.setResult(dItem.valueOf(determination).getItemStack());
player.updateInventory();
}
}
// <--[event]
// @Events
// item enchanted
// <item> enchanted
//
// @Triggers when an item is enchanted.
// @Context
// <context.location> returns the dLocation of the enchanting table.
// <context.inventory> returns the dInventory of the enchanting table.
// <context.item> returns the dItem to be enchanted.
// <context.button> returns which button was pressed to initiate the enchanting.
// <context.cost> returns the experience level cost of the enchantment.
//
// @Determine
// Element(Number) to set the experience level cost of the enchantment.
// "CANCELLED" to stop the item from being enchanted.
//
// -->
@EventHandler
public void enchantItemEvent(EnchantItemEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dPlayer player = dEntity.getPlayerFrom(event.getEnchanter());
dItem item = new dItem(event.getItem());
context.put("location", new dLocation(event.getEnchantBlock().getLocation()));
context.put("inventory", dInventory.mirrorBukkitInventory(event.getInventory()));
context.put("item", item);
context.put("button", new Element(event.whichButton()));
context.put("cost", new Element(event.getExpLevelCost()));
String determination = doEvents(Arrays.asList
("item enchanted",
item.identifySimple() + " enchanted"),
null, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (Argument.valueOf(determination).matchesPrimitive(aH.PrimitiveType.Integer))
event.setExpLevelCost(Integer.valueOf(determination));
}
// <--[language]
// @Name Inventory Actions
// @Group Useful Lists
// @Description
// Used by some inventory world events to describe the action of the inventory event.
//
// Actions, as described by the bukkit javadocs:
// CLONE_STACK
// A max-size stack of the clicked item is put on the cursor.
// COLLECT_TO_CURSOR
// The inventory is searched for the same material, and they are put on the cursor up to
// m@material.max_stack_size.
// DROP_ALL_CURSOR
// The entire cursor item is dropped.
// DROP_ALL_SLOT
// The entire clicked slot is dropped.
// DROP_ONE_CURSOR
// One item is dropped from the cursor.
// DROP_ONE_SLOT
// One item is dropped from the clicked slot.
// HOTBAR_MOVE_AND_READD
// The clicked item is moved to the hotbar, and the item currently there is re-added to the
// player's inventory.
// HOTBAR_SWAP
// The clicked slot and the picked hotbar slot are swapped.
// MOVE_TO_OTHER_INVENTORY
// The item is moved to the opposite inventory if a space is found.
// NOTHING
// Nothing will happen from the click.
// PICKUP_ALL
// All of the items on the clicked slot are moved to the cursor.
// PICKUP_HALF
// Half of the items on the clicked slot are moved to the cursor.
// PICKUP_ONE
// One of the items on the clicked slot are moved to the cursor.
// PICKUP_SOME
// Some of the items on the clicked slot are moved to the cursor.
// PLACE_ALL
// All of the items on the cursor are moved to the clicked slot.
// PLACE_ONE
// A single item from the cursor is moved to the clicked slot.
// PLACE_SOME
// Some of the items from the cursor are moved to the clicked slot (usually up to the max stack size).
// SWAP_WITH_CURSOR
// The clicked item and the cursor are exchanged.
// UNKNOWN
// An unrecognized ClickType.
//
// -->
// <--[event]
// @Events
// player clicks in inventory
// player (<click type>) clicks (<item>) (in <inventory type>) (with <item>)
// player (<click type>) clicks (<material>) (in <inventory type>) (with <item>)
// player (<click type>) clicks (<item>) (in <inventory type>) (with <material>)
// player (<click type>) clicks (<material>) (in <inventory type>) (with <material>)
//
// @Triggers when a player clicks in an inventory.
// @Context
// <context.item> returns the dItem the player has clicked on.
// <context.inventory> returns the dInventory.
// <context.cursor_item> returns the item the Player is clicking with.
// <context.click> returns an Element with the name of the click type.
// <context.slot_type> returns an Element with the name of the slot type that was clicked.
// <context.slot> returns an Element with the number of the slot that was clicked.
// <context.raw_slot> returns an Element with the raw number of the slot that was clicked.
// <context.is_shift_click> returns true if 'shift' was used while clicking.
// <context.action> returns the inventory_action. See <@link language Inventory Actions>.
// <context.hotbar_button> returns an Element of the button pressed as a number, or -1 if no number button was pressed.
//
// @Determine
// "CANCELLED" to stop the player from clicking.
//
// -->
@EventHandler
public void inventoryClickEvent(InventoryClickEvent event) {
// TODO: make this a script event...
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = null;
dItem holding;
dInventory inventory = dInventory.mirrorBukkitInventory(event.getInventory());
final dPlayer player = dEntity.getPlayerFrom((Player) event.getWhoClicked());
String type = event.getInventory().getType().name();
String click = event.getClick().name();
String slotType = event.getSlotType().name();
List<String> events = new ArrayList<String>();
events.add("player clicks in inventory");
events.add("player clicks in " + type);
events.add("player clicks in " + inventory.identifySimple());
String interaction = "player " + click + " clicks ";
events.add(interaction + "in inventory");
events.add(interaction + "in " + type);
events.add(interaction + "in " + inventory.identifySimple());
if (event.getCursor() != null) {
holding = new dItem(event.getCursor());
context.put("cursor_item", holding);
events.add(interaction + "in inventory with " + holding.identifySimple());
events.add(interaction + "in " + type + " with " + holding.identifySimple());
events.add(interaction + "in " + inventory.identifySimple() + " with " + holding.identifySimple());
events.add(interaction + "in inventory with " + holding.identifyMaterial());
events.add(interaction + "in " + type + " with " + holding.identifyMaterial());
events.add(interaction + "in " + inventory.identifySimple() + " with " + holding.identifyMaterial());
events.add("player clicks in inventory with " + holding.identifySimple());
events.add("player clicks in " + type + " with " + holding.identifySimple());
events.add("player clicks in " + inventory.identifySimple() + " with " + holding.identifySimple());
events.add("player clicks in inventory with " + holding.identifyMaterial());
events.add("player clicks in " + type + " with " + holding.identifyMaterial());
events.add("player clicks in " + inventory.identifySimple() + " with " + holding.identifyMaterial());
}
if (event.getCurrentItem() != null) {
item = new dItem(event.getCurrentItem());
events.add("player clicks " +
item.identifySimple() + " in inventory");
events.add(interaction +
item.identifySimple() + " in inventory");
events.add("player clicks " +
item.identifySimple() + " in " + type);
events.add(interaction +
item.identifySimple() + " in " + type);
events.add("player clicks " +
item.identifySimple() + " in " + inventory.identifySimple());
events.add(interaction +
item.identifySimple() + " in " + inventory.identifySimple());
events.add("player clicks " +
item.identifyMaterial() + " in inventory");
events.add(interaction +
item.identifyMaterial() + " in inventory");
events.add("player clicks " +
item.identifyMaterial() + " in " + type);
events.add(interaction +
item.identifyMaterial() + " in " + type);
events.add("player clicks " +
item.identifyMaterial() + " in " + inventory.identifySimple());
events.add(interaction +
item.identifyMaterial() + " in " + inventory.identifySimple());
if (event.getCursor() != null) {
holding = new dItem(event.getCursor());
final String[] itemStrings = new String[] {
item.identifySimple(),
item.identifyMaterial()
};
final String[] inventoryStrings = new String[] {
"inventory",
type,
inventory.identifySimple()
};
final String[] holdingStrings = new String[] {
holding.identifySimple(),
holding.identifyMaterial()
};
for (String itemString : itemStrings) {
for (String inventoryString : inventoryStrings) {
for (String holdingString : holdingStrings) {
String fullString = itemString + " in " + inventoryString + " with " + holdingString;
events.add("player clicks " + fullString);
events.add(interaction + fullString);
}
}
}
}
}
context.put("item", item);
context.put("inventory", inventory);
context.put("click", new Element(click));
context.put("slot_type", new Element(slotType));
context.put("slot", new Element(event.getSlot() + 1));
context.put("raw_slot", new Element(event.getRawSlot() + 1));
context.put("is_shift_click", new Element(event.isShiftClick()));
context.put("action", new Element(event.getAction().name()));
context.put("hotbar_button", new Element(event.getHotbarButton()));
String determination = doEvents(events, null, player, context, true);
if (determination.toUpperCase().startsWith("CANCELLED")) {
event.setCancelled(true);
final InventoryHolder holder = event.getInventory().getHolder();
new BukkitRunnable() {
@Override
public void run() {
player.getPlayerEntity().updateInventory();
if (holder != null && holder instanceof Player)
((Player) holder).updateInventory();
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), 1);
}
}
// <--[event]
// @Events
// player closes inventory
// player closes <inventory type>
// npc closes inventory
// npc closes <inventory type>
//
// @Triggers when a player closes an inventory. (EG, chests, not the player's main inventory.)
// @Context
// <context.inventory> returns the dInventory.
//
// -->
@EventHandler
public void inventoryCloseEvent(InventoryCloseEvent event) {
if (dEntity.isNPC(event.getPlayer()))
return;
Map<String, dObject> context = new HashMap<String, dObject>();
dEntity entity = new dEntity(event.getPlayer());
dPlayer player = null;
dNPC npc = null;
if (entity.isCitizensNPC()) npc = entity.getDenizenNPC();
else if (entity.isPlayer()) player = entity.getDenizenPlayer();
String type = event.getInventory().getType().name();
context.put("inventory", dInventory.mirrorBukkitInventory(event.getInventory()));
doEvents(Arrays.asList
((player != null ? "player": "npc") + " closes inventory",
(player != null ? "player": "npc") + " closes " + type),
npc, player, context);
}
// <--[event]
// @Events
// player drags in inventory
// player drags (<item>) (in <inventory type>)
//
// @Triggers when a player drags in an inventory.
// @Context
// <context.item> returns the dItem the player has dragged.
// <context.inventory> returns the dInventory.
// <context.slots> returns a dList of the slot numbers dragged through.
// <context.raw_slots> returns a dList of the raw slot numbers dragged through.
//
// @Determine
// "CANCELLED" to stop the player from dragging.
//
// -->
@EventHandler
public void inventoryDragEvent(InventoryDragEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
dItem item = null;
Inventory inventory = event.getInventory();
final dPlayer player = dEntity.getPlayerFrom((Player) event.getWhoClicked());
String type = event.getInventory().getType().name();