Player api

This commit is contained in:
2026-10-13 22:59:48 -03:00
parent b34e97e487
commit a10b15a3fd
22 changed files with 581 additions and 89 deletions
+3 -2
View File
@@ -16,14 +16,15 @@ export interface ClientContext {
// the way jei's recipes screen checks options.keyInventory
options: KeyMappings;
net: ClientNet;
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming. biome and season are where
// their feet are, undefined until that chunk loads
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming. is_sneaking: holding the sneak
// key, like isShiftKeyDown. biome and season are where their feet are, undefined until that chunk loads
player: {
readonly name: string;
readonly position: Readonly<Position>;
readonly biome: Id | undefined;
readonly season: Season | undefined;
is_swimming(): boolean;
is_sneaking(): boolean;
};
// read only, what this client sees
world: {
+24 -1
View File
@@ -68,7 +68,21 @@ export interface BlockComponent<P = any> {
export interface ItemComponent<P = any> {
on_create?(item: ItemStack, params: P): void;
get_lore?(item: ItemStack, params: P): string;
on_use?(item: ItemStack, params: P, player: Player): void;
// right clicked while holding it, at nothing or at the block in target, like minecraft's use and useOn
on_use?(item: ItemStack, params: P, player: Player, target: UseTarget | undefined): void;
// using it over time, see Player.start_using. every tick while they do, ticks counting from 1, like onUseTick
on_use_tick?(item: ItemStack, params: P, player: Player, ticks: number): void;
// they let go of use before it finished, like a bow being shot, after using it for ticks. like releaseUsing
on_release_using?(item: ItemStack, params: P, player: Player, ticks: number): void;
// they used it for as long as start_using said, like food being eaten. like finishUsingItem
on_finish_using?(item: ItemStack, params: P, player: Player): void;
}
// the block an item was used on, the face that was clicked, and where on the block (0 to 1 on each axis)
export interface UseTarget {
readonly block: BlockRef;
readonly face: Face;
readonly hit: readonly [number, number, number];
}
// P is the component's params from the effect json. see docs/modding/entities/effects.md
@@ -328,6 +342,15 @@ export interface Player {
remove_effect(id: Id): boolean;
// lying flat in water in the 0.8 tall box, like minecraft's isSwimming. their client says when
is_swimming(): boolean;
// holding the sneak key, flying or not, like minecraft's isShiftKeyDown. their client says when
is_sneaking(): boolean;
// keeps using the held item while they hold use, like minecraft's startUsingItem: its components' on_use_tick runs
// every tick, then on_finish_using after ticks, or on_release_using if they let go first. -1 uses it until they let
// go, like a bow. it slows them down like sneaking. for an on_use to call. switching away from it stops it
start_using(ticks: number): void;
// stops using it without on_release_using or on_finish_using, like stopUsingItem
stop_using(): void;
is_using(): boolean;
}
export interface EffectOptions {
+6 -2
View File
@@ -93,19 +93,23 @@ export interface SharedBlock extends Position {
}
// a player placing a block at x y z by clicking face of the block next to it, at hit (0 to 1 on each axis of that
// block), like minecraft's BlockPlaceContext. facing is the way the player looks, along the ground
// block), like minecraft's BlockPlaceContext. facing is the way the player looks, along the ground, and sneaking
// whether they hold the sneak key, for blocks placed another way round when they do
export interface BlockPlacement extends Position {
readonly face: Face;
readonly hit: readonly [number, number, number];
readonly facing: "north" | "east" | "south" | "west";
readonly sneaking: boolean;
}
// a player right clicking face of a block, at hit (0 to 1 on each axis of the block), like minecraft's BlockHitResult.
// facing is the way the player looks, along the ground
// facing is the way the player looks, along the ground, and sneaking whether they hold the sneak key. sneaking with
// an item in hand doesn't interact at all, the item is used or placed instead
export interface BlockInteraction {
readonly face: Face;
readonly hit: readonly [number, number, number];
readonly facing: "north" | "east" | "south" | "west";
readonly sneaking: boolean;
}
// P is the component's params from the block json
+11 -4
View File
@@ -10,7 +10,7 @@ export const AIR_ID = "bworld:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 20;
export const PROTOCOL_VERSION = 21;
export interface PlayerInfo {
id: string;
@@ -95,7 +95,8 @@ export type ClientMessage =
// answers chunk_batch_finished once the batch is loaded, with how many chunks a tick the client can take
| { type: "chunk_batch_received"; chunks_per_tick: number }
// teleport is the id of the last teleport the client heard of. moves from before the latest one are ignored.
// swimming is whether they're swimming, sent right away when it changes
// swimming is whether they're swimming and sneaking whether they hold the sneak key, both sent right away when they
// change
| {
type: "move";
x: number;
@@ -105,6 +106,7 @@ export type ClientMessage =
pitch: number;
teleport?: number;
swimming?: boolean;
sneaking?: boolean;
}
// seq numbers what this client guessed for it, the server answers with block_ack. see client/level/block_predictions.ts
| { type: "break_block"; x: number; y: number; z: number; seq?: number }
@@ -116,6 +118,8 @@ export type ClientMessage =
| { type: "swing" }
// right click without looking at a block, for items' on_use
| { type: "use_item" }
// the use button let go of after a use_item or use_block, which stops using an item over time, like shooting a bow
| { type: "release_use" }
// right click on a block: interact with it, or place the held block against `face`. hit is where on the block it
// clicked, 0 to 1 on each axis, like minecraft's click location. seq is like break_block's
| { type: "use_block"; x: number; y: number; z: number; face: Faces; hit?: [number, number, number]; seq?: number }
@@ -249,8 +253,11 @@ export type ServerMessage =
| { type: "suggestions"; id: number; start: number; suggestions: { text: string; tooltip?: string }[] }
// the player's own health, max health, hunger, saturation and air (out of 20, 2 a bubble), when they change
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number }
// the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair
| { type: "attack_speed"; value: number }
// the player's own attributes their client needs, when they change: attack speed for the attack indicator under the
// crosshair, and how far they reach blocks and entities
| { type: "attributes"; attack_speed: number; block_interaction_range: number; entity_interaction_range: number }
// whether they're using their held item over time, like drawing a bow, see ctx's player.start_using
| { type: "using_item"; using: boolean }
// whether the player flies, see /fly, when it changes
| { type: "flying"; flying: boolean }
// the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the