Skip to content

Commit c9f0806

Browse files
committed
Stop rendering an interface if the coroutine context is cancelled
1 parent 89ebcd3 commit c9f0806

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.noxcrew.interfaces.utilities.InterfacesCoroutineDetails
1818
import com.noxcrew.interfaces.utilities.forEachInGrid
1919
import kotlinx.coroutines.CoroutineScope
2020
import kotlinx.coroutines.Job
21+
import kotlinx.coroutines.isActive
2122
import kotlinx.coroutines.launch
2223
import kotlinx.coroutines.sync.Mutex
2324
import kotlinx.coroutines.withTimeout
@@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory
3233
import java.util.WeakHashMap
3334
import java.util.concurrent.ConcurrentLinkedQueue
3435
import java.util.concurrent.atomic.AtomicBoolean
36+
import kotlin.coroutines.coroutineContext
3537
import kotlin.time.Duration
3638
import kotlin.time.Duration.Companion.seconds
3739

@@ -180,7 +182,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
180182

181183
override suspend fun open() {
182184
// Don't open an interface for an offline player
183-
if (!player.isConnected) return
185+
if (!player.isConnected || !coroutineContext.isActive) return
184186

185187
// Indicate that the menu should be opened after the next time rendering completes
186188
// and that it should be open right now
@@ -430,6 +432,9 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
430432
false
431433
}
432434

435+
// Exit if the coroutine context is no longer active
436+
if (!coroutineContext.isActive) return
437+
433438
// Draw the contents of the inventory synchronously because
434439
// we don't want it to happen in between ticks and show
435440
// a half-finished inventory.

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/InterfaceView.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.noxcrew.interfaces.view
22

3-
import com.noxcrew.interfaces.InterfacesConstants
43
import kotlinx.coroutines.CoroutineScope
5-
import kotlinx.coroutines.currentCoroutineContext
64
import net.kyori.adventure.text.Component
75
import org.bukkit.entity.Player
86
import org.bukkit.event.inventory.InventoryCloseEvent
7+
import kotlin.coroutines.coroutineContext
98
import kotlin.time.Duration
109
import kotlin.time.Duration.Companion.seconds
1110

@@ -43,7 +42,7 @@ public interface InterfaceView {
4342
public suspend fun close(
4443
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN,
4544
changingView: Boolean = reason == InventoryCloseEvent.Reason.OPEN_NEW
46-
): Unit = close(CoroutineScope(currentCoroutineContext()), reason, changingView)
45+
): Unit = close(CoroutineScope(coroutineContext), reason, changingView)
4746

4847
/** Closes this view immediately, running any closing handling on [coroutineScope]. */
4948
public fun close(

0 commit comments

Comments
 (0)