summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
blob: e2c7a1dd894361f20445bd64c99a254c7eae19d6 (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
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 java.util.LinkedList
import java.util.Queue

@Suppress("UsePropertyAccessSyntax")
fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher {
    //@formatter:off
    return CommandBuilder()
        .setChatController(ParcelsChatController())
        .addParameterType(false, ParcelParameterType(plugin.parcelProvider))
        .addParameterType(true, ParcelTarget.PType(plugin.parcelProvider))

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

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

            .group("global", "g")
                .registerCommands(CommandsAddedStatusGlobal(plugin))
                .parent()

            .group("admin", "a")
                .registerCommands(CommandsAdmin(plugin))
                .parent()

            .putDebugCommands(plugin)

            .parent()
        .generateHelpAndSyntaxCommands()
        .getDispatcher()
    //@formatter:on
}

private fun CommandBuilder.putDebugCommands(plugin: ParcelsPlugin): CommandBuilder {
    //if (!logger.isDebugEnabled) return this
    //@formatter:off
    return group("debug", "d")
        .registerCommands(CommandsDebug(plugin))
        .parent()
    //@formatter:on
}

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)
        }
    }
}