Files
src
build.gradle.kts
manifest.kod
settings.gradle.kts
Code
14 702
src/main/kotlin/com/kodari/stats/LeaderboardManager.kt
package com.kodari.stats
import org.bukkit.ChatColor
import org.bukkit.Location
import org.bukkit.entity.ArmorStand
import org.bukkit.entity.Entity
import org.bukkit.persistence.PersistentDataType
import java.util.EnumMap
import java.util.UUID
class LeaderboardManager(
private val plugin: KodariStatsPlugin,
private val statsManager: StatsManager
) {
private val boards = EnumMap<LeaderboardType, Board>(LeaderboardType::class.java)
private val boardKey = org.bukkit.NamespacedKey(plugin, "leaderboard-type")
fun load() {
LeaderboardType.values().forEach { type ->
val section = plugin.config.getConfigurationSection("leaderboards.${type.key}") ?: return@forEach
val location = readLocation(section.getConfigurationSection("location")) ?: return@forEach
val entities = section.getStringList("entities").mapNotNull { id ->
runCatching { UUID.fromString(id) }.getOrNull()?.let { location.world?.getEntity(it) }
}
val armorStands = entities.filterIsInstance<ArmorStand>()
val board = Board(type, location, armorStands.toMutableList())
boards[type] = board
if (armorStands.size != ENTITY_COUNT) {
removeEntities(entities)
board.entities.clear()
spawnEntities(board)
} else {
refresh(board)
}
}
}
fun create(type: LeaderboardType, location: Location) {
boards[type]?.let { removeEntities(it.entities) }
val board = Board(type, location.clone(), mutableListOf())
boards[type] = board
spawnEntities(board)
refresh(board)
}
fun move(type: LeaderboardType, location: Location): Boolean {
val board = boards[type] ?: return false
removeEntities(board.entities)
board.location = location.clone()
board.entities.clear()
spawnEntities(board)
refresh(board)
return true
}
fun refreshAll() {
boards.values.forEach(::refresh)
}
private fun spawnEntities(board: Board) {
val world = board.location.world ?: return
val titleLocation = board.location.clone().add(0.0, 3.0, 0.0)
board.entities += spawn(world, titleLocation, board.type, "title")
repeat(ROW_COUNT) { index ->
val rowLocation = board.location.clone().add(0.0, 2.55 - index * 0.28, 0.0)
board.entities += spawn(world, rowLocation, board.type, "row-$index")
}
saveLocation(board)
}
private fun spawn(world: org.bukkit.World, location: Location, type: LeaderboardType, role: String): ArmorStand {
val stand = world.spawn(location, ArmorStand::class.java)
stand.isInvisible = true
stand.isMarker = true
stand.isInvulnerable = true
stand.setGravity(false)
stand.setCanPickupItems(false)
stand.persistentDataContainer.set(boardKey, PersistentDataType.STRING, "${type.key}:$role")
stand.addScoreboardTag("kodari-leaderboard")
return stand
}
private fun refresh(board: Board) {
if (board.entities.size != ENTITY_COUNT) return
val title = "${ChatColor.BOLD}${hexColor(board.type.titleColor, board.type.displayName)}"
setName(board.entities[0], title)
val entries = statsManager.top(board.type, ROW_COUNT)
repeat(ROW_COUNT) { index ->
val text = if (index < entries.size) {
val (name, value) = entries[index]
val displayedValue = if (board.type == LeaderboardType.PLAYTIME) formatTime(value) else value.toString()
"${ChatColor.GRAY}${index + 1}. ${ChatColor.WHITE}$name ${ChatColor.DARK_GRAY}- ${board.type.color}$displayedValue"
} else if (index == 0 && entries.isEmpty()) {
"${ChatColor.GRAY}No recorded stats yet"
} else {
""
}
setName(board.entities[index + 1], text)
}
saveLocation(board)
}
private fun setName(entity: Entity, text: String) {
entity.customName = text
entity.isCustomNameVisible = text.isNotEmpty()
}
private fun saveLocation(board: Board) {
val path = "leaderboards.${board.type.key}"
val location = board.location
plugin.config.set("$path.location.world", location.world?.name)
plugin.config.set("$path.location.x", location.x)
plugin.config.set("$path.location.y", location.y)
plugin.config.set("$path.location.z", location.z)
plugin.config.set("$path.location.yaw", location.yaw)
plugin.config.set("$path.location.pitch", location.pitch)
plugin.config.set("$path.entities", board.entities.map { it.uniqueId.toString() })
plugin.saveConfig()
}
private fun readLocation(section: org.bukkit.configuration.ConfigurationSection?): Location? {
val worldName = section?.getString("world") ?: return null
val world = plugin.server.getWorld(worldName) ?: return null
return Location(
world,
section.getDouble("x"),
section.getDouble("y"),
section.getDouble("z"),
section.getDouble("yaw").toFloat(),
section.getDouble("pitch").toFloat()
)
}
private fun removeEntities(entities: Collection<Entity>) {
entities.forEach { if (!it.isDead) it.remove() }
}
private fun formatTime(seconds: Long): String {
val hours = seconds / 3_600
val minutes = (seconds % 3_600) / 60
return if (hours > 0) "${hours}h ${minutes}m" else "${minutes}m ${seconds % 60}s"
}
private fun hexColor(hex: String, text: String): String =
"§x${hex.map { "§$it" }.joinToString("")}$text"
private data class Board(
val type: LeaderboardType,
var location: Location,
val entities: MutableList<ArmorStand>
)
companion object {
private const val ROW_COUNT = 10
private const val ENTITY_COUNT = ROW_COUNT + 1
}
}