From 307936c50554e3d667ee9924eab0cd3e59531087 Mon Sep 17 00:00:00 2001 From: Aeltumn Date: Fri, 16 Aug 2024 22:18:41 +0200 Subject: [PATCH] chore: Better mutex usage to ensure rendering happens once --- .../noxcrew/interfaces/InterfacesListeners.kt | 11 +++++- .../interfaces/view/AbstractInterfaceView.kt | 35 +++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt index 787d882..868b565 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt @@ -198,6 +198,15 @@ public class InterfacesListeners private constructor(private val plugin: Plugin) openPlayerInterfaceViews.put(playerId, view) } + /** Sets the background view for [playerId] to [view]. */ + public fun setBackgroundView(playerId: UUID, view: PlayerInterfaceView?) { + if (view == null) { + backgroundPlayerInterfaceViews.invalidate(playerId) + } else { + backgroundPlayerInterfaceViews.put(playerId, view) + } + } + /** Closes the given [view] of a player interface. */ public fun closePlayerInterface(playerId: UUID, view: PlayerInterfaceView?) { // Save the contents of their currently shown inventory @@ -244,7 +253,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin) // Move the current open inventory to the background to indicate // it is no longer the actually opened inventory! openPlayerInterfaceViews.getIfPresent(event.player.uniqueId)?.also { - backgroundPlayerInterfaceViews.put(event.player.uniqueId, it) + setBackgroundView(event.player.uniqueId, it) openPlayerInterfaceViews.invalidate(event.player.uniqueId) } diff --git a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt index d0d52a5..a92a187 100644 --- a/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt +++ b/interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt @@ -19,7 +19,6 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.withTimeout import net.kyori.adventure.text.Component import org.bukkit.Bukkit @@ -32,7 +31,6 @@ import org.slf4j.LoggerFactory import java.util.WeakHashMap import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.atomic.AtomicBoolean -import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -50,8 +48,8 @@ public abstract class AbstractInterfaceView, Unit>() @@ -202,12 +200,14 @@ public abstract class AbstractInterfaceView= 2) return + // If we're already rendering we queue up another render! + if (paneMutex.isLocked) { + debouncedRender.set(true) + return + } - // Await to acquire a semaphore before starting the render - queue.incrementAndGet() - semaphore.acquire() + // Await to acquire the mutex before we start rendering + paneMutex.lock() try { withTimeout(6.seconds) { pane = panes.collapse(backing.totalRows(), builder.fillMenuWithAir) @@ -219,8 +219,12 @@ public abstract class AbstractInterfaceView