Skip to content

Commit

Permalink
Filters now accept both regular and inverted at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 21, 2024
1 parent 0a0c8de commit 2b2d2ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ abstract class Filter<T, V>(
): Boolean {
val cfg = config.config

val isInverted = config.isInverted ?: return true
val types = config.types
val results = mutableSetOf<Boolean>()

return if (isInverted) {
!isMet(data, getValue(cfg, data, "not_$id"), config.compileData)
} else {
isMet(data, getValue(cfg, data, id), config.compileData)
if (FilterType.REGULAR in types) {
results += isMet(data, getValue(cfg, data, id), config.compileData)
}

if (FilterType.INVERTED in types) {
results += !isMet(data, getValue(cfg, data, "not_$id"), config.compileData)
}

return results.all { it }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.willfp.libreforge.filters
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.Compiled
import com.willfp.libreforge.triggers.TriggerData
import java.util.EnumSet

/**
* A single filter config block.
Expand All @@ -12,17 +13,23 @@ class FilterBlock<T, V> internal constructor(
override val config: Config,
override val compileData: T
) : Compiled<T> {
val isInverted: Boolean? by lazy {
val types: EnumSet<FilterType> by lazy {
val cfg = config

val regularPresent = cfg.has(filter.id)
val inversePresent = cfg.has("not_${filter.id}")

if (!regularPresent && !inversePresent) {
null
} else {
inversePresent
val inversions = mutableSetOf<FilterType>()

if (regularPresent) {
inversions.add(FilterType.REGULAR)
}

if (inversePresent) {
inversions.add(FilterType.INVERTED)
}

EnumSet.copyOf(inversions)
}

fun isMet(data: TriggerData) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.willfp.libreforge.filters

enum class FilterType {
INVERTED,
REGULAR,
}

0 comments on commit 2b2d2ad

Please sign in to comment.