-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
EntityExplodesScriptEvent.java
138 lines (126 loc) · 4.61 KB
/
EntityExplodesScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
public class EntityExplodesScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// entity explodes
// <entity> explodes
//
// @Regex ^on [^\s]+ explodes$
//
// @Group Entity
//
// @Location true
//
// @Cancellable true
//
// @Triggers when an entity explodes.
//
// @Context
// <context.blocks> returns a ListTag of blocks that the entity blew up.
// <context.entity> returns the EntityTag that exploded.
// <context.location> returns the LocationTag the entity blew up at.
// <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 EntityExplodesScriptEvent() {
instance = this;
}
public static EntityExplodesScriptEvent instance;
public EntityTag entity;
public LocationTag location;
public EntityExplodeEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgLowerAt(1).equals("explodes")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String target = path.eventArgLowerAt(0);
if (!tryEntity(entity, target)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "EntityExplodes";
}
@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 ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity;
case "location":
return location;
case "blocks":
ListTag blocks = new ListTag();
for (Block block : event.blockList()) {
blocks.addObject(new LocationTag(block.getLocation()));
}
return blocks;
case "strength":
return new ElementTag(event.getYield());
}
return super.getContext(name);
}
@EventHandler
public void onEntityExplodes(EntityExplodeEvent event) {
entity = new EntityTag(event.getEntity());
location = new LocationTag(event.getLocation());
this.event = event;
fire(event);
}
}