Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.github.pylonmc.pylon.core.entity

import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent
import com.github.shynixn.mccoroutine.bukkit.asyncDispatcher
import com.github.shynixn.mccoroutine.bukkit.launch
import com.github.shynixn.mccoroutine.bukkit.minecraftDispatcher
import com.github.shynixn.mccoroutine.bukkit.ticks
import io.github.pylonmc.pylon.core.PylonCore
import io.github.pylonmc.pylon.core.addon.PylonAddon
import io.github.pylonmc.pylon.core.block.BlockListener.logEventHandleErr
import io.github.pylonmc.pylon.core.block.BlockStorage
import io.github.pylonmc.pylon.core.config.PylonConfig
import io.github.pylonmc.pylon.core.entity.base.PylonTickableEntity
import io.github.pylonmc.pylon.core.event.PylonEntityDeathEvent
import io.github.pylonmc.pylon.core.event.PylonEntityLoadEvent
import io.github.pylonmc.pylon.core.event.PylonEntityUnloadEvent
Expand All @@ -24,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.function.Consumer
import kotlin.random.Random
import kotlin.time.Duration.Companion.milliseconds

/**
* Basically [BlockStorage], but for entities. Works on all the same principles.
Expand All @@ -37,6 +42,7 @@ object EntityStorage : Listener {
private val entitiesByKey: MutableMap<NamespacedKey, MutableSet<PylonEntity<*>>> = ConcurrentHashMap()
private val entityAutosaveTasks: MutableMap<UUID, Job> = ConcurrentHashMap()
private val whenEntityLoadsTasks: MutableMap<UUID, MutableSet<Consumer<PylonEntity<*>>>> = ConcurrentHashMap()
private val tickMap: MutableMap<UUID, Job> = ConcurrentHashMap()

/**
* All the loaded [PylonEntity]s
Expand Down Expand Up @@ -215,6 +221,28 @@ object EntityStorage : Listener {
whenEntityLoadsTasks.remove(pylonEntity.uuid)
}

if (pylonEntity is PylonTickableEntity) {
val dispatcher =
if (pylonEntity.isAsync) PylonCore.asyncDispatcher else PylonCore.minecraftDispatcher
tickMap[pylonEntity.uuid] = PylonCore.launch(dispatcher) {

var lastTickNanos = System.nanoTime()
while (true) {
delay(pylonEntity.tickInterval.ticks)
try {
val dt = (System.nanoTime() - lastTickNanos) / 1.0e9
lastTickNanos = System.nanoTime()
pylonEntity.tick(dt)
} catch (e: Exception) {
PylonCore.launch(PylonCore.minecraftDispatcher) {
PylonCore.logger.severe("Error when handling entity(${pylonEntity.key}, ${pylonEntity.entity.location}) error: ${e.localizedMessage}")
e.printStackTrace()
}
}
}
}
}

PylonEntityLoadEvent(pylonEntity).callEvent()
}
}
Expand All @@ -224,6 +252,7 @@ object EntityStorage : Listener {
@EventHandler
private fun onEntityUnload(event: EntityRemoveFromWorldEvent) {
val pylonEntity = get(event.entity.uniqueId) ?: return
tickMap[pylonEntity.uuid]?.cancel()

if (!event.entity.isDead) {
PylonEntity.serialize(pylonEntity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.pylonmc.pylon.core.entity.base

interface PylonTickableEntity {
/**
* The interval at which the [tick] function is called. You should generally use [setTickInterval]
* in your place constructor instead of overriding this.
*/
val tickInterval: Int

/**
* Whether the [tick] function should be called asynchronously. You should generally use
* [setAsync] in your place constructor instead of overriding this.
*/
val isAsync: Boolean

/**
* Sets how often the [tick] function should be called (in Minecraft ticks)
*/
fun setTickInterval(tickInterval: Int)

/**
* Sets whether the [tick] function should be called asynchronously.
*
* WARNING: Settings an entity to tick asynchronously could have unintended consequences.
*
* Only set this option if you understand what 'asynchronous' means, and note that you
* cannot interact with the world asynchronously.
*/
fun setAsync(isAsync: Boolean)

/**
* The function that should be called periodically.
*/
fun tick(deltaSeconds: Double)
}