Skip to content

Commit 1f9bc6c

Browse files
committed
fix: npc movement slaps
1 parent bc5f6ea commit 1f9bc6c

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/main/kotlin/cc/modlabs/kpaper/npc/NPCImpl.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class NPCImpl(
6565
private var spawnLocation: Location? = null // Spawn location to return to
6666
private var nearbyFollowTask: BukkitTask? = null
6767
private var nearbyFollowCheckInterval = 10L // Ticks between checking for nearby players (0.5 seconds - more responsive)
68+
private var consecutiveInvalidChecks = 0 // Track consecutive invalid entity checks
6869

6970
// Visibility state
7071
// null = visible to all players, non-null = only visible to players in the set
@@ -609,6 +610,7 @@ class NPCImpl(
609610
isFollowingNearbyPlayers = true
610611
nearbyFollowRange = range.coerceAtLeast(1.0)
611612
nearbyFollowDistance = followDistance.coerceAtLeast(1.0)
613+
consecutiveInvalidChecks = 0 // Reset counter when starting
612614
logDebug("[NPC] followNearbyPlayers: State set - range=$nearbyFollowRange, followDistance=$nearbyFollowDistance")
613615

614616
// Immediately check for nearby players before starting the timer
@@ -658,10 +660,21 @@ class NPCImpl(
658660

659661
val currentEntity = getMannequin() as? LivingEntity
660662
if (currentEntity == null || !currentEntity.isValid) {
661-
logDebug("[NPC] NearbyFollow task: Entity invalid, but continuing to check")
662-
// Don't stop the task, just skip this tick
663+
consecutiveInvalidChecks++
664+
// Stop task after 10 consecutive invalid checks (5 seconds at 0.5s intervals)
665+
if (consecutiveInvalidChecks >= 10) {
666+
logDebug("[NPC] NearbyFollow task: Entity invalid for ${consecutiveInvalidChecks} consecutive checks, stopping task")
667+
nearbyFollowTask?.cancel()
668+
nearbyFollowTask = null
669+
isFollowingNearbyPlayers = false
670+
return@timer
671+
}
672+
logDebug("[NPC] NearbyFollow task: Entity invalid (${consecutiveInvalidChecks}/10), skipping this tick")
663673
return@timer
664674
}
675+
676+
// Reset counter if entity is valid
677+
consecutiveInvalidChecks = 0
665678

666679
val npcLocation = currentEntity.location
667680
val npcWorld = npcLocation.world
@@ -821,6 +834,7 @@ class NPCImpl(
821834
isFollowingNearbyPlayers = false
822835
nearbyFollowTask?.cancel()
823836
nearbyFollowTask = null
837+
consecutiveInvalidChecks = 0 // Reset counter
824838

825839
// Stop following current entity if it was from nearby following
826840
if (isFollowing && followingEntity is Player) {

0 commit comments

Comments
 (0)