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

io.github.molumn.catrix.kommand.AbstractKommandNode.kt Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
package io.github.molumn.catrix.kommand

import io.github.molumn.catrix.annotations.KommandDSL
import io.github.molumn.catrix.common.Interactive
import io.github.molumn.catrix.kommand.node.NodeType
import io.github.molumn.catrix.utils.FakeSystem

@KommandDSL
abstract class AbstractKommandNode(
    val name: String
) {

    protected val children: MutableList = mutableListOf()
    protected var condition: FakeSystem.() -> Boolean = { true }
    protected var executor: ((Interactive, KommandContext, FakeSystem) -> Boolean)? = null


    fun condition(condition: FakeSystem.() -> Boolean) {
        this.condition = condition
    }

    fun then(
        name: String,
        init: LiteralNode.() -> Unit
    ) {
        children.add(LiteralNode(name).apply(init))
    }

    fun  then(
        node: Pair>,
        init: ArgumentNode.() -> Unit
    ) {
        children.add(ArgumentNode(node.first, node.second).apply(init))
    }

    fun execute(executor: (interactive: Interactive, context: KommandContext, system: FakeSystem) -> Boolean) {
        this.executor = executor
    }


    internal abstract fun build() : AbstractRegisteredKommandNode<*>

}

abstract class AbstractRegisteredKommandNode
internal constructor(
    val name: String,
    val condition: FakeSystem.() -> Boolean,
    val executor: ((Interactive, KommandContext, FakeSystem) -> Boolean)?,
    private val children: List>
) {

    abstract fun check(string: String) : Boolean
    abstract fun extract(string: String) : T
    abstract fun listSuggestion(string: String) : List

    fun find(string: String) : AbstractRegisteredKommandNode<*>? {
        return children.find { it.check(string) }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy