-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
61c1b7b
commit 09eb5e3
Showing
5 changed files
with
95 additions
and
52 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...aper-api/src/main/kotlin/plutoproject/framework/paper/util/coroutine/CoroutineContexts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
52 changes: 0 additions & 52 deletions
52
...work-paper-api/src/main/kotlin/plutoproject/framework/paper/util/coroutine/Dispatchers.kt
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
...rc/main/kotlin/plutoproject/framework/paper/util/coroutine/dispatchers/ChunkDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...c/main/kotlin/plutoproject/framework/paper/util/coroutine/dispatchers/EntityDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../kotlin/plutoproject/framework/paper/util/coroutine/dispatchers/GlobalRegionDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|