{"id":26701869,"url":"https://github.com/nickacpt/nbrigadier","last_synced_at":"2025-06-16T05:11:22.660Z","repository":{"id":55472827,"uuid":"324110659","full_name":"NickAcPT/NBrigadier","owner":"NickAcPT","description":"NBrigadier is a command parser \u0026 dispatcher, designed and developed for Minecraft: Java Edition, ported for C#.","archived":false,"fork":false,"pushed_at":"2021-10-25T09:42:11.000Z","size":311,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-14T22:11:29.829Z","etag":null,"topics":["brigadier","command","csharp","csharp-library","dispatcher","minecraft","nbrigadier","parser","port"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NickAcPT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-24T08:51:21.000Z","updated_at":"2024-12-19T08:37:48.000Z","dependencies_parsed_at":"2022-08-15T01:10:26.417Z","dependency_job_id":null,"html_url":"https://github.com/NickAcPT/NBrigadier","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NickAcPT/NBrigadier","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickAcPT%2FNBrigadier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickAcPT%2FNBrigadier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickAcPT%2FNBrigadier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickAcPT%2FNBrigadier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NickAcPT","download_url":"https://codeload.github.com/NickAcPT/NBrigadier/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickAcPT%2FNBrigadier/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260103190,"owners_count":22959052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brigadier","command","csharp","csharp-library","dispatcher","minecraft","nbrigadier","parser","port"],"created_at":"2025-03-27T02:19:54.737Z","updated_at":"2025-06-16T05:11:22.644Z","avatar_url":"https://github.com/NickAcPT.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NBrigadier [![License](https://img.shields.io/github/license/NickAcPT/NBrigadier.svg)](https://github.com/NickAcPT/NBrigadier/blob/master/LICENSE)\n\nNBrigadier is a command parser \u0026 dispatcher, designed and developed for Minecraft: Java Edition, ported to C# and now freely available for use elsewhere under the MIT license.\n\nCode ported originally from https://github.com/Mojang/brigadier.\n\n# Installation\nNBrigadier is available to NuGet. Its package id is `NBrigadier`\n\n# Usage\nAt the heart of NBrigadier, you need a `CommandDispatcher\u003cS\u003e`, where `\u003cS\u003e` is any custom object you choose to identify a \"command source\".\n\nA command dispatcher holds a \"command tree\", which is a series of `CommandNode`s that represent the various possible syntax options that form a valid command.\n\n## Registering a new command\nBefore we can start parsing and dispatching commands, we need to build up our command tree. Every registration is an append operation,\nso you can freely extend existing commands in a project without needing access to the source code that created them.\n\nCommand registration also encourages the use of a builder pattern to keep code cruft to a minimum.\n\nA \"command\" is a fairly loose term, but typically it means an exit point of the command tree.\nEvery node can have an `executes` function attached to it, which signifies that if the input stops here then this function will be called with the context so far.\n\nConsider the following example:\n```csharp\nvar dispatcher = new CommandDispatcher\u003cCommandSourceStack\u003e();\n\ndispatcher.Register(\n    LiteralArgumentBuilder\u003cCommandSourceStack\u003e.LiteralBuilder(\"foo\")\n        .Then(\n            RequiredArgumentBuilder\u003cCommandSourceStack, int\u003e.Argument(\"bar\", IntegerArgumentType.Integer())\n                .Executes(c =\u003e\n                {\n                    Console.WriteLine(\"Bar is \" + IntegerArgumentType.GetInteger(c, \"bar\"));\n                    return 1;\n                })\n        )\n        .Executes(c =\u003e\n        {\n            Console.WriteLine(\"Called foo with no arguments\");\n            return 1;\n        })\n);\n``` \n\nThis snippet registers two \"commands\": `foo` and `foo \u003cbar\u003e`. It is also common to refer to the `\u003cbar\u003e` as a \"subcommand\" of `foo`, as it's a child node.\n\nAt the start of the tree is a \"root node\", and it **must** have `LiteralCommandNode`s as children. Here, we register one command under the root: `literal(\"foo\")`, which means \"the user must type the literal string 'foo'\".\n\nUnder that is two extra definitions: a child node for possible further evaluation, or an `executes` block if the user input stops here.\n\nThe child node works exactly the same way, but is no longer limited to literals. The other type of node that is now allowed is an `ArgumentCommandNode`, which takes in a name and an argument type.\n\nArguments can be anything, and you are encouraged to build your own for seamless integration into your own product. There are some standard arguments included in nbrigadier, such as `IntegerArgumentType`.\n\nArgument types will be asked to parse input as much as they can, and then store the \"result\" of that argument however they see fit or throw a relevant error if they can't parse.\n\nFor example, an integer argument would parse \"123\" and store it as `123` (`int`), but throw an error if the input were `onetwothree`.\n\nWhen a command is actually run, it can access these arguments in the context provided to the registered function.\n\n## Parsing user input\nSo, we've registered some commands and now we're ready to take in user input. If you're in a rush, you can just call `dispatcher.execute(\"foo 123\", source)` and call it a day.\n\nThe result of `execute` is an integer that was returned from an evaluated command. The meaning of this integer depends on the command, and will typically not be useful to programmers.\n\nThe `source` is an object of `\u003cS\u003e`, your own custom class to track users/players/etc. It will be provided to the command so that it has some context on what's happening.\n\nIf the command failed or could not parse, some form of `CommandSyntaxException` will be thrown. It is also possible for a `RuntimeException` to be bubbled up, if not properly handled in a command.\n\nIf you wish to have more control over the parsing \u0026 executing of commands, or wish to cache the parse results so you can execute it multiple times, you can split it up into two steps:\n\n```csharp\nvar parse = dispatcher.Parse(\"foo 123\", source);\nvar result = dispatcher.Execute(parse);\n``` \n\nThis is highly recommended as the parse step is the most expensive, and may be easily cached depending on your application.\n\nYou can also use this to do further introspection on a command, before (or without) actually running it.\n\n## Inspecting a command\nIf you `parse` some input, you can find out what it will perform (if anything) and provide hints to the user safely and immediately.\n\nThe parse will never fail, and the `ParseResults\u003cS\u003e` it returns will contain a *possible* context that a command may be called with\n(and from that, you can inspect which nodes the user entered, complete with start/end positions in the input string).\nIt also contains a map of parse exceptions for each command node it encountered. If it couldn't build a valid context, then\nthe reason why is inside this exception map.\n\n## Displaying usage info\nThere are two forms of \"usage strings\" provided by this library, both require a target node.\n\n`GetAllUsage(node, source, restricted)`  will return a list of all possible commands (executable end-points) under the target node and their human readable path. If `restricted`, it will ignore commands that `source` does not have access to. This will look like [`foo`, `foo \u003cbar\u003e`]\n\n`GetSmartUsage(node, source)` will return a map of the child nodes to their \"smart usage\" human readable path. This tries to squash future-nodes together and show optional \u0026 typed information, and can look like `foo (\u003cbar\u003e)`\n\n[![GitHub forks](https://img.shields.io/github/forks/NickAcPT/NBrigadier.svg?style=social\u0026label=Fork)](https://github.com/NickAcPT/NBrigadier/fork) [![GitHub stars](https://img.shields.io/github/stars/NickAcPT/NBrigadier.svg?style=social\u0026label=Stars)](https://github.com/NickAcPT/NBrigadier/stargazers)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickacpt%2Fnbrigadier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickacpt%2Fnbrigadier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickacpt%2Fnbrigadier/lists"}