import { RESOURCE_PATHS, type SOUND_TYPES } from 'sandstone/arguments' import { ContainerNode } from '../../nodes' import type { SandstoneCore } from '../../sandstoneCore' import type { ListResource, ResourceClassArguments, ResourceNode } from '../resource' import { BinaryResource, JsonResource, ResourceClass, jsonStringify } from '../resource' import { SOUNDS } from 'sandstone/arguments/generated/_registry/sounds' import { JsonSymbolResource } from 'sandstone/arguments/generated/_json/dispatcher' /** * A node representing a Minecraft sound. */ export class SoundEventNode extends ContainerNode implements ResourceNode> { constructor( sandstoneCore: SandstoneCore, public resource: SoundEventClass, ) { super(sandstoneCore) } getValue = () => this.resource.buffer } export type SoundEventArguments = { /** * The sound's buffer. Path string in `./resources` will be used. Defaults to `/resources/resourcepack/assets/(namespace)/sounds/(type)/(name)`. */ sound?: string | ArrayBuffer | Buffer | Promise /** * Whether to automatically add this to a sounds.json file. Defaults to false. */ addToSounds?: boolean } & ResourceClassArguments<'default'> export class SoundEventClass extends ResourceClass> implements BinaryResource { static readonly resourceType = 'sound' buffer: Promise | ArrayBuffer | Buffer = {} as unknown as ArrayBuffer fullId: SOUNDS constructor( core: SandstoneCore, public type: Type, name: string, args: SoundEventArguments, ) { super( core, { packType: core.pack.resourcePack(), extension: 'ogg', encoding: false }, SoundEventNode, SoundEventClass.resourceType, core.pack.resourceToPath(name, [...RESOURCE_PATHS[SoundEventClass.resourceType].path, type]), args, ) this.fullId = `${this.type}.${name}` as SOUNDS if (args.addToSandstoneCore) { if (args.sound === undefined) { this.buffer = core.getExistingResource(this) } else { if (typeof args.sound === 'string') { this.buffer = core.getExistingResource(`${args.sound}${args.sound.endsWith('.ogg') ? '' : '.ogg'}`, false) } else { this.buffer = args.sound } } if (args.addToSounds) { this.pushIntoSoundsIndex() } } this.handleConflicts() } protected pushIntoSoundsIndex() { let def = this.core.sounds.get(this.path[0]) if (!def) { def = this.core.sounds .set( this.path[0], new SoundsIndexClass(this.core, this.path[0], { addToSandstoneCore: true, creator: 'sandstone', definitions: { [this.fullId]: { sounds: [this.fullId], } } }), ) .get(this.path[0]) } else { def!.push(this) } } // TODO: Add sound event methods; play, stop, etc. // silent() {} } /** * A node representing a Minecraft sounds index. */ export class SoundsIndexNode extends ContainerNode implements ResourceNode { constructor( sandstoneCore: SandstoneCore, public resource: SoundsIndexClass, ) { super(sandstoneCore) } getValue = () => jsonStringify(this.resource.json, this.resource._resourceType as keyof typeof RESOURCE_PATHS) } type SoundsJSON = NonNullable export type SoundsIndexArguments = { /** * The sounds definition map. */ definitions?: SoundsJSON } & ResourceClassArguments<'default'> export class SoundsIndexClass extends ResourceClass implements JsonResource, ListResource { static readonly resourceType = 'sounds' json: SoundsJSON | Promise constructor(core: SandstoneCore, namespace: string, args: SoundsIndexArguments) { super( core, { packType: core.pack.resourcePack() }, SoundsIndexNode, SoundsIndexClass.resourceType, core.pack.resourceToPath(`${namespace}:sounds`, RESOURCE_PATHS[SoundsIndexClass.resourceType].path), args, ) if (args.definitions !== undefined) { this.json = args.definitions } else { this.json = (async () => JSON.parse(await (core.getExistingResource(this) as Promise)))() } this.handleConflicts() } async push(...soundEvents: SoundsIndexClass[] | SoundEventClass[]) { await SoundsIndexClass.mergeSounds(this, soundEvents, /* prepend */ false) } async unshift(...soundEvents: SoundsIndexClass[] | SoundEventClass[]) { await SoundsIndexClass.mergeSounds(this, soundEvents, /* prepend */ true) } private static async mergeSounds( target: SoundsIndexClass, soundEvents: (SoundsIndexClass | SoundEventClass)[], prepend: boolean, ) { for (const event of soundEvents) { if (event instanceof SoundsIndexClass) { const def = await event.json const s = await target.json // TODO: Implement sound event merging target.json = prepend ? { ...def, ...s } : { ...s, ...def } } else { const s = await target.json // TODO: Implement sound event options s[event.fullId] = { sounds: [event.fullId], } } } } }