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
+52 -6
View File
@@ -1,7 +1,7 @@
# Sounds
A mod's sounds are `.ogg` files in its `sounds/` folder, put together into sounds by its `sounds.json`. Client phases
play them with `ctx.sounds`.
A mod's sounds are `.ogg` files in its `sounds/` folder, put together into sounds by its `sounds.json`. Server phases,
shared rules and client phases play them with `ctx.sounds`.
## Sound Files
@@ -43,6 +43,52 @@ A sound without a category only follows the player's master volume.
## Playing Sounds
Sounds are played where what makes them happens: a server phase for what happens in the world, a shared rule for what a
player's click does, a client phase for what only that player sees, like a screen. Whichever plays it, `volume` and
`pitch` multiply the sound's own, and a sound played at a volume over 1 is heard further away, not louder: at 2, twice
as far. Each throws for a sound no mod defines.
### From the Server
The server only plays a sound once, for the players who can hear it then. A sound that lasts, like a machine's hum, is a
client phase's to play, see [Lasting Sounds](#lasting-sounds).
```ts
// at a point, for every player within its range
ctx.sounds.play("copper_tools:anvil/land", x + 0.5, y, z + 0.5, { volume: 2 });
// at a block's middle
block.play_sound("copper_tools:machine/done");
// following a mob or another entity, for the players who see it within range
mob.play_sound("copper_tools:walker/growl", { pitch: 0.8 });
// only that player hears it, as it is wherever they are
player.play_sound("copper_tools:quest/complete");
```
`ctx.sounds.play` takes `except`, a player who doesn't get it. That's for a player whose own client already played it,
like when a client phase plays a click's sound right away and tells the server about it.
### From Shared Rules
A shared rule's sound, like a door opening in its `on_interact`, plays right away for the player who clicked, since
their client runs the rule too, and reaches everyone else from the server:
```ts
export function shared(ctx: SharedContext) {
ctx.components.register_block("copper_tools:bell", {
on_interact(block) {
ctx.sounds.play("copper_tools:ring", block.x + 0.5, block.y + 0.5, block.z + 0.5);
return true;
},
});
}
```
On the server, a rule running for a player's break, place or click plays it for every player in range but that one. A
rule running for anything else, like a script changing a block, plays it for everyone. A mob opening a door changes its
states from a script, which doesn't run `on_interact`, so that script plays the sound itself.
### On a Client
In a client phase, `ctx.sounds.play` plays a sound on that player's client only:
```ts
@@ -62,11 +108,11 @@ With nothing but its id it's heard as it is wherever the player is, like a click
| `at` | A point it comes from. It fades to nothing at its range, and isn't played at all from further away. |
| `follow` | An entity's id. It comes from where the entity is drawn, and stops when the entity goes. |
| `loop` | Plays again from the start until stopped. |
| `volume` | Multiplies the sound's own. Over 1 it's heard further away, not louder: at 2, twice as far. |
| `volume` | Multiplies the sound's own. |
| `pitch` | Multiplies the sound's own. 2 is an octave up, and twice as fast. |
`play` throws for a sound no mod defines, and `ctx.sounds.exists(id)` checks first. It returns the sound playing, which
can be stopped or changed while it plays:
`ctx.sounds.exists(id)` checks a sound is there before playing it. `play` returns the sound playing, which can be
stopped or changed while it plays:
```ts
const hum = ctx.sounds.play("copper_tools:machine/hum", { at: { x: 10.5, y: 64.5, z: 3.5 }, loop: true });
@@ -78,7 +124,7 @@ hum.stop();
A looping sound out of range waits silently, and when the player comes back in range it plays on from as far into the
file as it would have got, as if it never stopped.
### What Doesn't Play
#### What Doesn't Play
- **Before the player's first click or key press.** Browsers keep sound off until then. A sound played earlier is
dropped, and a loop starts then.