/
TravelListenerInstance.java
249 lines (213 loc) · 7.43 KB
/
TravelListenerInstance.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package net.aufdemrand.denizen.listeners.core;
import net.aufdemrand.denizen.listeners.AbstractListener;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.aH;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.List;
/**
* This is a listener that listens for a player to travel. There are different
* types of "traveling" this can entail:
* <p/>
* <ol>
* <li>
* Distance
* <dd>Number of blocks to travel.</dd>
* </li>
* <li>
* Target
* <dd>A NPC to travel to.</dd>
* </li>
* <li>
* Location
* <dd>A location to travel to</dd>
* </li>
* </ol>
*
* @author Jeebiss, Jeremy Schroeder
*/
public class TravelListenerInstance extends AbstractListener implements Listener {
enum TravelType {DISTANCE, TOLOCATION, TONPC, TOCUBOID}
//
// The type of Travel
//
private TravelType type;
//
// End point criteria
//
private dNPC target;
private dLocation end_point;
private dCuboid end_cuboid;
private int radius = 2;
//
// Counters
//
private Integer blocks_walked = 0;
private Integer distance_required = null;
/**
* This method is called when an instance of the travel listener is created.
* This class will then register with the event handler so we know when the
* player moves so that a determination of whether or not the player has
* reached the goal can be determined.
*/
@Override
public void constructed() {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}
/**
* This will be called when this travel listener is destroyed. This allows
* the class to unregister with the event handler so that no more events will
* be received.
*/
@Override
public void deconstructed() {
PlayerMoveEvent.getHandlerList().unregister(this);
}
@Override
public void onBuild(List<aH.Argument> args) {
for (aH.Argument arg : args) {
if (type == null && arg.matchesEnum(TravelType.values())) {
type = TravelType.valueOf(arg.getValue().toUpperCase());
}
// Distance/radius should be first, that way they aren't accidently
// intrepreted as NPCs.
else if (arg.matchesPrefix("d, distance")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
distance_required = aH.getIntegerFrom(arg.getValue());
}
else if (arg.matchesPrefix("r, radius")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
radius = aH.getIntegerFrom(arg.getValue());
}
else if (arg.matchesArgumentType(dCuboid.class)) {
end_cuboid = arg.asType(dCuboid.class);
}
else if (arg.matchesArgumentType(dLocation.class)) {
end_point = arg.asType(dLocation.class);
}
else if (arg.matchesArgumentType(dNPC.class)) {
target = arg.asType(dNPC.class);
}
}
//
// Check for mandatory arguments.
//
if (type == null) {
dB.echoError("Missing TYPE argument! Valid: DISTANCE, TOLOCATION, TONPC, TOCUBOID");
cancel();
}
if (type == TravelType.DISTANCE && distance_required == null) {
dB.echoError("Missing 'distance' argument!");
cancel();
}
if (type == TravelType.TOCUBOID && end_cuboid == null) {
dB.echoError("Missing 'cuboid' argument!");
cancel();
}
if (type == TravelType.TOLOCATION && end_point == null) {
dB.echoError("Missing 'location' argument!");
cancel();
}
if (type == TravelType.TONPC && target == null) {
dB.echoError("Missing 'npc' argument!");
cancel();
}
}
@Override
public void onCancel() {
// nothing to do here
}
@Override
public void onFinish() {
// nothing to do here
}
@Override
public void onLoad() {
type = TravelType.valueOf((String) get("Type"));
distance_required = (Integer) get("Distance");
blocks_walked = (Integer) get("Blocks Walked");
end_cuboid = dCuboid.valueOf((String) get("End Cuboid"));
end_point = dLocation.valueOf((String) get("End Location"));
}
@Override
public void onSave() {
store("Type", type.name());
store("Distance", distance_required);
store("Radius", radius);
store("Blocks Walked", blocks_walked);
if (end_point != null) {
store("End Location", end_point.identify());
}
if (end_cuboid != null) {
store("End Cuboid", end_cuboid.identify());
}
}
@Override
public String report() {
if (type == TravelType.DISTANCE) {
return player.getName() + "has traveled " + blocks_walked + " blocks out of " + distance_required;
}
else if (type == TravelType.TOLOCATION) {
return player.getName() + " is traveling to " + end_point;
}
else if (type == TravelType.TONPC) {
return player.getName() + " is traveling to NPC " + target.getId();
}
return "Failed to create detailed report";
}
/**
* This method will be called every time a player moves in the game. It's
* used to determine if a player has satisfied a certain travel goal.
*
* @param event The player movement event.
*/
@EventHandler
public void walking(PlayerMoveEvent event) {
// Only continue if the player moving owns this Listener
if (!event.getPlayer().equals(player.getPlayerEntity())) {
return;
}
// Don't look if the player hasn't moved a block yet...
if (event.getTo().getBlock().equals(event.getFrom().getBlock())) {
return;
}
////////////
// DISTANCE type Location Listener
///////
if (type == TravelType.DISTANCE) {
blocks_walked++;
check();
}
////////////
// TOLOCATION type Location Listener
///////
else if (type == TravelType.TOLOCATION) {
if (!player.getPlayerEntity().getLocation().getWorld().equals(end_point.getWorld())) {
return;
}
//if (player.getLocation().distance(endPoint) <= radius) {
if (Utilities.checkLocation(player.getPlayerEntity(), end_point, radius)) {
finish();
}
}
///////////
// TONPC type Location Listener
///////
else if (type == TravelType.TONPC) {
if (Utilities.checkLocation(player.getPlayerEntity(),
target.getLocation(), radius)) {
finish();
}
}
}
private void check() {
if (blocks_walked >= distance_required) {
finish();
}
}
}