Further refactor lookclose

This commit is contained in:
fullwall 2021-01-20 20:27:46 +08:00
parent fe3f12e424
commit eb9764aeb0

View File

@ -67,24 +67,21 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
* Finds a new look-close target * Finds a new look-close target
*/ */
public void findNewTarget() { public void findNewTarget() {
double min = Integer.MAX_VALUE; double min = range * range;
for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) { for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) {
if (!(entity instanceof Player)) if (!(entity instanceof Player))
continue; continue;
Player player = (Player) entity; Player player = (Player) entity;
Location location = player.getLocation(CACHE_LOCATION); Location location = player.getLocation(CACHE_LOCATION);
if (location.getWorld() != NPC_LOCATION.getWorld()) if (location.getWorld() != NPC_LOCATION.getWorld())
continue; continue;
double dist = location.distanceSquared(NPC_LOCATION); double dist = location.distanceSquared(NPC_LOCATION);
if (dist > range * range || CitizensAPI.getNPCRegistry().getNPC(entity) != null || isInvisible(player)) if (dist > min || CitizensAPI.getNPCRegistry().getNPC(entity) != null || isInvisible(player))
continue; continue;
if (dist < min) {
min = dist; min = dist;
lookingAt = player; lookingAt = player;
} }
} }
}
public int getRandomLookDelay() { public int getRandomLookDelay() {
return randomLookDelay; return randomLookDelay;