Skip to content

Commit

Permalink
Implemented weight for permanent effects
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed May 22, 2023
1 parent 1fa9681 commit 34d48ef
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
15 changes: 11 additions & 4 deletions core/src/main/kotlin/com/willfp/libreforge/HolderProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ data class ProvidedEffectBlocks(
data class ProvidedEffectBlock(
val effect: EffectBlock,
val holder: ProvidedHolder
)
) : Comparable<ProvidedEffectBlock> {
override fun compareTo(other: ProvidedEffectBlock): Int {
return this.effect.weight - other.effect.weight
}
}

private val providers = mutableListOf<HolderProvider>()

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/com/willfp/libreforge/effects/Chain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import com.willfp.libreforge.triggers.DispatchedTrigger
class EffectList(
effects: List<EffectBlock>
) : DelegatedList<EffectBlock>(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) }
Expand Down

0 comments on commit 34d48ef

Please sign in to comment.