bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
76 lines (75 loc) • 2.91 kB
JavaScript
import { Directories } from "../../file_manager.js";
import { NameData, currentFormatVersion } from "../../utils.js";
import { MinecraftDataType } from "../minecraft.js";
export class ClientSoundDefinitions extends MinecraftDataType {
static get DirectoryPath() {
return Directories.RESOURCE_PATH + 'sounds/';
}
constructor(filepath, template) {
super(filepath, template);
this.format_version = template.format_version;
this.sound_definitions = template.sound_definitions;
}
static createFilePath() {
return this.DirectoryPath + "sound_definitions.json";
}
static createFromTemplate() {
return new ClientSoundDefinitions(this.createFilePath(), {
format_version: currentFormatVersion,
sound_definitions: {}
});
}
static fileWithAddedSound(name, sound) {
const sound_def = ClientSoundDefinitions.fromPathOrTemplate(ClientSoundDefinitions, ClientSoundDefinitions.createFilePath());
sound_def.addSound(name, sound);
sound_def.convertSoundsToObjects();
return sound_def.toFile();
}
toFile() {
return { filePath: this.filePath, fileContents: this.serialize(), handleExisting: 'overwrite' };
}
replacer(key, value) {
if (key === "sounds") {
const writeArr = [];
value.forEach(sound => {
if (typeof (sound) === 'string') {
writeArr.push(sound);
}
else {
writeArr.push(`REMOVE${JSON.stringify(sound, (key, value) => {
switch (key) {
case "volume":
case "pitch":
if (value % 1 === 0) {
return `REMOVE${value}.0REMOVE`;
}
default:
return value;
}
})}REMOVE`.replace(/:/g, ': ').replace(/,/g, ', '));
}
});
return writeArr;
}
return value;
}
addSound(name, sound) {
if (!name.includes(NameData.ProjectName))
name = `${NameData.TeamName}_${NameData.ProjectName}:${name}`;
this.sound_definitions[name] = sound;
}
convertSoundsToObjects() {
Object.keys(this.sound_definitions).forEach(key => {
const newSounds = [];
this.sound_definitions[key].sounds.forEach(sound => {
if (typeof (sound) === 'string') {
newSounds.push({ name: sound, volume: 1, pitch: 1, load_on_low_memory: true });
}
else {
newSounds.push(sound);
}
});
this.sound_definitions[key].sounds = newSounds;
});
}
}