Skip to content

Commit 7e41f0e

Browse files
committed
Fix closeInventory being incorrectly called
1 parent c67cbf0 commit 7e41f0e

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,16 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
157157
view.savePersistentItems(event.inventory)
158158

159159
SCOPE.launch {
160+
// Determine if we can re-open a previous interface
161+
val openInterface = getOpenInterface(event.player.uniqueId)
162+
val shouldReopen = reason in REOPEN_REASONS && !event.player.isDead && openInterface != null
163+
160164
// Mark the current view as closed properly
161-
view.markClosed(reason)
165+
view.markClosed(reason, !shouldReopen && reason != Reason.OPEN_NEW)
162166

163-
// Try to open back up a previous interface
164-
if (reason in REOPEN_REASONS && !event.player.isDead) {
165-
getOpenInterface(event.player.uniqueId)?.open()
167+
// If possible, open back up a previous interface
168+
if (shouldReopen) {
169+
requireNotNull(openInterface).open()
166170
}
167171
}
168172
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
8282
public abstract fun openInventory()
8383

8484
/** Marks this menu as closed and processes it. */
85-
internal suspend fun markClosed(reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN) {
85+
internal suspend fun markClosed(
86+
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.UNKNOWN,
87+
closeInventory: Boolean = reason != InventoryCloseEvent.Reason.OPEN_NEW
88+
) {
8689
// End a possible chat query with the listener
8790
InterfacesListeners.INSTANCE.abortQuery(player.uniqueId, this)
8891

@@ -101,7 +104,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
101104
// properly.
102105
for ((child) in children) {
103106
if (child.shouldBeOpened.get()) {
104-
child.close()
107+
child.close(reason, closeInventory)
105108
}
106109
}
107110
}
@@ -153,10 +156,10 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
153156
}
154157
}
155158

156-
override suspend fun close() {
159+
override suspend fun close(reason: InventoryCloseEvent.Reason, closeInventory: Boolean) {
157160
markClosed()
158161

159-
if (isOpen()) {
162+
if (isOpen() && closeInventory) {
160163
// Ensure we always close on the main thread!
161164
runSync {
162165
player.closeInventory()

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.noxcrew.interfaces.view
22

33
import net.kyori.adventure.text.Component
44
import org.bukkit.entity.Player
5+
import org.bukkit.event.inventory.InventoryCloseEvent
56
import kotlin.time.Duration
67
import kotlin.time.Duration.Companion.seconds
78

@@ -27,7 +28,10 @@ public interface InterfaceView {
2728
public suspend fun open()
2829

2930
/** Closes this view. */
30-
public suspend fun close()
31+
public suspend fun close(
32+
reason: InventoryCloseEvent.Reason = InventoryCloseEvent.Reason.PLUGIN,
33+
closeInventory: Boolean = reason != InventoryCloseEvent.Reason.OPEN_NEW
34+
)
3135

3236
/** Returns whether this view is opened based on the player's current shown inventory. */
3337
public fun isOpen(): Boolean

0 commit comments

Comments
 (0)