-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shynixn
committed
May 4, 2024
1 parent
04e9ce1
commit 5d8f8df
Showing
10 changed files
with
159 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
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
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
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
49 changes: 49 additions & 0 deletions
49
...olia-core/src/main/java/com/github/shynixn/mccoroutine/folia/dispatcher/MainDispatcher.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,49 @@ | ||
package com.github.shynixn.mccoroutine.folia.dispatcher | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import org.bukkit.plugin.Plugin | ||
import java.util.concurrent.Executors | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class MainDispatcher( | ||
private val plugin: Plugin, | ||
) : CoroutineDispatcher(), AutoCloseable { | ||
private val executor = Executors.newFixedThreadPool(1) { r -> | ||
Thread(r, "MCCoroutine-${plugin.name}-MainThread") | ||
} | ||
private var threadId = -1L | ||
|
||
init { | ||
executor.submit { | ||
threadId = Thread.currentThread().id | ||
} | ||
} | ||
|
||
/** | ||
* Handles dispatching the coroutine on the correct thread. | ||
*/ | ||
override fun dispatch(context: CoroutineContext, block: Runnable) { | ||
if (!plugin.isEnabled) { | ||
return | ||
} | ||
|
||
if (Thread.currentThread().id != threadId) { | ||
executor.submit(block) | ||
} else { | ||
block.run() | ||
} | ||
} | ||
|
||
/** | ||
* Closes this resource, relinquishing any underlying resources. | ||
* This method is invoked automatically on objects managed by the | ||
* `try`-with-resources statement. | ||
* However, implementers of this interface are strongly encouraged | ||
* to make their `close` methods idempotent. | ||
* | ||
* @throws Exception if this resource cannot be closed | ||
*/ | ||
override fun close() { | ||
executor.shutdown() | ||
} | ||
} |
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
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
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