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

com.skillw.pouvoir.internal.feature.handler.AsahiHandlerBuilder.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.internal.feature.handler

import com.skillw.asahi.api.AsahiAPI.compile
import com.skillw.asahi.api.member.context.AsahiContext
import com.skillw.asahi.api.script.AsahiCompiledScript
import com.skillw.asahi.util.castSafely
import com.skillw.asahi.util.toLazyMap
import com.skillw.pouvoir.api.plugin.map.DataMap
import com.skillw.pouvoir.util.toMap
import org.bukkit.configuration.serialization.ConfigurationSerializable
import taboolib.common.platform.event.EventPriority
import taboolib.common5.cbool

/**
 * @className AsahiHandler
 *
 * @author Glom
 * @date 2023/1/15 23:12 Copyright 2024 Glom.
 */
class AsahiHandlerBuilder(
    initial: AsahiContext = AsahiContext.create(),
    receiver: AsahiHandlerBuilder.() -> Unit = {},
) : AsahiContext by initial,
    ConfigurationSerializable {
    var key = ""
    val triggers = HashSet()
    var priority = EventPriority.NORMAL
    val import = HashSet()
    val control = ArrayList>()
    val namespaces = HashSet()
    var condition: AsahiContext.() -> Boolean = { true }
    var deny: AsahiContext.() -> Unit = { }
    var release = false

    init {
        this.receiver()
    }

    constructor(key: String, map: Map = emptyMap()) : this() {
        val data = DataMap().apply { putAll(map) }
        this.key = key
        priority = data.get("priority", "NORMAL").castSafely() ?: EventPriority.NORMAL
        triggers.addAll(data.get("triggers", emptyList()))
        namespaces.addAll(data.get("namespaces", listOf("common", "lang")))
        val conditionScript =
            data.get("condition", "true").compile(*namespaces.toTypedArray())
        condition = { conditionScript.get().cbool }
        val denyScript = data.get("deny")?.toString()?.compile(*namespaces.toTypedArray())
        deny = { denyScript?.get() }
        import.addAll(data.get("import", emptyList()))
        context().putAll(data.get("context", emptyMap()).toLazyMap(*namespaces.toTypedArray()))
        control.addAll(data.get("when", emptyList>()).map {
            it.mapValues { entry ->
                if (entry.key == "if") entry.value.toString()
                    .compile(*namespaces.toTypedArray()) else entry.value
            }
        })
        data.get("functions", emptyMap()).forEach { (key, value) ->
            invokers[key] = value.toInvoker(key, namespaces) ?: return@forEach
        }
        release = true
    }

    fun build(): AsahiHandler {
        val builder = this
        return AsahiHandler(key, *triggers.toList().toTypedArray(), priority = priority.level) {
            import(*import.toTypedArray())
            putAll(builder.context())
        }.apply {
            release = builder.release
        }.handle {
            if (!builder.condition.invoke(this)) {
                builder.deny.invoke(this)
                return@handle
            }
            val goto = control.firstOrNull { (it["if"] as? AsahiCompiledScript?)?.run().cbool }?.get("goto")
                ?: control.lastOrNull()?.get("else") ?: "main"
            invokers[goto]?.invoke(this)
        }
    }

    override fun serialize(): MutableMap {
        return linkedMapOf()
    }

    companion object {
        @JvmStatic
        fun deserialize(section: org.bukkit.configuration.ConfigurationSection): AsahiHandlerBuilder {
            val key = section.name
            return AsahiHandlerBuilder(key, section.toMap())
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy