Skip to content

Commit

Permalink
Reworked repeat_count placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jul 20, 2023
1 parent 590518f commit 8812652
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 10 additions & 10 deletions core/src/main/kotlin/com/willfp/libreforge/NamedValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,42 @@ that give different hashes each time, to force a re-calculation of the expressio

internal class DynamicNumericValue(
identifiers: Collection<String>,
value: () -> Number
) : NamedValue(identifiers, value()) {
value: Number
) : NamedValue(identifiers, value) {
constructor(
identifier: String,
value: () -> Number
value: Number
) : this(listOf(identifier), value)

override val placeholders: List<InjectablePlaceholder> = identifiers.map {
DynamicHashPlaceholder(
ValueHashPlaceholder(
it,
value
)
}

private class DynamicHashPlaceholder(
private class ValueHashPlaceholder(
private val identifier: String,
private val fn: () -> Number
private val value: Number
) : SimpleInjectablePlaceholder(identifier) {
override fun getValue(p0: String, p1: PlaceholderContext) = fn().toString()
override fun getValue(p0: String, p1: PlaceholderContext) = value.toString()

override fun hashCode(): Int {
// Use the value of the function to force a re-calculation of the expression
return fn().toInt() * 31 + identifier.hashCode()
return value.toInt() * 31 + identifier.hashCode()
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

if (other !is DynamicHashPlaceholder) {
if (other !is ValueHashPlaceholder) {
return false
}

return other.identifier == this.identifier
&& other.fn() == this.fn()
&& other.value == this.value
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ abstract class ElementLike : ConfigurableElement {
var repeatTimes = 1
var repeatIncrement = 0.0
var repeatCount = 0.0

if (config.has("repeat")) {
// Extra initial injection, otherwise it's not possible to use injections
// in the repeat configs.
Expand All @@ -65,7 +66,7 @@ abstract class ElementLike : ConfigurableElement {
trigger.addPlaceholder(NamedValue("repeat_times", repeatTimes))
trigger.addPlaceholder(NamedValue("repeat_start", repeatStart))
trigger.addPlaceholder(NamedValue("repeat_increment", repeatIncrement))
trigger.addPlaceholder(DynamicNumericValue("repeat_count") { repeatCount })
trigger.addPlaceholder(DynamicNumericValue("repeat_count", repeatCount))
}

var delay = 0L
Expand Down Expand Up @@ -139,6 +140,9 @@ abstract class ElementLike : ConfigurableElement {
) true else didTrigger

repeatCount += repeatIncrement

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

// Can't delay initial execution for things that modify events.
Expand Down

0 comments on commit 8812652

Please sign in to comment.