Skip to content

Commit

Permalink
fix: Create abstract function without unnecessary params
Browse files Browse the repository at this point in the history
  • Loading branch information
Distractic committed Dec 18, 2023
1 parent e4ede85 commit b0d0b10
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/rushyverse/api/gui/GUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public abstract class GUI<T>(
* @param key Key to create the inventory for.
* @return New created inventory.
*/
protected abstract suspend fun createInventory(key: T): Inventory
protected abstract fun createInventory(key: T): Inventory

/**
* Create a new flow of [Item][ItemStack] to fill the inventory with.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/rushyverse/api/gui/PlayerGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class PlayerGUI(
* @param key The client to create the inventory for.
* @return The inventory for the client.
*/
override suspend fun createInventory(key: Client): Inventory {
override fun createInventory(key: Client): Inventory {
val player = key.requirePlayer()
return createInventory(player, key)
}
Expand Down
24 changes: 22 additions & 2 deletions src/main/kotlin/com/github/rushyverse/api/gui/SingleGUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.github.rushyverse.api.player.Client
import com.github.shynixn.mccoroutine.bukkit.scope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.job
import kotlinx.coroutines.plus
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.inventory.Inventory
import org.bukkit.plugin.Plugin

/**
Expand All @@ -33,19 +35,37 @@ public abstract class SingleGUI(
private val KEY = Any()
}

override suspend fun getKey(client: Client): Any {
return KEY
}

override suspend fun fillScope(key: Any): CoroutineScope {
val scope = plugin.scope
return scope + SupervisorJob(scope.coroutineContext.job)
}

override fun createInventory(key: Any): Inventory {
return createInventory()
}

/**
* @see createInventory(key)
*/
protected abstract fun createInventory(): Inventory

override suspend fun close(client: Client, closeInventory: Boolean): Boolean {
return if (closeInventory && contains(client)) {
client.player?.closeInventory(InventoryCloseEvent.Reason.PLUGIN)
true
} else false
}

override suspend fun getKey(client: Client): Any {
return KEY
override fun getItems(key: Any, size: Int): Flow<ItemStackIndex> {
return getItems(size)
}

/**
* @see getItems(key, size)
*/
protected abstract fun getItems(size: Int): Flow<ItemStackIndex>
}

0 comments on commit b0d0b10

Please sign in to comment.