-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
BukkitScriptEvent.java
392 lines (362 loc) · 15.1 KB
/
BukkitScriptEvent.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
package net.aufdemrand.denizen.events;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.IllegalPluginAccessException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Pattern;
public abstract class BukkitScriptEvent extends ScriptEvent {
public static Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) {
try {
clazz.getDeclaredMethod("getHandlerList");
return clazz;
} catch (NoSuchMethodException var3) {
if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Event.class) && Event.class.isAssignableFrom(clazz.getSuperclass())) {
return getRegistrationClass(clazz.getSuperclass().asSubclass(Event.class));
} else {
throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName() + ". Static getHandlerList method required!");
}
}
}
public static HandlerList getEventListeners(Class<? extends Event> type) {
try {
Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList");
method.setAccessible(true);
return (HandlerList)method.invoke(null);
} catch (Exception var3) {
throw new IllegalPluginAccessException(var3.toString());
}
}
private static Field REGISTERED_LISTENER_EXECUTOR_FIELD;
static {
try {
REGISTERED_LISTENER_EXECUTOR_FIELD = RegisteredListener.class.getDeclaredField("executor");
REGISTERED_LISTENER_EXECUTOR_FIELD.setAccessible(true);
}
catch (NoSuchFieldException ex) {
dB.echoError(ex);
}
}
public static EventExecutor getExecutor(RegisteredListener listener) {
try {
return (EventExecutor) REGISTERED_LISTENER_EXECUTOR_FIELD.get(listener);
}
catch (IllegalAccessException ex) {
dB.echoError(ex);
}
return null;
}
public HashMap<EventPriority, BukkitScriptEvent> priorityHandlers;
public List<Map.Entry<RegisteredListener, HandlerList>> registeredHandlers;
// <--[language]
// @name bukkit event priority
// @description
// Script events that are backed by standard Bukkit events are able to control what underlying Bukkit event priority
// they register as.
// This can be useful, for example, if a different plugin is cancelling the event at a later priority,
// and you're writing a script that needs to un-cancel the event.
// This can be done using the "bukkit_priority" switch.
// Valid priorities, in order of execution, are: LOWEST, LOW, NORMAL, HIGH, HIGHEST, MONITOR.
// Monitor is executed last, and is intended to only be used when reading the results of an event but not changing it.
// The default priority is "normal".
// -->
@Override
public void destroy() {
if (priorityHandlers != null) {
for (BukkitScriptEvent event : priorityHandlers.values()) {
event.destroy();
}
priorityHandlers = null;
}
if (registeredHandlers != null) {
for (Map.Entry<RegisteredListener, HandlerList> handler : registeredHandlers) {
handler.getValue().unregister(handler.getKey());
}
registeredHandlers = null;
}
}
@Override
public void init() {
if (this instanceof Listener) {
initListener((Listener) this);
}
}
public void initListener(Listener listener) {
if (priorityHandlers == null) {
priorityHandlers = new HashMap<>();
}
for (ScriptPath path : new ArrayList<>(eventPaths)) {
String bukkitPriority = path.switches.get("bukkit_priority");
if (bukkitPriority != null) {
try {
EventPriority priority = EventPriority.valueOf(bukkitPriority.toUpperCase());
BukkitScriptEvent handler = priorityHandlers.get(priority);
if (handler == null) {
handler = (BukkitScriptEvent) clone();
handler.eventPaths = new ArrayList<>();
handler.priorityHandlers = null;
handler.registeredHandlers = null;
priorityHandlers.put(priority, handler);
handler.initForPriority(priority, (Listener) handler);
}
handler.eventPaths.add(path);
eventPaths.remove(path);
}
catch (IllegalArgumentException ex) {
dB.echoError("Invalid 'bukkit_priority' switch for event '" + path.event + "' in script '" + path.container.getName() + "'.");
dB.echoError(ex);
}
}
}
if (!eventPaths.isEmpty()) {
initForPriority(EventPriority.NORMAL, listener);
}
}
public void initForPriority(EventPriority priority, Listener listener) {
if (registeredHandlers == null) {
registeredHandlers = new ArrayList<>();
}
Plugin plugin = DenizenAPI.getCurrentInstance();
for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry :
plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) {
for (RegisteredListener registeredListener : entry.getValue()) {
RegisteredListener newListener = new RegisteredListener(listener, getExecutor(registeredListener), priority, plugin, false);
HandlerList handlers = getEventListeners(getRegistrationClass(entry.getKey()));
handlers.register(newListener);
registeredHandlers.add(new HashMap.SimpleEntry<>(newListener, handlers));
}
}
}
@Deprecated
public boolean runInCheck(ScriptContainer scriptContainer, String s, String lower, Location location) {
return runInCheck(scriptContainer, s, lower, location, "in");
}
@Deprecated
public boolean runInCheck(ScriptContainer scriptContainer, String s, String lower, Location location, String innote) {
return runInCheck(new ScriptPath(scriptContainer, s), location, innote);
}
public boolean runInCheck(ScriptPath path, Location location) {
return runInCheck(path, location, "in");
}
public boolean runInCheck(ScriptPath path, Location location, String innote) {
String it = path.switches.get(innote);
if (it == null) {
int index;
for (index = 0; index < path.eventArgsLower.length; index++) {
if (path.eventArgsLower[index].equals(innote)) {
break;
}
}
if (index >= path.eventArgsLower.length) {
// No 'in ...' specified
return true;
}
it = path.eventArgLowerAt(index + 1);
if (it.equals("notable")) {
String subit = path.eventArgLowerAt(index + 2);
if (subit.equals("cuboid")) {
return dCuboid.getNotableCuboidsContaining(location).size() > 0;
}
else if (subit.equals("ellipsoid")) {
return dEllipsoid.getNotableEllipsoidsContaining(location).size() > 0;
}
else {
dB.echoError("Invalid event 'IN ...' check [" + getName() + "] ('in notable ???'): '" + path.event + "' for " + path.container.getName());
return false;
}
}
}
String lower = CoreUtilities.toLowerCase(it);
if (lower.equals("cuboid")) {
return dCuboid.getNotableCuboidsContaining(location).size() > 0;
}
else if (lower.equals("ellipsoid")) {
return dEllipsoid.getNotableEllipsoidsContaining(location).size() > 0;
}
else if (dWorld.matches(it)) {
return CoreUtilities.toLowerCase(location.getWorld().getName()).equals(lower);
}
else if (dCuboid.matches(it)) {
dCuboid cuboid = dCuboid.valueOf(it);
return cuboid.isInsideCuboid(location);
}
else if (dEllipsoid.matches(it)) {
dEllipsoid ellipsoid = dEllipsoid.valueOf(it);
return ellipsoid.contains(location);
}
else {
dB.echoError("Invalid event 'in:<area>' switch [" + getName() + "] ('in:???'): '" + path.event + "' for " + path.container.getName());
return false;
}
}
public boolean tryLocation(dLocation location, String comparedto) {
if (comparedto == null || comparedto.length() == 0) {
dB.echoError("Null or empty location string to compare");
return false;
}
if (comparedto.equals("notable")) {
return true;
}
comparedto = "l@" + comparedto;
dLocation loc = dLocation.valueOf(comparedto);
if (loc == null) {
dB.echoError("Invalid location in location comparison string: " + comparedto);
return false;
}
return loc.getBlock().equals(location.getBlock());
}
@Deprecated
public boolean runWithCheck(ScriptContainer scriptContainer, String s, String lower, dItem held) {
return runWithCheck(new ScriptPath(scriptContainer, s), held);
}
public static TagContext noDebugTagContext = new BukkitTagContext(null, null, false, null, false, null);
public boolean runWithCheck(ScriptPath path, dItem held) {
String with = path.switches.get("with");
if (with != null) {
if (with.equalsIgnoreCase("item")) {
return true;
}
if (held == null || !tryItem(held, with)) {
return false;
}
}
return true;
}
private static final String ASTERISK_QUOTED = Pattern.quote("*");
public String regexHandle(String input) {
if (input.startsWith("regex:")) {
return input.substring("regex:".length());
}
if (input.contains("*")) {
return Pattern.quote(input).replace(ASTERISK_QUOTED, "(.*)");
}
return null;
}
public boolean equalityCheck(String input, String compared, String regexed) {
input = CoreUtilities.toLowerCase(input);
return input.equals(compared) || (regexed != null && input.matches(regexed));
}
public boolean tryItem(dItem item, String comparedto) {
if (comparedto == null || comparedto.isEmpty() || item == null) {
return false;
}
comparedto = CoreUtilities.toLowerCase(comparedto);
if (comparedto.equals("item")) {
return true;
}
if (comparedto.equals("potion") && CoreUtilities.toLowerCase(item.getItemStack().getType().name()).contains("potion")) {
return true;
}
dMaterial quickOf = dMaterial.quickOfNamed(comparedto);
if (quickOf != null) {
dMaterial mat = item.getMaterial();
if (quickOf.getMaterial() != mat.getMaterial()) {
return false;
}
if (quickOf.equals(mat)) {
return true;
}
}
String regexd = regexHandle(comparedto);
item = new dItem(item.getItemStack().clone());
item.setAmount(1);
if (equalityCheck(item.identify().substring("i@".length()), comparedto, regexd)) {
return true;
}
else if (equalityCheck(item.identifyMaterialNoIdentifier(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(item.identifySimple().substring("i@".length()), comparedto, regexd)) {
return true;
}
item.setDurability((short) 0);
if (equalityCheck(item.identifyMaterialNoIdentifier(), comparedto, regexd)) {
return true;
}
return false;
}
public boolean tryMaterial(dMaterial mat, String comparedto) {
if (comparedto == null || comparedto.isEmpty() || mat == null) {
return false;
}
comparedto = CoreUtilities.toLowerCase(comparedto);
if (comparedto.equals("block") || comparedto.equals("material")) {
return true;
}
dMaterial quickOf = dMaterial.quickOfNamed(comparedto);
if (quickOf != null) {
if (quickOf.getMaterial() != mat.getMaterial()) {
return false;
}
if (quickOf.equals(mat)) {
return true;
}
}
String regexd = regexHandle(comparedto);
if (equalityCheck(mat.realName(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(mat.identifyNoIdentifier(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(mat.identifySimpleNoIdentifier(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(mat.identifyFullNoIdentifier(), comparedto, regexd)) {
return true;
}
return false;
}
public boolean tryEntity(dEntity entity, String comparedto) {
if (comparedto == null || comparedto.isEmpty() || entity == null) {
return false;
}
Entity bEntity = entity.getBukkitEntity();
comparedto = CoreUtilities.toLowerCase(comparedto);
if (comparedto.equals("entity")) {
return true;
}
else if (comparedto.equals("npc")) {
return entity.isCitizensNPC();
}
else if (comparedto.equals("player")) {
return entity.isPlayer();
}
else if (comparedto.equals("vehicle")) {
return bEntity instanceof Vehicle;
}
else if (comparedto.equals("projectile")) {
return bEntity instanceof Projectile;
}
else if (comparedto.equals("hanging")) {
return bEntity instanceof Hanging;
}
String regexd = regexHandle(comparedto);
if (entity.getEntityScript() != null && equalityCheck(entity.getEntityScript(), comparedto, regexd)) {
return true;
}
else if (equalityCheck(entity.getEntityType().getLowercaseName(), comparedto, regexd)) {
return true;
}
return false;
}
}