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

96 lines
2.4 KiB
Java
Raw Normal View History

2012-01-15 00:51:37 +01:00
package net.citizensnpcs.npc;
2012-01-23 16:42:01 +01:00
import net.citizensnpcs.api.DataKey;
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;
import net.citizensnpcs.api.npc.ai.Navigator;
2012-01-21 17:21:21 +01:00
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.npc.ai.CitizensNavigator;
2012-01-15 00:51:37 +01:00
import net.citizensnpcs.resources.lib.CraftNPC;
2012-01-19 11:52:58 +01:00
import net.citizensnpcs.util.Messaging;
2012-01-15 00:51:37 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
2012-01-20 14:15:14 +01:00
import org.bukkit.entity.Entity;
2012-01-15 00:51:37 +01:00
2012-01-23 10:46:06 +01:00
public class CitizensNPC extends AbstractNPC {
2012-01-19 12:43:21 +01:00
private CraftNPC mcEntity;
private boolean spawned;
private final CitizensNPCManager manager;
2012-01-23 10:46:06 +01:00
public CitizensNPC(CitizensNPCManager manager, int id, String name) {
super(id, name);
this.manager = manager;
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);
getHandle().die();
spawned = false;
2012-01-26 15:45:42 +01:00
return true;
2012-01-19 12:43:21 +01:00
}
@Override
2012-01-23 09:45:34 +01:00
public Entity getBukkitEntity() {
return getHandle().getBukkitEntity();
2012-01-19 12:43:21 +01:00
}
2012-01-23 09:45:34 +01:00
public CraftNPC getHandle() {
return mcEntity;
}
2012-01-19 12:43:21 +01:00
@Override
public Navigator getNavigator() {
return new CitizensNavigator(this);
2012-01-19 12:43:21 +01:00
}
2012-01-23 09:45:34 +01:00
@Override
public boolean isSpawned() {
return spawned;
}
@Override
public void remove() {
if (isSpawned())
despawn();
manager.remove(this);
}
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
2012-01-22 15:23:25 +01:00
if (mcEntity == null)
2012-01-20 08:27:14 +01:00
mcEntity = manager.spawn(this, loc);
2012-01-22 15:23:25 +01:00
else
2012-01-20 08:27:14 +01:00
manager.spawn(this, loc);
2012-01-20 08:48:55 +01:00
// Set the location
2012-01-22 08:10:25 +01:00
addTrait(new SpawnLocation(loc));
2012-01-20 08:48:55 +01:00
2012-01-20 08:27:14 +01:00
spawned = true;
2012-01-26 15:45:42 +01:00
return true;
2012-01-19 12:43:21 +01:00
}
2012-01-23 16:42:01 +01:00
@Override
2012-01-28 05:11:23 +01:00
public void load(DataKey root) {
2012-01-23 16:42:01 +01:00
}
2012-01-15 00:51:37 +01:00
}