Skip to content

Commit

Permalink
chore: Return boolean when open GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Distractic committed Dec 15, 2023
1 parent 8b0980d commit e058f80
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/main/kotlin/com/github/rushyverse/api/gui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,34 @@ public abstract class GUI : Closeable {
* If the client has another GUI opened, close it.
* If the client has the same GUI opened, do nothing.
* @param client Client to open the GUI for.
* @return True if the GUI was opened, false otherwise.
*/
public suspend fun open(client: Client) {
public suspend fun open(client: Client): Boolean {
requireOpen()

val gui = client.gui()
if (gui == this) return
if (gui == this) return false
// If the client has another GUI opened, close it.
gui?.close(client, true)

val player = client.player
if (player == null) {
logger.warn { "Cannot open inventory for player ${client.playerUUID}: player is null" }
return
return false
}
// If the player is dead, do not open the GUI because the interface cannot be shown to the player.
if (player.isDead) return
if (player.isDead) return false

openGUI(client)
return openGUI(client)
}

/**
* Open the GUI for the client.
* Called by [open] after all the checks.
* @param client Client to open the GUI for.
* @return True if the GUI was opened, false otherwise.
*/
protected abstract suspend fun openGUI(client: Client)
protected abstract suspend fun openGUI(client: Client): Boolean

/**
* Action to do when the client clicks on an item in the inventory.
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/rushyverse/api/gui/PersonalGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class PersonalGUI(public val title: String) : GUI() {

private val mutex = Mutex()

override suspend fun openGUI(client: Client) {
override suspend fun openGUI(client: Client): Boolean {
val inventory = createInventory(client)

mutex.withLock { inventories[client] }?.close()
Expand All @@ -33,6 +33,7 @@ public abstract class PersonalGUI(public val title: String) : GUI() {
player.openInventory(inventory)

mutex.withLock { inventories[client] = inventory }
return true
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/rushyverse/api/gui/SharedGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public abstract class SharedGUI : GUI() {

private var inventory: Inventory? = null

override suspend fun openGUI(client: Client) {
override suspend fun openGUI(client: Client): Boolean {
val player = client.requirePlayer()
val inventory = getOrCreateInventory()
player.openInventory(inventory)
return true
}

/**
Expand Down

0 comments on commit e058f80

Please sign in to comment.