From 09417d08346065d57aad5a540cfa7f51e0365878 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 18 Jun 2023 21:11:14 +0200 Subject: [PATCH] Expressions that are just numbers will be immediately converted rather than being evaluated --- .../spigot/math/DelegatedExpressionHandler.kt | 2 ++ .../eco/internal/spigot/math/ExpressionHandlers.kt | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/DelegatedExpressionHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/DelegatedExpressionHandler.kt index 4aebf32a..4470625b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/DelegatedExpressionHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/DelegatedExpressionHandler.kt @@ -15,6 +15,8 @@ class DelegatedExpressionHandler( .build() override fun evaluate(expression: String, context: PlaceholderContext): Double? { + expression.fastToDoubleOrNull()?.let { return it } + // Peak performance (totally not having fun with bitwise operators) val hash = (((expression.hashCode() shl 5) - expression.hashCode()) xor (context.player?.uniqueId?.hashCode() ?: 0) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/ExpressionHandlers.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/ExpressionHandlers.kt index f07bdce4..6f26c5ee 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/ExpressionHandlers.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/math/ExpressionHandlers.kt @@ -26,8 +26,10 @@ interface ExpressionHandler { fun evaluate(expression: String, context: PlaceholderContext): Double? } -private fun String.fastToDoubleOrNull(): Double? { - if (isEmpty()) { +fun String.fastToDoubleOrNull(): Double? { + val len = length + + if (len == 0) { return null } @@ -39,8 +41,7 @@ private fun String.fastToDoubleOrNull(): Double? { var decimalPart = 0.0 var decimalIdx = -1 - while (idx < length) { - + while (idx < len) { when (val char = this[idx]) { '.' -> { if (decimalIdx != -1) return null @@ -62,7 +63,7 @@ private fun String.fastToDoubleOrNull(): Double? { idx++ } - decimalPart /= 10.0.pow((length - decimalIdx - 1).toDouble()) + decimalPart /= 10.0.pow((len - decimalIdx - 1).toDouble()) return if (isNegative) -(integerPart + decimalPart) else integerPart + decimalPart }