You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/wiki/docs/coroutine.md
+44-6Lines changed: 44 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ how this can be translated to the world of minecraft plugins. It is recommended
9
9
### Starting a coroutine
10
10
11
11
For beginners, it is often confusing how to enter a coroutine. The examples in the official guide mostly use ``runBlocking``
12
-
because it makes sense for testing. However, keep in mind to **never**use``runblocking`` in any of your plugins.
12
+
because it makes sense for testing. However, keep in mind to **avoid**using``runblocking`` in any of your plugins.
13
13
14
14
* To enter a coroutine **anywhere** in your code at any time:
15
15
@@ -29,9 +29,11 @@ A dispatcher determines what thread or threads the corresponding coroutine uses
29
29
* minecraftDispatcher (Allows to execute coroutines on the main minecraft thread)
30
30
* asyncDispatcher (Allows to execute coroutines on the async minecraft threadpool)
31
31
32
-
However, it is recommend to use ``Dispatchers.IO`` instead of asyncDispatcher because it is more optimized.
32
+
!!! note "Important"
33
+
**However, it is highly recommend to use ``Dispatchers.IO`` instead of asyncDispatcher because the scheduling is more accurate.**
34
+
Additional technical details can be found here: [GitHub Issue](https://github.com/Shynixn/MCCoroutine/issues/87).
33
35
34
-
*An example how this works is shown below:
36
+
An example how this works is shown below:
35
37
36
38
```kotlin
37
39
funfoo() {
@@ -45,7 +47,8 @@ fun foo() {
45
47
46
48
// Here we are automatically back on the main thread again.
47
49
48
-
val result2 = withContext(plugin.asyncDispatcher) {
50
+
// Prefer using Dispatchers.IO instead of asyncDispatcher
51
+
val result2 = withContext(Dispatchers.IO) {
49
52
// Perform operations asynchronously.
50
53
" Max"
51
54
}
@@ -60,7 +63,6 @@ fun foo() {
60
63
Normally, you do not need to call ``plugin.minecraftDispatcher`` in your code. Instead, you are guaranteed to be always on the minecraft main thread
61
64
in the ``plugin.launch{}`` scope and use sub coroutines (e.g. withContext) to perform asynchronous operations. Such a case can be found below:
62
65
63
-
64
66
```kotlin
65
67
@EventHandler
66
68
funonPlayerJoinEvent(event:PlayerJoinEvent) {
@@ -69,7 +71,7 @@ fun onPlayerJoinEvent(event: PlayerJoinEvent) {
69
71
val name = event.player.name
70
72
val listOfFriends = withContext(Dispatchers.IO) {
71
73
// IO Thread
72
-
val friendNames =Files.readAllLines(Paths.get("$name.json"))
74
+
val friendNames =Files.readAllLines(Paths.get("$name.txt"))
73
75
friendNames
74
76
}
75
77
@@ -81,6 +83,42 @@ fun onPlayerJoinEvent(event: PlayerJoinEvent) {
81
83
82
84
```
83
85
86
+
### Plugin launch Execution order
87
+
88
+
If you use ``plugin.launch``, it is important to understand the execution order.
89
+
90
+
````kotlin
91
+
classFoo(privatevalplugin:Plugin) {
92
+
93
+
funbar() {
94
+
// Main Thread
95
+
println("I am first")
96
+
97
+
val job = plugin.launch {
98
+
println("I am second") // The context is not suspended when switching to the same suspendable context.
99
+
delay(1000)
100
+
println("I am fourth") // The context is given back after 1000 milliseconds and continuous here.
101
+
bob()
102
+
}
103
+
104
+
// When calling delay the suspendable context is suspended and the original context immediately continuous here.
105
+
println("I am third")
106
+
}
107
+
108
+
privatesuspendfunbob(){
109
+
println("I am fifth")
110
+
}
111
+
}
112
+
````
113
+
114
+
````kotlin
115
+
"I am first"
116
+
"I am second"
117
+
"I am third"
118
+
"I am fourth"
119
+
"I am fifth"
120
+
````
121
+
84
122
### Coroutines everywhere
85
123
86
124
Using ``plugin.launch{}``is valuable if you migrate existing plugins to use coroutines. However, if you write a new plugin from scratch, you may consider using
0 commit comments