All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.pouvoir.internal.manager.TriggerManagerImpl.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.pouvoir.internal.manager

import com.skillw.pouvoir.Pouvoir
import com.skillw.pouvoir.api.feature.trigger.BaseTrigger
import com.skillw.pouvoir.api.feature.trigger.TriggerController
import com.skillw.pouvoir.api.manager.sub.TriggerManager
import com.skillw.pouvoir.api.plugin.map.BaseMap
import com.skillw.pouvoir.api.plugin.map.KeyMap
import com.skillw.pouvoir.internal.feature.trigger.custom.CustomTriggerController

internal object TriggerManagerImpl : TriggerManager() {
    override val key = "TriggerManager"
    override val priority = 5
    override val subPouvoir = Pouvoir
    private val controllers = KeyMap>()
    private val controllersByType = BaseMap, TriggerController<*>>()
    override fun  call(trigger: T) {
        (controllersByType[trigger::class.java] as? TriggerController?)?.call(trigger)
    }

    override fun  addTask(triggerKey: String, key: String, priority: Int, run: (T) -> Unit) {
        onAddTask(triggerKey, key, priority, run)
    }

    override fun remove(triggerKey: String, key: String) {
        onRemoveTask(triggerKey, key)
    }

    override fun removeAllTask(triggerKey: String) {
        onRemoveTrigger(triggerKey)
    }

    override fun registerController(controller: TriggerController<*>) {
        controllers.register(controller)
        controllersByType.register(controller.triggerClass, controller)
    }

    private fun  controller(triggerKey: String): Pair> {
        val controller: TriggerController<*> =
            controllers.values.firstOrNull { it.predicate(triggerKey) } ?: CustomTriggerController
        return triggerKey to (controller as TriggerController)
    }

    private fun  onAddTask(triggerKey: String, key: String, priority: Int, run: (T) -> Unit) {
        val (trigger, controller) = controller(triggerKey)
        controller.onRemoveTask(trigger, key)
        controller.onAddTask(trigger, key, priority, run)
    }

    private fun onRemoveTrigger(triggerKey: String) {
        val (trigger, controller) = controller(triggerKey)
        controller.onRemoveTrigger(trigger)
    }

    private fun onRemoveTask(triggerKey: String, key: String) {
        val (trigger, controller) = controller(triggerKey)
        controller.onRemoveTask(trigger, key)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy