From 5de4914fd75d60cbca7986bf491ad5dc6e0f3417 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 3 May 2023 14:18:50 +0100 Subject: [PATCH] Fixed expression loading, improved hash codes down evaluation pipeline --- .../context/MergedInjectableContext.java | 22 +++++++++++++++++++ .../willfp/eco/internal/config/EcoConfig.kt | 17 +++++++++++++- .../internal/extensions/EcoExtensionLoader.kt | 2 +- .../spigot/math/DelegatedExpressionHandler.kt | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/placeholder/context/MergedInjectableContext.java b/eco-api/src/main/java/com/willfp/eco/core/placeholder/context/MergedInjectableContext.java index d0c8de46..2383fc29 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/placeholder/context/MergedInjectableContext.java +++ b/eco-api/src/main/java/com/willfp/eco/core/placeholder/context/MergedInjectableContext.java @@ -3,10 +3,12 @@ package com.willfp.eco.core.placeholder.context; import com.willfp.eco.core.placeholder.InjectablePlaceholder; import com.willfp.eco.core.placeholder.PlaceholderInjectable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; /** @@ -67,4 +69,24 @@ public class MergedInjectableContext implements PlaceholderInjectable { return injections; } + + @Override + public boolean equals(@Nullable final Object o) { + if (this == o) { + return true; + } + + if (!(o instanceof MergedInjectableContext that)) { + return false; + } + + return Objects.equals(baseContext, that.baseContext) + && Objects.equals(additionalContext, that.additionalContext) + && Objects.equals(extraInjections, that.extraInjections); + } + + @Override + public int hashCode() { + return Objects.hash(baseContext, additionalContext, extraInjections); + } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfig.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfig.kt index 7da5b3a5..459e8000 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfig.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/EcoConfig.kt @@ -251,6 +251,21 @@ open class EcoConfig( } override fun hashCode(): Int { - return Objects.hash(values, configType, injections) + /* + The keys are completely redundant, as they are only used to prevent + duplicate keys in the map. Therefore, we can ignore them and just + hash the actual placeholder values. + */ + + var injectionHash = 0 + + for (injection in injections.values) { + injectionHash = injectionHash * 31 + injection.hashCode() + } + + return Objects.hash( + values, + configType + ) + injectionHash } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/extensions/EcoExtensionLoader.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/extensions/EcoExtensionLoader.kt index 2a616271..3fe7b09e 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/extensions/EcoExtensionLoader.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/extensions/EcoExtensionLoader.kt @@ -59,7 +59,7 @@ class EcoExtensionLoader( val pluginVersion = Version(extensionYml.getStringOrNull("plugin-version") ?: "0.0.0") val pluginName = extensionYml.getStringOrNull("plugin") - if (pluginName != null && pluginName.equals(this.plugin.description.name, ignoreCase = true)) { + if (pluginName != null && !pluginName.equals(this.plugin.description.name, ignoreCase = true)) { throw ExtensionLoadException("${extensionJar.name} is only compatible with $pluginName!") } 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 10a0f834..e44384a7 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 @@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit class DelegatedExpressionHandler( plugin: EcoPlugin, private val handler: ExpressionHandler -): ExpressionHandler { +) : ExpressionHandler { private val evaluationCache: Cache = Caffeine.newBuilder() .expireAfterWrite(plugin.configYml.getInt("math-cache-ttl").toLong(), TimeUnit.MILLISECONDS) .build()