Improve commands
This commit is contained in:
@@ -382,7 +382,7 @@ and `items/cabbage_seeds.json` with `"places": "farmersdelight:cabbages"`.
|
||||
| --------------- | ------------------------------------------------------------------------------------------------------------ |
|
||||
| `days` | Watered mornings from planting to fully grown. |
|
||||
| `stages` | How many ages it shows, 0 to `stages - 1`, spread evenly over `days`. The `age` state must fit them. |
|
||||
| `drops` | Like a crop's loot table: `young` (not grown yet), `mature` (grown), and `bonus`, 0 to 3 more of an item when grown, like wheat's extra seeds. `young` and `mature` can be one id or a list. |
|
||||
| `drops` | Like a crop's loot table: `young` (not grown yet), `mature` (grown), and `bonus`, 1 to 4 of an item when grown, like wheat's seeds (Minecraft's 1 plus 3 tries at 57%). `young` and `mature` can be one id or a list. |
|
||||
| `fertilizer` | An item that grows it a day right away, like bone meal. |
|
||||
| `regrow_days`, `harvest` | Picked by right clicking once grown, dropping `harvest.item` (`count` is `[min, max]`), then grown again after `regrow_days`, like tomatoes. |
|
||||
| `water` | `"farmland"` (the default): needs watered farmland under it. `"always"`: grows every morning, like rice in water. |
|
||||
@@ -790,6 +790,55 @@ as the clients. Blocks in unloaded chunks can't be read or changed, and don't ti
|
||||
|
||||
### Commands
|
||||
|
||||
Commands work like Fabric's, which are Minecraft's Brigadier: a tree of literal words and typed arguments. Each
|
||||
argument type reads its text and suggests what could go there, and players see those suggestions above the chat box as
|
||||
they type (Tab puts one in, Up and Down pick). The tree is in `common/commands.ts`.
|
||||
|
||||
```ts
|
||||
const { literal, argument, types } = ctx.commands;
|
||||
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"))),
|
||||
),
|
||||
),
|
||||
);
|
||||
```
|
||||
|
||||
| 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`, then `context.get(name)` in `executes`. |
|
||||
| `.then(node)` | What can come next. |
|
||||
| `.executes(fn)` | Runs when the command ends here. Without it, ending here says what's missing. |
|
||||
| `.requires(fn)` | Only for sources it's 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 it and `send_message(text)` to answer them. Throwing
|
||||
`new CommandError(message)` tells the player what's wrong. Any other error is logged with the mod's id and the player
|
||||
is told something went wrong.
|
||||
|
||||
| `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()` and `block()` | an id. `carrot` works too while only one mod has a `carrot` | ids, with the name as tooltip |
|
||||
| `player()` | an online player's name, as a `Player` | online players |
|
||||
|
||||
The simple form is still there, it's a command taking the rest of the line split on spaces:
|
||||
|
||||
```ts
|
||||
ctx.commands.register("heal", {
|
||||
description: "Heal yourself",
|
||||
@@ -800,8 +849,18 @@ ctx.commands.register("heal", {
|
||||
});
|
||||
```
|
||||
|
||||
Commands run on the server when a player types `/name` in chat. Two mods using the same name, or a mod using one of the
|
||||
engine's (`give`, `time` and `tps`, until phase 5 moves `give` and `time` into `mods/bworld`), is a load error. `/copper_tools:heal` always works as the unambiguous form.
|
||||
Commands run on the server when a player types `/name` in chat, and the client asks the server for suggestions. Two
|
||||
mods using the same name, or a mod using one of the engine's (`give`, `time`, `tps` and `help`), is a load error.
|
||||
`/copper_tools:heal` always works as the unambiguous form.
|
||||
|
||||
The engine's commands:
|
||||
|
||||
| Command | What it does |
|
||||
| --------------------------------------------------------- | ----------------------------------------------------------------------- |
|
||||
| `/give <item> [<count>]` | Gives you an item |
|
||||
| `/time`, `/time set <day\|noon\|sunset\|night\|midnight\|ticks>`, `/time add <ticks>` | Shows or changes the time. Times of day are the next one, time never goes back |
|
||||
| `/tps` | How the game loop keeps up |
|
||||
| `/help [<command>]` | Every command's usage, or one's |
|
||||
|
||||
## Client scripts
|
||||
|
||||
|
||||
Reference in New Issue
Block a user