@@ -2,21 +2,12 @@ package cc.modlabs.kpaper.npc
22
33import cc.modlabs.kpaper.event.listen
44import cc.modlabs.kpaper.extensions.timer
5- import cc.modlabs.kpaper.util.getLogger
6- import cc.modlabs.kpaper.util.logDebug
7- import dev.fruxz.stacked.extension.asPlainString
8- import dev.fruxz.stacked.extension.asString
5+ import org.bukkit.Bukkit
96import org.bukkit.entity.Player
107import org.bukkit.event.entity.EntityDamageByEntityEvent
11- import org.bukkit.event.player.PlayerAnimationEvent
12- import org.bukkit.event.player.PlayerInteractAtEntityEvent
13- import org.bukkit.event.player.PlayerInteractEntityEvent
14- import org.bukkit.event.player.PlayerJoinEvent
15- import org.bukkit.event.player.PlayerQuitEvent
8+ import org.bukkit.event.player.*
169import org.bukkit.scheduler.BukkitTask
17- import org.bukkit.util.Vector
18- import org.bukkit.Bukkit
19- import java.util.UUID
10+ import java.util.*
2011import kotlin.math.atan2
2112import kotlin.math.sqrt
2213
@@ -69,10 +60,8 @@ object NPCEventListener {
6960 */
7061 fun register () {
7162 if (isRegistered) {
72- logDebug(" [NPCEventListener] register: Already registered, skipping" )
7363 return
7464 }
75- logDebug(" [NPCEventListener] register: Registering global event listener" )
7665 isRegistered = true
7766
7867 // Listen for player right-click on entities
@@ -222,17 +211,14 @@ object NPCEventListener {
222211 private fun startLookAtTask () {
223212 // Check if task exists and is still running
224213 if (lookAtTask != null && ! lookAtTask!! .isCancelled) {
225- logDebug(" [NPCEventListener] Look-at task already running, skipping start" )
226214 return
227215 }
228216
229217 // Cancel existing task if it's cancelled but still referenced
230218 if (lookAtTask != null && lookAtTask!! .isCancelled) {
231- logDebug(" [NPCEventListener] Look-at task was cancelled, cleaning up" )
232219 lookAtTask = null
233220 }
234221
235- logDebug(" [NPCEventListener] Starting look-at task (checks every 5 ticks for smooth looking)" )
236222 lookAtTask = timer(5 , " NPCLookAt" ) { // Run every 5 ticks for smooth looking
237223 val lookAtNPCs = proximityNPCs.entries.filter { it.value.isLookingAtPlayers() }
238224
@@ -283,58 +269,47 @@ object NPCEventListener {
283269 private fun startProximityMonitoring () {
284270 // Check if task exists and is still running
285271 if (proximityTask != null && ! proximityTask!! .isCancelled) {
286- logDebug(" [NPCEventListener] Proximity monitoring task already running, skipping start" )
287272 return
288273 }
289274
290275 // Cancel existing task if it's cancelled but still referenced
291276 if (proximityTask != null && proximityTask!! .isCancelled) {
292- logDebug(" [NPCEventListener] Proximity monitoring task was cancelled, cleaning up" )
293277 proximityTask = null
294278 }
295279
296- logDebug(" [NPCEventListener] Starting proximity monitoring task (checks every 10 ticks)" )
297280 proximityTask = timer(10 , " NPCProximity" ) { // Check every 10 ticks (reduced frequency to avoid performance issues)
298281 val currentTime = System .currentTimeMillis()
299282 val proximityNPCsCopy = proximityNPCs.entries.toList() // Copy to avoid concurrent modification
300283
301284 // Debug: Log if no NPCs are registered
302285 if (proximityNPCsCopy.isEmpty()) {
303- logDebug(" [NPCEventListener] Proximity task tick: No NPCs registered for proximity monitoring" )
304286 return @timer
305287 }
306288
307- logDebug(" [NPCEventListener] Proximity task tick: Checking ${proximityNPCsCopy.size} NPC(s)" )
308-
309289 proximityNPCsCopy.forEach { (npcId, npc) ->
310290 val entity = getEntityByUUID(npc, npcId)
311291 if (entity == null || ! entity.isValid) {
312- logDebug(" [NPCEventListener] Proximity task: NPC entity is null or invalid (ID: $npcId ), skipping" )
313292 return @forEach
314293 }
315294
316295 val npcLocation = entity.location
317296 val world = entity.world
318297 if (world == null ) {
319- logDebug(" [NPCEventListener] Proximity task: NPC world is null for entity ${entity.uniqueId} , skipping" )
320298 return @forEach
321299 }
322300
323301 val range = npc.getProximityRange()
324302 val npcName = entity.customName ? : entity.type.name
325- logDebug(" [NPCEventListener] Proximity task: Checking NPC '$npcName ' (${entity.uniqueId} ) at ${npcLocation.blockX} ,${npcLocation.blockY} ,${npcLocation.blockZ} , range=$range , lookAtPlayers=${npc.isLookingAtPlayers()} " )
326303
327304 // Get all nearby players
328305 val allNearbyEntities = world.getNearbyEntities(npcLocation, range, range, range)
329306 val nearbyPlayers = allNearbyEntities
330307 .filterIsInstance<Player >()
331308 .filter { it.isValid && ! it.isDead && it.location.distance(npcLocation) <= range }
332309
333- logDebug(" [NPCEventListener] Proximity task: Found ${nearbyPlayers.size} nearby player(s) for NPC '$npcName '" )
334310 if (nearbyPlayers.isNotEmpty()) {
335311 nearbyPlayers.forEach { player ->
336312 val distance = player.location.distance(npcLocation)
337- logDebug(" [NPCEventListener] Proximity task: Player ${player.name} is ${String .format(" %.2f" , distance)} blocks away from NPC '$npcName '" )
338313 }
339314 }
340315
@@ -351,7 +326,6 @@ object NPCEventListener {
351326
352327 if (isSneaking && ! wasSneaking) {
353328 // Player just started sneaking - trigger event
354- logDebug(" [NPCEventListener] Proximity task: Player ${player.name} started sneaking near NPC '$npcName '" )
355329 val sneakingEvent = NPCEvent (
356330 npc = npc,
357331 player = player,
@@ -371,7 +345,6 @@ object NPCEventListener {
371345 val lastPunchTime = playerPunchingState[player] ? : 0L
372346 if (currentTime - lastPunchTime < 500 ) {
373347 // Player is punching nearby - trigger event
374- logDebug(" [NPCEventListener] Proximity task: Player ${player.name} is punching near NPC '$npcName '" )
375348 val punchingEvent = NPCEvent (
376349 npc = npc,
377350 player = player,
@@ -416,13 +389,10 @@ object NPCEventListener {
416389
417390 val entityName = entity.customName ? : entity.type.name
418391
419- logDebug(" [NPCEventListener] makeEntityLookAt: Making '$entityName ' look at ${target.blockX} ,${target.blockY} ,${target.blockZ} (distance=${String .format(" %.2f" , distance)} , yaw=${String .format(" %.2f" , yaw)} , pitch=${String .format(" %.2f" , pitch)} )" )
420-
421392 // Ensure AI is enabled for mannequins to maintain look direction
422393 // In MC 1.21.10, mannequins need AI to look at entities properly
423394 if (! entity.hasAI()) {
424395 entity.setAI(true )
425- logDebug(" [NPCEventListener] makeEntityLookAt: Enabled AI for '$entityName ' to allow looking" )
426396 }
427397
428398 // Apply rotation using teleport (this works even without AI, but AI helps maintain it)
@@ -433,9 +403,7 @@ object NPCEventListener {
433403 try {
434404 // Use teleport with relative rotation to update look direction
435405 entity.teleport(newLocation)
436- logDebug(" [NPCEventListener] makeEntityLookAt: Successfully updated '$entityName ' look direction" )
437406 } catch (e: Exception ) {
438- logDebug(" [NPCEventListener] makeEntityLookAt: ERROR updating '$entityName ' look direction - ${e.message} " )
439407 e.printStackTrace()
440408 }
441409 }
@@ -474,12 +442,8 @@ object NPCEventListener {
474442 val entity = npc.getEntity()
475443 val npcName = entity?.customName ? : entity?.type?.name ? : " Unknown"
476444 val npcId = npc.getID() ? : return
477-
478- logDebug(" [NPCEventListener] registerProximityNPC: Registering NPC '$npcName ' ($npcId ) for proximity monitoring" )
479- logDebug(" [NPCEventListener] registerProximityNPC: Current proximityNPCs count: ${proximityNPCs.size} " )
480-
445+
481446 proximityNPCs[npcId] = npc
482- logDebug(" [NPCEventListener] registerProximityNPC: Added NPC, new count: ${proximityNPCs.size} " )
483447
484448 // Ensure the event listener is registered and proximity monitoring is started
485449 register()
@@ -489,28 +453,21 @@ object NPCEventListener {
489453 if (npc.isLookingAtPlayers()) {
490454 startLookAtTask()
491455 }
492-
493- logDebug(" [NPCEventListener] registerProximityNPC: NPC '$npcName ' registered. Proximity task running: ${proximityTask != null && ! proximityTask!! .isCancelled} , Look-at task running: ${lookAtTask != null && ! lookAtTask!! .isCancelled} " )
494456 }
495457
496458 /* *
497459 * Unregisters an NPC from proximity event monitoring.
498460 */
499461 fun unregisterProximityNPC (npc : NPC ) {
500- val entity = npc.getEntity()
501- val npcName = entity?.customName ? : entity?.type?.name ? : " Unknown"
502462 val npcId = npc.getID() ? : return
503- logDebug(" [NPCEventListener] unregisterProximityNPC: Unregistering NPC '$npcName ' from proximity monitoring" )
504463 proximityNPCs.remove(npcId)
505- logDebug(" [NPCEventListener] unregisterProximityNPC: Removed NPC, new count: ${proximityNPCs.size} " )
506464 }
507465
508466 /* *
509467 * Registers an NPC for visibility management.
510468 * Called automatically when an NPC has visibility restrictions.
511469 */
512470 fun registerVisibilityNPC (npc : NPC ) {
513- val entity = npc.getEntity()
514471 val npcId = npc.getID() ? : return
515472 visibilityNPCs[npcId] = npc
516473 }
@@ -519,7 +476,6 @@ object NPCEventListener {
519476 * Unregisters an NPC from visibility management.
520477 */
521478 fun unregisterVisibilityNPC (npc : NPC ) {
522- val entity = npc.getEntity()
523479 val npcId = npc.getID() ? : return
524480 visibilityNPCs.remove(npcId)
525481 }
0 commit comments