Skip to content

Commit

Permalink
#116 Added isFolia loaded and ensurePluginMain thread function.
Browse files Browse the repository at this point in the history
  • Loading branch information
shynixn committed Jun 19, 2024
1 parent d613033 commit b6b1fbb
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tasks.register("printVersion") {

subprojects {
group 'com.github.shynixn.mccoroutine'
version '2.16.0'
version '2.17.0'

sourceCompatibility = 1.8

Expand Down
36 changes: 18 additions & 18 deletions docs/wiki/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,62 @@ 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")
}
```

=== "BungeeCord"

```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")
}
```

=== "Fabric"

```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")
}
```

=== "Folia"

```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")
}
```

=== "Minestom"

```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")
}
```

=== "Sponge"

```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")
}
```

=== "Velocity"

```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")
}
```

Expand All @@ -87,17 +87,17 @@ 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"

**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
```


Expand Down
2 changes: 1 addition & 1 deletion docs/wiki/docs/unittests.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

Expand Down
2 changes: 1 addition & 1 deletion mccoroutine-bukkit-sample/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions mccoroutine-fabric-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion mccoroutine-folia-sample/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"entrypoint": "com.github.shynixn.mccoroutine.minestom.sample.extension.MCCoroutineSampleExtension",
"name": "MCCoroutineSampleExtension",
"version": "2.16.0"
"version": "2.17.0"
}
2 changes: 1 addition & 1 deletion mccoroutine-sponge-sample/src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit b6b1fbb

Please sign in to comment.