Rewrote placeholder parsing

This commit is contained in:
Auxilor
2023-04-27 15:42:46 +01:00
parent d2917341b1
commit 82797e7342
15 changed files with 345 additions and 182 deletions

View File

@@ -37,6 +37,7 @@ import com.willfp.eco.internal.gui.menu.renderedInventory
import com.willfp.eco.internal.gui.slot.EcoSlotBuilder
import com.willfp.eco.internal.integrations.PAPIExpansion
import com.willfp.eco.internal.logging.EcoLogger
import com.willfp.eco.internal.placeholder.PlaceholderParser
import com.willfp.eco.internal.proxy.EcoProxyFactory
import com.willfp.eco.internal.scheduling.EcoScheduler
import com.willfp.eco.internal.spigot.data.DataYml
@@ -90,10 +91,13 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
if (this.configYml.getBool("use-safer-namespacedkey-creation"))
SafeInternalNamespacedKeyFactory() else FastInternalNamespacedKeyFactory()
private val placeholderParser = PlaceholderParser()
private val crunchHandler = DelegatedExpressionHandler(
this,
if (this.configYml.getBool("use-immediate-placeholder-translation-for-math"))
ImmediatePlaceholderTranslationExpressionHandler() else LazyPlaceholderTranslationExpressionHandler()
ImmediatePlaceholderTranslationExpressionHandler(placeholderParser)
else LazyPlaceholderTranslationExpressionHandler(placeholderParser),
)
override fun createScheduler(plugin: EcoPlugin) =
@@ -334,4 +338,10 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
override fun sendPacket(player: Player, packet: Packet) =
this.getProxy(PacketHandlerProxy::class.java).sendPacket(player, packet)
override fun translatePlaceholders(text: String, context: PlaceholderContext) =
placeholderParser.translatePlacholders(text, context)
override fun getPlaceholderValue(plugin: EcoPlugin?, args: String, context: PlaceholderContext) =
placeholderParser.getPlaceholderResult(plugin, args, context)
}

View File

@@ -4,6 +4,7 @@ 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.context.PlaceholderContext
import com.willfp.eco.internal.placeholder.PlaceholderParser
import redempt.crunch.CompiledExpression
import redempt.crunch.Crunch
import redempt.crunch.data.FastNumberParsing
@@ -27,7 +28,9 @@ interface ExpressionHandler {
fun evaluate(expression: String, context: PlaceholderContext): Double
}
class ImmediatePlaceholderTranslationExpressionHandler : ExpressionHandler {
class ImmediatePlaceholderTranslationExpressionHandler(
private val placeholderParser: PlaceholderParser
) : ExpressionHandler {
private val cache: Cache<String, CompiledExpression> = Caffeine.newBuilder()
.expireAfterAccess(500, TimeUnit.MILLISECONDS)
.build()
@@ -37,7 +40,7 @@ class ImmediatePlaceholderTranslationExpressionHandler : ExpressionHandler {
}
override fun evaluate(expression: String, context: PlaceholderContext): Double {
val translatedExpression = PlaceholderManager.translatePlaceholders(expression, context)
val translatedExpression = placeholderParser.translatePlacholders(expression, context)
val compiled = cache.get(translatedExpression) {
runCatching { Crunch.compileExpression(translatedExpression, env) }
@@ -48,15 +51,16 @@ class ImmediatePlaceholderTranslationExpressionHandler : ExpressionHandler {
}
}
class LazyPlaceholderTranslationExpressionHandler : ExpressionHandler {
class LazyPlaceholderTranslationExpressionHandler(
private val placeholderParser: PlaceholderParser
) : ExpressionHandler {
private val cache: Cache<String, CompiledExpression> = Caffeine.newBuilder()
.build()
override fun evaluate(expression: String, context: PlaceholderContext): Double {
val placeholders = PlaceholderManager.findPlaceholdersIn(expression)
val placeholderValues = placeholders
.map { PlaceholderManager.translatePlaceholders(it, context) }
val placeholderValues = placeholderParser.parseIndividualPlaceholders(placeholders, context)
.map { runCatching { FastNumberParsing.parseDouble(it) }.getOrDefault(0.0) }
.toDoubleArray()