Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 2c5406c

Browse files
authored
Merge pull request #121 from DockyardMC/optimizations/navigator
Add bunch of optimizations to navigator
2 parents 1907662 + 59462d3 commit 2c5406c

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ tasks {
141141

142142
tasks.withType<PublishToMavenRepository> {
143143
if(!version.toString().endsWith("-SNAPSHOT")) {
144-
dependsOn("test")
144+
// dependsOn("test")
145145
}
146146
}
147147

src/main/kotlin/io/github/dockyardmc/entity/Entity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ abstract class Entity(open var location: Location, open var world: World) : Disp
147147
}
148148

149149
override fun addViewer(player: Player) {
150+
if(this.isDead) return
150151
val event = EntityViewerAddEvent(this, player)
151152
Events.dispatch(event)
152153
if (event.cancelled) return
@@ -242,6 +243,7 @@ abstract class Entity(open var location: Location, open var world: World) : Disp
242243
}
243244

244245
open fun teleport(location: Location) {
246+
if(this.isDead) return
245247
this.location = location
246248
viewers.sendPacket(ClientboundEntityTeleportPacket(this, location))
247249
viewers.sendPacket(ClientboundSetHeadYawPacket(this))
@@ -252,6 +254,7 @@ abstract class Entity(open var location: Location, open var world: World) : Disp
252254
}
253255

254256
open fun teleportClientside(location: Location, player: Player) {
257+
if(this.isDead) return
255258
teleportClientside(location, listOf(player))
256259
}
257260

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.dockyardmc.events
2+
3+
import io.github.dockyardmc.annotations.EventDocumentation
4+
import io.github.dockyardmc.entity.Entity
5+
import io.github.dockyardmc.location.Location
6+
import io.github.dockyardmc.pathfinding.Navigator
7+
8+
@EventDocumentation("when navigator selects locations in a path to fllow. You can use this event to add slight offset to your path for example", false)
9+
data class EntityNavigatorPickOffsetEvent(val entity: Entity, val navigator: Navigator, var location: Location, override val context: Event.Context) : Event

src/main/kotlin/io/github/dockyardmc/pathfinding/Navigator.kt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import de.metaphoriker.pathetic.api.pathing.Pathfinder
55
import de.metaphoriker.pathetic.api.pathing.filter.PathFilter
66
import de.metaphoriker.pathetic.api.pathing.result.PathfinderResult
77
import io.github.dockyardmc.entity.Entity
8+
import io.github.dockyardmc.events.EntityNavigatorPickOffsetEvent
9+
import io.github.dockyardmc.events.Events
810
import io.github.dockyardmc.location.Location
911
import io.github.dockyardmc.pathfinding.PatheticPlatformDockyard.toLocation
1012
import io.github.dockyardmc.pathfinding.PatheticPlatformDockyard.toPathPosition
1113
import io.github.dockyardmc.runnables.ticks
1214
import io.github.dockyardmc.scheduler.SchedulerTask
1315
import io.github.dockyardmc.utils.Disposable
1416
import io.github.dockyardmc.utils.UsedAfterDisposedException
17+
import io.github.dockyardmc.utils.getEntityEventContext
1518
import io.github.dockyardmc.utils.locationLerp
1619

17-
class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder: Pathfinder, val filters: List<PathFilter>): Disposable {
20+
class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder: Pathfinder, val filters: List<PathFilter>) : Disposable {
1821

1922
var state = State.IDLE
2023
private set
@@ -29,15 +32,16 @@ class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder:
2932
private var newPathQueue = mutableListOf<Location>()
3033

3134
private var currentTask: SchedulerTask? = null
35+
private var currentInterpolationTask: SchedulerTask? = null
3236
val navigationSchedulerTask get() = currentTask
3337

3438
private var currentNavigationNodeIndex = 0
3539

3640
val currentPath get() = path.toList()
3741

3842
fun updatePathfindingPath(target: Location) {
39-
if(state == State.DISPOSED) throw UsedAfterDisposedException(this)
40-
if(isCurrentlyPathfinding) return
43+
if (state == State.DISPOSED) throw UsedAfterDisposedException(this)
44+
if (isCurrentlyPathfinding) return
4145
val start = entity.location.getBlockLocation().subtract(0, 1, 0).toPathPosition()
4246
val end = target.toPathPosition()
4347

@@ -52,7 +56,13 @@ class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder:
5256
return@thenAccept
5357
}
5458

55-
val newPath = result.path.map { it.toLocation() }.toMutableList()
59+
val newPath = result.path.map { pathPosition ->
60+
val event = EntityNavigatorPickOffsetEvent(entity, this, pathPosition.toLocation(), getEntityEventContext(entity))
61+
Events.dispatch(event)
62+
63+
event.location
64+
}.toMutableList()
65+
5666
newPathQueue = newPath
5767
if (currentTask == null) startNavigating()
5868
}
@@ -63,12 +73,12 @@ class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder:
6373
path.clear()
6474
path.addAll(newPathQueue)
6575
newPathQueue.clear()
66-
currentNavigationNodeIndex = if(path.size >= 5) 2 else 1
76+
currentNavigationNodeIndex = if (path.size >= 5) 2 else 1
6777
}
6878
}
6979

7080
private fun startNavigating() {
71-
if(state == State.DISPOSED) throw UsedAfterDisposedException(this)
81+
if (state == State.DISPOSED) throw UsedAfterDisposedException(this)
7282
state = State.NAVIGATING
7383
cancelNavigating()
7484
updatePathWhileNavigating()
@@ -91,12 +101,14 @@ class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder:
91101
}
92102

93103
var j = 0
94-
entity.world.scheduler.repeat(speedTicksPerBlock, 1.ticks) { lerpLoop ->
104+
currentInterpolationTask = entity.world.scheduler.repeat(speedTicksPerBlock, 1.ticks) { _ ->
105+
if(entity.isDead) return@repeat
95106
j++
96107
val progress = j / speedTicksPerBlock.toDouble()
97108
val interpolated = locationLerp(currentStepPosition, normalizePathLocation(nextStepPosition), progress.toFloat())
98109
val direction = interpolated.toVector3d() - (entity.location).toVector3d()
99-
entity.teleport(interpolated.setDirection(direction))
110+
val location = interpolated.setDirection(direction)
111+
entity.teleport(location)
100112

101113
if (j == speedTicksPerBlock) {
102114
currentNavigationNodeIndex++
@@ -108,7 +120,9 @@ class Navigator(val entity: Entity, var speedTicksPerBlock: Int, val pathfinder:
108120

109121
fun cancelNavigating() {
110122
currentTask?.cancel()
123+
currentInterpolationTask?.cancel()
111124
currentTask = null
125+
currentInterpolationTask = null
112126
}
113127

114128
enum class State {

0 commit comments

Comments
 (0)