summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/command/ParcelCommandBuilder.kt
blob: 1eddf979a7d396a41e5c05bead00539e437bd66f (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
72
73
74
75
76
77
78
79
80
81
82
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.Interactables
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(CommandsPrivilegesLocal(plugin))

            group("option", "opt", "o") {
                setGroupDescription(
                    "changes interaction options for this parcel",
                    "Sets whether players who are not allowed to",
                    "build here can interact with certain things."
                )

                group("interact", "i") {
                    val command = ParcelOptionsInteractCommand(plugin.parcelProvider)
                    Interactables.classesById.forEach {
                        addSubCommand(it.name, command)
                    }
                }
            }

            group("global", "g") {
                registerCommands(CommandsPrivilegesGlobal(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)
        }
    }
}