diff options
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/io/github/moulberry/notenoughupdates/util/brigadier/BrigadierRoot.kt | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/brigadier/BrigadierRoot.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/brigadier/BrigadierRoot.kt index 0010f1a6..f11738fc 100644 --- a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/brigadier/BrigadierRoot.kt +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/brigadier/BrigadierRoot.kt @@ -21,14 +21,18 @@ package io.github.moulberry.notenoughupdates.util.brigadier import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.ParseResults +import com.mojang.brigadier.tree.ArgumentCommandNode +import com.mojang.brigadier.tree.CommandNode import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent import io.github.moulberry.notenoughupdates.util.LRUCache import net.minecraft.command.ICommandSender import net.minecraftforge.client.ClientCommandHandler +import java.util.* @NEUAutoSubscribe object BrigadierRoot { + private val help: MutableMap<CommandNode<DefaultSource>, String> = IdentityHashMap() var dispatcher = CommandDispatcher<DefaultSource>() private set val parseText = @@ -36,6 +40,34 @@ object BrigadierRoot { dispatcher.parse(text, sender) }, 1) + fun getHelpForNode(node: CommandNode<DefaultSource>): String? { + return help[node] + } + + fun setHelpForNode(node: CommandNode<DefaultSource>, helpText: String) { + help[node] = helpText + } + + + fun getAllUsages( + path: String, + node: CommandNode<ICommandSender>, + visited: MutableSet<CommandNode<ICommandSender>> + ): Sequence<NEUBrigadierHook.Usage> = sequence { + if (node in visited) return@sequence + visited.add(node) + yield(NEUBrigadierHook.Usage(path, getHelpForNode(node))) + node.children.forEach { + val nodeName = when(node) { + is ArgumentCommandNode<*, *> -> "<${node.name}>" + else -> node.name + } + yieldAll(getAllUsages("$path $nodeName", it, visited)) + } + visited.remove(node) + } + + fun updateHooks() = registerHooks(ClientCommandHandler.instance) fun registerHooks(handler: ClientCommandHandler) { @@ -45,6 +77,8 @@ object BrigadierRoot { iterator.remove() } dispatcher = CommandDispatcher() + help.clear() + parseText.clearCache() val event = RegisterBrigadierCommandEvent(this) event.post() event.hooks.forEach { |