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

45 lines
1.3 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(listener: Listener<T>) =
on(object : TypedListener<T>(T::class.java), Listener<T> by listener {})
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
}