11 KiB
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
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
interface Player {
readonly id: string;
readonly name: string;
readonly position: { x: number; y: number; z: number }; // their feet
readonly eye_position: { x: number; y: number; z: number }; // where they see from, lower while swimming
readonly look_direction: { x: number; y: number; z: number }; // which way they look, 1 long
readonly biome: string; // where their feet are, see ../worldgen/biomes.md#seasons
readonly season: Season; // that biome's
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
exchange_held(result: ItemStack): void; // one of the held item becomes result, see below
send_message(text: string, options?: { overlay?: boolean }): void; // in chat, or above the hotbar with overlay
set_worn(key: string, items: ItemStack[]): void; // what's drawn on them, see Worn Items
teleport(x: number, y: number, z: number): void;
readonly health: number; // out of max_health
readonly max_health: number; // 20, or what the bworld:max_health attribute makes it
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;
attribute(id: string): number;
add_modifier(attribute: string, modifier: AttributeModifier): void;
remove_modifier(attribute: string, id: string): boolean;
add_effect(id: string, duration: number, options?: EffectOptions): void;
effect(id: string): { amplifier: number; duration: number } | undefined;
remove_effect(id: string): boolean;
is_swimming(): boolean; // see Swimming
is_sneaking(): boolean; // see Sneaking
start_using(ticks: number): void; // see Using Items Over Time
stop_using(): void;
is_using(): boolean;
}
inventory is a live container, and held_item a live stack: changing them changes
what the player has.
exchange_held(result) is for an item that turns into another when used, like an empty bucket filling: one of the held
stack goes, and result goes straight into their hand when that was the last one, or into their inventory (at their
feet when it's full) when there are more.
eye_position and look_direction are for aiming. With ctx.world.raycast they find
what a player is looking at, like Minecraft's getPlayerPOVHitResult:
const eye = player.eye_position;
const look = player.look_direction;
const reach = player.attribute("bworld:block_interaction_range");
const to = { x: eye.x + look.x * reach, y: eye.y + look.y * reach, z: eye.z + look.z * reach };
const hit = ctx.world.raycast(eye, to, { fluids: "source_only" });
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.
- Drowning: with their eyes under water they hold their breath for 15 seconds, shown as bubbles above the hunger,
then lose a heart every second. The
bworld:water_breathingeffect stops it. - The void: below the world, they keep taking damage until they die.
- Fighting: other players and mobs can hit them, which also pushes them away.
Players are living entities, like mobs, so damage works the same for both. After being hurt they can't be hurt again for half a second, unless the new hit is harder, and then only by the difference.
A player's client moves them, except while they're knocked back: then the server moves them until they land, so it works even when their game isn't running, like in a background tab.
damage(amount, cause) hurts them like anything else would. cause shows in the player_damage and player_death
events and in the death message. Before events can change the amount or cancel it.
Attributes
A player's stats are attributes, like Minecraft's: a base value that modifiers change without touching it.
| Attribute | Base | Meaning |
|---|---|---|
bworld:attack_damage |
1 | How much their hits do. |
bworld:attack_speed |
4 | Full strength attacks a second. Hitting sooner than that does less damage. |
bworld:block_interaction_range |
6 | How far from their eyes they break and use blocks, in blocks. |
bworld:entity_interaction_range |
3 | How far from their eyes they hit entities, in blocks. |
player.add_modifier("bworld:attack_damage", { id: "my_mod:rage", amount: 2, operation: "add_value" });
player.attribute("bworld:attack_damage"); // 3
player.remove_modifier("bworld:attack_damage", "my_mod:rage");
The value is the base, plus every add_value, plus that times the amount of each add_multiplied_base, then times 1
plus the amount of each add_multiplied_total. A modifier with the same id replaces the old one. Modifiers aren't
saved: a mod that gives them should give them again when the player joins, the way the base game's curios slots do for
worn items.
Like Minecraft, an attack right after the last one, or right after changing the held item, does only a fifth of the
damage, rising to full after 20 / attack_speed ticks. A bar under the crosshair shows it filling up, and the server
sends players their attack_speed whenever it changes so the bar matches.
The two ranges are how far a player reaches, like NeoForge's BLOCK_INTERACTION_RANGE and ENTITY_INTERACTION_RANGE.
Their client aims that far, and the server allows a little more, for where they moved since they aimed. A tool that
reaches further gives a modifier while it's held, like Create's extendo grip:
ctx.events.after.tick.subscribe(() => {
for (const player of ctx.players.all()) {
if (player.held_item?.id === "copper_tools:grabber") {
player.add_modifier("bworld:block_interaction_range", {
id: "copper_tools:grabber",
amount: 3,
operation: "add_value",
});
} else {
player.remove_modifier("bworld:block_interaction_range", "copper_tools:grabber");
}
}
});
Status Effects
Players can have any mod's status effects, like the base game's bworld:speed, bworld:regeneration or
bworld:water_breathing.
player.add_effect("bworld:regeneration", 100, { amplifier: 1 }); // regeneration II for 5 seconds
player.effect("bworld:regeneration"); // { amplifier: 1, duration: 100 }
player.remove_effect("bworld:regeneration");
duration is in ticks, -1 for forever. amplifier is the level minus 1. The options also take ambient,
show_particles and show_icon. Like Minecraft, adding an effect they already have keeps the stronger one, and a
weaker one that lasts longer waits under it, taking over when the stronger one runs out. A player's effects aren't
saved: they're gone when the player leaves, and when they die.
Worn Items
set_worn(key, items) says what a mod shows on a player, like the base game's curios slots do for what's in them. Each
mod uses its own key, like "bworld:curios", and setting a key replaces its list, [] clears it. Only items whose JSON
has worn show.
Everyone is told what each player wears, the player too, and players joining later get it with the others. It isn't saved: set it again when the player joins, the way the curios slots do.
Swimming
Players swim by sprinting with their eyes under water. They keep swimming while they sprint in water, along the surface too, and stop when they let go of sprint or leave the water. While swimming they lie flat in a box 0.8 blocks tall, with their eyes at 0.6, so they fit through 1-block gaps. Under a low ceiling they stay in that box until there's room to stand up.
player.is_swimming() says whether a player is swimming, like NeoForge's isSwimming(). It works on the server and in
client scripts, where ctx.player.is_swimming() is about the player on this client:
if (player.is_swimming()) player.add_effect("bworld:speed", 20);
Sneaking
player.is_sneaking() says whether a player holds the sneak key, like NeoForge's isShiftKeyDown(). It's the key, not
the pose: a player flying down holds it too. Their client tells the server as soon as it changes. In client scripts,
ctx.player.is_sneaking() is about the player on this client.
Sneaking changes what right clicking a block does, like Minecraft: with an item in hand, the block isn't used at all,
and the item is used or placed against it instead. That's how a block is placed against a chest without opening it. With
an empty hand, the block is used as usual, and its handlers can read player.is_sneaking(). Blocks' placement and
interaction rules get sneaking too, see Placing.
Using Items Over Time
Some items are used for as long as the use button is held, like drawing a bow or eating in Minecraft. An item's on_use
starts it with player.start_using(ticks), and the item's components hear how it goes, see
Using Over Time. stop_using() ends it without either ending, and is_using()
says whether they're using one. While they are, they walk at a fifth of their speed and can't sprint.
Pushing
player.push(x, y, z) adds to how fast they're moving, in blocks a second, like Minecraft's Entity.push: a fan
blowing them along does it every tick. Their client moves them, so it's sent there and added to theirs.
Death
When a player dies, everything they carried falls out where they died, and their tools lose durability (see
Tools). They respawn at spawn right away with full health and hunger, and player_death
fires.