Update modding docs
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
# Entities
|
||||
|
||||
Entities are the things in the world that aren't blocks and move on their own: animals, dropped items, the fishing hook,
|
||||
and players. Mods define new kinds in their `entities/` folder, and draw them with a model from `models/`.
|
||||
|
||||
## Defining an Entity
|
||||
|
||||
`entities/chicken.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": 1,
|
||||
"entity": {
|
||||
"id": "bworld:chicken",
|
||||
"model": "bworld:chicken",
|
||||
"texture": "bworld:chicken",
|
||||
"width": 0.3,
|
||||
"height": 0.4,
|
||||
"max_health": 4,
|
||||
"movement_speed": 0.25
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Default | Meaning |
|
||||
| ----------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
|
||||
| `id` | required | The entity's id. |
|
||||
| `texture` | required | Any texture, stretched over the model's `texture_width` and `texture_height`. |
|
||||
| `model` | none | An [entity model](#entity-models). Without one it's drawn as its texture, facing the camera. |
|
||||
| `behavior` | `"animal"` | `"animal"` or `"scripted"`, see below. |
|
||||
| `width`, `height` | required | Its collision box in blocks, up to 16. `width` is for both x and z. Its eyes are at 85% of the height. |
|
||||
| `max_health` | `10` | In half hearts. |
|
||||
| `movement_speed` | `0.25` | Minecraft's per tick speed, 0.25 for most animals. |
|
||||
|
||||
`/summon <entity>` spawns one where you stand.
|
||||
|
||||
## Behaviors
|
||||
|
||||
**Animals** (the default) are mobs: they wander around, panic when hurt, look at players now and then, swim up in water,
|
||||
and never despawn. They're saved with the world, and players can't place solid blocks where one is standing.
|
||||
|
||||
**Scripted** entities stay exactly where a script puts them. They don't fall, think or get hurt, and aren't saved with
|
||||
the world, so a script never finds one it doesn't know about after a restart. The fishing hook is one:
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": 1,
|
||||
"entity": {
|
||||
"id": "bworld:fishing_hook",
|
||||
"texture": "bworld:fishing_hook",
|
||||
"behavior": "scripted",
|
||||
"width": 0.25,
|
||||
"height": 0.25
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Spawning From Scripts
|
||||
|
||||
```ts
|
||||
const hook = ctx.world.spawn_entity("bworld:fishing_hook", x, y, z); // feet at x, y, z
|
||||
hook.teleport(x, y + 0.1, z);
|
||||
if (!hook.removed) hook.remove();
|
||||
```
|
||||
|
||||
`spawn_entity` throws for a kind that doesn't exist, and returns an `EntityRef`:
|
||||
|
||||
```ts
|
||||
interface EntityRef {
|
||||
readonly id: string; // this entity's own id
|
||||
readonly type: string; // its kind, like "bworld:fishing_hook"
|
||||
readonly position: { x: number; y: number; z: number };
|
||||
readonly removed: boolean;
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
remove(): void;
|
||||
}
|
||||
```
|
||||
|
||||
Once an entity is removed, by a script or anything else, it stays removed, and teleporting it does nothing. Check
|
||||
`removed` before relying on one you kept from earlier.
|
||||
|
||||
## Entity Models
|
||||
|
||||
Entity models are `models/<name>.geo.json`, as Blockbench exports them for Bedrock (the "Bedrock Entity" format), and
|
||||
get the id `<mod id>:<name>`. From the file's first geometry, bworld uses:
|
||||
|
||||
- the description's `texture_width` and `texture_height`,
|
||||
- each bone's `name`, `parent`, `pivot` and `rotation`,
|
||||
- each cube's `origin`, `size`, `uv` and `inflate`.
|
||||
|
||||
Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to
|
||||
a block with its feet at 0, and shows the way Blockbench shows it.
|
||||
|
||||
Models don't animate yet. The whole model turns with the entity, and a bone named `head` turns to where it looks.
|
||||
|
||||
Without a model, the whole texture is drawn facing the camera, 16 pixels to a block, centered on the middle of the
|
||||
entity's box.
|
||||
@@ -0,0 +1,64 @@
|
||||
# Players
|
||||
|
||||
Server scripts see players as `Player` objects: in events, in component handlers, from `ctx.players`, and as the source
|
||||
of a command.
|
||||
|
||||
## Finding Players
|
||||
|
||||
```ts
|
||||
ctx.players.all(); // everyone online
|
||||
ctx.players.get(id); // by id, undefined when they're offline
|
||||
ctx.players.by_name("alice");
|
||||
```
|
||||
|
||||
A `Player` is only valid while they're online. Keep their `id` or `name`, not the object, and look them up again later.
|
||||
|
||||
## The Player API
|
||||
|
||||
```ts
|
||||
interface Player {
|
||||
readonly id: string;
|
||||
readonly name: string;
|
||||
readonly position: { x: number; y: number; z: number }; // their feet
|
||||
readonly inventory: Container; // 36 slots, the hotbar is 0-8
|
||||
readonly selected_slot: number;
|
||||
readonly held_item: ItemStack | undefined;
|
||||
|
||||
give_item(id: string, count?: number, data?: unknown): void; // what doesn't fit drops at their feet
|
||||
send_message(text: string): void;
|
||||
teleport(x: number, y: number, z: number): void;
|
||||
|
||||
readonly health: number; // out of max_health
|
||||
readonly max_health: number; // 20
|
||||
readonly food: number; // hunger, out of 20
|
||||
readonly saturation: number; // never above food
|
||||
|
||||
damage(amount: number, cause?: string): void;
|
||||
heal(amount: number): void;
|
||||
feed(nutrition: number, saturation_modifier: number): void;
|
||||
add_exhaustion(amount: number): void;
|
||||
}
|
||||
```
|
||||
|
||||
`inventory` is a live [container](../inventories/containers.md), and `held_item` a live stack: changing them changes
|
||||
what the player has.
|
||||
|
||||
## Health and Hunger
|
||||
|
||||
Players follow Minecraft's rules, with 20 health (10 hearts) and 20 hunger:
|
||||
|
||||
- **Exhaustion** comes from sprinting, swimming, jumping, breaking blocks, taking damage and healing. Every 4 costs a
|
||||
point of saturation, or of hunger once saturation is gone.
|
||||
- **Healing** happens on a full stomach: quickly while there's saturation, slowly with at least 18 hunger.
|
||||
- **Starving**: at 0 hunger they lose health every 4 seconds, down to half a heart.
|
||||
- **Falling** hurts 1 per block after the first 3, unless they land in water.
|
||||
- **The void**: below the world, they keep taking damage until they die.
|
||||
|
||||
`damage(amount, cause)` hurts them like anything else would. `cause` shows in the `player_damage` and `player_death`
|
||||
[events](../concepts/events.md) and in the death message. Before events can change the amount or cancel it.
|
||||
|
||||
## Death
|
||||
|
||||
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
|
||||
[Tools](../items/tools.md#durability)). They respawn at spawn right away with full health and hunger, and `player_death`
|
||||
fires.
|
||||
Reference in New Issue
Block a user