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
View File
@@ -46,6 +46,9 @@ export const ATTRIBUTES = {
// blocks a fall can be without hurting
safe_fall_distance: attribute("safe_fall_distance", 3, -1024, 1024),
fall_damage_multiplier: attribute("fall_damage_multiplier", 1, 0, 100),
// how far a player reaches, in blocks from their eyes: blocks they break and use, and entities they hit
block_interaction_range: attribute("block_interaction_range", 6, 0, 64),
entity_interaction_range: attribute("entity_interaction_range", 3, 0, 64),
} satisfies Record<string, AttributeType>;
const BY_ID = new Map<string, AttributeType>(Object.values(ATTRIBUTES).map((type) => [type.id, type]));
+110 -32
View File
@@ -46,7 +46,7 @@ import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
import { physics } from "$/common/physics.ts";
import { EXHAUSTION } from "./vitals.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import type { LootContext } from "$/common/mod_api/server.ts";
import type { LootContext, UseTarget } from "$/common/mod_api/server.ts";
import type { EntityJson } from "$/common/mod_data.ts";
import {
argument,
@@ -85,8 +85,9 @@ import { damage_source, type DamageSource, type SavedEntity } from "./entity/ent
import { effect_type, effect_type_ids, EffectInstance } from "./entity/effects.ts";
import { ATTRIBUTES } from "./entity/attributes.ts";
// how far from a player's eyes a block can be changed, a bit more than the client's reach
const MAX_REACH = 8;
// how much further than their reach attributes the server lets a player use blocks and hit entities, for where they
// moved since their client aimed
const REACH_LEEWAY = 2;
// how far past a player's box items get picked up from, sideways and up or down, like minecraft
const PICKUP_REACH_XZ = 1;
const PICKUP_REACH_Y = 0.5;
@@ -372,6 +373,7 @@ export class GameServer {
this.#new_days();
this.liquids.tick(this.#tick);
this.#tick_vitals();
for (const player of this.#players.values()) this.#tick_using(player);
this.#tick_entities();
this.mods.tick();
@@ -892,6 +894,7 @@ export class GameServer {
this.#on_move(player, x, y, z);
Object.assign(player, { x, y, z, yaw, pitch });
player.set_swimming(message.swimming === true);
player.sneaking = message.sneaking === true;
this.#broadcast(player_move(player), player);
this.#update_view(player);
break;
@@ -928,16 +931,25 @@ export class GameServer {
player.attack_strength_ticker = 0;
break;
case "use_item":
this.#use_item(player);
// a client that hadn't heard it's using an item yet, still clicking
if (!player.using) this.#use_item(player);
break;
case "release_use":
this.#release_using(player);
break;
case "use_block":
if (is_block_position(message.x, message.y, message.z) && faces.includes(message.face)) {
// the middle of the block when a client doesn't say, or says something that isn't on it
const hit = Array.isArray(message.hit) && message.hit.length === 3 &&
message.hit.every((v) => typeof v === "number" && v >= 0 && v <= 1)
? message.hit
: [0.5, 0.5, 0.5];
this.#use_block(player, message.x, message.y, message.z, message.face, hit);
if (player.using) {
// a client that hadn't heard it's using an item yet: what it guessed the click did, it didn't
this.#correct_use(player, message.x, message.y, message.z, message.face);
} else {
// the middle of the block when a client doesn't say, or says something that isn't on it
const hit = Array.isArray(message.hit) && message.hit.length === 3 &&
message.hit.every((v) => typeof v === "number" && v >= 0 && v <= 1)
? message.hit
: [0.5, 0.5, 0.5];
this.#use_block(player, message.x, message.y, message.z, message.face, hit);
}
}
this.#ack(player, message.seq);
break;
@@ -1042,11 +1054,11 @@ export class GameServer {
// right click at nothing: eating, or the item's on_use
#use_item(player: ServerPlayer): boolean {
return this.#eat(player) || this.#use_item_components(player);
return this.#eat(player) || this.#use_item_components(player, undefined);
}
// the held item's on_use handlers, returns whether it has any
#use_item_components(player: ServerPlayer): boolean {
// the held item's on_use handlers, with the block it was used on if it was, returns whether it has any
#use_item_components(player: ServerPlayer, target: UseTarget | undefined): boolean {
const held = player.held_item;
if (!held) {
return false;
@@ -1058,19 +1070,71 @@ export class GameServer {
const player_api = this.mods.player(player);
const item = player_api.held_item!;
for (const { mod, id, component, params } of components) {
run_guarded(mod, `${id} on_use`, () => component.on_use!(item, params, player_api));
run_guarded(mod, `${id} on_use`, () => component.on_use!(item, params, player_api, target));
}
return true;
}
// a script's start_using: the held item is used over time from now, while they hold use
start_using(player: ServerPlayer, duration: number) {
const held = player.held_item;
if (!held) throw new Error("start_using needs an item in their hand");
player.using = { slot: player.selected_slot, type_id: held.type_id, ticks: 0, duration };
}
// the held item's on_use_tick handlers every tick it's used, and on_finish_using once it's used for its duration.
// switching away from it, or it being used up, dropped or swapped, stops it without either, like minecraft
#tick_using(player: ServerPlayer) {
const using = player.using;
if (!using) return;
if (!player.alive || player.selected_slot !== using.slot || player.held_item?.type_id !== using.type_id) {
player.using = undefined;
return;
}
using.ticks += 1;
this.#run_using(player, "on_use_tick", using.ticks);
// a handler can stop it, or start it over
if (player.using === using && using.duration !== -1 && using.ticks >= using.duration) {
player.using = undefined;
this.#run_using(player, "on_finish_using");
}
}
// they let go of use: on_release_using with how long they used it, when they were using something
#release_using(player: ServerPlayer) {
const using = player.using;
if (!using) return;
player.using = undefined;
if (player.selected_slot === using.slot && player.held_item?.type_id === using.type_id) {
this.#run_using(player, "on_release_using", using.ticks);
}
}
#run_using(player: ServerPlayer, handler: "on_use_tick" | "on_release_using" | "on_finish_using", ticks = 0) {
const player_api = this.mods.player(player);
const item = player_api.held_item;
if (!item) return;
for (const { mod, id, component, params } of this.mods.item_components_of(item.id)) {
const run = component[handler];
if (run) run_guarded(mod, `${id} ${handler}`, () => run(item, params, player_api, ticks));
}
}
// what a client may have guessed clicking face of the block at x y z did, to the block and to where a block would
// be placed, put back to what's really there
#correct_use(player: ServerPlayer, x: number, y: number, z: number, face: Faces) {
const target = placement_target(x, y, z, FACE_OFFSETS[face], this.world.get_block_info(x, y, z));
this.#correct(player, x, y, z);
this.#correct(player, target.x, target.y, target.z);
}
#use_block(player: ServerPlayer, x: number, y: number, z: number, face: Faces, hit: number[]) {
const info = this.world.get_block_info(x, y, z);
const target = placement_target(x, y, z, FACE_OFFSETS[face], info);
const [tx, ty, tz] = [target.x, target.y, target.z];
if (!info || !this.#in_reach(player, x, y, z)) {
this.#correct(player, x, y, z);
this.#correct(player, tx, ty, tz);
this.#correct_use(player, x, y, z, face);
return;
}
@@ -1086,31 +1150,35 @@ export class GameServer {
this.mods.before.block_interact.emit(before_interact);
if (before_interact.cancel) {
// the client may have guessed the shared rules' on_interact, or a placement
this.#correct(player, x, y, z);
this.#correct(player, tx, ty, tz);
this.#correct_use(player, x, y, z, face);
return;
}
// the shared rules first, the ones the client guessed, then the server script's
let handled = this.mods.rules.interact(used, x, y, z, { face, hit: [hit[0], hit[1], hit[2]] }, player.yaw);
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
if (component.on_interact) {
handled = run_guarded(mod, `${component_id} on_interact`, () =>
component.on_interact!(block, params, player_api)) ===
true || handled;
// the shared rules first, the ones the client guessed, then the server script's. sneaking with an item skips the
// block, like minecraft's secondary use, so a block can be placed against a chest
const sneaking = player.sneaking;
let handled = false;
if (!(sneaking && held_slot.has_item())) {
const interaction = { face, hit: [hit[0], hit[1], hit[2]] as [number, number, number], sneaking };
handled = this.mods.rules.interact(used, x, y, z, interaction, player.yaw);
for (const { mod, id: component_id, component, params } of this.mods.components_of(used.id)) {
if (component.on_interact) {
handled = run_guarded(mod, `${component_id} on_interact`, () =>
component.on_interact!(block, params, player_api)) ===
true || handled;
}
}
}
if (handled) {
this.mods.after.block_interact.emit(interact);
// the client guessed the shared rules' on_interact from what it sees, which might not be what's here. it didn't
// guess a placement for interactive blocks, but it might have for others
this.#correct(player, x, y, z);
this.#correct(player, tx, ty, tz);
this.#correct_use(player, x, y, z, face);
return;
}
// an item that does something when used does that instead of being placed
if (this.#use_item_components(player)) {
if (this.#use_item_components(player, { block, face, hit: [hit[0], hit[1], hit[2]] })) {
this.#correct(player, tx, ty, tz);
return;
}
@@ -1145,6 +1213,7 @@ export class GameServer {
z: tz,
face,
hit: [hit[0], hit[1], hit[2]],
sneaking: player.sneaking,
}),
placing,
this.world.get_block_value(tx, ty, tz),
@@ -1894,8 +1963,14 @@ export class GameServer {
[health, max_health, food, saturation, air],
() => ({ type: "vitals", health, max_health, food, saturation, air }),
);
const attack_speed = player.attribute(ATTRIBUTES.attack_speed);
sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed }));
const attributes = {
attack_speed: player.attribute(ATTRIBUTES.attack_speed),
block_interaction_range: player.attribute(ATTRIBUTES.block_interaction_range),
entity_interaction_range: player.attribute(ATTRIBUTES.entity_interaction_range),
};
sync("attributes", attributes, () => ({ type: "attributes", ...attributes }));
const using = player.using !== undefined;
sync("using_item", using, () => ({ type: "using_item", using }));
const flying = player.flying;
sync("flying", flying, () => ({ type: "flying", flying }));
const effects = [...player.effects.values()]
@@ -2042,11 +2117,13 @@ export class GameServer {
this.#next_chunk_player = first + 1;
}
// from the eyes to the block's middle
#in_reach(player: ServerPlayer, x: number, y: number, z: number) {
const dx = x + 0.5 - player.x;
const dy = y + 0.5 - (player.y + player.eye_height);
const dz = z + 0.5 - player.z;
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
const reach = player.attribute(ATTRIBUTES.block_interaction_range) + REACH_LEEWAY;
return dx * dx + dy * dy + dz * dz <= reach * reach;
}
// from the eyes to the closest point of its box
@@ -2056,7 +2133,8 @@ export class GameServer {
const dx = Math.max(entity.x - half - player.x, 0, player.x - entity.x - half);
const dy = Math.max(entity.y - eye_y, 0, eye_y - entity.y - entity.height);
const dz = Math.max(entity.z - half - player.z, 0, player.z - entity.z - half);
return dx * dx + dy * dy + dz * dz <= MAX_REACH * MAX_REACH;
const reach = player.attribute(ATTRIBUTES.entity_interaction_range) + REACH_LEEWAY;
return dx * dx + dy * dy + dz * dz <= reach * reach;
}
// the blocks within SIMULATION_DISTANCE of a player that have on_new_day and no data, in chunks that are built
+11
View File
@@ -1340,6 +1340,17 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
},
remove_effect: (id) => player.remove_effect(known_effect(id).id),
is_swimming: () => player.swimming,
is_sneaking: () => player.sneaking,
start_using(ticks) {
if (!Number.isInteger(ticks) || (ticks < 1 && ticks !== -1)) {
throw new Error("start_using takes a whole number of ticks, or -1 until they let go");
}
game.start_using(player, ticks);
},
stop_using() {
player.using = undefined;
},
is_using: () => player.using !== undefined,
};
}
+7
View File
@@ -58,6 +58,11 @@ export class ServerPlayer extends LivingEntity {
override eye_height = PLAYER_EYE_HEIGHT;
// lying flat in the short box, as their client says. their client decides, like where they are
swimming = false;
// holding the sneak key, as their client says
sneaking = false;
// the held item they're using over time, like drawing a bow: which slot and item it was, how many ticks they've
// used it, and for how long they can (-1 until they let go). see GameServer.start_using
using: { slot: number; type_id: string; ticks: number; duration: number } | undefined;
// flying, from /fly. their client moves them without gravity, and they don't fall or get hungry from moving
flying = false;
@@ -127,6 +132,8 @@ export class ServerPlayer extends LivingEntity {
[ATTRIBUTES.attack_speed],
[ATTRIBUTES.physical_shielding],
[ATTRIBUTES.arcane_shielding],
[ATTRIBUTES.block_interaction_range],
[ATTRIBUTES.entity_interaction_range],
];
}