Skip to content

Commit d6916d9

Browse files
committed
Properly mark demoted player interfaces as re-openable
1 parent df599a8 commit d6916d9

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
219219
return true
220220
}
221221

222-
/** Moves any currently open player interface to the background view. */
223-
public fun demoteOpenView(playerId: UUID) {
224-
val view = openPlayerInterfaceViews.remove(playerId) ?: return
225-
backgroundPlayerInterfaceViews[playerId] = view
226-
}
227-
228222
/** Marks that rendering has completed. */
229223
public fun completeRendering(playerId: UUID, view: InterfaceView) {
230224
renderingPlayerInterfaceViews.remove(playerId, view)
@@ -235,6 +229,8 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
235229
if (view == null) {
236230
backgroundPlayerInterfaceViews -= playerId
237231
} else {
232+
// For something to be the background view it has to be openable!
233+
view.markAsReopenable()
238234
backgroundPlayerInterfaceViews[playerId] = view
239235
}
240236
}
@@ -280,15 +276,36 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
280276

281277
// Close the previous view first with open new as the reason, unless we
282278
// are currently opening this view!
283-
if (openInventory[event.player] != view) {
284-
dontReopen = true
285-
openInventory.put(event.player, view)?.markClosed(SCOPE, Reason.OPEN_NEW)
286-
dontReopen = false
279+
val openView = openInventory[event.player]
280+
if (openView != null && openView != view) {
281+
// If the previously opened view is a player interface, attempt to demote it to
282+
// the background interface and gracefully close it while marking it as re-openable
283+
// again after we're done.
284+
if (openView is PlayerInterfaceView && openPlayerInterfaceViews.remove(event.player.uniqueId, openView)) {
285+
backgroundPlayerInterfaceViews[event.player.uniqueId] = openView
286+
287+
val reopen = openView.shouldStillBeOpened
288+
withoutReopen {
289+
openView.markClosed(SCOPE, Reason.OPEN_NEW)
290+
}
291+
if (reopen) {
292+
openView.markAsReopenable()
293+
}
294+
} else {
295+
// Close whatever was previously opened completely
296+
withoutReopen {
297+
openView.markClosed(SCOPE, Reason.OPEN_NEW)
298+
}
299+
}
287300
}
288301

289-
// Move any currently open player inventory to the background to indicate
290-
// it is no longer the actually opened inventory!
291-
demoteOpenView(event.player.uniqueId)
302+
// Set the new open inventory
303+
openInventory[event.player] = view
304+
305+
// If there is an open view left, destroy it as something went wrong!
306+
withoutReopen {
307+
openPlayerInterfaceViews.remove(event.player.uniqueId)?.markClosed(SCOPE, Reason.OPEN_NEW)
308+
}
292309

293310
// Abort any previous query the player had
294311
abortQuery(event.player.uniqueId, null)
@@ -754,7 +771,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
754771
runSync {
755772
// Close the current inventory to open another to avoid close reasons
756773
val reopen = view.shouldStillBeOpened
757-
INSTANCE.withoutReopen {
774+
withoutReopen {
758775
view.player.closeInventory(Reason.OPEN_NEW)
759776
}
760777

0 commit comments

Comments
 (0)