UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

96 lines (95 loc) 3.16 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 Log_1 = __importDefault(require("../../core/Log")); const ste_events_1 = require("ste-events"); const StorageUtilities_1 = __importDefault(require("../../storage/StorageUtilities")); const Database_1 = __importDefault(require("../Database")); class MojangBlocksDefinition { _data; _file; _isLoaded = false; _onLoaded = new ste_events_1.EventDispatcher(); get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } static async load(isPreview) { let metadataFolder = undefined; if (isPreview) { metadataFolder = await Database_1.default.loadPreviewMetadataFolder(); } else { metadataFolder = await Database_1.default.loadReleaseMetadataFolder(); } if (!metadataFolder) { return undefined; } const blocksFile = await metadataFolder.ensureFileFromRelativePath("/vanilladata_modules/mojang_blocks.json"); if (!blocksFile) { return undefined; } if (!(await blocksFile.exists())) { return undefined; } const mojangBlocksDef = await MojangBlocksDefinition.ensureOnFile(blocksFile); return mojangBlocksDef; } static async ensureOnFile(file, loadHandler) { let mbd; if (file.manager === undefined) { mbd = new MojangBlocksDefinition(); mbd.file = file; file.manager = mbd; } if (file.manager !== undefined && file.manager instanceof MojangBlocksDefinition) { mbd = file.manager; if (!mbd.isLoaded) { if (loadHandler) { mbd.onLoaded.subscribe(loadHandler); } await mbd.loadBlocks(); } } return mbd; } async loadBlocks() { if (this._isLoaded) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("LDDF"); return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (!this._file.content || this._file.content instanceof Uint8Array) { this._isLoaded = true; this._onLoaded.dispatch(this, this); return; } let data = {}; // Use getJsonObject (not getJsonObjectWithComments) since this is read-only metadata let result = StorageUtilities_1.default.getJsonObject(this._file); if (result) { data = result; } this._data = data; this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = MojangBlocksDefinition;