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

com.skillw.pouvoir.api.plugin.TotalManager.kt Maven / Gradle / Ivy

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

import com.skillw.pouvoir.api.manager.ManagerData
import com.skillw.pouvoir.api.plugin.annotation.AutoRegister
import com.skillw.pouvoir.api.plugin.handler.ClassHandler
import com.skillw.pouvoir.api.plugin.map.KeyMap
import com.skillw.pouvoir.api.plugin.map.component.Registrable
import com.skillw.pouvoir.internal.core.plugin.SubPouvoirHandler
import com.skillw.pouvoir.util.existClass
import com.skillw.pouvoir.util.instance
import com.skillw.pouvoir.util.plugin.PluginUtils
import com.skillw.pouvoir.util.safe
import com.skillw.pouvoir.util.static
import org.bukkit.Bukkit
import org.bukkit.plugin.Plugin
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.library.reflex.ClassStructure
import taboolib.library.reflex.ReflexClass
import taboolib.platform.util.bukkitPlugin
import java.util.concurrent.ConcurrentHashMap

object TotalManager : KeyMap() {
    internal val pluginData = ConcurrentHashMap()
    val allStaticClasses = ConcurrentHashMap()
    private val allClasses = HashSet()

    @Awake(LifeCycle.LOAD)
    fun load() {
        val postLoads = ArrayList<() -> Unit>()
        Bukkit.getPluginManager().plugins
            .filter { isDependPouvoir(it) }
            .sortedWith { p1, p2 ->
                if (p1.isDepend(p2)) 1 else -1
            }
            .forEach {
                safe { loadSubPou(it, postLoads) }
            }
        allClasses.forEach { clazz ->
            handlers.forEach {
                it.inject(clazz)
            }
        }
        postLoads.forEach { safe(it) }
        postLoads.clear()
    }

    private val handlers = ArrayList()
    private fun loadSubPou(plugin: Plugin, postLoads: ArrayList<() -> Unit>) {
        if (!isDependPouvoir(plugin)) return

        val classes = PluginUtils.getClasses(plugin::class.java).map { ReflexClass.of(it).structure }

        classes.forEach {
            kotlin.runCatching { allStaticClasses[it.simpleName.toString()] = it.owner.static() }
        }
        allClasses.addAll(classes)

        handlers.addAll(classes
            .filter { ClassHandler::class.java.isAssignableFrom(it.owner) && it.simpleName != "ClassHandler" }
            .mapNotNull {
                it.owner.instance as? ClassHandler?
            })

        classes.forEach classFor@{ clazz ->
            //优先加载Managers
            safe { SubPouvoirHandler.inject(clazz, plugin) }
        }
        pluginData[plugin]?.let {
            ManagerData(it).register()
        }

        classes.filter { clazz ->
            clazz.isAnnotationPresent(AutoRegister::class.java)
        }.forEach { clazz ->
            kotlin.runCatching {
                val auto = clazz.getAnnotation(AutoRegister::class.java)
                val test = auto.property("test") ?: ""
                val postLoad = auto.property("postLoad") ?: false
                if ((test.isEmpty() || test.existClass()))
                    if (postLoad) {
                        postLoads.add {
                            (clazz.owner.instance as? Registrable<*>?)?.register()
                        }
                    } else (clazz.owner.instance as? Registrable<*>?)?.register()
            }.exceptionOrNull()?.printStackTrace()
        }
    }

    private fun Plugin.isDepend(other: Plugin) =
        description.depend.contains(other.name) || description.softDepend.contains(other.name)

    private fun isDependPouvoir(plugin: Plugin): Boolean {
        return plugin.isDepend(bukkitPlugin) || plugin.name == "Pouvoir"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy