Skip to content

Commit

Permalink
Optimised filters
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jul 20, 2023
1 parent 72ac583 commit cc43f24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
17 changes: 12 additions & 5 deletions core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.libreforge.effects

import com.willfp.eco.core.integrations.antigrief.AntigriefManager
import com.willfp.eco.core.placeholder.InjectablePlaceholder
import com.willfp.libreforge.ConfigurableElement
import com.willfp.libreforge.DynamicNumericValue
import com.willfp.libreforge.NamedValue
Expand Down Expand Up @@ -29,6 +30,15 @@ abstract class ElementLike : ConfigurableElement {
*/
open val isElementOwnChain: Boolean = false

// Inject placeholders into all config blocks.
private fun injectPlaceholders(placeholders: List<InjectablePlaceholder>) {
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(placeholders) }
}

/*
The replacement for the old ConfiguredEffect#invoke method.
Expand Down Expand Up @@ -93,11 +103,7 @@ abstract class ElementLike : ConfigurableElement {

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(trigger.placeholders) }
injectPlaceholders(trigger.placeholders)

// Filter
val filterResult = if (config.getBool("filters_before_mutation")) {
Expand Down Expand Up @@ -143,6 +149,7 @@ abstract class ElementLike : ConfigurableElement {

// Re-inject new placeholder with different hash
trigger.addPlaceholder(DynamicNumericValue("repeat_count", repeatCount))
injectPlaceholders(DynamicNumericValue("repeat_count", repeatCount).placeholders)
}

// Can't delay initial execution for things that modify events.
Expand Down
9 changes: 2 additions & 7 deletions core/src/main/kotlin/com/willfp/libreforge/filters/Filter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ abstract class Filter<T, V>(
): Boolean {
val cfg = config.config

val regularPresent = cfg.has(id)
val inversePresent = cfg.has("not_$id")
val isInverted = config.isInverted ?: return true

if (!regularPresent && !inversePresent) {
return true
}

return if (inversePresent) {
return if (isInverted) {
!isMet(data, getValue(cfg, data, "not_$id"), config.compileData)
} else {
isMet(data, getValue(cfg, data, id), config.compileData)
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/kotlin/com/willfp/libreforge/filters/FilterBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ class FilterBlock<T, V> internal constructor(
override val config: Config,
override val compileData: T
) : Compiled<T> {
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)
}

0 comments on commit cc43f24

Please sign in to comment.