/
KillListenerInstance.java
362 lines (298 loc) · 13 KB
/
KillListenerInstance.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package net.aufdemrand.denizen.listeners.core;
import net.aufdemrand.denizencore.tags.ReplaceableTagEvent;
import net.aufdemrand.denizen.listeners.AbstractListener;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.EventHandler;
import java.util.Arrays;
import java.util.List;
public class KillListenerInstance extends AbstractListener implements Listener {
public static enum KillType { NPC, PLAYER, GROUP, ENTITY }
//
// The type of Kill
//
KillType type = null;
//
// The targets
//
dList targets;
//
// The names
//
dList names;
//
// The counters
//
int required = 1;
int kills_so_far = 0;
//
// Modifiers
//
String region = null;
dCuboid cuboid = null;
@Override
public String report() {
// Called by the '/denizen listener --report id' command, meant to give information
// to server-operators about the current status of this listener.
return player.getName() + " currently has quest listener '" + id
+ "' active and must kill " + Arrays.toString(targets.toArray())
+ " '" + type.name() + "'(s) named " + Arrays.toString(names.toArray()) + ". Current progress '" + kills_so_far + "/" + required + "'.";
}
@Override
public void onBuild(List<aH.Argument> args) {
// Build the listener from script arguments. onBuild() is called when a new listener is
// made with the LISTEN command. All arguments except type, id, and script
// are passed through to here.
for (aH.Argument arg : args) {
if (arg.matchesEnum(KillType.values()) && type == null)
this.type = KillType.valueOf(arg.getValue().toUpperCase());
else if (arg.matchesPrefix("qty, q")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
this.required = aH.getIntegerFrom(arg.getValue());
else if (arg.matchesPrefix("region, r"))
this.region = arg.getValue();
else if (arg.matchesPrefix("cuboid, c")
&& arg.matchesArgumentType(dCuboid.class))
this.cuboid = arg.asType(dCuboid.class);
else if (arg.matchesPrefix("targets, target, t, name, names"))
targets = arg.asType(dList.class);
else if (arg.matchesPrefix("mobnames, mn")) {
names = arg.asType(dList.class);
}
}
if (targets == null)
targets = new dList("*");
if (names == null) {
names = new dList("*");
}
if (type == null) {
dB.echoError("Missing TYPE argument! Valid: NPC, ENTITY, PLAYER, GROUP");
cancel();
}
else if (type == KillType.NPC && Depends.citizens == null) {
dB.echoError("Invalid TYPE argument 'NPC': Citizens2 is not enabled.");
cancel();
}
// At this point, constructed() is called.
}
@Override
public void onLoad() {
// Build the listener from saved data. id and type are saved automatically.
// onBuild() will not be called, this should handle everything onBuild() would with the
// saved data from onSave().
type = KillType.valueOf(((String) get("Type")));
targets = new dList((List<String>) get("Targets"));
names = new dList((List<String>) get("Names"));
required = (Integer) get("Quantity");
kills_so_far = (Integer) get("Current Kills");
region = (String) get("Region");
cuboid = dCuboid.valueOf((String) get("Cuboid"));
// At this point, constructed() is called.
}
@Override
public void onSave() {
// If the player leaves the game while a listener is in progress, save the information
// so that it can be rebuilt onLoad(). id and type are done automatically.
store("Type", type.name());
store("Targets", targets);
store("Names", names);
store("Quantity", required);
store("Current Kills", kills_so_far);
store("Region", region);
if (cuboid != null) store("Cuboid", cuboid.identify());
// At this point, deconstructed() is called.
}
@Override
public void onFinish() {
// Nothing to do here for now, but this is called when the quest listener is
// finished, after the script is run, and right before deconstructed().
// At this point, deconstructed() is called.
}
@Override
public void onCancel() {
// Nothing to do here for now, but this is called when the quest listener is
// cancelled, right before deconstructed().
// At this point, deconstructed() is called.
}
@Override
public void constructed() {
// Called after build and load methods. Perfect place to register
// any bukkit events!
denizen.getServer().getPluginManager().registerEvents(this, denizen);
TagManager.registerTagEvents(this);
// Report to the console
dB.log(report());
}
@Override
public void deconstructed() {
// Called when the instance is deconstructed due to either it being
// saved, finished, or cancelled.
// This is the perfect place to unregister any bukkit events so it
// can be cleanly removed from memory.
EntityDeathEvent.getHandlerList().unregister(this);
TagManager.unregisterTagEvents(this);
}
@EventHandler
public void listen(EntityDeathEvent event) {
// Only continue if the event is an event for the player that owns this listener.
if (event.getEntity().getKiller() != player.getPlayerEntity()) return;
// If REGION argument specified, check. If not in region, don't count kill!
if (region != null) {
//if (!WorldGuardUtilities.inRegion(player.getLocation(), region)) return;
}
// Same with the CUBOID argument...
if (cuboid != null)
if (!cuboid.isInsideCuboid(player.getLocation())) return;
//
// ENTITY type Kill Listener
//
if (type == KillType.ENTITY) {
// Get entity killed
dEntity ent = new dEntity(event.getEntity());
boolean count_it = false;
// Check targets, if any match entity killed, count_it!
for (String target : targets) {
if (dEntity.valueOf(target) != null)
if (ent.comparesTo(dEntity.valueOf(target)) == 1)
count_it = true;
}
boolean right_name = false;
for (String name : names) {
if (ChatColor.stripColor(ent.getName()).contains(name)) {
right_name = true;
}
}
// If an entity was found, or targets is '*', increment the
// kills_so_far
if (count_it || targets.contains("*")) {
if (right_name || names.contains("*")) {
kills_so_far++;
dB.log(player.getName() + " killed a "
+ ent.identify() + ". Current progress '"
+ kills_so_far + "/" + required + "'.");
// Check the number of kills so far
check();
}
}
}
//
// NPC type Kill Listener
//
else if (type == KillType.NPC) {
// If a NPC wasn't killed, return.
if (!CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) return;
// Get the NPC killed
dNPC npc = dNPC.mirrorCitizensNPC(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
boolean count_it = false;
// Check targets, if any match entity killed, count_it!
for (String target : targets) {
// Check against a physical NPC object (n@7, n@18, etc.)
if (dNPC.valueOf(target) != null) {
if (dNPC.valueOf(target).getId() == npc.getId())
count_it = true;
}
// Cannot be 'else if' since dNPC.valueOf will return true for names now, as well as ids.
// Check against name of NPC (n@fullwall, aufdemrand, etc)... object notation is optional.
if (npc.getName().equalsIgnoreCase(target.toLowerCase().replace("n@", "")))
count_it = true;
}
// If NPC was matched or targets contains '*', increment
// the kills so far.
if (count_it || targets.contains("*")) {
kills_so_far++;
dB.log(player.getName() + " killed "
+ npc.toString() + ". Current progress '"
+ kills_so_far + "/" + required + "'.");
// Check the number of kills so far
check();
}
}
//
// PLAYER type Kill Listener
//
else if (type == KillType.PLAYER) {
// Check to make sure entity is a Player, and not a NPC
if (event.getEntityType() != EntityType.PLAYER) return;
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) return;
// Get player killed
dPlayer player = dPlayer.mirrorBukkitPlayer((Player) event.getEntity());
boolean count_it = false;
// Check targets, if any match entity killed, count_it!
for (String target : targets) {
if (dPlayer.valueOf(target) != null)
if (dPlayer.valueOf(target).getName().equalsIgnoreCase(player.getName()))
count_it = true;
}
// If an entity was found, or targets is '*', increment the
// kills_so_far
if (count_it || targets.contains("*")) {
kills_so_far++;
dB.log(player.getName() + " killed "
+ player.getName() + ". Current progress '"
+ kills_so_far + "/" + required + "'.");
// Check the number of kills so far
check();
}
}
//
// GROUP type Kill Listener
//
else if (type == KillType.GROUP) {
// Require the entity to be a Player
if (event.getEntityType() == EntityType.PLAYER)
// Iterate through groups on the Player
for (String group : Depends.permissions.getPlayerGroups((Player) event.getEntity()))
// If a group matches, count it!
if (targets.contains(group.toUpperCase())) {
kills_so_far++;
dB.log(player.getName() + " killed " + ((Player) event.getEntity()).getName().toUpperCase() + " of group " + group + ".");
check();
break;
}
}
}
public void check() {
// Check current kills vs. required kills; finish() if necessary.
if (kills_so_far >= required)
finish();
}
@TagManager.TagEvents
public void listenTag(ReplaceableTagEvent event) {
if (!event.matches("LISTENER")) return;
if (!event.getType().equalsIgnoreCase(id)) return;
if (event.getValue().equalsIgnoreCase("region")) {
event.setReplaced(region);
}
else if (event.getValue().equalsIgnoreCase("required")) {
event.setReplaced(String.valueOf(required));
}
else if (event.getValue().equalsIgnoreCase("currentkills")) {
event.setReplaced(String.valueOf(kills_so_far));
}
else if (event.getValue().equalsIgnoreCase("targets")) {
String targetList = "";
for (String curTar : targets){
targetList = targetList + curTar + ", ";
targetList = targetList.substring(0, targetList.length() - 1);
}
event.setReplaced(targetList);
}
else if (event.getValue().equalsIgnoreCase("mobnames")){
String namesList = "";
for (String curNam : names){
namesList = namesList + curNam + ", ";
namesList = namesList.substring(0, namesList.length() - 1);
}
event.setReplaced(namesList);
}
}
}