-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
BlockExplodesScriptEvent.java
134 lines (122 loc) · 4.36 KB
/
BlockExplodesScriptEvent.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
package com.denizenscript.denizen.events.block;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import java.util.List;
public class BlockExplodesScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// blocks explodes
// <block> explodes
//
// @Regex ^on [^\s]+ explodes$
//
// @Group Block
//
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Cancellable true
//
// @Triggers when a block explodes.
//
// @Context
// <context.blocks> returns a ListTag of blocks that the entity blew up.
// <context.strength> returns an ElementTag(Decimal) of the strength of the explosion.
//
// @Determine
// ListTag(LocationTag) to set a new lists of blocks that are to be affected by the explosion.
// ElementTag(Decimal) to change the strength of the explosion.
//
// -->
public BlockExplodesScriptEvent() {
instance = this;
}
public static BlockExplodesScriptEvent instance;
public BlockExplodeEvent event;
public List<Block> blocks;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgLowerAt(1).equals("explodes")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String target = path.eventArgLowerAt(0);
if (!target.equals("block")) {
boolean matched = false;
for (Block block : blocks) {
if (tryMaterial(new MaterialTag(block.getType()), target)) {
matched = true;
break;
}
}
if (!matched) {
return false;
}
}
if (!runInCheck(path, blocks.get(0).getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "BlockExplodes";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
if (ArgumentHelper.matchesDouble(determination)) {
event.setYield(Float.parseFloat(determination));
return true;
}
if (ListTag.matches(determination)) {
event.blockList().clear();
for (String loc : ListTag.valueOf(determination, getTagContext(path))) {
LocationTag location = LocationTag.valueOf(loc, getTagContext(path));
if (location == null) {
Debug.echoError("Invalid location '" + loc + "' check [" + getName() + "]: ' for " + path.container.getName());
}
else {
event.blockList().add(location.getWorld().getBlockAt(location));
}
}
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("blocks")) {
ListTag blocks = new ListTag();
for (Block block : this.blocks) {
blocks.addObject(new LocationTag(block.getLocation()));
}
return blocks;
}
else if (name.equals("strength")) {
return new ElementTag(event.getYield());
}
return super.getContext(name);
}
@EventHandler
public void onBlockExplodes(BlockExplodeEvent event) {
this.blocks = event.blockList();
this.event = event;
fire(event);
}
}