diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/CrunchHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/CrunchHandler.kt index fd4b5695..856de3c7 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/CrunchHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/CrunchHandler.kt @@ -10,10 +10,21 @@ import redempt.crunch.CompiledExpression import redempt.crunch.Crunch import redempt.crunch.data.FastNumberParsing import redempt.crunch.functional.EvaluationEnvironment +import redempt.crunch.functional.Function +import kotlin.math.max +import kotlin.math.min private val cache: Cache = Caffeine.newBuilder().build() private val goToZero = Crunch.compileExpression("0") +private val min = Function("min", 2) { + min(it[0], it[1]) +} + +private val max = Function("max", 2) { + max(it[0], it[1]) +} + fun evaluateExpression(expression: String, player: Player?, context: PlaceholderInjectable, additional: Collection): Double { val placeholderValues = PlaceholderManager.findPlaceholdersIn(expression) .map { PlaceholderManager.translatePlaceholders(it, player, context, additional) } @@ -24,6 +35,7 @@ fun evaluateExpression(expression: String, player: Player?, context: Placeholder val placeholders = PlaceholderManager.findPlaceholdersIn(it) val env = EvaluationEnvironment() env.setVariableNames(*placeholders.toTypedArray()) + env.addFunctions(min, max) runCatching { Crunch.compileExpression(expression, env) }.getOrDefault(goToZero) }