Files
bworld/docs/modding/entities/players.md
T
2026-10-01 16:58:30 -03:00

4.5 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 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, 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;
}

inventory is a live container, 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.
  • 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.
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.

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.