2012-01-15 00:51:37 +01:00
|
|
|
package net.citizensnpcs.npc;
|
|
|
|
|
2012-02-12 19:29:33 +01:00
|
|
|
import net.citizensnpcs.Settings.Setting;
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
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-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.npc.character.Character;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
2012-02-23 12:17:15 +01:00
|
|
|
import net.citizensnpcs.api.trait.trait.Spawned;
|
2012-03-09 21:53:02 +01:00
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
2012-02-04 10:58:51 +01:00
|
|
|
import net.citizensnpcs.npc.ai.CitizensAI;
|
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-03-01 13:07:54 +01:00
|
|
|
import net.citizensnpcs.util.StringHelper;
|
2012-02-04 10:58:51 +01:00
|
|
|
import net.minecraft.server.EntityLiving;
|
2012-01-15 00:51:37 +01:00
|
|
|
|
2012-05-11 17:13:10 +02:00
|
|
|
import org.apache.commons.lang.Validate;
|
2012-01-15 00:51:37 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
2012-02-04 10:58:51 +01:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2012-02-05 08:18:40 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2012-03-01 11:45:44 +01:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2012-01-15 00:51:37 +01:00
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
public abstract class CitizensNPC extends AbstractNPC {
|
2012-03-12 11:17:28 +01:00
|
|
|
private final CitizensAI ai = new CitizensAI(this);
|
|
|
|
private final CitizensNPCManager manager;
|
2012-03-14 08:05:15 +01:00
|
|
|
protected EntityLiving mcEntity;
|
2012-03-27 16:42:15 +02:00
|
|
|
private final CitizensTraitManager traitManager;
|
2012-02-03 10:20:48 +01:00
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
protected CitizensNPC(CitizensNPCManager manager, int id, String name) {
|
2012-02-07 11:37:12 +01:00
|
|
|
super(id, name);
|
2012-01-23 10:46:06 +01:00
|
|
|
this.manager = manager;
|
2012-03-10 20:41:09 +01:00
|
|
|
traitManager = (CitizensTraitManager) CitizensAPI.getTraitManager();
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-18 23:29:29 +01:00
|
|
|
@Override
|
|
|
|
public void chat(Player player, String message) {
|
|
|
|
Messaging.sendWithNPC(player, Setting.CHAT_PREFIX.asString() + message, this);
|
2012-02-12 19:29:33 +01:00
|
|
|
}
|
|
|
|
|
2012-02-27 14:01:38 +01:00
|
|
|
@Override
|
2012-03-02 11:36:54 +01:00
|
|
|
public void chat(String message) {
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers())
|
|
|
|
chat(player, message);
|
2012-02-27 14:01:38 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 11:36:54 +01:00
|
|
|
protected abstract EntityLiving createHandle(Location loc);
|
|
|
|
|
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-01-23 09:45:34 +01:00
|
|
|
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
|
2012-05-10 14:51:11 +02:00
|
|
|
boolean keepSelected = getTrait(Spawned.class).shouldSpawn();
|
|
|
|
if (!keepSelected)
|
|
|
|
removeMetadata("selectors", CitizensAPI.getPlugin());
|
|
|
|
getBukkitEntity().remove();
|
2012-01-29 20:14:10 +01:00
|
|
|
mcEntity = 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
|
|
|
|
public CitizensAI getAI() {
|
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-02-04 10:58:51 +01:00
|
|
|
public LivingEntity getBukkitEntity() {
|
|
|
|
return (LivingEntity) getHandle().getBukkitEntity();
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 10:58:51 +01:00
|
|
|
public EntityLiving getHandle() {
|
2012-01-23 09:45:34 +01:00
|
|
|
return mcEntity;
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-03-02 11:36:54 +01:00
|
|
|
public org.bukkit.inventory.Inventory getInventory() {
|
|
|
|
Inventory inventory = Bukkit.getServer().createInventory(this, 36, StringHelper.parseColors(getFullName()));
|
|
|
|
inventory.setContents(getTrait(net.citizensnpcs.api.trait.trait.Inventory.class).getContents());
|
|
|
|
return inventory;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-03-10 20:41:09 +01:00
|
|
|
@Override
|
2012-04-30 15:38:18 +02:00
|
|
|
public Trait getTraitFor(Class<? extends Trait> clazz) {
|
|
|
|
return traitManager.getTrait(clazz, this);
|
2012-03-10 20:41:09 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 09:45:34 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSpawned() {
|
2012-01-29 19:23:42 +01:00
|
|
|
return getHandle() != 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
|
|
|
Character character = CitizensAPI.getCharacterManager().getCharacter(root.getString("character"));
|
|
|
|
|
|
|
|
// Load the character if it exists
|
|
|
|
if (character != null) {
|
2012-04-24 17:05:38 +02:00
|
|
|
try {
|
|
|
|
character.load(root.getRelative("characters." + character.getName()));
|
|
|
|
} catch (NPCLoadException e) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Messaging.severe(String.format("Unable to load character '%s'.", character.getName()));
|
2012-04-24 17:05:38 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2012-03-27 16:42:15 +02:00
|
|
|
setCharacter(character);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load traits
|
|
|
|
for (DataKey traitKey : root.getRelative("traits").getSubKeys()) {
|
|
|
|
Trait trait = traitManager.getTrait(traitKey.name(), this);
|
2012-04-24 17:05:38 +02:00
|
|
|
if (trait == null) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Messaging.severe(String.format(
|
|
|
|
"Found missing trait '%s' while loading NPC ID: '%d' - skipped. Has the name changed?",
|
|
|
|
traitKey.name(), getId()));
|
2012-04-24 17:05:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-27 16:42:15 +02:00
|
|
|
addTrait(trait);
|
|
|
|
try {
|
|
|
|
getTrait(trait.getClass()).load(traitKey);
|
|
|
|
} catch (Exception ex) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Messaging.log(String.format("The trait '%s' failed to load properly for NPC ID: '%d'.",
|
|
|
|
traitKey.name(), getId()), ex.getMessage());
|
2012-03-27 16:42:15 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn the NPC
|
2012-05-11 17:13:10 +02:00
|
|
|
if (getTrait(Spawned.class).shouldSpawn()) {
|
|
|
|
Location spawnLoc = getTrait(CurrentLocation.class).getLocation();
|
|
|
|
if (spawnLoc != null)
|
|
|
|
spawn(spawnLoc);
|
|
|
|
}
|
2012-03-27 16:42:15 +02:00
|
|
|
}
|
|
|
|
|
2012-01-23 09:45:34 +01:00
|
|
|
@Override
|
|
|
|
public void remove() {
|
2012-03-12 19:50:48 +01:00
|
|
|
super.remove();
|
2012-03-01 11:45:44 +01:00
|
|
|
manager.remove(this);
|
2012-01-23 09:45:34 +01:00
|
|
|
if (isSpawned())
|
|
|
|
despawn();
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:42:15 +02:00
|
|
|
public void save(DataKey root) {
|
|
|
|
root.setString("name", getFullName());
|
|
|
|
|
|
|
|
// Save the character if it exists
|
|
|
|
if (getCharacter() != null) {
|
|
|
|
root.setString("character", getCharacter().getName());
|
|
|
|
getCharacter().save(root.getRelative("characters." + getCharacter().getName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save all existing traits
|
2012-04-30 15:38:18 +02:00
|
|
|
for (Trait trait : traits.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
|
|
|
|
public void setName(String name) {
|
|
|
|
super.setName(name);
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-01-26 15:45:42 +01:00
|
|
|
public boolean spawn(Location loc) {
|
2012-05-11 17:13:10 +02:00
|
|
|
Validate.notNull(loc, "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
|
|
|
}
|
|
|
|
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc);
|
|
|
|
Bukkit.getPluginManager().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-02-04 07:48:23 +01:00
|
|
|
mcEntity = createHandle(loc);
|
2012-03-15 12:38:10 +01:00
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
mcEntity.world.addEntity(mcEntity);
|
2012-02-06 09:30:45 +01:00
|
|
|
mcEntity.world.players.remove(mcEntity);
|
2012-01-20 08:48:55 +01:00
|
|
|
|
2012-02-03 10:20:48 +01:00
|
|
|
// Set the spawned state
|
2012-05-11 17:13:10 +02:00
|
|
|
getTrait(CurrentLocation.class).setLocation(loc);
|
2012-03-08 02:03:32 +01:00
|
|
|
getTrait(Spawned.class).setSpawned(true);
|
2012-03-11 09:53:19 +01:00
|
|
|
|
|
|
|
// Modify NPC using traits after the entity has been created
|
2012-04-30 15:38:18 +02:00
|
|
|
for (Trait trait : traits.values())
|
|
|
|
trait.onNPCSpawn();
|
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();
|
|
|
|
ai.update();
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Messaging.log("Exception while updating " + getId() + ": " + ex.getMessage() + ".");
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2012-02-04 10:58:51 +01:00
|
|
|
}
|
2012-01-15 00:51:37 +01:00
|
|
|
}
|