2012-01-15 00:51:37 +01:00
|
|
|
package net.citizensnpcs.npc;
|
|
|
|
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
2012-05-26 17:46:57 +02:00
|
|
|
import net.citizensnpcs.api.abstraction.WorldVector;
|
|
|
|
import net.citizensnpcs.api.attachment.Attachment;
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.exception.NPCLoadException;
|
2012-01-23 10:46:06 +01:00
|
|
|
import net.citizensnpcs.api.npc.AbstractNPC;
|
2012-05-26 17:46:57 +02:00
|
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
2012-03-12 07:26:08 +01:00
|
|
|
import net.citizensnpcs.trait.CurrentLocation;
|
2012-01-19 11:52:58 +01:00
|
|
|
import net.citizensnpcs.util.Messaging;
|
2012-01-19 12:43:21 +01:00
|
|
|
|
2012-05-26 17:46:57 +02:00
|
|
|
public class CitizensNPC extends AbstractNPC {
|
2012-05-31 08:31:31 +02:00
|
|
|
private boolean spawned;
|
2012-02-12 19:29:33 +01:00
|
|
|
|
2012-05-26 17:46:57 +02:00
|
|
|
public CitizensNPC(NPCRegistry registry, String name) {
|
|
|
|
super(registry, name);
|
2012-02-27 14:01:38 +01:00
|
|
|
}
|
|
|
|
|
2012-05-31 08:31:31 +02:00
|
|
|
public CitizensNPC(String name) {
|
|
|
|
super(CitizensAPI.getNPCRegistry(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Attachment getAttachmentFor(Class<? extends Attachment> clazz) {
|
2012-06-23 12:01:28 +02:00
|
|
|
return CitizensAPI.getAttachmentFactory().getAttachment(clazz);
|
2012-05-31 08:31:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Attachment getAttachmentFor(String name) {
|
2012-06-23 12:01:28 +02:00
|
|
|
return CitizensAPI.getAttachmentFactory().getAttachment(name);
|
2012-03-10 20:41:09 +01:00
|
|
|
}
|
|
|
|
|
2012-04-24 17:05:38 +02:00
|
|
|
public void load(DataKey root) {
|
2012-03-27 16:42:15 +02:00
|
|
|
// Load traits
|
2012-05-26 17:46:57 +02:00
|
|
|
for (DataKey attachmentKey : root.getRelative("traits").getSubKeys()) {
|
2012-05-31 08:31:31 +02:00
|
|
|
Attachment trait = getAttachmentFor(attachmentKey.name());
|
2012-04-24 17:05:38 +02:00
|
|
|
if (trait == null) {
|
2012-05-26 17:46:57 +02:00
|
|
|
Messaging.severeF("Skipped missing attachment '%s' while loading NPC ID: '%d'. Has the name changed?",
|
|
|
|
attachmentKey.name(), getId());
|
2012-04-24 17:05:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-31 08:31:31 +02:00
|
|
|
attach(trait);
|
2012-03-27 16:42:15 +02:00
|
|
|
try {
|
2012-05-26 17:46:57 +02:00
|
|
|
getAttachment(trait.getClass()).load(attachmentKey);
|
2012-05-17 15:34:03 +02:00
|
|
|
} catch (NPCLoadException ex) {
|
2012-05-26 17:46:57 +02:00
|
|
|
Messaging.logF("The attachment '%s' failed to load for NPC ID: '%d'.", attachmentKey.name(), getId(),
|
2012-05-17 15:34:03 +02:00
|
|
|
ex.getMessage());
|
2012-03-27 16:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-17 18:06:31 +02:00
|
|
|
|
|
|
|
// Spawn the NPC
|
2012-05-31 08:31:31 +02:00
|
|
|
spawned = root.getBoolean("spawned");
|
|
|
|
if (spawned) {
|
2012-05-26 17:46:57 +02:00
|
|
|
WorldVector spawnLoc = getAttachment(CurrentLocation.class).getLocation();
|
2012-05-17 18:06:31 +02:00
|
|
|
if (spawnLoc != null)
|
|
|
|
spawn(spawnLoc);
|
|
|
|
}
|
2012-03-27 16:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void save(DataKey root) {
|
2012-05-31 08:31:31 +02:00
|
|
|
root.setString("name", name);
|
2012-03-27 16:42:15 +02:00
|
|
|
// Save all existing traits
|
2012-05-26 17:46:57 +02:00
|
|
|
for (Attachment trait : attachments.values()) {
|
2012-04-30 15:38:18 +02:00
|
|
|
trait.save(root.getRelative("traits." + trait.getName()));
|
2012-03-27 16:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 11:17:28 +01:00
|
|
|
@Override
|
2012-02-04 10:58:51 +01:00
|
|
|
public void update() {
|
2012-05-12 09:13:58 +02:00
|
|
|
try {
|
|
|
|
super.update();
|
|
|
|
} catch (Exception ex) {
|
2012-05-23 10:51:33 +02:00
|
|
|
Messaging.logF("Exception while updating %d: %s.", getId(), ex.getMessage());
|
2012-05-12 09:13:58 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2012-02-04 10:58:51 +01:00
|
|
|
}
|
2012-01-15 00:51:37 +01:00
|
|
|
}
|