/
PlayerChangesWorldScriptEvent.java
113 lines (97 loc) · 3.68 KB
/
PlayerChangesWorldScriptEvent.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
package net.aufdemrand.denizen.events.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import java.util.List;
public class PlayerChangesWorldScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player changes world (from <world>) (to <world>)
//
// @Regex ^on player world( from [^\s]+)?( to [^\s]+)?( in ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Triggers when a player moves to a different world.
//
// @Context
// <context.origin_world> returns the dWorld that the player was previously on.
// <context.destination_world> returns the dWorld that the player is now in.
//
// -->
public PlayerChangesWorldScriptEvent() {
instance = this;
}
public static PlayerChangesWorldScriptEvent instance;
public dWorld origin_world;
public dWorld destination_world;
public PlayerChangedWorldEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
return CoreUtilities.toLowerCase(s).startsWith("player changes world");
}
@Override
public boolean matches(ScriptPath path) {
String s = path.event;
String lower = path.eventLower;
List<String> data = CoreUtilities.split(lower, ' ');
for (int index = 3; index < data.size(); index++) {
if (data.get(index).equals("from")) {
if (!data.get(index + 1).equals(CoreUtilities.toLowerCase(origin_world.getName()))) {
return false;
}
}
else if (data.get(index).equals("to")) {
if (!data.get(index + 1).equals(CoreUtilities.toLowerCase(destination_world.getName()))) {
return false;
}
}
}
return true;
}
@Override
public String getName() {
return "PlayerChangesWorld";
}
@Override
public void destroy() {
PlayerChangedWorldEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(new dPlayer(event.getPlayer()), null);
}
@Override
public dObject getContext(String name) {
if (name.equals("origin_world")) {
return origin_world;
}
else if (name.equals("destination_world")) {
return destination_world;
}
return super.getContext(name);
}
@EventHandler
public void onPlayerChangesWorld(PlayerChangedWorldEvent event) {
if (dEntity.isNPC(event.getPlayer())) {
return;
}
origin_world = new dWorld(event.getFrom());
destination_world = new dWorld(event.getPlayer().getWorld());
this.event = event;
fire();
}
}