Updated/fixed crunch

This commit is contained in:
Auxilor
2022-03-09 11:20:54 +00:00
parent af198d30f7
commit ca9c940b2b
2 changed files with 11 additions and 20 deletions

View File

@@ -2,7 +2,7 @@ group 'com.willfp'
version rootProject.version version rootProject.version
dependencies { dependencies {
implementation 'com.github.Redempt:Crunch:1.0' implementation 'com.github.Redempt:Crunch:1.1.2'
compileOnly 'net.kyori:adventure-platform-bukkit:4.1.0' compileOnly 'net.kyori:adventure-platform-bukkit:4.1.0'
compileOnly 'org.apache.maven:maven-artifact:3.8.1' compileOnly 'org.apache.maven:maven-artifact:3.8.1'
compileOnly 'com.google.code.gson:gson:2.8.8' compileOnly 'com.google.code.gson:gson:2.8.8'

View File

@@ -1,5 +1,7 @@
package com.willfp.eco.internal.spigot.math 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.integrations.placeholder.PlaceholderManager
import org.bukkit.entity.Player import org.bukkit.entity.Player
import redempt.crunch.CompiledExpression import redempt.crunch.CompiledExpression
@@ -7,32 +9,21 @@ import redempt.crunch.Crunch
import redempt.crunch.data.FastNumberParsing import redempt.crunch.data.FastNumberParsing
import redempt.crunch.functional.EvaluationEnvironment import redempt.crunch.functional.EvaluationEnvironment
private val cache = mutableMapOf<String, CompiledExpression>() private val cache: Cache<String, CompiledExpression> = Caffeine.newBuilder().build()
private val goToZero = Crunch.compileExpression("0") private val goToZero = Crunch.compileExpression("0")
fun evaluateExpression(expression: String, player: Player?): Double { fun evaluateExpression(expression: String, player: Player?): Double {
val placeholderValues = PlaceholderManager.findPlaceholdersIn(expression) val placeholderValues = PlaceholderManager.findPlaceholdersIn(expression)
.map { PlaceholderManager.translatePlaceholders(expression, player) } .map { PlaceholderManager.translatePlaceholders(it, player) }
.map { runCatching { FastNumberParsing.parseDouble(it) }.getOrDefault(0.0) } .map { runCatching { FastNumberParsing.parseDouble(it) }.getOrDefault(0.0) }
.toDoubleArray() .toDoubleArray()
val compiled = generateExpression(expression) val compiled = cache.get(expression) {
return runCatching { compiled.evaluate(*placeholderValues) }.getOrDefault(0.0) val placeholders = PlaceholderManager.findPlaceholdersIn(it)
} val env = EvaluationEnvironment()
env.setVariableNames(*placeholders.toTypedArray())
private fun generateExpression(expression: String): CompiledExpression { runCatching { Crunch.compileExpression(expression, env) }.getOrDefault(goToZero)
val cached = cache[expression]
if (cached != null) {
return cached
} }
val placeholders = PlaceholderManager.findPlaceholdersIn(expression) return runCatching { compiled.evaluate(*placeholderValues) }.getOrDefault(0.0)
val env = EvaluationEnvironment()
env.setVariableNames(*placeholders.toTypedArray())
val compiled = runCatching { Crunch.compileExpression(expression, env) }.getOrDefault(goToZero)
cache[expression] = compiled
return compiled
} }