9
0
mirror of https://github.com/Auxilor/EcoJobs.git synced 2025-12-28 19:39:24 +00:00

Added %ecojobs_top_<job>_<place>_<name/amount>% placeholder

This commit is contained in:
_OfTeN_
2023-02-17 04:45:45 +03:00
parent 2014a6dd5f
commit 4034f5098f
3 changed files with 43 additions and 0 deletions

View File

@@ -1,7 +1,10 @@
package com.willfp.ecojobs
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.placeholder.DynamicPlaceholder
import com.willfp.eco.core.placeholder.PlayerPlaceholder
import com.willfp.eco.util.savedDisplayName
import com.willfp.eco.util.toNiceString
import com.willfp.ecojobs.api.activeJobs
import com.willfp.ecojobs.api.getJobLevel
import com.willfp.ecojobs.api.jobLimit
@@ -14,6 +17,7 @@ import com.willfp.ecojobs.jobs.PriceHandler
import com.willfp.ecojobs.jobs.ResetOnQuitListener
import com.willfp.libreforge.LibReforgePlugin
import org.bukkit.event.Listener
import java.util.regex.Pattern
class EcoJobsPlugin : LibReforgePlugin() {
init {
@@ -46,6 +50,26 @@ class EcoJobsPlugin : LibReforgePlugin() {
}
level.toString()
}.register()
DynamicPlaceholder(
this,
Pattern.compile("top_[a-z]+_[0-9]+_[a-z]+")
) {
val split = it.split("_")
val jobId = split.getOrNull(1) ?: return@DynamicPlaceholder "You must specify the job id!"
val job = Jobs.getByID(jobId) ?: return@DynamicPlaceholder "Invalid job id!"
val placeString = split.getOrNull(2) ?: return@DynamicPlaceholder "You must specify the place!"
val place = placeString.toIntOrNull() ?: return@DynamicPlaceholder "Invalid place!"
val type = split.getOrNull(3) ?: return@DynamicPlaceholder "You must specify the top type!"
val topEntry = job.getTop(place)
return@DynamicPlaceholder when(type) {
"name" -> topEntry?.player?.savedDisplayName
?: this.langYml.getFormattedString("top.name-empty")
"amount" -> topEntry?.amount?.toNiceString()
?: this.langYml.getFormattedString("top.amount-empty")
else -> "Invalid type: $type! Available types: name/amount"
}
}.register()
}
override fun loadPluginCommands(): List<PluginCommand> {

View File

@@ -47,6 +47,8 @@ import kotlin.math.max
class Job(
val id: String, val config: Config, private val plugin: EcoJobsPlugin
) {
private val topCache = Caffeine.newBuilder().build<Int, LeaderboardCacheEntry?>()
val name = config.getFormattedString("name")
val description = config.getFormattedString("description")
val isUnlockedByDefault = config.getBool("unlocked-by-default")
@@ -354,6 +356,14 @@ class Job(
return jobXpGains.sumOf { it.getCount(event) }
}
fun getTop(place: Int): LeaderboardCacheEntry? {
return topCache.get(place) {
val players = Bukkit.getOfflinePlayers().sortedByDescending { it.getJobLevel(this) }
val target = players.getOrNull(place-1) ?: return@get null
return@get LeaderboardCacheEntry(target, target.getJobLevel(this))
}
}
override fun equals(other: Any?): Boolean {
if (other !is Job) {
return false
@@ -373,6 +383,11 @@ private class LevelPlaceholder(
operator fun invoke(level: Int) = function(level)
}
data class LeaderboardCacheEntry(
val player: OfflinePlayer,
val amount: Int
)
private fun Collection<LevelPlaceholder>.format(string: String, level: Int): String {
var process = string
for (placeholder in this) {

View File

@@ -42,3 +42,7 @@ menu:
title: "Jobs"
infinity: "∞"
top:
name-empty: "&cEmpty"
amount-empty: "0"