Entity and block sounds !

This commit is contained in:
2026-11-22 08:23:11 -03:00
parent da44685f54
commit d18fe19027
17 changed files with 332 additions and 20 deletions
+2
View File
@@ -161,6 +161,8 @@ export interface BlockRegistry {
interactive?: boolean;
// the mouse wheel turned while looking at it goes to its components' on_scroll instead of the hotbar
scrollable?: boolean;
// what its sounds' ids start with, see BLOCK_SOUNDS in common/mod_data.ts
sounds?: string;
// placing a block into it replaces it, like water
replaceable?: boolean;
// the blocks (ids or #tags) it can stand on, like crops on farmland. it can't be placed elsewhere, and breaks when
+16
View File
@@ -4,11 +4,13 @@
// checks between mods: dependencies, load order, and ids one mod uses from another. pure, so the build, check-mods
// and the server (on .cubi files) all use it
import {
BLOCK_SOUNDS,
type BlockJson,
choice_list,
type ClipJson,
type EffectJson,
entity_category,
ENTITY_SOUNDS,
type EntityJson,
type EntityModelJson,
type FluidJson,
@@ -193,6 +195,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
const textures = new Set<string>(engine_textures);
const sounds = new Set<string>();
// terrain data by kind, then id
const worldgen_owners: Record<string, Map<string, string[]>> = {
noise: new Map(),
@@ -248,6 +251,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
tag_owners.get(json.registry)!.add(json.id);
}
for (const texture of mod.textures) textures.add(texture);
for (const { json } of mod.sounds ?? []) sounds.add(json.id);
}
const mod_ids = new Set(mods.map((mod) => mod.id));
@@ -295,6 +299,16 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
};
// a block's or entity's "sounds" with none of what it can have, which is a typo
const uses_sounds = (file: string, prefix: string, kinds: readonly string[]) => {
check_namespace(file, prefix);
if (!kinds.some((kind) => sounds.has(`${prefix}/${kind}`))) {
errors.push(
`${file}: sounds ${prefix} has none of ${kinds.map((kind) => `${prefix}/${kind}`).join(", ")}`,
);
}
};
for (const { file, json } of mod.blocks) {
if ((block_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: block ${json.id} is also defined by ${block_owners.get(json.id)!.join(", ")}`);
@@ -309,6 +323,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (json.item_model) uses(file, json.item_model, "model");
if (json.drops) uses(file, json.drops, "item");
if (json.loot_table) uses(file, json.loot_table, "loot table");
if (json.sounds) uses_sounds(file, json.sounds, BLOCK_SOUNDS);
if (json.waterloggable) {
uses(file, json.waterloggable, "block");
if (block_owners.has(json.waterloggable) && !fluid_blocks.has(json.waterloggable)) {
@@ -391,6 +406,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (json.model !== undefined) uses(file, json.model, "entity model");
uses(file, json.texture, "texture");
if (json.loot_table) uses(file, json.loot_table, "loot table");
if (json.sounds) uses_sounds(file, json.sounds, ENTITY_SOUNDS);
// scripts register behaviors, the server checks them when it starts
if (json.behavior?.includes(":")) check_namespace(file, json.behavior);
}
+20
View File
@@ -69,6 +69,8 @@ const tint_color = z.custom<TintColor>(is_tint_color, 'must be "grass", "foliage
// a texture id, or texture variables mapped to ids
const textures = z.union([id, z.record(z.string(), id)], "must be a texture id or { variable: texture id }");
const stack = z.object({ id, count: z.int().min(1) });
// what a block's or entity's sounds start with, like "my_mod:wood" for my_mod:wood/break
const sound_prefix = z.string('must be a sound id like "my_mod:wood"').regex(TAG_ID_PATTERN);
// the manifest of the mod in the folder folder_name, which is its id
const manifest_schema = (folder_name: string) =>
@@ -118,6 +120,7 @@ export interface BlockJson {
item?: boolean;
interactive?: boolean;
scrollable?: boolean;
sounds?: string;
replaceable?: boolean;
place_on?: string[];
place_in?: string[];
@@ -196,6 +199,8 @@ const block_fields = z.object({
item: z.boolean().optional(),
interactive: z.boolean().optional(),
scrollable: z.boolean().optional(),
// what it sounds like: <sounds>/break, /place, /step and /hit, see BLOCK_SOUNDS
sounds: sound_prefix.optional(),
replaceable: z.boolean().optional(),
place_on: z.array(id_or_tag).min(1).optional(),
place_in: z.array(id_or_tag).min(1).optional(),
@@ -458,6 +463,8 @@ const entity_schema = z.object({
arcane_shielding: z.number().min(0).optional(),
// what it drops when it dies, see common/loot.ts
loot_table: loot_table_id.optional(),
// what it sounds like: <sounds>/hurt, /death, /step and /ambient, see ENTITY_SOUNDS
sounds: sound_prefix.optional(),
});
export type EntityJson = z.infer<typeof entity_schema>;
@@ -638,6 +645,17 @@ export function validate_sound(json: unknown): Problems {
return find_problems(sound_schema, json);
}
// the sounds the engine plays for a block or an entity whose json has "sounds", each after its prefix and a /. a sound
// that isn't defined isn't played
export const BLOCK_SOUNDS = ["break", "place", "step", "hit"] as const;
export const ENTITY_SOUNDS = ["hurt", "death", "step", "ambient"] as const;
// the id of the sound prefix has for kind, undefined when it has none
export function sound_of(prefix: string | undefined, kind: string): string | undefined {
const id = `${prefix}/${kind}`;
return prefix !== undefined && EverythingRegistry.get("sounds", id) ? id : undefined;
}
// how far away a sound is heard at volume 1 when its definition doesn't say
const DEFAULT_SOUND_RANGE = 16;
@@ -796,6 +814,7 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (block.loot_table !== undefined) json.loot_table = block.loot_table;
if (!has_item) json.item = false;
if (block.interactive) json.interactive = true;
if (block.sounds !== undefined) json.sounds = block.sounds;
if (block.scrollable) json.scrollable = true;
if (!!block.replaceable !== !!block.fluid) json.replaceable = !!block.replaceable;
if (block.place_on) json.place_on = block.place_on;
@@ -848,6 +867,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
if (json.loot_table !== undefined) block.loot_table = json.loot_table;
if (json.interactive) block.interactive = true;
if (json.scrollable) block.scrollable = true;
if (json.sounds !== undefined) block.sounds = json.sounds;
if (json.replaceable ?? json.fluid) block.replaceable = true;
if (json.place_on) block.place_on = json.place_on;
if (json.place_in) block.place_in = json.place_in;