diff --git a/build.gradle b/build.gradle index 304dd18..d27415b 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ tasks.register("printVersion") { subprojects { group 'com.github.shynixn.mccoroutine' - version '2.16.0' + version '2.17.0' sourceCompatibility = 1.8 diff --git a/docs/wiki/docs/installation.md b/docs/wiki/docs/installation.md index e55a2a1..465a789 100644 --- a/docs/wiki/docs/installation.md +++ b/docs/wiki/docs/installation.md @@ -8,8 +8,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.17.0") } ``` @@ -17,8 +17,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-bungeecord-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-bungeecord-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-bungeecord-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-bungeecord-core:2.17.0") } ``` @@ -26,8 +26,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-core:2.17.0") } ``` @@ -35,8 +35,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-folia-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-folia-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-folia-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-folia-core:2.17.0") } ``` @@ -44,8 +44,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-minestom-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-minestom-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-minestom-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-minestom-core:2.17.0") } ``` @@ -53,8 +53,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-sponge-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-sponge-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-sponge-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-sponge-core:2.17.0") } ``` @@ -62,8 +62,8 @@ In order to use the MCCoroutine Kotlin API, you need to include the following li ```groovy dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-velocity-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-velocity-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-velocity-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-velocity-core:2.17.0") } ``` @@ -87,8 +87,8 @@ dependencies { **plugin.yml** ```yaml libraries: - - com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.16.0 - - com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.16.0 + - com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.17.0 + - com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.17.0 ``` === "Folia" @@ -96,8 +96,8 @@ dependencies { **plugin.yml** ```yaml libraries: - - com.github.shynixn.mccoroutine:mccoroutine-folia-api:2.16.0 - - com.github.shynixn.mccoroutine:mccoroutine-folia-core:2.16.0 + - com.github.shynixn.mccoroutine:mccoroutine-folia-api:2.17.0 + - com.github.shynixn.mccoroutine:mccoroutine-folia-core:2.17.0 ``` diff --git a/docs/wiki/docs/unittests.md b/docs/wiki/docs/unittests.md index 7724998..0a74e18 100644 --- a/docs/wiki/docs/unittests.md +++ b/docs/wiki/docs/unittests.md @@ -18,7 +18,7 @@ feedback to the real environment. ```kotlin dependencies { - testImplementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-test:2.16.0") + testImplementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-test:2.17.0") } ``` diff --git a/mccoroutine-bukkit-sample/src/main/resources/plugin.yml b/mccoroutine-bukkit-sample/src/main/resources/plugin.yml index 87d1c34..3fba58a 100644 --- a/mccoroutine-bukkit-sample/src/main/resources/plugin.yml +++ b/mccoroutine-bukkit-sample/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: MCCoroutine-Sample -version: 2.16.0 +version: 2.17.0 author: Shynixn main: com.github.shynixn.mccoroutine.bukkit.sample.MCCoroutineSamplePlugin commands: diff --git a/mccoroutine-bungeecord-sample/src/main/resources/plugin.yml b/mccoroutine-bungeecord-sample/src/main/resources/plugin.yml index f399a84..fc83f09 100644 --- a/mccoroutine-bungeecord-sample/src/main/resources/plugin.yml +++ b/mccoroutine-bungeecord-sample/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: MCCoroutine-Sample -version: 2.16.0 +version: 2.17.0 author: Shynixn main: com.github.shynixn.mccoroutine.bungeecord.sample.MCCoroutineSamplePlugin commands: diff --git a/mccoroutine-fabric-sample/build.gradle.kts b/mccoroutine-fabric-sample/build.gradle.kts index 43a884b..c127f4f 100644 --- a/mccoroutine-fabric-sample/build.gradle.kts +++ b/mccoroutine-fabric-sample/build.gradle.kts @@ -9,8 +9,8 @@ repositories { mavenLocal() } dependencies { - implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-api:2.16.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-core:2.16.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-api:2.17.0") + implementation("com.github.shynixn.mccoroutine:mccoroutine-fabric-core:2.17.0") minecraft("com.mojang", "minecraft", project.extra["minecraft_version"] as String) mappings("net.fabricmc", "yarn", project.extra["yarn_mappings"] as String, null, "v2") diff --git a/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/CoroutineSession.kt b/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/CoroutineSession.kt index ece273a..a5f7147 100644 --- a/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/CoroutineSession.kt +++ b/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/CoroutineSession.kt @@ -31,7 +31,7 @@ interface CoroutineSession { /** * The main dispatcher represents the main thread of a plugin. */ - val dispatcherMain : CoroutineContext + val dispatcherMain: CoroutineContext /** * Manipulates the bukkit server heart beat on startup. @@ -43,6 +43,11 @@ interface CoroutineSession { */ val mcCoroutineConfiguration: MCCoroutineConfiguration + /** + * The thread id of the dispatcherMain. + */ + val dispatcherMainThreadId: Long + /** * Gets if the Folia schedulers where successfully loaded into MCCoroutine. * Returns false if MCCoroutine falls back to the BukkitScheduler. diff --git a/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/MCCoroutine.kt b/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/MCCoroutine.kt index 870f656..1a2ad22 100644 --- a/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/MCCoroutine.kt +++ b/mccoroutine-folia-api/src/main/java/com/github/shynixn/mccoroutine/folia/MCCoroutine.kt @@ -55,11 +55,29 @@ val Plugin.asyncDispatcher: CoroutineContext /** * Gets the plugin main dispatcher. The main dispatcher consists of a single thread dedicated for your plugin */ -val Plugin.mainDispatcher : CoroutineContext +val Plugin.mainDispatcher: CoroutineContext get() { return mcCoroutine.getCoroutineSession(this).dispatcherMain } +/** + * Validates if the current Thread is the Plugin Main Thread of mainDispatcher. + * Throws an Exception if not. + */ +fun Plugin.ensurePluginThread() { + val threadId = mcCoroutine.getCoroutineSession(this).dispatcherMainThreadId + if (Thread.currentThread().id != threadId) { + throw IllegalStateException("The current thread is not the main thread of plugin ${name}.") + } +} + +/** + * Gets if the current server uses Folia instead of Bukkit. + */ +fun Plugin.isFoliaLoaded(): Boolean { + return mcCoroutine.getCoroutineSession(this).isFoliaLoaded +} + /** * Gets the dispatcher to schedule tasks on the region that owns the entity. * If Folia is not loaded, this falls back to the bukkit minecraftDispatcher. diff --git a/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/dispatcher/MainDispatcher.kt b/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/dispatcher/MainDispatcher.kt index 20e0ac9..0ce307c 100644 --- a/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/dispatcher/MainDispatcher.kt +++ b/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/dispatcher/MainDispatcher.kt @@ -11,7 +11,7 @@ class MainDispatcher( private val executor = Executors.newFixedThreadPool(1) { r -> Thread(r, "MCCoroutine-${plugin.name}-MainThread") } - private var threadId = -1L + var threadId = -1L init { executor.submit { diff --git a/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/impl/CoroutineSessionImpl.kt b/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/impl/CoroutineSessionImpl.kt index 64c9946..6bab87b 100644 --- a/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/impl/CoroutineSessionImpl.kt +++ b/mccoroutine-folia-core/src/main/java/com/github/shynixn/mccoroutine/folia/impl/CoroutineSessionImpl.kt @@ -125,13 +125,21 @@ internal class CoroutineSessionImpl( wakeUpBlockService.isManipulatedServerHeartBeatEnabled = value } + /** + * The thread id of the dispatcherMain. + */ + override val dispatcherMainThreadId: Long + get() { + return dispatcherMain.threadId + } + init { // Root Exception Handler. All Exception which are not consumed by the caller end up here. val exceptionHandler = CoroutineExceptionHandler { _, e -> val mcCoroutineExceptionEvent = MCCoroutineExceptionEvent(plugin, e) if (plugin.isEnabled) { - plugin.launch(plugin.globalRegionDispatcher, CoroutineStart.DEFAULT){ + plugin.launch(plugin.globalRegionDispatcher, CoroutineStart.DEFAULT) { plugin.server.pluginManager.callEvent(mcCoroutineExceptionEvent) if (!mcCoroutineExceptionEvent.isCancelled) { diff --git a/mccoroutine-folia-sample/src/main/resources/plugin.yml b/mccoroutine-folia-sample/src/main/resources/plugin.yml index 1372ec4..bdf2f75 100644 --- a/mccoroutine-folia-sample/src/main/resources/plugin.yml +++ b/mccoroutine-folia-sample/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: MCCoroutine-Sample -version: 2.16.0 +version: 2.17.0 author: Shynixn main: com.github.shynixn.mccoroutine.folia.sample.MCCoroutineSamplePlugin folia-supported: true diff --git a/mccoroutine-minestom-sample/src/main/resources/extension.json b/mccoroutine-minestom-sample/src/main/resources/extension.json index 8caf807..6156600 100644 --- a/mccoroutine-minestom-sample/src/main/resources/extension.json +++ b/mccoroutine-minestom-sample/src/main/resources/extension.json @@ -1,5 +1,5 @@ { "entrypoint": "com.github.shynixn.mccoroutine.minestom.sample.extension.MCCoroutineSampleExtension", "name": "MCCoroutineSampleExtension", - "version": "2.16.0" + "version": "2.17.0" } diff --git a/mccoroutine-sponge-sample/src/main/resources/mcmod.info b/mccoroutine-sponge-sample/src/main/resources/mcmod.info index 23bb903..f597b90 100644 --- a/mccoroutine-sponge-sample/src/main/resources/mcmod.info +++ b/mccoroutine-sponge-sample/src/main/resources/mcmod.info @@ -1,7 +1,7 @@ [{ "modid": "mccoroutinesample", "name": "MCCoroutineSample", - "version": "2.16.0", + "version": "2.17.0", "description": "MCCoroutineSample is sample plugin to use MCCoroutine in Sponge.", "authorList": [ "Shynixn"