/
PlayerChangesWorldScriptEvent.java
95 lines (84 loc) · 2.97 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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
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 changes world( from [^\s]+)?( to [^\s]+)?$
//
// @Group Player
//
// @Location true
//
// @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.
//
// @Player Always.
//
// -->
public PlayerChangesWorldScriptEvent() {
instance = this;
}
public static PlayerChangesWorldScriptEvent instance;
public WorldTag origin_world;
public WorldTag destination_world;
public PlayerChangedWorldEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player changes world");
}
@Override
public boolean matches(ScriptPath path) {
String[] data = path.eventArgsLower;
// TODO: Switches
for (int index = 3; index < data.length; index++) {
if (data[index].equals("from") && !origin_world.tryAdvancedMatcher(data[index + 1])) {
return false;
}
else if (data[index].equals("to") && !destination_world.tryAdvancedMatcher(data[index + 1])) {
return false;
}
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerChangesWorld";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@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);
}
}