-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimised trigger dispatching, reworked counters
- Loading branch information
Showing
7 changed files
with
125 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 0 additions & 80 deletions
80
core/src/main/kotlin/com/willfp/libreforge/counters/CounterHandler.kt
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
core/src/main/kotlin/com/willfp/libreforge/counters/bind/BoundCounter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.willfp.libreforge.counters.bind | ||
|
||
import com.willfp.eco.core.placeholder.context.placeholderContext | ||
import com.willfp.eco.util.evaluateExpression | ||
import com.willfp.libreforge.counters.Accumulator | ||
import com.willfp.libreforge.counters.Counter | ||
import com.willfp.libreforge.triggers.DispatchedTrigger | ||
|
||
/* | ||
The current binding system feels a bit messy, so it's all marked as internal since | ||
I'll probably change it later, and I don't want someone to hook into a system | ||
that may break between updates. | ||
*/ | ||
|
||
internal data class BoundCounter( | ||
val counter: Counter, | ||
val accumulator: Accumulator | ||
) { | ||
fun accept(trigger: DispatchedTrigger) { | ||
val data = trigger.data | ||
|
||
val player = trigger.player | ||
val value = data.value | ||
|
||
if (!counter.canBeTriggeredBy(trigger.trigger)) { | ||
return | ||
} | ||
|
||
if (!counter.conditions.areMet(player, data.holder)) { | ||
return | ||
} | ||
|
||
if (!counter.filters.isMet(data)) { | ||
return | ||
} | ||
|
||
val config = counter.config | ||
|
||
// Inject placeholders, totally not stolen from ElementLike | ||
listOf(counter.filters, counter.conditions) | ||
.flatten() | ||
.map { it.config } | ||
.plusElement(config) | ||
.forEach { it.addInjectablePlaceholder(trigger.placeholders) } | ||
|
||
val (argumentsMet, met, notMet) = counter.arguments.checkMet(counter, trigger) | ||
|
||
if (!argumentsMet) { | ||
notMet.forEach { it.ifNotMet(counter, trigger) } | ||
return | ||
} | ||
|
||
val multiplier = evaluateExpression( | ||
counter.multiplierExpression, | ||
placeholderContext( | ||
player = player, | ||
injectable = config | ||
) | ||
) | ||
|
||
met.forEach { it.ifMet(counter, trigger) } | ||
|
||
accumulator.accept(player, value * multiplier) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
core/src/main/kotlin/com/willfp/libreforge/counters/bind/BoundCounters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.willfp.libreforge.counters.bind | ||
|
||
import com.willfp.eco.core.map.listMap | ||
import com.willfp.libreforge.counters.Accumulator | ||
import com.willfp.libreforge.counters.Counter | ||
|
||
internal object BoundCounters { | ||
private val bindings = listMap<Counter, BoundCounter>() | ||
|
||
fun bind(counter: Counter, accumulator: Accumulator) { | ||
bindings[counter] += BoundCounter(counter, accumulator) | ||
} | ||
|
||
fun unbind(counter: Counter) { | ||
bindings.remove(counter) | ||
} | ||
|
||
fun values(): List<Counter> = | ||
bindings.keys.toList() | ||
|
||
val Counter.bindings: List<BoundCounter> | ||
get() = BoundCounters.bindings[this].toList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
core/src/main/kotlin/com/willfp/libreforge/triggers/PotentiallyTriggerable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.willfp.libreforge.triggers | ||
|
||
interface PotentiallyTriggerable { | ||
/** | ||
* Check if this can be triggered by some [trigger]. | ||
*/ | ||
fun canBeTriggeredBy(trigger: Trigger): Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters