/
dNPC.java
538 lines (462 loc) · 18.8 KB
/
dNPC.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.npc.dNPCRegistry;
import net.aufdemrand.denizen.npc.traits.AssignmentTrait;
import net.aufdemrand.denizen.npc.traits.HealthTrait;
import net.aufdemrand.denizen.npc.traits.NicknameTrait;
import net.aufdemrand.denizen.npc.traits.TriggerTrait;
import net.aufdemrand.denizen.scripts.commands.npc.EngageCommand;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer;
import net.aufdemrand.denizen.scripts.containers.core.InteractScriptHelper;
import net.aufdemrand.denizen.scripts.triggers.AbstractTrigger;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.core.NPCTags;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.trait.Anchors;
import net.citizensnpcs.util.Anchor;
import net.minecraft.server.v1_6_R2.EntityLiving;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.InventoryHolder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class dNPC implements dObject {
public static dNPC mirrorCitizensNPC(NPC npc) {
if (dNPCRegistry.denizenNPCs.containsKey(npc.getId())) return dNPCRegistry.denizenNPCs.get(npc.getId());
else return new dNPC(npc);
}
@ObjectFetcher("n")
public static dNPC valueOf(String string) {
if (string == null) return null;
////////
// Match NPC id
string = string.replace("n@", "");
NPC npc;
if (aH.matchesInteger(string)) {
if (dNPCRegistry.denizenNPCs.containsKey(aH.getIntegerFrom(string)))
return dNPCRegistry.denizenNPCs.get(aH.getIntegerFrom(string));
npc = CitizensAPI.getNPCRegistry().getById(aH.getIntegerFrom(string));
if (npc != null) return new dNPC(npc);
}
return null;
}
public static boolean matches(String string) {
string = string.replace("n@", "");
NPC npc;
if (aH.matchesInteger(string)) {
npc = CitizensAPI.getNPCRegistry().getById(aH.getIntegerFrom(string));
if (npc != null) return true;
}
return false;
}
public boolean isValid() {
return getCitizen() != null;
}
private int npcid = -1;
private final org.bukkit.Location locationCache = new org.bukkit.Location(null, 0, 0, 0);
public dNPC(NPC citizensNPC) {
if (citizensNPC != null)
this.npcid = citizensNPC.getId();
if (npcid >= 0 && !dNPCRegistry.denizenNPCs.containsKey(npcid))
dNPCRegistry.denizenNPCs.put(npcid, this);
}
public EntityLiving getHandle() {
return ((CraftLivingEntity) getEntity()).getHandle();
}
public NPC getCitizen() {
NPC npc = CitizensAPI.getNPCRegistry().getById(npcid);
if (npc == null)
dB.log("Uh oh! Denizen has encountered a NPE while trying to fetch a NPC. Has this NPC been removed?");
return npc;
}
public LivingEntity getEntity() {
try {
return getCitizen().getBukkitEntity();
} catch (NullPointerException e) {
dB.log("Uh oh! Denizen has encountered a NPE while trying to fetch a NPC entity. Has this NPC been removed?");
return null;
}
}
public dEntity getDenizenEntity() {
try {
return new dEntity(getCitizen().getBukkitEntity());
} catch (NullPointerException e) {
dB.log("Uh oh! Denizen has encountered a NPE while trying to fetch a NPC entity. Has this NPC been removed?");
return null;
}
}
public EntityType getEntityType() {
return getCitizen().getBukkitEntity().getType();
}
public Navigator getNavigator() {
return getCitizen().getNavigator();
}
public int getId() {
return getCitizen().getId();
}
public String getName() {
return getCitizen().getName();
}
public InteractScriptContainer getInteractScript(dPlayer player, Class<? extends AbstractTrigger> triggerType) {
return InteractScriptHelper.getInteractScript(this, player, triggerType);
}
public InteractScriptContainer getInteractScriptQuietly(dPlayer player, Class<? extends AbstractTrigger> triggerType) {
boolean db = dB.debugMode;
dB.debugMode = false;
InteractScriptContainer script = InteractScriptHelper.getInteractScript(this, player, triggerType);
dB.debugMode = db;
return script;
}
public void destroy() {
getCitizen().destroy();
}
public dLocation getLocation() {
if (isSpawned()) return
new dLocation(getCitizen().getBukkitEntity().getLocation(locationCache));
else return null;
}
public dLocation getEyeLocation() {
if (isSpawned()) return
new dLocation(getCitizen().getBukkitEntity().getEyeLocation());
else return null;
}
public World getWorld() {
if (isSpawned()) return getEntity().getWorld();
else return null;
}
@Override
public String toString() {
return getCitizen().getName() + "/" + getCitizen().getId();
}
public boolean isEngaged() {
return EngageCommand.getEngaged(getCitizen());
}
public boolean isSpawned() {
return getCitizen().isSpawned();
}
public boolean isVulnerable() {
return true;
}
public String getOwner() {
return getCitizen().getTrait(Owner.class).getOwner();
}
public AssignmentTrait getAssignmentTrait() {
if (!getCitizen().hasTrait(AssignmentTrait.class))
getCitizen().addTrait(AssignmentTrait.class);
return getCitizen().getTrait(AssignmentTrait.class);
}
public NicknameTrait getNicknameTrait() {
if (!getCitizen().hasTrait(NicknameTrait.class))
getCitizen().addTrait(NicknameTrait.class);
return getCitizen().getTrait(NicknameTrait.class);
}
public HealthTrait getHealthTrait() {
if (!getCitizen().hasTrait(HealthTrait.class))
getCitizen().addTrait(HealthTrait.class);
return getCitizen().getTrait(HealthTrait.class);
}
public TriggerTrait getTriggerTrait() {
if (!getCitizen().hasTrait(TriggerTrait.class))
getCitizen().addTrait(TriggerTrait.class);
return getCitizen().getTrait(TriggerTrait.class);
}
public void action(String actionName, dPlayer player, Map<String, Object> context) {
if (getCitizen() != null)
{
if (getCitizen().hasTrait(AssignmentTrait.class))
DenizenAPI.getCurrentInstance().getNPCRegistry()
.getActionHandler().doAction(
actionName,
this,
player,
getAssignmentTrait().getAssignment(),
context);
}
}
public void action(String actionName, dPlayer player) {
action(actionName, player, null);
}
private String prefix = "npc";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getType() {
return "NPC";
}
@Override
public String identify() {
return "n@" + npcid;
}
@Override
public dNPC setPrefix(String prefix) {
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
// <--[tag]
// @attribute <npc>
// @returns dNPC
// @description
// Returns the dNPC of the NPC.
// -->
// <--[tag]
// @attribute <npc.name.nickname>
// @returns Element
// @description
// returns the NPC's nickname provided by the nickname trait, or null if the npc does not have the nickname trait.
// -->
if (attribute.startsWith("name.nickname"))
return new Element(getCitizen().hasTrait(NicknameTrait.class) ? getCitizen().getTrait(NicknameTrait.class)
.getNickname() : getName()).getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.name>
// @returns Element
// @description
// returns the player's nickname provided by the nickname trait, or null if the NPC does not have a nickname
// -->
if (attribute.startsWith("name"))
return new Element(ChatColor.stripColor(getName()))
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <npc.anchor.list>
// @returns dList
// @description
// returns a dList of anchor names currently assigned to the NPC.
// -->
if (attribute.startsWith("anchor.list")
|| attribute.startsWith("anchors.list")) {
List<String> list = new ArrayList<String>();
for (Anchor anchor : getCitizen().getTrait(Anchors.class).getAnchors())
list.add(anchor.getName());
return new dList(list).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <npc.has_anchors>
// @returns Element(boolean)
// @description
// returns true if the NPC has anchors assigned, false otherwise.
// -->
if (attribute.startsWith("has_anchors")) {
return (new Element(String.valueOf(getCitizen().getTrait(Anchors.class).getAnchors().size() > 0)))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <npc.anchor[name]>
// @returns dLocation
// @description
// returns a dLocation associated with the specified anchor, or 'null' if it doesn't exist.
// -->
if (attribute.startsWith("anchor")) {
if (attribute.hasContext(1)
&& getCitizen().getTrait(Anchors.class).getAnchor(attribute.getContext(1)) != null)
return new dLocation(getCitizen().getTrait(Anchors.class)
.getAnchor(attribute.getContext(1)).getLocation())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <npc.flag[flag_name]>
// @returns Flag dList
// @description
// returns 'flag dList' of the NPC's flag_name specified.
// -->
if (attribute.startsWith("flag")) {
String flag_name;
if (attribute.hasContext(1)) flag_name = attribute.getContext(1);
else return "null";
attribute.fulfill(1);
if (attribute.startsWith("is_expired")
|| attribute.startsWith("isexpired"))
return new Element(!FlagManager.npcHasFlag(this, flag_name))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("size") && !FlagManager.npcHasFlag(this, flag_name))
return new Element(0).getAttribute(attribute.fulfill(1));
if (FlagManager.npcHasFlag(this, flag_name))
return new dList(DenizenAPI.getCurrentInstance().flagManager()
.getNPCFlag(getId(), flag_name))
.getAttribute(attribute);
else return "null";
}
// <--[tag]
// @attribute <npc.id>
// @returns Element(number)
// @description
// returns the NPC's 'npcid' provided by Citizens.
// -->
if (attribute.startsWith("id"))
return new Element(String.valueOf(getId())).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <npc.owner>
// @returns Element
// @description
// returns the owner of the NPC.
// -->
if (attribute.startsWith("owner"))
return new Element(getOwner()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <npc.inventory>
// @returns dInventory
// @description
// Returns the dInventory of the NPC.
// NOTE: This currently only works with player-type NPCs.
// -->
if (attribute.startsWith("inventory"))
return new dInventory((InventoryHolder) getDenizenEntity().getLivingEntity()).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <npc.is_spawned>
// @returns Element(boolean)
// @description
// returns 'true' if the NPC is spawned, otherwise 'false'.
// -->
if (attribute.startsWith("is_spawned"))
return new Element(String.valueOf(isSpawned())).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <npc.location.previous_location>
// @returns dLocation
// @description
// returns the NPC's previous navigated location.
// -->
if (attribute.startsWith("location.previous_location"))
return (NPCTags.previousLocations.containsKey(getId())
? NPCTags.previousLocations.get(getId()).getAttribute(attribute.fulfill(2))
: "null");
// <--[tag]
// @attribute <npc.script>
// @returns dScript
// @description
// returns the NPC's assigned script.
// -->
if (attribute.startsWith("script")) {
NPC citizen = getCitizen();
if (!citizen.hasTrait(AssignmentTrait.class) || !citizen.getTrait(AssignmentTrait.class).hasAssignment()) {
return "null";
}
else {
return new Element(citizen.getTrait(AssignmentTrait.class).getAssignment().getName())
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <npc.navigator.is_navigating>
// @returns Element(boolean)
// @description
// returns true if the NPC is currently navigating, false otherwise.
// -->
if (attribute.startsWith("navigator.is_navigating"))
return new Element(String.valueOf(getNavigator().isNavigating())).getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.speed>
// @returns Element(number)
// @description
// returns the current speed of the NPC.
// -->
if (attribute.startsWith("navigator.speed"))
return new Element(String.valueOf(getNavigator().getLocalParameters().speed()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.range>
// @returns Element(number)
// @description
// returns the maximum pathfinding range
// -->
if (attribute.startsWith("navigator.range"))
return new Element(String.valueOf(getNavigator().getLocalParameters().range()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.attack_strategy>
// @returns Element
// @description
// returns the NPC's attack strategy
// -->
if (attribute.startsWith("navigator.attack_strategy"))
return new Element(String.valueOf(getNavigator().getLocalParameters().attackStrategy().toString()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.speed_modifier>
// @returns Element(number)
// @description
// returns the NPC movement speed modifier
// -->
if (attribute.startsWith("navigator.speed_modifier"))
return new Element(String.valueOf(getNavigator().getLocalParameters().speedModifier()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.base_speed>
// @returns Element(number)
// @description
// returns the base navigation speed
// -->
if (attribute.startsWith("navigator.base_speed"))
return new Element(String.valueOf(getNavigator().getLocalParameters().baseSpeed()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.avoid_water>
// @returns Element(boolean)
// @description
// returns whether the NPC will avoid water
// -->
if (attribute.startsWith("navigator.avoid_water"))
return new Element(String.valueOf(getNavigator().getLocalParameters().avoidWater()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.target_location>
// @returns dLocation
// @description
// returns the location the NPC is curently navigating towards
// -->
if (attribute.startsWith("navigator.target_location"))
return (getNavigator().getTargetAsLocation() != null
? new dLocation(getNavigator().getTargetAsLocation()).getAttribute(attribute.fulfill(2))
: "null");
// <--[tag]
// @attribute <npc.navigator.is_fighting>
// @returns Element(boolean)
// @description
// returns whether the NPC is in combat
// -->
if (attribute.startsWith("navigator.is_fighting"))
return new Element(String.valueOf(getNavigator().getEntityTarget().isAggressive()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.target_type>
// @returns Element
// @description
// returns the entity type of the target
// -->
if (attribute.startsWith("navigator.target_type"))
return new Element(String.valueOf(getNavigator().getTargetType().toString()))
.getAttribute(attribute.fulfill(2));
// <--[tag]
// @attribute <npc.navigator.target_entity>
// @returns dEntity
// @description
// returns the entity being targeted
// -->
if (attribute.startsWith("navigator.target_entity"))
return (getNavigator().getEntityTarget().getTarget() != null
? new dEntity(getNavigator().getEntityTarget().getTarget()).getAttribute(attribute.fulfill(2))
: "null");
return (getEntity() != null
? new dEntity(getEntity()).getAttribute(attribute.fulfill(0))
: new Element(identify()).getAttribute(attribute.fulfill(0)));
}
}