UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

250 lines (249 loc) 8.5 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Utilities_1 = __importDefault(require("../core/Utilities")); const ste_events_1 = require("ste-events"); const Log_1 = __importDefault(require("../core/Log")); const ManagedComponent_1 = require("./ManagedComponent"); class BiomeResourceDefinition { _typeId = ""; _file; _id; _isLoaded = false; _data; _managed = {}; _onLoaded = new ste_events_1.EventDispatcher(); _onComponentAdded = new ste_events_1.EventDispatcher(); _onComponentRemoved = new ste_events_1.EventDispatcher(); _onComponentChanged = new ste_events_1.EventDispatcher(); get data() { return this._data; } get clientBiomeData() { this._ensureBiomeDataInitialized(); return this._data["minecraft:client_biome"]; } get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } get onComponentAdded() { return this._onComponentAdded.asEvent(); } get onComponentRemoved() { return this._onComponentRemoved.asEvent(); } get onComponentChanged() { return this._onComponentChanged.asEvent(); } constructor(typeId) { if (typeId) { this._typeId = typeId; this._id = typeId; } } get typeId() { return this._typeId; } set typeId(newId) { this._typeId = newId; this._id = newId; } get id() { return this._id || this._typeId || ""; } set id(newId) { this._id = newId; this._typeId = newId; } _ensureBiomeDataInitialized() { if (!this._data || !this._data["minecraft:client_biome"]) { this._data = { format_version: "1.16.0", "minecraft:client_biome": { description: { identifier: this._typeId, }, components: {}, }, }; } if (!this._data["minecraft:client_biome"]) { this._data["minecraft:client_biome"] = { description: { identifier: this._typeId, }, components: {}, }; } if (!this._data["minecraft:client_biome"].description) { this._data["minecraft:client_biome"].description = { identifier: this._typeId, }; } if (!this._data["minecraft:client_biome"].components) { this._data["minecraft:client_biome"].components = {}; } } ensureComponent(id, defaultData) { const comp = this.getComponent(id); if (comp) { return comp; } return this.addComponent(id, defaultData); } getComponent(id) { if (this._data === undefined) { return undefined; } if (!this._managed[id]) { if (this._data["minecraft:client_biome"]?.components) { const comp = this._data["minecraft:client_biome"].components[id]; if (comp) { this._managed[id] = new ManagedComponent_1.ManagedComponent(this._data["minecraft:client_biome"].components, id, comp); } } } return this._managed[id]; } notifyComponentUpdated(id) { const component = this.getComponent(id); if (component === undefined) { Log_1.default.unexpectedUndefined("BDNCU"); } else { this._onComponentChanged.dispatch(this, component); } } getAllComponents() { return this.getComponents(); } getComponents() { const componentSet = []; if (this._data !== undefined && this._data["minecraft:client_biome"]?.components) { for (const componentName in this._data["minecraft:client_biome"].components) { const component = this.getComponent(componentName); if (component !== undefined) { componentSet.push(component); } } } return componentSet; } addComponent(id, componentOrData) { this._ensureBiomeDataInitialized(); const biomeData = this._data; this.ensureDefinition(); if (!biomeData || !biomeData["minecraft:client_biome"] || !biomeData["minecraft:client_biome"].components) { throw new Error(); } const mc = componentOrData instanceof ManagedComponent_1.ManagedComponent ? componentOrData : new ManagedComponent_1.ManagedComponent(biomeData["minecraft:client_biome"].components, id, componentOrData); biomeData["minecraft:client_biome"].components[id] = mc.getData(); this._managed[id] = mc; this._onComponentAdded.dispatch(this, mc); return mc; } removeComponent(id) { if (this._data === undefined || !this._data["minecraft:client_biome"]?.components) { return; } const newComponents = {}; const newManagedComponents = {}; for (const name in this._data["minecraft:client_biome"].components) { if (name !== id) { if (Utilities_1.default.isUsableAsObjectKey(name)) { const component = this._data["minecraft:client_biome"].components[name]; newComponents[name] = component; } } } for (const name in this._managed) { if (name !== id) { newManagedComponents[name] = this._managed[name]; } } this._data["minecraft:client_biome"].components = newComponents; this._managed = newManagedComponents; this._onComponentRemoved.dispatch(this, id); } static async ensureOnFile(file) { if (file.manager === undefined) { const bd = new BiomeResourceDefinition(); bd._file = file; if (!bd.isLoaded) { await bd.load(); } file.manager = bd; } if (file.manager instanceof BiomeResourceDefinition) { return file.manager; } return undefined; } async persist() { if (this._file === undefined) { return false; } Log_1.default.assert(this._data !== null, "ITDP"); if (!this._data) { return false; } return this._file.setObjectContentIfSemanticallyDifferent(this._data); } async load() { if (this._file === undefined) { return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } const fileContent = this._file.content; if (fileContent === null || fileContent instanceof Uint8Array) { this._isLoaded = true; this._onLoaded.dispatch(this, this); return; } try { this._data = JSON.parse(fileContent); if (this._data && this._data["minecraft:client_biome"]?.description?.identifier) { this._typeId = this._data["minecraft:client_biome"].description.identifier; this._id = this._typeId; } this._isLoaded = true; this._onLoaded.dispatch(this, this); } catch (e) { Log_1.default.error("Could not load biome definition: " + e); } } get shortId() { if (this._typeId && this._typeId.indexOf(":") >= 0) { return this._typeId.substring(this._typeId.indexOf(":") + 1); } return this._typeId; } get namespace() { if (this._typeId && this._typeId.indexOf(":") >= 0) { return this._typeId.substring(0, this._typeId.indexOf(":")); } return undefined; } ensureDefinition() { this._ensureBiomeDataInitialized(); if (this._data && this._data["minecraft:client_biome"]?.description) { this._data["minecraft:client_biome"].description.identifier = this._typeId; } } } exports.default = BiomeResourceDefinition;