-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
dNPC.java
349 lines (279 loc) · 12.4 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
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_R1.EntityLiving;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_6_R1.entity.CraftLivingEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.HashMap;
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;
}
private int npcid = -1;
private boolean is_valid = true;
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 an 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 an 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 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) {
if (getCitizen() != null)
{
if (getCitizen().hasTrait(AssignmentTrait.class))
DenizenAPI.getCurrentInstance().getNPCRegistry()
.getActionHandler().doAction(
actionName,
this,
player,
getAssignmentTrait().getAssignment());
}
}
private String prefix = "npc";
@Override
public String getPrefix() {
return prefix; //To change body of implemented methods use File | Settings | File Templates.
}
@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; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public dNPC setPrefix(String prefix) {
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
if (attribute.startsWith("name.nickname"))
return new Element(getCitizen().hasTrait(NicknameTrait.class) ? getCitizen().getTrait(NicknameTrait.class)
.getNickname() : getName()).getAttribute(attribute.fulfill(2));
if (attribute.startsWith("name"))
return new Element(ChatColor.stripColor(getName()))
.getAttribute(attribute.fulfill(1));
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));
}
if (attribute.startsWith("has_anchors")) {
return (new Element(String.valueOf(getCitizen().getTrait(Anchors.class).getAnchors().size() > 0)))
.getAttribute(attribute.fulfill(1));
}
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));
}
if (attribute.startsWith("flag")) {
if (attribute.hasContext(1)) {
if (FlagManager.npcHasFlag(this, attribute.getContext(1)))
return new dList(DenizenAPI.getCurrentInstance().flagManager()
.getNPCFlag(getId(), attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
return "null";
}
else return null;
}
if (attribute.startsWith("id"))
return new Element(String.valueOf(getId())).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("owner"))
return new Element(getOwner()).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("is_spawned"))
return new Element(String.valueOf(isSpawned())).getAttribute(attribute.fulfill(1));
if (attribute.startsWith("location.previous_location"))
return (NPCTags.previousLocations.containsKey(getId())
? NPCTags.previousLocations.get(getId()).getAttribute(attribute.fulfill(2))
: "null");
if (attribute.startsWith("navigator.is_navigating"))
return new Element(String.valueOf(getNavigator().isNavigating())).getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.speed"))
return new Element(String.valueOf(getNavigator().getLocalParameters().speed()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.attack_strategy"))
return new Element(String.valueOf(getNavigator().getLocalParameters().attackStrategy().toString()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.speed_modifier"))
return new Element(String.valueOf(getNavigator().getLocalParameters().speedModifier()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.base_speed"))
return new Element(String.valueOf(getNavigator().getLocalParameters().baseSpeed()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.avoid_water"))
return new Element(String.valueOf(getNavigator().getLocalParameters().avoidWater()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.target_location"))
return (getNavigator().getTargetAsLocation() != null
? new dLocation(getNavigator().getTargetAsLocation()).getAttribute(attribute.fulfill(2))
: "null");
if (attribute.startsWith("navigator.is_fighting"))
return new Element(String.valueOf(getNavigator().getEntityTarget().isAggressive()))
.getAttribute(attribute.fulfill(2));
if (attribute.startsWith("navigator.target_type"))
return new Element(String.valueOf(getNavigator().getTargetType().toString()))
.getAttribute(attribute.fulfill(2));
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)));
}
}