diff --git a/core/src/main/kotlin/com/willfp/libreforge/NamedValue.kt b/core/src/main/kotlin/com/willfp/libreforge/NamedValue.kt index f52d5fcef..cb934fed2 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/NamedValue.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/NamedValue.kt @@ -42,29 +42,29 @@ that give different hashes each time, to force a re-calculation of the expressio internal class DynamicNumericValue( identifiers: Collection, - 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 = 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 { @@ -72,12 +72,12 @@ internal class DynamicNumericValue( 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 } } } diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt index dc3575116..6b765e32b 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt @@ -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. @@ -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 @@ -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.