/
EntityTeleportScriptEvent.java
171 lines (154 loc) · 5.49 KB
/
EntityTeleportScriptEvent.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
package net.aufdemrand.denizen.events.entity;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.dObject;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
public class EntityTeleportScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// entity teleports
// <entity> teleports
//
// @Regex ^on [^\s]+ teleports$
//
// @Switch in <area>
// @Switch cause <cause>
//
// @Triggers when an entity teleports.
//
// @Cancellable true
//
// @Context
// <context.entity> returns the dEntity.
// <context.origin> returns the dLocation the entity teleported from.
// <context.destination> returns the dLocation the entity teleported to.
// <context.cause> returns an Element of the teleport cause. Can be:
// COMMAND, END_PORTAL, ENDER_PEARL, NETHER_PORTAL, PLUGIN, END_GATEWAY, CHORUS_FRUIT, SPECTATE, UNKNOWN, or ENTITY_TELEPORT
//
// @Determine
// "ORIGIN:" + dLocation to change the location the entity teleported from.
// "DESTINATION:" + dLocation to change the location the entity teleports to.
//
// @Player when the entity being teleported is a player.
//
// @NPC when the entity being teleported is an NPC.
//
// -->
public EntityTeleportScriptEvent() {
instance = this;
}
public static EntityTeleportScriptEvent instance;
public dEntity entity;
public dLocation from;
public dLocation to;
public String cause;
public EntityTeleportEvent event;
public PlayerTeleportEvent pEvent;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return CoreUtilities.xthArgEquals(1, lower, "teleports");
}
@Override
public boolean matches(ScriptPath path) {
if (!tryEntity(entity, path.eventArgLowerAt(0))) {
return false;
}
if (!runGenericSwitchCheck(path, "cause", cause)) {
return false;
}
if (!runInCheck(path, from)) {
return false;
}
return true;
}
@Override
public String getName() {
return "EntityTeleports";
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
String dlow = CoreUtilities.toLowerCase(determination);
if (dlow.startsWith("origin:")) {
dLocation new_from = dLocation.valueOf(determination.substring("origin:".length()));
if (new_from != null) {
from = new_from;
return true;
}
}
else if (dlow.startsWith("destination:")) {
dLocation new_to = dLocation.valueOf(determination.substring("destination:".length()));
if (new_to != null) {
to = new_to;
return true;
}
}
else if (dLocation.matches(determination)) {
dLocation new_to = dLocation.valueOf(determination);
if (new_to != null) {
to = new_to;
return true;
}
}
return super.applyDetermination(container, determination);
}
@Override
public ScriptEntryData getScriptEntryData() {
// TODO: Store the player / npc?
return new BukkitScriptEntryData(pEvent != null ? dEntity.getPlayerFrom(pEvent.getPlayer()) : null,
entity.isCitizensNPC() ? entity.getDenizenNPC() : null);
}
@Override
public dObject getContext(String name) {
if (name.equals("origin")) {
return from;
}
else if (name.equals("destination")) {
return to;
}
else if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("cause")) {
return new Element(cause);
}
return super.getContext(name);
}
@EventHandler
public void onEntityTeleports(EntityTeleportEvent event) {
if (event.getEntity() instanceof Player) {
return;
}
to = new dLocation(event.getTo());
from = new dLocation(event.getFrom());
entity = new dEntity(event.getEntity());
cause = "ENTITY_TELEPORT";
this.event = event;
pEvent = null;
fire(event);
event.setFrom(from);
event.setTo(to);
}
@EventHandler
public void onPlayerTeleports(PlayerTeleportEvent event) {
from = new dLocation(event.getFrom());
to = new dLocation(event.getTo());
entity = new dEntity(event.getPlayer());
cause = event.getCause().name();
this.event = null;
pEvent = event;
fire(event);
event.setFrom(from);
event.setTo(to);
}
}