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

com.skillw.pouvoir.api.feature.realizer.component.ScheduledRealizer.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.api.feature.realizer.component

import com.skillw.pouvoir.api.feature.realizer.BaseRealizer
import taboolib.common.platform.function.submit
import taboolib.common.platform.service.PlatformExecutor

/**
 * @className Realizable
 *
 * @author Glom
 * @date 2023/1/5 16:25 Copyright 2022 user.
 */
abstract class ScheduledRealizer(key: String, val async: Boolean = false) : BaseRealizer(key), Awakeable {
    private var task:
            PlatformExecutor.PlatformTask? = null

    abstract val defaultPeriod: Long
    protected fun period(): Long {
        return config.getOrDefault("period", defaultPeriod).toString().toLong()
    }

    abstract fun task()

    protected fun refreshTask() {
        cancelTask()
        task = submit(period = period(), async = async) {
            task()
        }
    }

    protected fun cancelTask() {
        task?.cancel()
    }

    override fun onEnable() {
        refreshTask()
    }

    override fun onReload() {
        refreshTask()
    }

    override fun onDisable() {
        cancelTask()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy