From 68f07ea16e88d3190347f9f6b8d882339befc3a0 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 23 Aug 2024 15:28:38 +0100 Subject: [PATCH] Fixed bug with filters --- .../com/willfp/libreforge/filters/Filter.kt | 4 +--- .../com/willfp/libreforge/filters/FilterBlock.kt | 16 ++-------------- .../com/willfp/libreforge/filters/Filters.kt | 14 ++++++++++---- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filter.kt b/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filter.kt index 975ee0307..f5e4d9e5c 100644 --- a/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filter.kt +++ b/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filter.kt @@ -36,9 +36,7 @@ abstract class Filter( ): Boolean { val cfg = config.config - val isInverted = config.isInverted ?: return true - - return if (isInverted) { + return if (config.isInverted) { !isMet(data, getValue(cfg, data, "not_$id"), config.compileData) } else { isMet(data, getValue(cfg, data, id), config.compileData) diff --git a/core/common/src/main/kotlin/com/willfp/libreforge/filters/FilterBlock.kt b/core/common/src/main/kotlin/com/willfp/libreforge/filters/FilterBlock.kt index 9d9c1e027..158223bb1 100644 --- a/core/common/src/main/kotlin/com/willfp/libreforge/filters/FilterBlock.kt +++ b/core/common/src/main/kotlin/com/willfp/libreforge/filters/FilterBlock.kt @@ -10,21 +10,9 @@ import com.willfp.libreforge.triggers.TriggerData class FilterBlock internal constructor( val filter: Filter, override val config: Config, - override val compileData: T + override val compileData: T, + val isInverted: Boolean ) : Compiled { - val isInverted: Boolean? by lazy { - val cfg = config - - val regularPresent = cfg.has(filter.id) - val inversePresent = cfg.has("not_${filter.id}") - - if (!regularPresent && !inversePresent) { - null - } else { - inversePresent - } - } - fun isMet(data: TriggerData) = filter.isMet(data, this) } diff --git a/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filters.kt b/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filters.kt index e8aa002a3..84734cb12 100644 --- a/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filters.kt +++ b/core/common/src/main/kotlin/com/willfp/libreforge/filters/Filters.kt @@ -52,8 +52,13 @@ object Filters : Registry>() { val blocks = mutableListOf>() for (key in config.getKeys(false)) { - val filter = get(key) ?: get(key.removePrefix("not_")) ?: continue - blocks += makeBlock(filter, config, context) ?: continue + if (key.startsWith("not_")) { + val filter = get(key.removePrefix("not_")) ?: continue + blocks += makeBlock(filter, config, true, context) ?: continue + } else { + val filter = get(key) ?: continue + blocks += makeBlock(filter, config, false, context) ?: continue + } } return FilterList(blocks) @@ -62,6 +67,7 @@ object Filters : Registry>() { private fun makeBlock( filter: Filter, config: Config, + inverted: Boolean, context: ViolationContext ): FilterBlock? { if (filter.deprecationMessage != null) { @@ -77,14 +83,14 @@ object Filters : Registry>() { return null } - val configKey = if (config.has("not_${filter.id}")) { + val configKey = if (inverted) { "not_${filter.id}" } else { filter.id } val compileData = filter.makeCompileData(config, context, filter.getValue(config, null, configKey)) - return FilterBlock(filter, config, compileData) + return FilterBlock(filter, config, compileData, inverted) } init {