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
+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;