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

io.github.freya022.botcommands.internal.commands.CommandPathImpl.kt Maven / Gradle / Ivy

Go to download

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

There is a newer version: 3.0.0-alpha.18
Show newest version
package io.github.freya022.botcommands.internal.commands

import io.github.freya022.botcommands.api.commands.CommandPath
import net.dv8tion.jda.internal.utils.Checks
import kotlin.math.min

internal class CommandPathImpl internal constructor(
    private val name: String,
    private val group: String?,
    private val subname: String?
) : CommandPath {
    private val path: String
    private val count: Int

    init {
        if (group != null) Checks.matches(group, Checks.ALPHANUMERIC_WITH_DASH, "Subcommand group name")
        if (subname != null) Checks.matches(subname, Checks.ALPHANUMERIC_WITH_DASH, "Subcommand name")

        val components = listOfNotNull(name, group, subname)

        this.path = components.joinToString(" ")
        this.count = components.size
    }

    override fun getName(): String = name
    override fun getGroup(): String? = group
    override fun getSubname(): String? = subname

    override fun getNameCount(): Int = count

    override fun getParent(): CommandPath? = when {
        group != null && subname != null -> CommandPath.of(name, group) // /name group sub
        group != null -> CommandPath.of(name)  // /name group
        subname != null -> CommandPath.of(name)  // /name sub
        else -> null // /name
    }


    override fun getFullPath(): String {
        return path
    }

    override fun getFullPath(separator: Char): String {
        return path.replace(' ', separator)
    }

    override fun getLastName(): String = when {
        group != null -> subname!! //If a group exist, a subname exists, otherwise it's a bug.
        subname != null -> subname
        else -> name
    }

    override fun getNameAt(i: Int): String? = when (i) {
        0 -> name
        1 ->
            group ?: // /name group subname
            subname // /name subname
        2 -> subname
        else -> throw IllegalArgumentException("Invalid name count: $i")
    }

    override fun startsWith(o: CommandPath): Boolean {
        if (o.nameCount > nameCount) return false
        for (i in 0 until min(nameCount, o.nameCount)) {
            if (o.getNameAt(i) != getNameAt(i)) {
                return false
            }
        }
        return true
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as CommandPathImpl

        if (path != other.path) return false

        return true
    }

    override fun hashCode(): Int {
        return path.hashCode()
    }

    override fun toString(): String {
        return path
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy