Reworked placeholder system
This commit is contained in:
@@ -176,7 +176,7 @@ abstract class EcoSpigotPlugin : EcoPlugin() {
|
||||
val tpsProxy = getProxy(TPSProxy::class.java)
|
||||
ServerUtils.initialize { tpsProxy.getTPS() }
|
||||
|
||||
NumberUtils.initCrunch { exp, player -> evaluateExpression(exp, player) }
|
||||
NumberUtils.initCrunch { expression, player, statics -> evaluateExpression(expression, player, statics) }
|
||||
|
||||
postInit()
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.willfp.eco.internal.spigot.math
|
||||
import com.github.benmanes.caffeine.cache.Cache
|
||||
import com.github.benmanes.caffeine.cache.Caffeine
|
||||
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager
|
||||
import com.willfp.eco.core.placeholder.StaticPlaceholder
|
||||
import org.bukkit.entity.Player
|
||||
import redempt.crunch.CompiledExpression
|
||||
import redempt.crunch.Crunch
|
||||
@@ -12,14 +13,16 @@ import redempt.crunch.functional.EvaluationEnvironment
|
||||
private val cache: Cache<String, CompiledExpression> = Caffeine.newBuilder().build()
|
||||
private val goToZero = Crunch.compileExpression("0")
|
||||
|
||||
fun evaluateExpression(expression: String, player: Player?): Double {
|
||||
fun evaluateExpression(expression: String, player: Player?, statics: Iterable<StaticPlaceholder>): Double {
|
||||
val placeholderValues = PlaceholderManager.findPlaceholdersIn(expression)
|
||||
.map { PlaceholderManager.translatePlaceholders(it, player) }
|
||||
.union(statics.map { it.value })
|
||||
.toList()
|
||||
.map { runCatching { FastNumberParsing.parseDouble(it) }.getOrDefault(0.0) }
|
||||
.toDoubleArray()
|
||||
|
||||
val compiled = cache.get(expression) {
|
||||
val placeholders = PlaceholderManager.findPlaceholdersIn(it)
|
||||
val placeholders = PlaceholderManager.findPlaceholdersIn(it) union statics.map { static -> "%${static.identifier}%" }
|
||||
val env = EvaluationEnvironment()
|
||||
env.setVariableNames(*placeholders.toTypedArray())
|
||||
runCatching { Crunch.compileExpression(expression, env) }.getOrDefault(goToZero)
|
||||
|
||||
Reference in New Issue
Block a user