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.LivingEntity;
|
|
|
|
import net.citizensnpcs.api.abstraction.WorldVector;
|
|
|
|
import net.citizensnpcs.api.attachment.Attachment;
|
|
|
|
import net.citizensnpcs.api.attachment.builtin.Spawned;
|
2012-01-15 00:58:47 +01:00
|
|
|
import net.citizensnpcs.api.event.NPCDespawnEvent;
|
2012-01-15 00:51:37 +01:00
|
|
|
import net.citizensnpcs.api.event.NPCSpawnEvent;
|
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 {
|
|
|
|
public CitizensNPC(String name) {
|
|
|
|
super(CitizensAPI.getNPCRegistry(), name);
|
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-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-01-26 15:45:42 +01:00
|
|
|
public boolean despawn() {
|
2012-01-23 09:45:34 +01:00
|
|
|
if (!isSpawned()) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Messaging.debug(String.format("The NPC with the ID '%d' is already despawned.", getId()));
|
2012-01-26 15:45:42 +01:00
|
|
|
return false;
|
2012-01-23 09:45:34 +01:00
|
|
|
}
|
2012-01-19 12:43:21 +01:00
|
|
|
|
2012-05-26 17:46:57 +02:00
|
|
|
CitizensAPI.getServer().callEvent(new NPCDespawnEvent(this));
|
|
|
|
getEntity().remove();
|
|
|
|
controller = null;
|
2012-01-23 09:45:34 +01:00
|
|
|
|
2012-01-26 15:45:42 +01:00
|
|
|
return true;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
@Override
|
2012-05-26 17:46:57 +02:00
|
|
|
public LivingEntity getEntity() {
|
|
|
|
return (LivingEntity) controller.getEntity();
|
2012-03-02 11:36:54 +01:00
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-05-26 17:46:57 +02:00
|
|
|
public boolean isSpawned() {
|
|
|
|
return getEntity() != null;
|
2012-03-10 20:41:09 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 09:45:34 +01:00
|
|
|
@Override
|
2012-05-26 17:46:57 +02:00
|
|
|
protected Attachment getAttachmentFor(Class<? extends Attachment> clazz) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
2012-01-23 09:45:34 +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()) {
|
|
|
|
Attachment trait = attachmentFactory.getTrait(attachmentKey.name(), this);
|
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-26 17:46:57 +02:00
|
|
|
addAttachment(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-26 17:46:57 +02:00
|
|
|
if (getAttachment(Spawned.class).shouldSpawn()) {
|
|
|
|
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) {
|
|
|
|
root.setString("name", getFullName());
|
|
|
|
// 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-02 11:36:54 +01:00
|
|
|
@Override
|
2012-05-26 17:46:57 +02:00
|
|
|
public boolean spawn(WorldVector at) {
|
|
|
|
if (at == null)
|
|
|
|
throw new IllegalArgumentException("location cannot be null");
|
2012-01-19 12:43:21 +01:00
|
|
|
if (isSpawned()) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Messaging.debug("NPC (ID: " + getId() + ") is already spawned.");
|
2012-01-26 15:45:42 +01:00
|
|
|
return false;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
2012-05-26 17:46:57 +02:00
|
|
|
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, at);
|
|
|
|
CitizensAPI.getServer().callEvent(spawnEvent);
|
2012-01-22 15:23:25 +01:00
|
|
|
if (spawnEvent.isCancelled())
|
2012-01-26 15:45:42 +01:00
|
|
|
return false;
|
2012-01-19 12:43:21 +01:00
|
|
|
|
2012-05-26 17:46:57 +02:00
|
|
|
controller.spawn(at);
|
2012-01-20 08:48:55 +01:00
|
|
|
|
2012-02-03 10:20:48 +01:00
|
|
|
// Set the spawned state
|
2012-05-26 17:46:57 +02:00
|
|
|
getAttachment(CurrentLocation.class).setLocation(at);
|
|
|
|
getAttachment(Spawned.class).setSpawned(true);
|
2012-03-11 09:53:19 +01:00
|
|
|
|
|
|
|
// Modify NPC using traits after the entity has been created
|
2012-05-26 17:46:57 +02:00
|
|
|
for (Attachment attached : attachments.values())
|
|
|
|
attached.onSpawn();
|
2012-01-26 15:45:42 +01:00
|
|
|
return true;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
2012-02-04 10:58:51 +01: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
|
|
|
}
|