9
0
mirror of https://github.com/Auxilor/EcoJobs.git synced 2025-12-23 17:09:27 +00:00

Merge pull request #21

Fixed inappropriate xp gain handling
This commit is contained in:
Will FP
2023-02-18 12:07:38 +00:00
committed by GitHub
6 changed files with 49 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ allprojects {
}
dependencies {
compileOnly 'com.willfp:eco:6.46.0'
compileOnly 'com.willfp:eco:6.51.1'
implementation 'com.willfp:libreforge:3.129.2'
implementation 'com.willfp:ecomponent:1.3.0'
implementation 'org.joml:joml:1.10.4'

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

@@ -38,6 +38,7 @@ import org.bukkit.Bukkit
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.time.Duration
import java.util.DoubleSummaryStatistics
import java.util.Objects
import java.util.concurrent.TimeUnit
@@ -47,6 +48,10 @@ import kotlin.math.max
class Job(
val id: String, val config: Config, private val plugin: EcoJobsPlugin
) {
private val topCache = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofSeconds(plugin.configYml.getInt("leaderboard-cache-lifetime").toLong()))
.build<Int, LeaderboardCacheEntry?>()
val name = config.getFormattedString("name")
val description = config.getFormattedString("description")
val isUnlockedByDefault = config.getBool("unlocked-by-default")
@@ -354,6 +359,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 +386,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

@@ -17,7 +17,7 @@ object JobTriggerXPGainListener : Listener {
val amount = job.getXP(event)
if (amount <= 0.0) {
return
continue
}
player.giveJobExperience(job, amount)

View File

@@ -312,6 +312,7 @@ point-names: # If you have point names that look ugly (eg g_souls) then you can
use-faster-move-trigger: true # Disable if you want move trigger to detect sub-1-block movements
raytrace-distance: 80 # The distance that alt_click should check for a location
leaderboard-cache-lifetime: 180 # How often will top placeholders update their cache (in seconds)
potions:
icon:

View File

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