Citizens2/src/main/java/net/citizensnpcs/npc/CitizensNPC.java

124 lines
4.0 KiB
Java
Raw Normal View History

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;
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()) {
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
}
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);
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());
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()) {
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()) {
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
@Override
2012-02-04 10:58:51 +01:00
public void update() {
try {
super.update();
} catch (Exception ex) {
Messaging.logF("Exception while updating %d: %s.", getId(), ex.getMessage());
ex.printStackTrace();
}
2012-02-04 10:58:51 +01:00
}
2012-01-15 00:51:37 +01:00
}