-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
AreaEnterExitScriptEvent.java
347 lines (321 loc) · 12.9 KB
/
AreaEnterExitScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.NotedAreaTracker;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import java.util.*;
public class AreaEnterExitScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// <entity> enters|exits <area>
//
// @Group Entity
//
// @Triggers when an entity enters or exits a noted area (cuboid, ellipsoid, or polygon). On Spigot servers, only fires for players. Paper is required for other mob types.
//
// @Warning cancelling this event will have different results depending on the cause. Teleporting the entity away 1 tick later might be safer.
//
// @Cancellable true
//
// @Context
// <context.area> returns the area object that was entered or exited.
// <context.cause> returns the cause of the event. Can be: WALK, WORLD_CHANGE, JOIN, QUIT, TELEPORT, VEHICLE.
// <context.to> returns the location the entity moved to (might not be available in exit events).
// <context.from> returns the location the entity moved from (when available, depending on cause).
// <context.entity> returns the entity that entered/exited an area.
//
// @Player When the entity is a player.
//
// -->
public AreaEnterExitScriptEvent() {
registerCouldMatcher("<entity> enters|exits <area>");
}
public EntityTag currentEntity;
public AreaContainmentObject area;
public boolean isEntering;
public Location to;
@Override
public boolean matches(ScriptPath path) {
if (isEntering && !path.eventArgLowerAt(1).equals("enters")) {
return false;
}
if (!isEntering && !path.eventArgLowerAt(1).equals("exits")) {
return false;
}
String areaName = path.eventArgLowerAt(2);
if (areaName.equals("notable")) { // TODO: Deprecate?
areaName = path.eventArgLowerAt(3);
}
if (!area.tryAdvancedMatcher(areaName, path.context)) {
return false;
}
if (!path.tryArgObject(0, currentEntity)) {
return false;
}
return super.matches(path);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(currentEntity);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("area")) {
return area;
}
else if (name.equals("cause")) {
String cause;
if (currentEvent instanceof PlayerJoinEvent) {
cause = "JOIN";
}
else if (currentEvent instanceof PlayerQuitEvent) {
cause = "QUIT";
}
else if (currentEvent instanceof PlayerChangedWorldEvent) {
cause = "WORLD_CHANGE";
}
else if (currentEvent instanceof PlayerTeleportEvent) {
cause = "TELEPORT";
}
else if (currentEvent instanceof VehicleMoveEvent) {
cause = "VEHICLE";
}
else if (currentEvent instanceof PlayerMoveEvent) {
cause = "WALK";
}
else {
cause = "UNKNOWN";
}
return new ElementTag(cause);
}
else if (name.equals("to") && to != null) {
return new LocationTag(to);
}
else if (name.equals("from")) {
if (currentEvent instanceof PlayerMoveEvent) {
return new LocationTag(((PlayerMoveEvent) currentEvent).getFrom());
}
else if (currentEvent instanceof VehicleMoveEvent) {
return new LocationTag(((VehicleMoveEvent) currentEvent).getFrom());
}
else {
return new LocationTag(currentEntity.getLocation());
}
}
else if (name.equals("entity")) {
return currentEntity.getDenizenObject();
}
return super.getContext(name);
}
public void registerCorrectClass() {
initListener(new SpigotListeners());
}
@Override
public void init() {
doTrackAll = false;
boolean needsMatchers = false;
HashSet<String> exacts = new HashSet<>();
List<MatchHelper> matchList = new ArrayList<>();
HashSet<String> flags = new HashSet<>();
onlyTrackPlayers = true;
for (ScriptPath path : eventPaths) {
if (!path.eventArgLowerAt(0).equals("player")) {
onlyTrackPlayers = false;
}
String area = path.eventArgLowerAt(2);
if (area.equals("notable")) {
area = path.eventArgLowerAt(3);
}
if (area.equals("cuboid") || area.equals("ellipsoid") || area.equals("polygon")) {
doTrackAll = true;
needsMatchers = true;
}
MatchHelper matcher = createMatcher(area);
if (matcher instanceof AlwaysMatchHelper) {
doTrackAll = true;
needsMatchers = true;
}
else if (!needsMatchers && (matcher instanceof ExactMatchHelper)) {
exacts.add(area);
}
else {
needsMatchers = true;
}
matchList.add(matcher);
if (area.startsWith("area_flagged:")) {
flags.add(CoreUtilities.toLowerCase(area.substring("area_flagged:".length())));
needsMatchers = true;
}
}
exactTracked = needsMatchers ? null : exacts.toArray(new String[0]);
matchers = needsMatchers ? matchList.toArray(new MatchHelper[0]) : null;
flagTracked = flags.size() > 0 ? flags.toArray(new String[0]) : null;
registerCorrectClass();
}
public boolean doTrackAll = false;
public String[] exactTracked = null;
public String[] flagTracked = null;
public MatchHelper[] matchers = null;
public boolean onlyTrackPlayers = true;
public static HashMap<UUID, HashSet<AreaContainmentObject>> entitiesInArea = new HashMap<>();
@Override
public void cancellationChanged() {
if (cancelled) {
HashSet<AreaContainmentObject> inAreas = entitiesInArea.get(currentEntity.getUUID());
if (isEntering) {
inAreas.remove(area);
}
else {
inAreas.add(area);
}
}
super.cancellationChanged();
}
public boolean anyMatch(String name, FlaggableObject flaggable) {
if (doTrackAll) {
return true;
}
if (matchers != null) {
for (MatchHelper matcher : matchers) {
if (matcher.doesMatch(name)) {
return true;
}
}
}
if (flagTracked != null) {
for (String flag : flagTracked) {
AbstractFlagTracker tracker = flaggable.getFlagTracker();
if (tracker != null && tracker.hasFlag(flag)) {
return true;
}
}
}
return false;
}
public void processSingle(AreaContainmentObject obj, EntityTag entity, HashSet<AreaContainmentObject> inAreas, Location pos, Event eventCause) {
boolean containedNow = pos != null && obj.doesContainLocation(pos);
boolean wasContained = inAreas != null && inAreas.contains(obj);
if (containedNow == wasContained) {
return;
}
if (inAreas == null) {
inAreas = new HashSet<>();
entitiesInArea.put(entity.getUUID(), inAreas);
}
if (containedNow) {
inAreas.add(obj);
}
else {
inAreas.remove(obj);
}
currentEntity = entity;
isEntering = containedNow;
area = obj;
to = pos;
fire(eventCause);
}
private final static List<AreaContainmentObject> reusableClearList = new ArrayList<>();
public void processNewPosition(EntityTag entity, Location pos, Event eventCause) {
if (onlyTrackPlayers && !entity.isPlayer()) {
return;
}
HashSet<AreaContainmentObject> inAreas = entitiesInArea.get(entity.getUUID());
if (doTrackAll || matchers != null || flagTracked != null) {
if (pos != null) {
NotedAreaTracker.forEachAreaThatContains(new LocationTag(pos), (a) -> {
if (a instanceof FlaggableObject && anyMatch(a.getNoteName(), (FlaggableObject) a)) {
processSingle(a, entity, inAreas, pos, eventCause);
}
});
}
if (inAreas != null) {
reusableClearList.addAll(inAreas);
for (AreaContainmentObject area : reusableClearList) {
if (area.getNoteName() == null) {
inAreas.remove(area);
}
else {
processSingle(area, entity, inAreas, pos, eventCause);
}
}
reusableClearList.clear();
}
}
else {
for (String name : exactTracked) {
Notable obj = NoteManager.getSavedObject(name);
if (!(obj instanceof AreaContainmentObject)) {
Debug.echoError("Invalid area enter/exit event area '" + name + "'");
continue;
}
processSingle((AreaContainmentObject) obj, entity, inAreas, pos, eventCause);
}
}
if (inAreas != null && inAreas.isEmpty()) {
entitiesInArea.remove(entity.getUUID());
}
}
public class SpigotListeners implements Listener {
@EventHandler
public void onQuit(PlayerQuitEvent event) {
processNewPosition(new EntityTag(event.getPlayer()), null, event);
entitiesInArea.remove(event.getPlayer().getUniqueId());
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
processNewPosition(new EntityTag(event.getPlayer()), event.getPlayer().getLocation(), event);
}
@EventHandler
public void onMove(PlayerMoveEvent event) {
if (LocationTag.isSameBlock(event.getFrom(), event.getTo())) {
return;
}
processNewPosition(new EntityTag(event.getPlayer()), event.getTo(), event);
}
@EventHandler
public void onTeleport(PlayerTeleportEvent event) {
processNewPosition(new EntityTag(event.getPlayer()), event.getTo(), event);
}
@EventHandler
public void onTeleport(EntityTeleportEvent event) {
if (!onlyTrackPlayers) {
processNewPosition(new EntityTag(event.getEntity()), event.getTo(), event);
}
}
@EventHandler
public void onWorldChange(PlayerChangedWorldEvent event) {
processNewPosition(new EntityTag(event.getPlayer()), event.getPlayer().getLocation(), event);
}
@EventHandler
public void onVehicleMove(VehicleMoveEvent event) {
if (LocationTag.isSameBlock(event.getFrom(), event.getTo())) {
return;
}
if (!onlyTrackPlayers) {
processNewPosition(new EntityTag(event.getVehicle()), event.getTo(), event);
}
for (Entity entity : event.getVehicle().getPassengers()) {
if (!onlyTrackPlayers || EntityTag.isPlayer(entity)) {
processNewPosition(new EntityTag(entity), event.getTo(), event);
}
}
}
}
}