getTrait is safer

This commit is contained in:
aPunch 2012-02-04 16:58:40 -06:00
parent 76a769489c
commit 6aca4590a3
5 changed files with 45 additions and 32 deletions

Binary file not shown.

View File

@ -1,8 +1,15 @@
package net.citizensnpcs;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityLiving;
public class NPCUpdater implements Runnable {
private final CitizensNPCManager npcManager;
@ -13,7 +20,42 @@ public class NPCUpdater implements Runnable {
@Override
public void run() {
for (NPC npc : npcManager)
((CitizensNPC) npc).update();
for (NPC npc : npcManager) {
if (!npc.isSpawned()) {
Messaging.debug(npc.getName() + " is not spawned.");
continue;
}
Messaging.debug(npc.getName());
CitizensNPC handle = (CitizensNPC) npc;
handle.update();
// This needs to be handled somewhere...is this the best place?
EntityLiving search = null;
if ((search = handle.getHandle().world.findNearbyPlayer(handle.getHandle(), 5)) != null
&& npc.getTrait(LookClose.class).shouldLookClose())
faceEntity(handle, search.getBukkitEntity());
}
}
private void faceEntity(CitizensNPC npc, Entity target) {
if (npc.getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = npc.getBukkitEntity().getLocation();
double xDiff = target.getLocation().getX() - loc.getX();
double yDiff = target.getLocation().getY() - loc.getY();
double zDiff = target.getLocation().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);
}
npc.getHandle().yaw = (float) yaw - 90;
npc.getHandle().pitch = (float) pitch;
}
}

View File

@ -11,7 +11,6 @@ import net.minecraft.server.World;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.LivingEntity;
@SuppressWarnings("unchecked")
public abstract class CitizensMobNPC extends CitizensNPC {
@ -44,11 +43,6 @@ public abstract class CitizensMobNPC extends CitizensNPC {
return entity;
}
@Override
public LivingEntity getBukkitEntity() {
return (LivingEntity) getHandle().getBukkitEntity();
}
private static Map<Class<? extends Entity>, Integer> classToInt;
private static Map<Integer, Class<? extends Entity>> intToClass;

View File

@ -51,7 +51,7 @@ public abstract class CitizensNPC extends AbstractNPC {
@Override
public CitizensAI getAI() {
return new CitizensAI(this);
return ai;
}
@Override

View File

@ -7,7 +7,6 @@ import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.util.Messaging;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
public class CitizensAI implements AI {
@ -25,28 +24,6 @@ public class CitizensAI implements AI {
}
private void faceEntity(Entity target) {
if (npc.getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = npc.getBukkitEntity().getLocation();
double xDiff = target.getLocation().getX() - loc.getX();
double yDiff = target.getLocation().getY() - loc.getY();
double zDiff = target.getLocation().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);
}
npc.getHandle().yaw = (float) yaw - 90;
npc.getHandle().pitch = (float) pitch;
}
@Override
public void registerNavigationCallback(NavigationCallback callback) {
}