Sounds on shared and server

This commit is contained in:
2026-11-22 07:44:13 -03:00
parent 47934b272c
commit 8e587f6446
16 changed files with 386 additions and 30 deletions
+53 -14
View File
@@ -48,7 +48,8 @@ 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, Position, SchematicPlacement, UseTarget } from "@/common/mod_api/server.ts";
import type { EntityJson } from "@/common/mod_data.ts";
import { type EntityJson, sound_range, sound_to_play } from "@/common/mod_data.ts";
import type { PlayOptions } from "@/common/mod_api/shared.ts";
import { encode_schematic } from "@/common/schematic.ts";
import { capture_schematic, placed_schematic, type RegisteredSchematic } from "./schematics.ts";
import {
@@ -88,7 +89,7 @@ import {
} from "./entity/item_entity.ts";
import { Mob } from "./entity/mob.ts";
import { LivingEntity } from "./entity/living_entity.ts";
import { EntityManager } from "./entity/entity_manager.ts";
import { EntityManager, sound_message } from "./entity/entity_manager.ts";
import { NaturalSpawner } from "./natural_spawner.ts";
import { create_entity, entity_type_ids, load_entity } from "./entity/entity_types.ts";
import { damage_source, type DamageSource, type Entity, type SavedEntity } from "./entity/entity.ts";
@@ -763,6 +764,40 @@ export class GameServer {
}
}
// a sound at x y z for every player within its range, except one whose client already played it. see
// ServerSounds.play
play_sound(sound: string, x: number, y: number, z: number, options: PlayOptions & { except?: ServerPlayer } = {}) {
const range = sound_range(sound_to_play(sound, options), options.volume);
const message = sound_message(sound, options, { at: [x, y, z] });
for (const player of this.#players.values()) {
if (player !== options.except && player.hears(x, y, z, range)) this.#send(player, message);
}
}
// a sound only player hears, as it is wherever they are, see Player.play_sound
play_sound_to(player: ServerPlayer, sound: string, options: PlayOptions = {}) {
sound_to_play(sound, options);
this.#send(player, sound_message(sound, options));
}
// the player whose break, place or use the shared rules are running for. their client ran the same rules to guess
// it, so it already played what the rules play, see SharedContext.sounds
get rules_player() {
return this.#rules_player;
}
#rules_player: ServerPlayer | undefined;
// fn with the shared rules it runs running for player, see rules_player
#as_player<T>(player: ServerPlayer, fn: () => T): T {
const before = this.#rules_player;
this.#rules_player = player;
try {
return fn();
} finally {
this.#rules_player = before;
}
}
// a script pushing a player, see Player.push. their client moves them, so it's added there, unless the server is
// moving them while they're knocked back
push_player(player: ServerPlayer, x: number, y: number, z: number) {
@@ -1294,7 +1329,7 @@ export class GameServer {
}
// creative breaks drop nothing, since placing doesn't use anything up
this.destroy_block(x, y, z, player, { collect: player.creative });
this.#as_player(player, () => this.destroy_block(x, y, z, player, { collect: player.creative }));
player.vitals.add_exhaustion(EXHAUSTION.break_block);
this.mods.after.block_break.emit(event);
}
@@ -1464,7 +1499,7 @@ export class GameServer {
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);
handled = this.#as_player(player, () => 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`, () =>
@@ -1509,15 +1544,19 @@ export class GameServer {
}
// solid blocks can't go where a mob or player is, the one placing it included, like minecraft
const placing = EverythingRegistry.get<BlockRegistry>("blocks", held.block_id)!;
const ruled_value = this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
x: tx,
y: ty,
z: tz,
face,
hit: [hit[0], hit[1], hit[2]],
sneaking: player.sneaking,
item: held_slot.type_id!,
});
const ruled_value = this.#as_player(
player,
() =>
this.mods.rules.placement_value(placing, player.yaw, player.pitch, {
x: tx,
y: ty,
z: tz,
face,
hit: [hit[0], hit[1], hit[2]],
sneaking: player.sneaking,
item: held_slot.type_id!,
}),
);
// its rules say it can't go there
if (ruled_value === undefined) {
this.#correct(player, tx, ty, tz);
@@ -1539,7 +1578,7 @@ export class GameServer {
const place = this.#place_event(player, { id: held.block_id, x: tx, y: ty, z: tz }, face, hit);
if (!place) return;
this.set_block(tx, ty, tz, held.block_id, player, state);
this.#as_player(player, () => this.set_block(tx, ty, tz, held.block_id!, player, state));
if (!player.creative) held_slot.amount = held_slot.amount! - 1;
this.mods.after.block_place.emit(place);
}