Update modding docs
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
# 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 Fabric'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<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 | |
|
||||
| `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 `/<mod id>:<name>`, like `/copper_tools:heal`.
|
||||
|
||||
Commands can only be registered during `setup`.
|
||||
|
||||
## The Engine's Commands
|
||||
|
||||
| Command | What it does |
|
||||
| ------------------------------------------------------- | -------------------------------------------------------- |
|
||||
| `/give <item> [count]` | Gives you an item |
|
||||
| `/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 |
|
||||
| `/tps` | Shows how the game loop keeps up |
|
||||
| `/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 |
|
||||
@@ -0,0 +1,41 @@
|
||||
# Time
|
||||
|
||||
The server runs at a fixed 20 ticks a second, and everything in the game is measured in ticks: cooking times, burn
|
||||
times, timers and the time of day.
|
||||
|
||||
## Timers
|
||||
|
||||
`ctx.system` runs code later, counted in ticks:
|
||||
|
||||
```ts
|
||||
const handle = ctx.system.run_timeout(() => player.send_message("Five seconds later"), 5 * 20);
|
||||
const every_minute = ctx.system.run_interval(() => save_stats(), 60 * 20);
|
||||
ctx.system.clear_run(every_minute);
|
||||
|
||||
ctx.system.current_tick; // ticks since the server started
|
||||
```
|
||||
|
||||
`run_timeout(fn, 0)` runs on the next tick, which is how a [before event](../concepts/events.md) gets something done
|
||||
after it. Timers aren't saved: they're gone after a restart, so set them up again in `setup`.
|
||||
|
||||
For work every tick, subscribe to the `tick` event. For work on a block, use a
|
||||
[component](../blocks/components.md#ticking)'s `on_tick` or `on_second`.
|
||||
|
||||
## World Time
|
||||
|
||||
`ctx.world.time` counts ticks since 06:00 of the world's first day, and never wraps around:
|
||||
|
||||
- A **day**, 06:00 to 18:00, is 36000 ticks: 30 minutes.
|
||||
- A **night**, 18:00 to 06:00, is 12000 ticks: 10 minutes. So in game hours go by faster at night.
|
||||
- Dusk is the first minute of the night and dawn the last, so it's bright all day.
|
||||
|
||||
`ctx.world.day` is the number of days since the world started, 0 on the first day, going up at 06:00. Every morning at
|
||||
06:00, blocks' `on_new_day` runs, then the `new_day` event fires. That's the clock crops grow on.
|
||||
|
||||
`ctx.world.set_time(ticks)` changes the time for everyone. Moving it forward past mornings runs each of them, up to the
|
||||
last 64. Moving it back doesn't repeat mornings.
|
||||
|
||||
`common/time.ts` has `clock`, `format_clock`, `is_day` and `daylight` for turning ticks into something readable. Client
|
||||
scripts can read the time as `ctx.world.time`.
|
||||
|
||||
Players can change it with `/time set <day|noon|sunset|night|midnight|ticks>` and `/time add <ticks>`.
|
||||
Reference in New Issue
Block a user