aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt
blob: 335197098118d3c90d8b489f12709e176992b424 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.features.chat

import com.mojang.brigadier.arguments.StringArgumentType.string
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.get
import moe.nea.firmament.commands.suggestsList
import moe.nea.firmament.commands.thenArgument
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.MaskCommands
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.repo.RepoManager
import moe.nea.firmament.util.MC

object AutoCompletions : FirmamentFeature {

    object TConfig : ManagedConfig(identifier) {
        val provideWarpTabCompletion by toggle("warp-complete") { true }
        val replaceWarpIsByWarpIsland by toggle("warp-is") { true }
    }

    override val config: ManagedConfig?
        get() = TConfig
    override val identifier: String
        get() = "auto-completions"

    @Subscribe
    fun onMaskCommands(event: MaskCommands) {
        if (TConfig.provideWarpTabCompletion) {
            event.mask("warp")
        }
    }

    @Subscribe
    fun onCommandEvent(event: CommandEvent) {
        if (!TConfig.provideWarpTabCompletion) return
        event.deleteCommand("warp")
        event.register("warp") {
            thenArgument("to", string()) { toArg ->
                suggestsList {
                    RepoManager.neuRepo.constants?.islands?.warps?.flatMap { listOf(it.warp) + it.aliases } ?: listOf()
                }
                thenExecute {
                    val warpName = get(toArg)
                    if (warpName == "is" && TConfig.replaceWarpIsByWarpIsland) {
                        MC.sendServerCommand("warp island")
                    } else {
                        MC.sendServerCommand("warp $warpName")
                    }
                }
            }
        }
    }
}