@@ -219,12 +219,6 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
219
219
return true
220
220
}
221
221
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
-
228
222
/* * Marks that rendering has completed. */
229
223
public fun completeRendering (playerId : UUID , view : InterfaceView ) {
230
224
renderingPlayerInterfaceViews.remove(playerId, view)
@@ -235,6 +229,8 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
235
229
if (view == null ) {
236
230
backgroundPlayerInterfaceViews - = playerId
237
231
} else {
232
+ // For something to be the background view it has to be openable!
233
+ view.markAsReopenable()
238
234
backgroundPlayerInterfaceViews[playerId] = view
239
235
}
240
236
}
@@ -280,15 +276,36 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
280
276
281
277
// Close the previous view first with open new as the reason, unless we
282
278
// 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
+ }
287
300
}
288
301
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
+ }
292
309
293
310
// Abort any previous query the player had
294
311
abortQuery(event.player.uniqueId, null )
@@ -754,7 +771,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
754
771
runSync {
755
772
// Close the current inventory to open another to avoid close reasons
756
773
val reopen = view.shouldStillBeOpened
757
- INSTANCE . withoutReopen {
774
+ withoutReopen {
758
775
view.player.closeInventory(Reason .OPEN_NEW )
759
776
}
760
777
0 commit comments