Files
bworld/docs/modding/misc/commands.md
T

137 lines
7.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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 phase:
```ts
import type { Player, ServerContext } from "cubi/server";
export function server(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<number>("amount")))
.then(
argument("target", types.player())
.executes((context) => heal(context.get<Player>("target"), context.get<number>("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 | |
| `json()` | a JSON value, the rest of the line like `greedy_string()` | |
| `choice([...])` | one of the given words | them |
| `item()`, `block()`, `fluid()` | 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 `/<mod id>:<name>`, like `/copper_tools:heal`.
Commands can only be registered while the server phase runs.
## The Engine's Commands
| Command | What it does |
| ------------------------------------------------------- | -------------------------------------------------------------------- |
| `/give <item> [count] [data]` | Gives you an item, with `data` as JSON |
| `/give <item> <count> fluid <fluid> [data]` | Gives you a fluid container full of the fluid, with the fluid's data |
| `/effect <effect> [seconds]` | Gives you a status effect, for 30 seconds by default |
| `/summon <entity>` | Spawns an entity where you are |
| `/time` | Shows the time |
| `/time set <day\|noon\|sunset\|night\|midnight\|ticks>` | Jumps to the next time of day. Time never goes back |
| `/time add <ticks>` | Moves time forward |
| `/locatebiome <biome>` | Says where the nearest biome of that kind is, up to 6400 blocks away |
| `/tp <x> <y> <z>` | Teleports you there |
| `/fill <x1> <y1> <z1> <x2> <y2> <z2> <block>` | Fills the box between two corners with a block, `air` to clear it |
| `/fly` | Toggles flying: jump goes up, sneak goes down, sprint goes faster |
| `/creative` | Toggles placing blocks without using them up and instant breaking |
| `/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 |
`/fill` and `/exportschematic` suggest the block you're looking at for each corner.
`/give`'s data is the stack's `data`, and with `fluid` it's the fluid's (see
[Fluids](../resources/fluids.md#containers)):
```
/give bworld:wood_pickaxe 1 {"damage": 5}
/give bworld:glass_bottle 3 fluid bworld:juice {"item": "bworld:strawberry"}
```