hibiscus/src/main/kotlin/codes/som/hibiscus/api/feature/Feature.kt

53 lines
1.5 KiB
Kotlin

package codes.som.hibiscus.api.feature
import codes.som.hibiscus.Hibiscus
import codes.som.hibiscus.api.command.Command
import codes.som.hibiscus.api.event.*
import codes.som.hibiscus.api.feature.values.ValueRegistry
abstract class Feature(val name: String, val category: FeatureCategory) {
private val listeners = mutableListOf<TypedListener<*>>()
protected inline fun <reified T : Event> on(
crossinline cond: () -> Boolean = { true },
phase: EventPhase = EventPhase.NORMAL,
listener: Listener<T>
) =
on(object : TypedListener<T>(T::class.java, phase) {
override fun on(event: T) {
if (cond()) listener.on(event)
}
})
protected fun <T : Event> on(listener: TypedListener<T>) =
listeners.add(listener)
val values = ValueRegistry()
var enabled: Boolean = false
set(value) {
val hasChanged = value != field
if (hasChanged) {
field = value
if (value) {
onEnable()
listeners.forEach { Hibiscus.bus.registerTyped(it) }
} else {
listeners.forEach { Hibiscus.bus.unregisterTyped(it) }
onDisable()
}
}
}
var hiddenInOverlay = false
open fun onEnable() {}
open fun onDisable() {}
open fun createFeatureCommand(): Command {
return FeatureCommand(this)
}
// TODO: Module commands
}