/
BlockDestroyedByExplosionEvent.java
116 lines (104 loc) · 4.32 KB
/
BlockDestroyedByExplosionEvent.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
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.ArrayList;
import java.util.List;
public class BlockDestroyedByExplosionEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// <block> destroyed by explosion
//
// @Group Block
//
// @Location true
//
// @Cancellable true
//
// @Warning This event fires extremely rapidly. One single TNT detonation can destroy a hundred blocks.
//
// @Triggers when a block is destroyed by an explosion (caused by either an entity or a block exploding).
//
// @Switch source_entity:<matcher> to only fire the event if the source is an entity that matches the given type. Note that "Primed_Tnt" is an entity, not a block.
// @Switch source_block:<matcher> to only fire the event if the source is a block that matches the given type.
//
// @Context
// <context.block> returns the block that exploded.
// <context.source_location> returns the location of the source block or entity.
// <context.source_entity> returns the entity that exploded, if any.
// <context.strength> returns an ElementTag(Decimal) of the strength of the explosion.
//
// -->
public BlockDestroyedByExplosionEvent() {
instance = this;
registerCouldMatcher("<block> destroyed by explosion");
}
public static BlockDestroyedByExplosionEvent instance;
public BlockExplodeEvent blockEvent;
public EntityExplodeEvent entityEvent;
public LocationTag location;
public List<Block> rawList;
@Override
public boolean matches(ScriptPath path) {
if (!location.tryAdvancedMatcher(path.eventArgLowerAt(0))) {
return false;
}
if (!runInCheck(path, location)) {
return false;
}
if (path.switches.containsKey("source_entity") && (entityEvent == null || !new EntityTag(entityEvent.getEntity()).tryAdvancedMatcher(path.switches.get("source_entity")))) {
return false;
}
if (path.switches.containsKey("source_block") && (blockEvent == null || !new LocationTag(blockEvent.getBlock().getLocation()).tryAdvancedMatcher(path.switches.get("source_block")))) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "BlockDestroyedByExplosion";
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "block": return location;
case "source_location": return new LocationTag(blockEvent != null ? blockEvent.getBlock().getLocation() : entityEvent.getLocation());
case "source_entity": return entityEvent == null ? null : new EntityTag(entityEvent.getEntity());
case "strength": return new ElementTag(blockEvent != null ? blockEvent.getYield() : entityEvent.getYield());
default: return super.getContext(name);
}
}
@Override
public void cancellationChanged() {
if (cancelled) {
rawList.remove(location.getBlock());
}
}
@EventHandler
public void onBlockExplodes(BlockExplodeEvent event) {
this.blockEvent = event;
this.entityEvent = null;
this.rawList = event.blockList();
for (Block block : new ArrayList<>(rawList)) {
this.location = new LocationTag(block.getLocation());
fire(event);
}
}
@EventHandler
public void onEntityExplodes(EntityExplodeEvent event) {
this.entityEvent = event;
this.blockEvent = null;
this.rawList = event.blockList();
for (Block block : new ArrayList<>(rawList)) {
this.location = new LocationTag(block.getLocation());
fire(event);
}
}
}