diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 9a28bde1b..33d258b30 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -1147,7 +1147,7 @@ public class NPCCommands { @Command( aliases = { "npc" }, - usage = "lookclose --range [range] (-r[ealistic looking]) --(random|r)look [true|false] --(random|r)pitchrange [min,max] --(random|r)yawrange [min,max]", + usage = "lookclose --range [range] -r[ealistic looking] --(random|r)look [true|false] --(random|r)pitchrange [min,max] --(random|r)yawrange [min,max]", desc = "Toggle whether a NPC will look when a player is near", modifiers = { "lookclose", "look", "rotate" }, min = 1, diff --git a/main/src/main/java/net/citizensnpcs/trait/LookClose.java b/main/src/main/java/net/citizensnpcs/trait/LookClose.java index 09e2f266b..b8e4b04ba 100644 --- a/main/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/main/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -132,10 +132,6 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable return lookingAt; } - private boolean isEqual(float[] array) { - return Math.abs(array[0] - array[1]) < 0.001; - } - private boolean isInvisible(Player player) { return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY) || isPluginVanished(player) || !canSee(player); @@ -189,28 +185,37 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable : rand.doubles(randomPitchRange[0], randomPitchRange[1]).iterator().next().floatValue(); float yaw = isEqual(randomYawRange) ? randomYawRange[0] : rand.doubles(randomYawRange[0], randomYawRange[1]).iterator().next().floatValue(); - Util.assumePose(npc.getEntity(), yaw, pitch); + Util.face(npc.getEntity(), yaw, pitch); } @Override public void run() { - if (!enabled || !npc.isSpawned()) { + if (!npc.isSpawned()) return; + + if (enableRandomLook) { + if (!npc.getNavigator().isNavigating() && lookingAt == null && t <= 0) { + randomLook(); + t = randomLookDelay; + } else { + t--; + } } - if (npc.getNavigator().isNavigating() && disableWhileNavigating()) { + if (!enabled) return; - } + + if (npc.getNavigator().isNavigating() && disableWhileNavigating()) + return; + npc.getEntity().getLocation(NPC_LOCATION); if (tryInvalidateTarget()) { findNewTarget(); } + if (npc.getNavigator().isNavigating()) { npc.getNavigator().setPaused(lookingAt != null); - } else if (lookingAt == null && enableRandomLook && t <= 0) { - randomLook(); - t = randomLookDelay; } - t--; + if (lookingAt == null) return; Util.faceEntity(npc.getEntity(), lookingAt); @@ -298,6 +303,10 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable return realisticLooking; } + private static boolean isEqual(float[] array) { + return Math.abs(array[0] - array[1]) < 0.001; + } + private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0); private static final Location NPC_LOCATION = new Location(null, 0, 0, 0); private static final Location PLAYER_LOCATION = new Location(null, 0, 0, 0); diff --git a/main/src/main/java/net/citizensnpcs/util/Util.java b/main/src/main/java/net/citizensnpcs/util/Util.java index 7f655e5fb..3c9a43f6e 100644 --- a/main/src/main/java/net/citizensnpcs/util/Util.java +++ b/main/src/main/java/net/citizensnpcs/util/Util.java @@ -76,6 +76,12 @@ public class Util { return yaw; } + public static void face(Entity entity, float yaw, float pitch) { + Vector vector = new Vector(Math.cos(yaw) * Math.cos(pitch), Math.sin(pitch), Math.sin(yaw) * Math.cos(pitch)) + .normalize(); + faceLocation(entity, entity.getLocation(AT_LOCATION).clone().add(vector)); + } + public static void faceEntity(Entity entity, Entity at) { if (at == null || entity == null || entity.getWorld() != at.getWorld()) return;