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

137 lines
3.7 KiB
Java
Raw Normal View History

2012-01-15 00:51:37 +01:00
package net.citizensnpcs.npc;
import net.citizensnpcs.Citizens;
2012-02-12 19:29:33 +01:00
import net.citizensnpcs.Settings.Setting;
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-01-23 10:46:06 +01:00
import net.citizensnpcs.api.npc.AbstractNPC;
2012-02-23 11:22:48 +01:00
import net.citizensnpcs.api.trait.builtin.Inventory;
import net.citizensnpcs.api.trait.builtin.SpawnLocation;
import net.citizensnpcs.api.trait.builtin.Spawned;
2012-02-04 10:58:51 +01:00
import net.citizensnpcs.npc.ai.CitizensAI;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.util.Messaging;
2012-02-04 10:58:51 +01:00
import net.minecraft.server.EntityLiving;
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-01-15 00:51:37 +01:00
public abstract class CitizensNPC extends AbstractNPC {
2012-02-03 10:20:48 +01:00
protected final CitizensNPCManager manager;
2012-02-04 10:58:51 +01:00
protected final CitizensAI ai = new CitizensAI(this);
2012-02-14 11:52:22 +01:00
protected EntityLiving mcEntity;
protected final NPCInventory inventory;
2012-02-03 10:20:48 +01:00
protected CitizensNPC(CitizensNPCManager manager, int id, String name) {
super(id, name);
2012-01-23 10:46:06 +01:00
this.manager = manager;
inventory = new NPCInventory(this);
2012-01-19 12:43:21 +01:00
}
2012-02-12 19:29:33 +01:00
@Override
public void chat(String message) {
for (Player player : Bukkit.getOnlinePlayers())
chat(player, message);
}
@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-04 10:58:51 +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()) {
Messaging.debug("The NPC with the ID '" + getId() + "' is already despawned.");
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));
manager.despawn(this, getTrait(Spawned.class).shouldSpawn());
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
}
@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-02-04 10:58:51 +01:00
public CitizensAI getAI() {
2012-02-04 23:58:40 +01:00
return ai;
2012-01-19 12:43:21 +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
}
@Override
public void remove() {
if (isSpawned())
despawn();
manager.remove(this);
((Citizens) Bukkit.getServer().getPluginManager().getPlugin("Citizens")).getStorage().getKey("npc").removeKey(
String.valueOf(getId()));
2012-01-23 09:45:34 +01:00
}
2012-01-19 12:43:21 +01:00
@Override
2012-01-26 15:45:42 +01:00
public boolean spawn(Location loc) {
2012-01-19 12:43:21 +01:00
if (isSpawned()) {
Messaging.debug("The NPC with the 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
mcEntity = createHandle(loc);
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
// Set the location
2012-02-03 10:20:48 +01:00
addTrait(new SpawnLocation(loc));
// Set the spawned state
addTrait(new Spawned(true));
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-02-05 08:18:40 +01:00
@Override
2012-02-05 10:10:09 +01:00
public org.bukkit.inventory.Inventory getInventory() {
return inventory.asInventory();
2012-02-05 08:18:40 +01:00
}
@Override
public boolean openInventory(Player player) {
2012-02-05 12:31:08 +01:00
if (!isSpawned())
2012-02-05 08:18:40 +01:00
return false;
getInventory().setContents(getTrait(Inventory.class).getContents());
2012-02-05 10:10:09 +01:00
inventory.show(player);
return true;
2012-02-05 08:18:40 +01:00
}
2012-02-07 10:45:43 +01:00
@Override
2012-02-04 10:58:51 +01:00
public void update() {
2012-02-07 10:45:43 +01:00
super.update();
2012-02-04 10:58:51 +01:00
ai.update();
}
@Override
public void setName(String name) {
super.setName(name);
inventory.setName(name);
}
2012-01-15 00:51:37 +01:00
}