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

189 lines
6.2 KiB
Java
Raw Normal View History

2012-01-15 00:51:37 +01:00
package net.citizensnpcs.npc;
2012-02-03 10:20:48 +01:00
import java.lang.reflect.Field;
import java.util.Map;
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;
2012-01-29 19:23:42 +01:00
import net.citizensnpcs.api.npc.trait.trait.Spawned;
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-02-03 10:20:48 +01:00
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityTypes;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldServer;
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.Server;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
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-02-03 10:20:48 +01:00
protected EntityLiving mcEntity;
protected final CitizensNPCManager manager;
private Map<Integer, Class<? extends net.minecraft.server.Entity>> intToClass;
private Map<Class<? extends net.minecraft.server.Entity>, Integer> classToInt;
private Class<? extends EntityLiving> entityClass;
private double lookRange = 5;
2012-01-19 12:43:21 +01:00
2012-02-03 10:20:48 +01:00
protected CitizensNPC(CitizensNPCManager manager, int id, String name, Class<? extends EntityLiving> entityClass) {
2012-01-23 10:46:06 +01:00
super(id, name);
this.manager = manager;
2012-02-03 10:20:48 +01:00
if (intToClass == null || classToInt == null)
createMaps();
this.entityClass = entityClass;
// No need to register entity class for normal human NPC entity
if (entityClass != null)
registerEntityClass(entityClass);
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);
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-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-03 10:20:48 +01:00
public EntityLiving getHandle() {
2012-01-23 09:45:34 +01:00
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() {
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()) {
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-03 10:20:48 +01:00
if (entityClass != null) {
mcEntity = createEntityFromClass(getWorldServer(loc.getWorld()));
mcEntity.setPosition(loc.getX(), loc.getY(), loc.getZ());
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
protected WorldServer getWorldServer(World world) {
return ((CraftWorld) world).getHandle();
}
protected MinecraftServer getMinecraftServer(Server server) {
return ((CraftServer) server).getServer();
}
public void tick() {
if (mcEntity != null)
if (getTrait(LookClose.class).shouldLookClose()
&& mcEntity.world.findNearbyPlayer(mcEntity, lookRange) != null)
faceEntity(mcEntity.world.findNearbyPlayer(mcEntity, lookRange).getBukkitEntity());
}
protected void faceEntity(Entity target) {
if (getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = getBukkitEntity().getLocation();
Location targetLoc = target.getLocation();
double xDiff = targetLoc.getX() - loc.getX();
double yDiff = targetLoc.getY() - loc.getY();
double zDiff = targetLoc.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;
}
private void registerEntityClass(Class<? extends net.minecraft.server.Entity> clazz) {
if (classToInt == null || intToClass == null)
return;
Class<?> search = clazz;
while ((search = search.getSuperclass()) != null && net.minecraft.server.Entity.class.isAssignableFrom(search)) {
if (!classToInt.containsKey(search))
continue;
int code = classToInt.get(search);
intToClass.put(code, clazz);
classToInt.put(clazz, code);
return;
}
throw new IllegalArgumentException("unable to find valid entity superclass");
}
@SuppressWarnings("unchecked")
private void createMaps() {
try {
Field field = EntityTypes.class.getDeclaredField("d");
field.setAccessible(true);
intToClass = (Map<Integer, Class<? extends net.minecraft.server.Entity>>) field.get(null);
field = EntityTypes.class.getDeclaredField("e");
field.setAccessible(true);
classToInt = (Map<Class<? extends net.minecraft.server.Entity>, Integer>) field.get(null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private EntityLiving createEntityFromClass(net.minecraft.server.World world) {
try {
return entityClass.getConstructor(net.minecraft.server.World.class).newInstance(world);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
2012-01-15 00:51:37 +01:00
}