Entity and block sounds !
This commit is contained in:
@@ -48,7 +48,7 @@ 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, sound_range, sound_to_play } from "@/common/mod_data.ts";
|
||||
import { type EntityJson, sound_of, 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";
|
||||
@@ -774,6 +774,13 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// a block's sound of kind from the middle of x y z, when it has one, for everyone but heard_by, whose client played
|
||||
// it already. see BLOCK_SOUNDS
|
||||
#block_sound(block: BlockRegistry, kind: string, x: number, y: number, z: number, heard_by?: ServerPlayer) {
|
||||
const sound = sound_of(block.sounds, kind);
|
||||
if (sound) this.play_sound(sound, x + 0.5, y + 0.5, z + 0.5, { except: heard_by });
|
||||
}
|
||||
|
||||
// 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);
|
||||
@@ -853,12 +860,13 @@ export class GameServer {
|
||||
y: number,
|
||||
z: number,
|
||||
player?: ServerPlayer,
|
||||
how: { harvest?: boolean; collect?: boolean } = {},
|
||||
how: { harvest?: boolean; collect?: boolean; heard_by?: ServerPlayer } = {},
|
||||
): ItemStack[] | undefined {
|
||||
const info = this.world.get_block_info(x, y, z);
|
||||
if (!info) {
|
||||
return undefined;
|
||||
}
|
||||
this.#block_sound(info, "break", x, y, z, how.heard_by);
|
||||
const held = EverythingRegistry.get<ItemRegistry>("items", player?.held_item?.type_id ?? "");
|
||||
const tool = held?.tool;
|
||||
const right_tool = tool !== undefined && tool.type === info.tool_to_break && tool.tier >= (info.tool_tier ?? 0);
|
||||
@@ -1329,7 +1337,11 @@ export class GameServer {
|
||||
}
|
||||
|
||||
// creative breaks drop nothing, since placing doesn't use anything up
|
||||
this.#as_player(player, () => this.destroy_block(x, y, z, player, { collect: player.creative }));
|
||||
// their client played its break sound as it guessed the break
|
||||
this.#as_player(
|
||||
player,
|
||||
() => this.destroy_block(x, y, z, player, { collect: player.creative, heard_by: player }),
|
||||
);
|
||||
player.vitals.add_exhaustion(EXHAUSTION.break_block);
|
||||
this.mods.after.block_break.emit(event);
|
||||
}
|
||||
@@ -1579,6 +1591,10 @@ export class GameServer {
|
||||
if (!place) return;
|
||||
|
||||
this.#as_player(player, () => this.set_block(tx, ty, tz, held.block_id!, player, state));
|
||||
// their client guessed the placing, and played its sound, unless the item or the clicked block might have done
|
||||
// something else, see MultiPlayerGameMode's #guess_use
|
||||
const guessed = !held.components && (!info.interactive || player.sneaking);
|
||||
this.#block_sound(placing, "place", tx, ty, tz, guessed ? player : undefined);
|
||||
if (!player.creative) held_slot.amount = held_slot.amount! - 1;
|
||||
this.mods.after.block_place.emit(place);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user