diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt index 8b4f5f4..6eef1a5 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/Job.kt @@ -28,7 +28,8 @@ import com.willfp.ecojobs.api.getJobXPRequired import com.willfp.ecojobs.api.hasJobActive import com.willfp.ecojobs.api.jobLimit import com.willfp.ecojobs.jobs.JobsLeaderboard.getPosition -import com.willfp.ecojobs.util.LeaderboardCacheEntry +import com.willfp.ecojobs.jobs.JobsLeaderboard.getTop +import com.willfp.ecojobs.util.LeaderboardEntry import com.willfp.ecojobs.util.LevelInjectable import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.conditions.ConditionList @@ -447,12 +448,12 @@ class Job( } } - fun getTop(place: Int): LeaderboardCacheEntry? { - return JobsLeaderboard.getTop(this, place) + fun getTop(place: Int): LeaderboardEntry? { + return getTop(this, place) } fun getPosition(uuid: UUID): Int? { - return JobsLeaderboard.getPosition(this, uuid) + return getPosition(this, uuid) } override fun getID(): String { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JobsLeaderboard.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JobsLeaderboard.kt index 096e7ec..bcb129b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JobsLeaderboard.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/jobs/JobsLeaderboard.kt @@ -4,74 +4,39 @@ import com.github.benmanes.caffeine.cache.Caffeine import com.willfp.eco.core.tuples.Pair import com.willfp.ecojobs.EcoJobsPlugin import com.willfp.ecojobs.api.getJobLevel -import com.willfp.ecojobs.util.LeaderboardCacheEntry +import com.willfp.ecojobs.util.LeaderboardEntry import org.bukkit.Bukkit import java.time.Duration import java.util.* +import java.util.concurrent.TimeUnit object JobsLeaderboard { - private var topLeaderboard = Caffeine.newBuilder() - .expireAfterWrite( - Duration.ofSeconds( - EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong() - ) - ) - .build> { + private var leaderboardCache = Caffeine.newBuilder() + .expireAfterWrite(1, TimeUnit.MINUTES) + .build>> { val offlinePlayers = Bukkit.getOfflinePlayers() - val top = mutableMapOf() + val top = mutableMapOf>() for (job in Jobs.values()) - top.putAll(offlinePlayers.sortedByDescending { it.getJobLevel(job) } - .mapIndexed { place, player -> - TopEntry(job, place + 1) to LeaderboardCacheEntry( - player, - player.getJobLevel(job) - ) - }) + top[job] = offlinePlayers.sortedByDescending { it.getJobLevel(job) }.map { it.uniqueId } return@build top } - private var posLeaderboard = Caffeine.newBuilder() - .expireAfterWrite( - Duration.ofSeconds( - EcoJobsPlugin.instance.configYml.getInt("leaderboard-cache-lifetime").toLong() - ) - ) - .build> { - val offlinePlayers = Bukkit.getOfflinePlayers() - val pos = mutableMapOf() - for (job in Jobs.values()) - pos.putAll(offlinePlayers.sortedByDescending { it.getJobLevel(job) } - .map { PosEntry(job, it.uniqueId) to it.getJobLevel(job) }) - return@build pos - } - fun getTop(job: Job, place: Int): LeaderboardCacheEntry? { - return topLeaderboard.get(true)[TopEntry(job, place)] + fun getTop(job: Job, position: Int): LeaderboardEntry? { + require(position > 0) { "Position must be greater than 0" } + + val uuid = leaderboardCache.get(true)[job]?.getOrNull(position - 1) ?: return null + + val player = Bukkit.getOfflinePlayer(uuid).takeIf { it.hasPlayedBefore() } ?: return null + + return LeaderboardEntry( + player, + player.getJobLevel(job) + ) } fun getPosition(job: Job, uuid: UUID): Int? { - return posLeaderboard.get(true)[PosEntry(job, uuid)]?.plus(1) - } - - private class TopEntry(val job: Job, val place: Int) { - override fun equals(other: Any?): Boolean { - if (other !is TopEntry) return false - return job == other.job && place == other.place - } - - override fun hashCode(): Int { - return Objects.hash(job, place) - } - } - - private class PosEntry(val job: Job, val uuid: UUID) { - - override fun equals(other: Any?): Boolean { - if (other !is PosEntry) return false - return job == other.job && uuid == other.uuid - } - - override fun hashCode(): Int { - return Objects.hash(job, uuid) - } + val leaderboard = leaderboardCache.get(true)[job] + val index = leaderboard?.indexOf(uuid) + return if (index == -1) null else index?.plus(1) } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardCacheEntry.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardEntry.kt similarity index 77% rename from eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardCacheEntry.kt rename to eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardEntry.kt index 8d74038..5070b12 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardCacheEntry.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LeaderboardEntry.kt @@ -2,7 +2,7 @@ package com.willfp.ecojobs.util import org.bukkit.OfflinePlayer -data class LeaderboardCacheEntry( +data class LeaderboardEntry( val player: OfflinePlayer, val level: Int ) \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index b0d9395..8b11df8 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -8,9 +8,6 @@ # cross-server sync. use-local-storage: false -# Time in seconds for the lifetime of the leaderboard cache. -leaderboard-cache-lifetime: 60 - jobs: limit: 3 # The most jobs a player can have at once. # You can set custom limits with the ecojobs.limit. permission