diff --git a/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java b/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java index 6289aab..e2e6701 100644 --- a/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java +++ b/src/main/java/net/knarcraft/blacksmithvisuals/listener/BlacksmithListener.java @@ -172,10 +172,7 @@ public class BlacksmithListener implements Listener { BlacksmithVisuals.debug("Restored LookCLose range"); } - BlacksmithVisuals.debug("Scheduling movement back to idle location"); - Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> - entity.teleport(targetLocation), getWalkTime(entity.getLocation(), targetLocation)); - npc.getNavigator().setTarget(targetLocation); + moveNPC(npc, entity, targetLocation); } /** @@ -212,19 +209,30 @@ public class BlacksmithListener implements Listener { npc.getName() + " is unreachable!"); return 0; } - Location finalTargetLocation = targetLocation; + return moveNPC(npc, entity, targetLocation); + } + + /** + * Moves an NPC to a different location + * + * @param npc
The NPC to be moved
+ * @param entityThe NPC's entity
+ * @param finalTargetLocationThe location the NPC should be moved to
+ * @returnThe assumed time it will take the NPC to get to the position
+ */ + private long moveNPC(@NotNull NPC npc, @NotNull Entity entity, @NotNull Location finalTargetLocation) { // Move NPC using Citizens path-finding - Location current = entity.getLocation().clone(); BlacksmithVisuals.debug("Preparing rotation and walk to working location"); - npc.teleport(current.clone().setDirection(targetLocation.toVector().subtract(current.clone().toVector())), PlayerTeleportEvent.TeleportCause.PLUGIN); + Location current = entity.getLocation().clone(); + npc.teleport(current.setDirection(finalTargetLocation.toVector().subtract(current.toVector())), PlayerTeleportEvent.TeleportCause.PLUGIN); Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> npc.getNavigator().setTarget(finalTargetLocation), 2); // Teleport the NPC tp get it in the exact final location - long walkTime = getWalkTime(entity.getLocation(), targetLocation); + long walkTime = getWalkTime(entity.getLocation(), finalTargetLocation); BlacksmithVisuals.debug("Queuing teleportation to working position"); Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> - entity.teleport(finalTargetLocation), walkTime); + entity.teleport(finalTargetLocation, PlayerTeleportEvent.TeleportCause.PLUGIN), walkTime); return walkTime; }