# Commands Players run commands by typing `/name` in chat. Commands run on the server, and players see suggestions above the chat box as they type: Tab puts one in, Up and Down pick one. bworld's commands work like NeoForge's, which are Minecraft's Brigadier: a tree of literal words and typed arguments. Each argument type reads its part of the text and suggests what could go there. ## Registering a Command In the server script's `setup`: ```ts import type { Player, ServerContext } from "bworld/server"; export function setup(ctx: ServerContext) { const { literal, argument, types } = ctx.commands; const heal = (player: Player, amount: number) => player.heal(amount); ctx.commands.register( literal("heal") .describe("Heals a player") .requires((source) => source.player.name === ctx.storage.get("owner")) .executes((context) => heal(context.source.player, 20)) .then( argument("amount", types.integer(1, 20)) .executes((context) => heal(context.source.player, context.get("amount"))) .then( argument("target", types.player()) .executes((context) => heal(context.get("target"), context.get("amount"))), ), ), ); } ``` That's `/heal`, `/heal 5` and `/heal 5 alice`. | Builder | What it does | | ---------------------- | --------------------------------------------------------------------------------------------- | | `literal(name)` | A word that has to be typed as is. Words are tried before arguments. | | `argument(name, type)` | A value read by `type`, which `executes` gets with `context.get(name)`. | | `.then(node)` | What can come next. | | `.executes(fn)` | Runs when the command ends here. Without it, ending here tells the player what's missing. | | `.requires(fn)` | Only for sources it returns `true` for. Others can't run it and don't see it suggested. | | `.suggests(fn)` | `(partial, source) => [{ text, tooltip? }]`, in place of the argument type's own suggestions. | | `.describe(text)` | Shown by `/help`. | `context.source` has the `player` who typed the command and `send_message(text)` to answer them. ## Argument Types | `types.` | Reads | Suggests | | --------------------- | --------------------------------------------------------------- | -------------- | | `integer(min?, max?)` | a whole number | | | `number(min?, max?)` | any number | | | `boolean()` | `true` or `false` | both | | `word()` | one word | | | `greedy_string()` | the rest of the line, spaces and all. Nothing can come after it | | | `choice([...])` | one of the given words | them | | `item()`, `block()` | an id. `carrot` works too, while only one mod has a `carrot` | ids | | `player()` | an online player's name, as a `Player` | online players | ## Errors To tell the player what's wrong, answer them and return: ```ts .executes((context) => { const player = context.source.player; if (ctx.items.tool(player.held_item?.id ?? "") !== "pickaxe") { context.source.send_message("You need to be holding a pickaxe"); return; } // ... }) ``` An error thrown from a mod's command is logged with the mod's id, and the player is told something went wrong. > [!NOTE] > The engine's own commands throw a `CommandError` to show the player a message, but mods can't yet: the API doesn't > export the class, and a copy bundled into a mod isn't recognized. ## The Simple Form A command can also take the rest of the line, split on spaces: ```ts ctx.commands.register("hello", { description: "Says hello", usage: "/hello [name]", run(args, player) { player.send_message(`Hello ${args[0] ?? player.name}!`); }, }); ``` ## Names Command names are `a-z`, `0-9` and `_`. Two mods registering the same name, or a mod registering one of the engine's, stops the server from starting. Every mod command can also be run as `/:`, like `/copper_tools:heal`. Commands can only be registered during `setup`. ## The Engine's Commands | Command | What it does | | ------------------------------------------------------- | -------------------------------------------------------------------- | | `/give [count]` | Gives you an item | | `/effect [seconds]` | Gives you a status effect, for 30 seconds by default | | `/summon ` | Spawns an entity where you are | | `/time` | Shows the time | | `/time set ` | Jumps to the next time of day. Time never goes back | | `/time add ` | Moves time forward | | `/locatebiome ` | Says where the nearest biome of that kind is, up to 6400 blocks away | | `/tp ` | Teleports you there | | `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster | | `/tps` | Shows how the game loop keeps up | | `/viewdistance [chunks]` | Shows or changes how far the server sends chunks, until it restarts | | `/skin [url] [slim\|normal]` | Changes your skin to a 64×64 PNG, or back to the default | | `/help [command]` | Every command's usage, or one's |