diff --git a/core/src/main/kotlin/com/willfp/libreforge/HolderProvider.kt b/core/src/main/kotlin/com/willfp/libreforge/HolderProvider.kt index f45cdc79e..00cbcaace 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/HolderProvider.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/HolderProvider.kt @@ -87,7 +87,11 @@ data class ProvidedEffectBlocks( data class ProvidedEffectBlock( val effect: EffectBlock, val holder: ProvidedHolder -) +) : Comparable { + override fun compareTo(other: ProvidedEffectBlock): Int { + return this.effect.weight - other.effect.weight + } +} private val providers = mutableListOf() @@ -281,9 +285,10 @@ fun Player.updateEffects() { val beforeF = before.flatten() val afterF = after.flatten() - val added = afterF without beforeF - val removed = beforeF without afterF - val toReload = afterF without added + // Permanent effects also have a run order, so we need to sort them. + val added = (afterF without beforeF).sorted() + val removed = (beforeF without afterF).sorted() + val toReload = (afterF without added).sorted() for ((effect, holder) in removed) { effect.disable(this, holder) @@ -294,6 +299,8 @@ fun Player.updateEffects() { } // Reloading is now done by disabling all, then enabling all. Effect#reload is deprecated. + // Since permanent effects are not allowed in chains, they are always done in the correct + // order as mixing weights is not a concern. for ((effect, holder) in toReload) { effect.disable(this, holder, isReload = true) diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/Chain.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/Chain.kt index 4ed8be80c..2cade969c 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/Chain.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/Chain.kt @@ -15,6 +15,8 @@ class Chain( it.effect.runOrder.weight } ) { + val weight = effects.sumOf { it.effect.runOrder.weight } + fun trigger( trigger: DispatchedTrigger, executor: ChainExecutor = this.executor diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt index 5c2479e52..e877166e3 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt @@ -28,6 +28,8 @@ class EffectBlock internal constructor( ) : ElementLike() { override val supportsDelay = effects.all { it.supportsDelay } + val weight = effects.weight + @JvmOverloads fun enable( player: Player, diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectList.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectList.kt index 66f9d6b1b..f851aa41b 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectList.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectList.kt @@ -9,8 +9,7 @@ import com.willfp.libreforge.triggers.DispatchedTrigger class EffectList( effects: List ) : DelegatedList(effects.sortedBy { - val total = it.effects.sumOf { element -> element.effect.runOrder.weight } - total / it.effects.size.coerceAtLeast(1) + it.weight }) { fun trigger(trigger: DispatchedTrigger) = this.forEach { it.trigger(trigger) }