Effect.kt

Branches > main > be9c7ea > files / src / main / kotlin / net / bowmap / stormbow / effect / Effect.kt

package net.bowmap.stormbow.effect

import kotlinx.collections.immutable.*

/// An effect is a piece of data representing a side effect that can be executed,
/// such as with a Bukkit API call
interface Effect

/// An effect that knows how to execute itself.
/// Some effects are to be run by event listeners themselves
interface DefaultEffect : Effect {
  fun execute()
}

fun execute(effects: PersistentList<Effect>) {
  effects.forEach { effect ->
    if (effect is DefaultEffect) {
      effect.execute()
    }
  }
}

fun effects(vararg e: Effect) = persistentListOf(*e)

/// Returns a the first effect of type T in the list effects
inline fun <reified T : Effect> find(effects: PersistentList<Effect>): T? {
  return effects.filterIsInstance<T>().firstOrNull()
}

/// Returns whether an effect of type T exists in the list of effects
inline fun <reified T : Effect> contains(effects: PersistentList<Effect>): Boolean {
  return effects.any {it is T}
}