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
+21
View File
@@ -11,6 +11,7 @@ import type {
Id,
ItemStack,
ModInfo,
PlayOptions,
Position,
RecipeLookup,
Season,
@@ -32,6 +33,7 @@ export type {
Id,
ItemStack,
ModInfo,
PlayOptions,
Position,
Season,
TagApi,
@@ -65,9 +67,19 @@ export interface ServerContext {
ui: ServerUi;
net: ServerNet;
storage: ModStorage;
// sounds from the mods' sounds.json, played on players' clients, see docs/modding/resources/sounds.md
sounds: ServerSounds;
log(...args: unknown[]): void;
}
// one-off sounds. what lasts, like a machine's hum, is a client phase's to play, see ClientSounds in cubi/client
export interface ServerSounds {
// once at x y z, for every player within its range. except leaves out a player whose own client already played it,
// like the one whose click a client phase answered with a sound. throws for a sound no mod defines
play(sound: Id, x: number, y: number, z: number, options?: PlayOptions & { except?: Player }): void;
exists(sound: Id): boolean;
}
// components
export interface BlockRef {
@@ -90,6 +102,8 @@ export interface BlockRef {
// turns on_tick and on_second off for this block, or back on, like a machine with nothing to do. it stays off until
// it's turned on again, saved with the world, and goes with the block
set_ticking(ticking: boolean): void;
// ctx.sounds.play at its middle
play_sound(sound: Id, options?: PlayOptions): void;
}
// P is the component's params from the block json
@@ -295,6 +309,8 @@ export interface MobRef extends LivingRef {
// removes one
get_client_data<T>(field: string): T | undefined;
set_client_data(field: string, value: unknown): void;
// a sound following it, like EntityRef.play_sound
play_sound(sound: Id, options?: PlayOptions): void;
}
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
@@ -570,6 +586,8 @@ export interface EntityRef {
// changes what an item lying around is, like ItemEntity.setItem. undefined or a count of 0 removes it. throws for
// every other kind of entity
set_item(item: ItemStack | undefined): void;
// a sound following it, once, for every player who sees it and is within its range
play_sound(sound: Id, options?: PlayOptions): void;
}
export interface Player {
@@ -634,6 +652,9 @@ export interface Player {
// 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;
// a sound only they hear, as it is wherever they are, like one in a menu. ctx.sounds.play for one others near
// them hear too
play_sound(sound: Id, options?: PlayOptions): void;
// stops using it without on_release_using or on_finish_using, like stopUsingItem
stop_using(): void;
is_using(): boolean;
+6
View File
@@ -190,6 +190,12 @@ export interface SharedContext {
// registers stops its mod loading once every shared phase has set up. see RecipeType
register_type<R, I>(type: Id, rules: RecipeType<R, I>): void;
} & RecipeLookup;
readonly sounds: {
// once at x y z, from a rule. on the server, for every player in range but the one whose break, place or click
// ran the rule, since their client ran it too and played it already. on a client, only there. throws for a
// sound no mod defines. see docs/modding/resources/sounds.md
play(sound: Id, x: number, y: number, z: number, options?: PlayOptions): void;
};
readonly components: {
// the rules of a block component, the same id the server phase registers the rest of it under
// deno-lint-ignore no-explicit-any
+24
View File
@@ -8,6 +8,7 @@ import { is_tint_color, type TintColor } from "./worldgen/biome_colors.ts";
import {
type BlockRegistry,
type BlockStateDefinition,
EverythingRegistry,
FLUID_DEFAULTS,
FLUID_LEVEL,
type FluidContainerProperties,
@@ -25,6 +26,7 @@ import {
import type { BlockPart, BlockVariant, ModelFileJson } from "./block_models.ts";
import { expand_items, list_matches } from "./tags.ts";
import { GAME_VERSION } from "./constants.ts";
import type { PlayOptions } from "./mod_api/shared.ts";
import {
by_kind,
color,
@@ -636,6 +638,28 @@ export function validate_sound(json: unknown): Problems {
return find_problems(sound_schema, json);
}
// how far away a sound is heard at volume 1 when its definition doesn't say
const DEFAULT_SOUND_RANGE = 16;
// how far a sound played at volume is heard: its range, further for a louder one, like twice as far at volume 2
export function sound_range(json: SoundJson, volume = 1) {
return (json.range ?? DEFAULT_SOUND_RANGE) * Math.max(1, (json.volume ?? 1) * volume);
}
// a mod playing sound with options: its definition, or an error saying what's wrong with them
export function sound_to_play(sound: unknown, options: PlayOptions = {}): SoundJson {
const json = typeof sound === "string" ? EverythingRegistry.get<SoundJson>("sounds", sound) : undefined;
if (!json) throw new Error(`unknown sound ${sound}`);
const { volume = 1, pitch = 1 } = options;
if (typeof volume !== "number" || !(volume >= 0) || !Number.isFinite(volume)) {
throw new Error(`a sound's volume must be 0 or more`);
}
if (typeof pitch !== "number" || !(pitch > 0) || !Number.isFinite(pitch)) {
throw new Error(`a sound's pitch must be more than 0`);
}
return json;
}
// whether a file starts the way every ogg does, so a wav or mp3 renamed to .ogg is caught when the mod is checked
// instead of failing to play
export function is_ogg(bytes: Uint8Array): boolean {
+11 -1
View File
@@ -12,7 +12,7 @@ export const AIR_ID = "cubi: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 = 29;
export const PROTOCOL_VERSION = 30;
export interface PlayerInfo {
id: string;
@@ -267,6 +267,16 @@ export type ServerMessage =
// cracks on a block a machine is breaking, from 0 to BREAK_STAGES, to the players who have its chunk. none without
// a stage, and when the block changes
| { type: "break_progress"; x: number; y: number; z: number; stage?: number }
// a sound from a mod's sounds.json, played once: at a point, following an entity, or with neither heard as it is.
// volume and pitch multiply its own, 1 when not given
| {
type: "sound";
sound: string;
at?: [number, number, number];
entity?: string;
volume?: number;
pitch?: number;
}
| { type: "container"; container: ContainerKey; items: (ItemData | null)[] }
| { type: "cursor"; item: ItemData | null }
// a menu opened, replacing any other. id counts 1 to 100 and around, type is the menu type whose screen draws it.
+11 -1
View File
@@ -3,7 +3,8 @@
// what mods' shared phases get, built the same way on the server and every client so they register the same rules and
// recipes. each side gives it its own world, rules and recipe book
import type { ModInfo, SharedContext, SharedWorld } from "./mod_api/shared.ts";
import type { ModInfo, PlayOptions, SharedContext, SharedWorld } from "./mod_api/shared.ts";
import { sound_to_play } from "./mod_data.ts";
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
import { AIR_ID } from "./protocol.ts";
import { is_solid } from "./block_shapes.ts";
@@ -22,6 +23,8 @@ export interface SharedSide {
recipes: () => RecipeBook;
// whether the shared phases are running, the only time they can register
running: () => boolean;
// a rule's sound, see SharedContext.sounds. checked already
play_sound(sound: string, x: number, y: number, z: number, options: PlayOptions): void;
}
export function shared_context(mod: ModInfo, side: SharedSide): SharedContext {
@@ -43,6 +46,13 @@ export function shared_context(mod: ModInfo, side: SharedSide): SharedContext {
},
recipes: shared_recipes_api(mod.id, side.recipes, register),
world: side.world,
sounds: {
play(sound, x, y, z, options = {}) {
if (![x, y, z].every(Number.isFinite)) throw new Error("a position is three numbers");
sound_to_play(sound, options);
side.play_sound(sound, x, y, z, { volume: options.volume, pitch: options.pitch });
},
},
components: {
register_block(id, rules) {
register.new_id("component", id, side.block_rules.has(id));