Citizens2/src/main/java/net/citizensnpcs/trait/LookClose.java

107 lines
3.3 KiB
Java
Raw Normal View History

2012-02-03 10:20:48 +01:00
package net.citizensnpcs.trait;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.citizensnpcs.Settings.Setting;
2012-05-31 08:31:31 +02:00
import net.citizensnpcs.api.abstraction.WorldVector;
import net.citizensnpcs.api.abstraction.entity.Entity;
2012-06-23 12:01:28 +02:00
import net.citizensnpcs.api.abstraction.entity.LandMob;
2012-05-31 08:31:31 +02:00
import net.citizensnpcs.api.abstraction.entity.Player;
import net.citizensnpcs.api.attachment.Attachment;
import net.citizensnpcs.api.exception.NPCLoadException;
2012-02-07 10:45:43 +01:00
import net.citizensnpcs.api.npc.NPC;
2012-02-12 09:00:19 +01:00
import net.citizensnpcs.api.util.DataKey;
2012-02-03 10:20:48 +01:00
2012-05-31 08:31:31 +02:00
public class LookClose extends Attachment implements Runnable, Toggleable {
private boolean enabled = Setting.DEFAULT_LOOK_CLOSE.asBoolean();
private Player lookingAt;
2012-06-23 12:01:28 +02:00
private final LandMob entity;
2012-02-03 10:20:48 +01:00
2012-02-07 10:45:43 +01:00
public LookClose(NPC npc) {
2012-06-23 12:01:28 +02:00
this.entity = (LandMob) npc.getEntity();
2012-02-03 10:20:48 +01:00
}
private void faceEntity(Entity from, Entity at) {
if (from.getWorld() != at.getWorld())
return;
2012-05-31 08:31:31 +02:00
WorldVector loc = from.getLocation();
double xDiff = at.getLocation().getX() - loc.getX();
double yDiff = at.getLocation().getY() - loc.getY();
double zDiff = at.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);
}
2012-05-31 08:31:31 +02:00
from.setRotation((float) yaw - 90, (float) pitch);
}
2012-03-27 16:42:15 +02:00
@Override
public void load(DataKey key) throws NPCLoadException {
2012-04-26 11:01:12 +02:00
enabled = key.getBoolean("");
2012-03-27 16:42:15 +02:00
}
@Override
public void run() {
2012-06-23 12:01:28 +02:00
if (!enabled || entity.hasDestination())
return;
2012-06-23 12:01:28 +02:00
if (hasInvalidTarget())
findNewTarget();
2012-06-23 12:01:28 +02:00
if (lookingAt != null)
faceEntity(entity, lookingAt);
}
private void findNewTarget() {
2012-06-23 12:01:28 +02:00
List<Entity> nearby = entity.getNearbyEntities(2.5, 5, 2.5);
Collections.sort(nearby, new Comparator<Entity>() {
@Override
public int compare(Entity o1, Entity o2) {
2012-06-23 12:01:28 +02:00
double d1 = o1.getLocation().distanceSquared(entity.getLocation());
double d2 = o2.getLocation().distanceSquared(entity.getLocation());
return Double.compare(d1, d2);
}
});
for (Entity entity : nearby) {
if (entity instanceof Player) {
lookingAt = (Player) entity;
return;
}
}
lookingAt = null;
}
private boolean hasInvalidTarget() {
if (lookingAt == null)
return true;
2012-06-23 12:01:28 +02:00
if (!lookingAt.isOnline() || lookingAt.getWorld() != entity.getWorld()
|| lookingAt.getLocation().distanceSquared(entity.getLocation()) > 5) {
lookingAt = null;
return true;
}
return false;
2012-03-27 16:42:15 +02:00
}
@Override
public void save(DataKey key) {
key.setBoolean("", enabled);
2012-03-27 16:42:15 +02:00
}
@Override
public boolean toggle() {
2012-05-31 08:31:31 +02:00
return (enabled = !enabled);
2012-03-27 16:42:15 +02:00
}
2012-02-03 10:20:48 +01:00
@Override
public String toString() {
return "LookClose{" + enabled + "}";
2012-02-03 10:20:48 +01:00
}
}