summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
blob: b9dfe1e62a6b311fa48a8250235b56c02dc84b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package io.dico.parcels2.command

import io.dico.dicore.command.CommandBuilder
import io.dico.dicore.command.ICommandAddress
import io.dico.dicore.command.ICommandDispatcher
import io.dico.dicore.command.registration.reflect.ReflectiveRegistration
import io.dico.parcels2.ParcelsPlugin
import io.dico.parcels2.logger
import java.util.LinkedList
import java.util.Queue

@Suppress("UsePropertyAccessSyntax")
fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher =
    with(CommandBuilder()) {
        setChatController(ParcelsChatController())
        addParameterType(false, ParcelParameterType(plugin.parcelProvider))
        addParameterType(false, ProfileParameterType())
        addParameterType(true, ParcelTarget.PType(plugin.parcelProvider))

        group("parcel", "plot", "plots", "p") {
            addRequiredPermission("parcels.command")
            registerCommands(CommandsGeneral(plugin))
            registerCommands(CommandsAddedStatusLocal(plugin))

            group("option", "opt", "o") {
                CommandsParcelOptions.setGroupDescription(this)
                registerCommands(CommandsParcelOptions(plugin))
            }

            group("global", "g") {
                registerCommands(CommandsAddedStatusGlobal(plugin))
            }

            group("admin", "a") {
                registerCommands(CommandsAdmin(plugin))
            }

            if (!logger.isDebugEnabled) return@group

            group("debug", "d") {
                registerCommands(CommandsDebug(plugin))
            }
        }

        generateHelpAndSyntaxCommands()
        getDispatcher()
    }

inline fun CommandBuilder.group(name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) {
    group(name, *aliases)
    config()
    parent()
}

private fun CommandBuilder.generateHelpAndSyntaxCommands(): CommandBuilder {
    generateCommands(dispatcher as ICommandAddress, "help", "syntax")
    return this
}

private fun generateCommands(address: ICommandAddress, vararg names: String) {
    val addresses: Queue<ICommandAddress> = LinkedList()
    addresses.offer(address)

    while (addresses.isNotEmpty()) {
        val cur = addresses.poll()
        addresses.addAll(cur.children.values.distinct())
        if (cur.hasCommand()) {
            ReflectiveRegistration.generateCommands(cur, names)
        }
    }
}