2025-11-30 17:22:48 +01:00
|
|
|
package fr.azur.tcoww.ui
|
|
|
|
|
|
|
|
|
|
import fr.azur.tcoww.Tcoww
|
|
|
|
|
import fr.azur.tcoww.roles.Role
|
|
|
|
|
import net.kyori.adventure.text.Component
|
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor
|
|
|
|
|
import org.bukkit.NamespacedKey
|
|
|
|
|
import org.bukkit.event.inventory.InventoryClickEvent
|
|
|
|
|
import org.bukkit.event.inventory.InventoryCloseEvent
|
|
|
|
|
import org.bukkit.inventory.Inventory
|
|
|
|
|
import org.bukkit.inventory.ItemStack
|
|
|
|
|
import org.bukkit.inventory.meta.Damageable
|
|
|
|
|
import java.util.concurrent.CompletableFuture
|
|
|
|
|
import kotlin.math.ceil
|
|
|
|
|
|
|
|
|
|
class GameConfig(
|
|
|
|
|
playerCount: Int,
|
|
|
|
|
) : CustomUI {
|
|
|
|
|
// [index, [role key, amount]]
|
|
|
|
|
val rolesConfig = hashMapOf<Int, Pair<NamespacedKey, Int>>()
|
|
|
|
|
val size = (Role.registerRoles.count() / 9 + 1) * 9
|
|
|
|
|
val future = CompletableFuture<Map<NamespacedKey, Int>>()
|
|
|
|
|
|
|
|
|
|
private val inventory =
|
2025-11-30 19:04:28 +01:00
|
|
|
Tcoww.instance.server.createInventory(this, size, Component.text("Game Config.", NamedTextColor.GREEN)).apply {
|
2025-11-30 17:22:48 +01:00
|
|
|
Role.registerRoles.values.sortedBy { it.order }.onEachIndexed { index, role ->
|
|
|
|
|
setItem(index, createItem(index, role))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init {
|
2025-11-30 19:04:28 +01:00
|
|
|
val werewolfCount = ceil((playerCount * (Tcoww.instance.config.getInt("werewolf-default-percentage") / 100f))).toInt()
|
2025-11-30 17:22:48 +01:00
|
|
|
editItem(0, playerCount - werewolfCount)
|
|
|
|
|
editItem(1, werewolfCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun createItem(
|
|
|
|
|
index: Int,
|
|
|
|
|
role: Role,
|
|
|
|
|
): ItemStack {
|
|
|
|
|
rolesConfig[index] = Pair(role.id, 0)
|
|
|
|
|
|
|
|
|
|
return ItemStack.of(role.icon).apply {
|
|
|
|
|
itemMeta =
|
|
|
|
|
(itemMeta as Damageable).apply {
|
|
|
|
|
lore(
|
|
|
|
|
role.description,
|
|
|
|
|
)
|
|
|
|
|
itemName(
|
|
|
|
|
role.displayName,
|
|
|
|
|
)
|
|
|
|
|
setMaxStackSize(1)
|
|
|
|
|
setMaxDamage(1)
|
|
|
|
|
damage = 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun editItem(
|
|
|
|
|
index: Int,
|
|
|
|
|
newAmount: Int,
|
|
|
|
|
) {
|
|
|
|
|
if (newAmount !in 0..99) throw IllegalArgumentException("Amount must be in 0 and 99 !")
|
|
|
|
|
rolesConfig[index] = Pair(rolesConfig[index]!!.first, newAmount)
|
|
|
|
|
val item = inventory.getItem(index) ?: return
|
|
|
|
|
item.itemMeta =
|
|
|
|
|
(item.itemMeta as Damageable).apply {
|
|
|
|
|
if (newAmount == 0) {
|
|
|
|
|
setMaxStackSize(1)
|
|
|
|
|
setMaxDamage(1)
|
|
|
|
|
damage = 1
|
|
|
|
|
} else {
|
|
|
|
|
damage = 0
|
|
|
|
|
setMaxStackSize(99)
|
|
|
|
|
item.amount = newAmount
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getInventory(): Inventory = inventory
|
|
|
|
|
|
|
|
|
|
override fun onClick(event: InventoryClickEvent) {
|
|
|
|
|
event.isCancelled = true
|
|
|
|
|
val index = event.slot
|
|
|
|
|
if (index !in 0..<Role.registerRoles.size || event.clickedInventory != inventory) return
|
|
|
|
|
if (event.isLeftClick) {
|
|
|
|
|
for (i in 0..<Role.registerRoles.size) {
|
|
|
|
|
if (i == index) continue
|
|
|
|
|
val count = rolesConfig[i]!!.second
|
|
|
|
|
if (count != 0) {
|
|
|
|
|
val targetCount = rolesConfig[index]!!.second
|
|
|
|
|
editItem(i, count - 1)
|
|
|
|
|
editItem(index, targetCount + 1)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (event.isRightClick) {
|
|
|
|
|
for (i in 0..<Role.registerRoles.size) {
|
|
|
|
|
if (i == index) continue
|
|
|
|
|
val count = rolesConfig[i]!!.second
|
|
|
|
|
if (count != 0) {
|
|
|
|
|
val targetCount = rolesConfig[index]!!.second
|
|
|
|
|
if (targetCount == 0) break
|
|
|
|
|
editItem(i, count + 1)
|
|
|
|
|
editItem(index, targetCount - 1)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onClose(event: InventoryCloseEvent) {
|
|
|
|
|
future.complete(
|
|
|
|
|
buildMap {
|
|
|
|
|
rolesConfig.forEach {
|
|
|
|
|
if (it.value.second != 0) put(it.value.first, it.value.second)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|