From 5d40c32c1e62d9a1ce84ec6dfa3e943521b404cf Mon Sep 17 00:00:00 2001 From: Exanthiax <107284021+Exanthiax@users.noreply.github.com> Date: Tue, 28 Jan 2025 23:35:34 +0000 Subject: [PATCH] Fixed xp-required and placeholders. --- build.gradle.kts | 2 +- .../com/willfp/ecojobs/api/EcoJobsAPI.kt | 2 +- .../kotlin/com/willfp/ecojobs/jobs/Job.kt | 50 ++++++++++--------- .../willfp/ecojobs/util/LevelInjectable.kt | 27 ++++++++++ 4 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LevelInjectable.kt diff --git a/build.gradle.kts b/build.gradle.kts index c97d0d0..e62cec2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,7 +39,7 @@ allprojects { } dependencies { - compileOnly("com.willfp:eco:6.55.0") + compileOnly("com.willfp:eco:6.56.0") compileOnly("org.jetbrains:annotations:23.0.0") compileOnly("org.jetbrains.kotlin:kotlin-stdlib:2.1.0") } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/api/EcoJobsAPI.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/api/EcoJobsAPI.kt index ca65a26..46371c2 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/api/EcoJobsAPI.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/api/EcoJobsAPI.kt @@ -140,7 +140,7 @@ fun OfflinePlayer.resetJob(job: Job) { /** * Get the experience required to advance to the next level. */ -fun OfflinePlayer.getJobXPRequired(job: Job) = job.getExpForLevel(this.getJobLevel(job) + 1) +fun OfflinePlayer.getJobXPRequired(job: Job) = job.getFormattedExpForLevel(this.getJobLevel(job) + 1) /** * Get progress to next level between 0 and 1, where 0 is none and 1 is complete. 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 a43455c..752f8b9 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 @@ -6,11 +6,10 @@ import com.willfp.eco.core.data.keys.PersistentDataKey import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.core.items.Items import com.willfp.eco.core.items.builder.ItemStackBuilder -import com.willfp.eco.core.placeholder.InjectablePlaceholder import com.willfp.eco.core.placeholder.PlayerPlaceholder import com.willfp.eco.core.placeholder.PlayerStaticPlaceholder import com.willfp.eco.core.placeholder.PlayerlessPlaceholder -import com.willfp.eco.core.placeholder.StaticPlaceholder +import com.willfp.eco.core.placeholder.context.placeholderContext import com.willfp.eco.core.price.ConfiguredPrice import com.willfp.eco.core.price.impl.PriceEconomy import com.willfp.eco.core.registry.Registrable @@ -27,6 +26,7 @@ import com.willfp.ecojobs.api.getJobXP import com.willfp.ecojobs.api.getJobXPRequired import com.willfp.ecojobs.api.hasJobActive import com.willfp.ecojobs.api.jobLimit +import com.willfp.ecojobs.util.LevelInjectable import com.willfp.libreforge.ViolationContext import com.willfp.libreforge.conditions.ConditionList import com.willfp.libreforge.conditions.Conditions @@ -286,13 +286,8 @@ class Job( val withPlaceholders = lore.map { it.replace("%percentage_progress%", (player.getJobProgress(this) * 100).toNiceString()) .replace("%current_xp%", player.getJobXP(this).toNiceString()) - .replace("%required_xp%", this.getExpForLevel(player.getJobLevel(this) + 1).let { req -> - if (req == Int.MAX_VALUE) { - plugin.langYml.getFormattedString("infinity") - } else { - req.toNiceString() - } - }).replace("%description%", this.description).replace("%job%", this.name) + .replace("%required_xp%", this.getFormattedExpForLevel(player.getJobLevel(this) + 1)) + .replace("%description%", this.description).replace("%job%", this.name) .replace("%level%", (forceLevel ?: player.getJobLevel(this)).toString()) .replace("%level_numeral%", NumberUtils.toNumeral(forceLevel ?: player.getJobLevel(this))) .replace("%join_price%", this.joinPrice.getDisplay(player)) @@ -358,27 +353,34 @@ class Job( }.build() } - fun getExpForLevel(level: Int): Int { - if (level < 1 || level > maxLevel) { - return Int.MAX_VALUE - } - - // Use levelXpRequirements if it exists and covers the level - if (levelXpRequirements != null && levelXpRequirements.size >= level) { - return levelXpRequirements[level - 1].toInt() - } - - // Fallback to using the xpFormula if it exists + /** + * Get the XP required to reach the next level, if currently at [level]. + */ + fun getExpForLevel(level: Int): Double { if (xpFormula != null) { return evaluateExpression( - xpFormula.replace("%level%", level.toString()) - ).toInt() + xpFormula, + placeholderContext( + injectable = LevelInjectable(level) + ) + ) } - // Default fallback if neither configuration is usable - return Int.MAX_VALUE + if (levelXpRequirements != null) { + return levelXpRequirements.getOrNull(level) ?: Double.POSITIVE_INFINITY + } + + return Double.POSITIVE_INFINITY } + fun getFormattedExpForLevel(level: Int): String { + val required = getExpForLevel(level) + return if (required.isInfinite()) { + plugin.langYml.getFormattedString("infinity") + } else { + required.toNiceString() + } + } fun executeLevelCommands(player: Player, level: Int) { val commands = levelCommands[level] ?: emptyList() diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LevelInjectable.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LevelInjectable.kt new file mode 100644 index 0000000..606607c --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecojobs/util/LevelInjectable.kt @@ -0,0 +1,27 @@ +package com.willfp.ecojobs.util + +import com.willfp.eco.core.placeholder.InjectablePlaceholder +import com.willfp.eco.core.placeholder.PlaceholderInjectable +import com.willfp.eco.core.placeholder.StaticPlaceholder + +class LevelInjectable( + level: Int +) : PlaceholderInjectable { + private val placeholders = listOf( + StaticPlaceholder( + "level" + ) { level.toString() } + ) + + override fun getPlaceholderInjections(): List { + return placeholders + } + + override fun addInjectablePlaceholder(p0: Iterable) { + return + } + + override fun clearInjectedPlaceholders() { + return + } +}