-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
GPClaimEnterEvent.java
139 lines (123 loc) · 4.88 KB
/
GPClaimEnterEvent.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
package net.gnomeffinway.depenizen.events.griefprevention;
import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.events.ScriptEvent;
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 net.gnomeffinway.depenizen.objects.griefprevention.GriefPreventionClaim;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
public class GPClaimEnterEvent extends ScriptEvent implements Listener {
// TODO: in area?
// <--[event]
// @Events
// gp player enters <gpclaim>
// gp player exits <gpclaim>
// gp player enters gpclaim
// gp player exits gpclaim
//
// @Regex ^on gp player (enters|exits) [^\s]+$
//
// @Warning Cancelling this event will fire a similar event immediately after.
//
// @Cancellable true
//
// @Triggers when a player enters or exits a Grief Prevention claim.
//
// @Context
// <context.from> returns the block location moved from.
// <context.to> returns the block location moved to.
// <context.new_claim> returns the gpclaim being entered.
// <context.old_claim> returns the gpclaim being exited.
//
// @Plugin Depenizen, GriefPrevention
// -->
public GPClaimEnterEvent() {
instance = this;
}
public static GPClaimEnterEvent instance;
public dLocation from;
public dLocation to;
public GriefPreventionClaim new_claim;
public GriefPreventionClaim old_claim;
public PlayerMoveEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.startsWith("gp player enters")
|| lower.startsWith("gp player exits");
}
@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String claim_test = lower.substring(lower.lastIndexOf(' ') + 1);
String direction = lower.substring("gp player ".length(), lower.lastIndexOf(' ') - 1).trim();
return (claim_test.equals("gpclaim") && ((direction.equals("enters") && new_claim != null) || (direction.equals("exits") && old_claim != null)))
|| (direction.equals("enters") && new_claim != null && claim_test.equals(CoreUtilities.toLowerCase(new_claim.simple())))
|| (direction.equals("exits") && old_claim != null && claim_test.equals(CoreUtilities.toLowerCase(old_claim.simple())));
}
@Override
public String getName() {
return "GPClaimEnter";
}
@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void destroy() {
PlayerMoveEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}
@Override
public ScriptEntryData getScriptEntryData() {
// TODO: Store the player?
return new BukkitScriptEntryData(event != null ? dEntity.getPlayerFrom(event.getPlayer()) : null, null);
}
@Override
public dObject getContext(String name) {
if (name.equals("to")) {
return to;
}
else if (name.equals("from")) {
return from;
}
else if (name.equals("old_claim")) {
return old_claim;
}
else if (name.equals("new_claim")) {
return new_claim;
}
return super.getContext(name);
}
@EventHandler
public void onPlayerEntersExitsGPClaim(PlayerMoveEvent event) {
if (event.getTo().getBlock().getLocation().equals(event.getFrom().getBlock().getLocation())) {
return;
}
from = new dLocation(event.getFrom());
to = new dLocation(event.getTo());
Claim fclaim = GriefPrevention.instance.dataStore.getClaimAt(from, false, null);
Claim tclaim = GriefPrevention.instance.dataStore.getClaimAt(to, false, null);
if (tclaim == fclaim) {
return;
}
new_claim = tclaim == null ? null: new GriefPreventionClaim(tclaim);
old_claim = fclaim == null ? null: new GriefPreventionClaim(fclaim);
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
}