2012-01-15 00:51:37 +01:00
|
|
|
package net.citizensnpcs.npc;
|
|
|
|
|
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-01-23 07:12:45 +01:00
|
|
|
import net.citizensnpcs.api.npc.ai.Navigator;
|
2012-01-21 17:21:21 +01:00
|
|
|
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
|
2012-01-29 19:23:42 +01:00
|
|
|
import net.citizensnpcs.api.npc.trait.trait.Spawned;
|
2012-01-25 11:09:02 +01:00
|
|
|
import net.citizensnpcs.npc.ai.CitizensNavigator;
|
2012-02-03 10:20:48 +01:00
|
|
|
import net.citizensnpcs.trait.LookClose;
|
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-02-03 10:20:48 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2012-01-15 00:51:37 +01:00
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
public abstract class CitizensNPC extends AbstractNPC {
|
|
|
|
private static final double lookRange = 5;
|
2012-02-03 10:20:48 +01:00
|
|
|
protected final CitizensNPCManager manager;
|
2012-02-04 07:48:23 +01:00
|
|
|
protected net.minecraft.server.Entity mcEntity;
|
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-01-23 10:46:06 +01:00
|
|
|
super(id, name);
|
|
|
|
this.manager = manager;
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
protected abstract net.minecraft.server.Entity 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-01-25 16:29:54 +01:00
|
|
|
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);
|
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-02-04 07:48:23 +01:00
|
|
|
// TODO: is this necessary? it's a helper method...
|
|
|
|
protected void faceEntity(Location target) {
|
|
|
|
if (getBukkitEntity().getWorld() != target.getWorld())
|
|
|
|
return;
|
|
|
|
Location loc = getBukkitEntity().getLocation();
|
|
|
|
|
|
|
|
double xDiff = target.getX() - loc.getX();
|
|
|
|
double yDiff = target.getY() - loc.getY();
|
|
|
|
double zDiff = target.getZ() - loc.getZ();
|
|
|
|
|
|
|
|
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
|
|
|
|
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
|
|
|
|
|
|
|
|
double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI);
|
|
|
|
double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90;
|
|
|
|
if (zDiff < 0.0) {
|
|
|
|
yaw = yaw + (Math.abs(180 - yaw) * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
mcEntity.yaw = (float) yaw - 90;
|
|
|
|
mcEntity.pitch = (float) pitch;
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
2012-02-03 10:20:48 +01:00
|
|
|
public Entity getBukkitEntity() {
|
2012-01-29 20:29:23 +01:00
|
|
|
return getHandle().getBukkitEntity();
|
2012-01-19 12:43:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 07:48:23 +01:00
|
|
|
public net.minecraft.server.Entity getHandle() {
|
2012-01-23 09:45:34 +01:00
|
|
|
return mcEntity;
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:43:21 +01:00
|
|
|
@Override
|
|
|
|
public Navigator getNavigator() {
|
2012-01-25 11:09:02 +01:00
|
|
|
return new CitizensNavigator(this);
|
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);
|
|
|
|
}
|
|
|
|
|
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()) {
|
2012-01-25 16:29:54 +01:00
|
|
|
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-02-04 07:48:23 +01:00
|
|
|
mcEntity = createHandle(loc);
|
|
|
|
mcEntity.world.addEntity(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-03 10:20:48 +01:00
|
|
|
|
|
|
|
public void tick() {
|
2012-02-04 07:48:23 +01:00
|
|
|
// TODO: this needs to be less hard-coded... does everyone want this
|
|
|
|
// behaviour?
|
|
|
|
if (mcEntity != null) {
|
2012-02-03 10:20:48 +01:00
|
|
|
if (getTrait(LookClose.class).shouldLookClose()
|
|
|
|
&& mcEntity.world.findNearbyPlayer(mcEntity, lookRange) != null)
|
2012-02-04 07:48:23 +01:00
|
|
|
faceEntity(mcEntity.world.findNearbyPlayer(mcEntity, lookRange).getBukkitEntity().getLocation());
|
2012-02-03 10:20:48 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-15 00:51:37 +01:00
|
|
|
}
|