Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: 优化 Paper Dispatchers,添加注释
Browse files Browse the repository at this point in the history
nostalfinals committed Jan 10, 2025
1 parent 61c1b7b commit 09eb5e3
Showing 5 changed files with 95 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package plutoproject.framework.paper.util.coroutine

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import org.bukkit.Chunk
import org.bukkit.Location
import org.bukkit.Server
import org.bukkit.entity.Entity
import plutoproject.framework.paper.util.coroutine.dispatchers.ChunkDispatcher
import plutoproject.framework.paper.util.coroutine.dispatchers.EntityDispatcher
import plutoproject.framework.paper.util.coroutine.dispatchers.GlobalRegionDispatcher
import plutoproject.framework.paper.util.isFolia
import plutoproject.framework.paper.util.server
import plutoproject.framework.paper.util.toNms
import kotlin.coroutines.CoroutineContext

/**
* 获取服务器的 [CoroutineContext]。
* 在 Paper 上为基于服务器 EventLoop 的 [CoroutineDispatcher],在 Folia 上为 [GlobalRegionDispatcher]。
*/
val Server.coroutineContext: CoroutineContext
get() = if (isFolia) GlobalRegionDispatcher else toNms().asCoroutineDispatcher()

/**
* 获取实体的 [CoroutineContext]。
* 在 Paper 上为基于服务器 EventLoop 的 [CoroutineDispatcher],在 Folia 上为 [EntityDispatcher]。
*/
val Entity.coroutineContext: CoroutineContext
get() = if (isFolia) EntityDispatcher(this) else server.coroutineContext

/**
* 获取区块的 [CoroutineContext]。
* 在 Paper 上为基于服务器 EventLoop 的 [CoroutineDispatcher],在 Folia 上为 [ChunkDispatcher]。
*/
val Chunk.coroutineContext: CoroutineContext
get() = if (isFolia) ChunkDispatcher(this) else server.coroutineContext

/**
* 获取该位置区块的 [CoroutineContext]。
* 在 Paper 上为基于服务器 EventLoop 的 [CoroutineDispatcher],在 Folia 上为 [ChunkDispatcher]。
*/
val Location.coroutineContext
get() = chunk.coroutineContext

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package plutoproject.framework.paper.util.coroutine.dispatchers

import io.papermc.paper.threadedregions.scheduler.RegionScheduler
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Runnable
import org.bukkit.Chunk
import plutoproject.framework.paper.util.plugin
import plutoproject.framework.paper.util.server
import kotlin.coroutines.CoroutineContext

/**
* 基于 Folia [RegionScheduler] 的 [CoroutineDispatcher]。
*/
class ChunkDispatcher(private val chunk: Chunk) : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
server.regionScheduler.execute(plugin, chunk.world, chunk.x, chunk.z, block)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package plutoproject.framework.paper.util.coroutine.dispatchers

import io.papermc.paper.threadedregions.scheduler.EntityScheduler
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Runnable
import org.bukkit.entity.Entity
import plutoproject.framework.paper.util.plugin
import kotlin.coroutines.CoroutineContext

/**
* 基于 Folia [EntityScheduler] 的 [CoroutineDispatcher]。
*/
class EntityDispatcher(private val entity: Entity) : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
entity.scheduler.execute(plugin, block, {}, 0L)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package plutoproject.framework.paper.util.coroutine.dispatchers

import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler
import kotlinx.coroutines.CoroutineDispatcher
import plutoproject.framework.paper.util.plugin
import plutoproject.framework.paper.util.server
import kotlin.coroutines.CoroutineContext

/**
* 基于 Folia [GlobalRegionScheduler] 的 [CoroutineDispatcher]。
*/
object GlobalRegionDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
server.globalRegionScheduler.execute(plugin, block)
}
}

0 comments on commit 09eb5e3

Please sign in to comment.