Skip to content

Commit 89ebcd3

Browse files
committed
Make InterfaceView#close non-suspending so it can be safely used in non-suspending logic
1 parent 66d467e commit 89ebcd3

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val javaVersion: Int = 21
1515

1616
allprojects {
1717
group = "com.noxcrew.interfaces"
18-
version = "1.3.1-SNAPSHOT"
18+
version = "1.3.2-SNAPSHOT"
1919

2020
tasks.withType<JavaCompile> {
2121
sourceCompatibility = javaVersion.toString()

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.noxcrew.interfaces.transform.AppliedTransform
1616
import com.noxcrew.interfaces.utilities.CollapsablePaneMap
1717
import com.noxcrew.interfaces.utilities.InterfacesCoroutineDetails
1818
import com.noxcrew.interfaces.utilities.forEachInGrid
19+
import kotlinx.coroutines.CoroutineScope
1920
import kotlinx.coroutines.Job
2021
import kotlinx.coroutines.launch
2122
import kotlinx.coroutines.sync.Mutex
@@ -103,7 +104,8 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
103104
* Marks this menu as closed and processes it. This does not actually perform
104105
* closing the menu, this method only handles the closing.
105106
*/
106-
internal suspend fun markClosed(
107+
internal fun markClosed(
108+
coroutineScope: CoroutineScope,
107109
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN,
108110
changingView: Boolean = reason == InventoryCloseEvent.Reason.OPEN_NEW
109111
) {
@@ -120,7 +122,11 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
120122
(!changingView || builder.callCloseHandlerOnViewSwitch) &&
121123
::currentInventory.isInitialized
122124
) {
123-
builder.closeHandlers[reason]?.invoke(reason, this)
125+
builder.closeHandlers[reason]?.also {
126+
coroutineScope.launch {
127+
it.invoke(reason, this@AbstractInterfaceView)
128+
}
129+
}
124130
}
125131

126132
// Don't close children when changing views!
@@ -132,7 +138,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
132138
// properly.
133139
for ((child) in children) {
134140
if (child.shouldBeOpened.get()) {
135-
child.close(reason, false)
141+
child.close(coroutineScope, reason, false)
136142
}
137143
}
138144
}
@@ -196,8 +202,8 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
196202
}
197203
}
198204

199-
override suspend fun close(reason: InventoryCloseEvent.Reason, changingView: Boolean) {
200-
markClosed(reason, changingView)
205+
override fun close(coroutineScope: CoroutineScope, reason: InventoryCloseEvent.Reason, changingView: Boolean) {
206+
markClosed(coroutineScope, reason, changingView)
201207

202208
// Ensure we always close on the main thread! Don't close if we are
203209
// changing views though.

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

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

3+
import com.noxcrew.interfaces.InterfacesConstants
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.currentCoroutineContext
36
import net.kyori.adventure.text.Component
47
import org.bukkit.entity.Player
58
import org.bukkit.event.inventory.InventoryCloseEvent
@@ -40,6 +43,13 @@ public interface InterfaceView {
4043
public suspend fun close(
4144
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN,
4245
changingView: Boolean = reason == InventoryCloseEvent.Reason.OPEN_NEW
46+
): Unit = close(CoroutineScope(currentCoroutineContext()), reason, changingView)
47+
48+
/** Closes this view immediately, running any closing handling on [coroutineScope]. */
49+
public fun close(
50+
coroutineScope: CoroutineScope,
51+
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN,
52+
changingView: Boolean = reason == InventoryCloseEvent.Reason.OPEN_NEW
4353
)
4454

4555
/** Returns whether this view is opened based on the player's current shown inventory. */

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.noxcrew.interfaces.InterfacesListeners
44
import com.noxcrew.interfaces.interfaces.PlayerInterface
55
import com.noxcrew.interfaces.inventory.PlayerInterfacesInventory
66
import com.noxcrew.interfaces.pane.PlayerPane
7+
import kotlinx.coroutines.CoroutineScope
78
import net.kyori.adventure.text.Component
89
import org.bukkit.entity.Player
910
import org.bukkit.event.inventory.InventoryCloseEvent
@@ -59,8 +60,8 @@ public class PlayerInterfaceView internal constructor(
5960
onOpen()
6061
}
6162

62-
override suspend fun close(reason: InventoryCloseEvent.Reason, changingView: Boolean) {
63-
markClosed(reason, changingView)
63+
override fun close(coroutineScope: CoroutineScope, reason: InventoryCloseEvent.Reason, changingView: Boolean) {
64+
markClosed(coroutineScope, reason, changingView)
6465

6566
// Ensure we update the interface state in the main thread!
6667
// Even if the menu is not currently on the screen.

0 commit comments

Comments
 (0)