aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/brigadier/NEUBrigadierHook.kt
blob: fc4fd39c13b0a484ef9f5faa579b50c74cba8cae (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2023 Linnea Gräf
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.util.brigadier

import com.mojang.brigadier.ParseResults
import com.mojang.brigadier.exceptions.CommandSyntaxException
import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.tree.CommandNode
import net.minecraft.command.CommandBase
import net.minecraft.command.ICommandSender
import net.minecraft.util.BlockPos
import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting.YELLOW
import java.util.concurrent.CompletableFuture
import java.util.function.Predicate

/**
 * Hook for converting brigadier commands to normal legacy Minecraft commands (string array style).
 */
class NEUBrigadierHook(
    val brigadierRoot: BrigadierRoot,
    val commandNode: CommandNode<DefaultSource>,
    val aliases: List<String>
) : CommandBase() {
    /**
     * Runs before the command gets executed. Return false to prevent execution.
     */
    var beforeCommand: Predicate<ParseResults<DefaultSource>>? = null

    override fun getCommandName(): String {
        return commandNode.name
    }

    override fun getCommandAliases(): List<String> {
        return aliases
    }

    data class Usage(
        val path: String,
        val help: String?,
    )


    override fun getCommandUsage(sender: ICommandSender): String {
        return brigadierRoot.getAllUsages("/$commandName", commandNode, mutableSetOf()).joinToString("\n") { "${it.path} - ${it.help ?: "Missing help"}"}
    }

    private fun getText(args: Array<out String>) = "${commandNode.name} ${args.joinToString(" ")}"

    override fun processCommand(sender: ICommandSender, args: Array<out String>) {
        val results = brigadierRoot.parseText.apply(sender to getText(args).trim())
        if (beforeCommand?.test(results) == false)
            return
        try {
            brigadierRoot.dispatcher.execute(results)
        } catch (syntax: CommandSyntaxException) {
            brigadierRoot.getAllUsages("/$commandName", commandNode, mutableSetOf()).forEach {
                sender.addChatMessage(ChatComponentText("${YELLOW}[NEU] ${it.path} - ${it.help}"))
            }
        }
    }

    // We love async tab completion (may end up requiring pressing tab multiple times, but uhhhhh .get() bad)
    private var lastCompletionText: String? = null
    private var lastCompletion: CompletableFuture<Suggestions>? = null
    override fun addTabCompletionOptions(
        sender: ICommandSender,
        args: Array<out String>,
        pos: BlockPos
    ): List<String> {
        val originalText = getText(args)
        var lc: CompletableFuture<Suggestions>? = null
        if (lastCompletionText == originalText) {
            lc = lastCompletion
        }
        if (lc == null) {
            lastCompletion?.cancel(true)
            val results = brigadierRoot.parseText.apply(sender to originalText)
            lc = brigadierRoot.dispatcher.getCompletionSuggestions(results)
        }
        lastCompletion = lc
        lastCompletionText = originalText
        val suggestions = lastCompletion?.getNow(null) ?: return emptyList()
        return suggestions.list.map { it.text }
    }

    override fun canCommandSenderUseCommand(sender: ICommandSender): Boolean {
        return true // Permissions are checked by brigadier instead (or by the beforeCommand hook)
    }

}