-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
PlayerChangesWorldScriptEvent.java
98 lines (85 loc) · 3.22 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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;
public class PlayerChangesWorldScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player changes world (from <world>) (to <world>)
//
// @Regex ^on player world( from [^\s]+)?( to [^\s]+)?$
// @Switch in <area>
//
// @Triggers when a player moves to a different world.
//
// @Context
// <context.origin_world> returns the WorldTag that the player was previously on.
// <context.destination_world> returns the WorldTag that the player is now in.
//
// -->
public PlayerChangesWorldScriptEvent() {
instance = this;
}
public static PlayerChangesWorldScriptEvent instance;
public WorldTag origin_world;
public WorldTag 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[] data = path.eventArgsLower;
for (int index = 3; index < data.length; index++) {
if (data[index].equals("from")) {
if (!data[index + 1].equals(CoreUtilities.toLowerCase(origin_world.getName()))) {
return false;
}
}
else if (data[index].equals("to")) {
if (!data[index + 1].equals(CoreUtilities.toLowerCase(destination_world.getName()))) {
return false;
}
}
}
return true;
}
@Override
public String getName() {
return "PlayerChangesWorld";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(new PlayerTag(event.getPlayer()), null);
}
@Override
public ObjectTag 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 (EntityTag.isNPC(event.getPlayer())) {
return;
}
origin_world = new WorldTag(event.getFrom());
destination_world = new WorldTag(event.getPlayer().getWorld());
this.event = event;
fire(event);
}
}