UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

238 lines (237 loc) 8.37 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 ManagedComponent_1 = require("./ManagedComponent"); const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities")); const Database_1 = __importDefault(require("./Database")); const MinecraftUtilities_1 = __importDefault(require("./MinecraftUtilities")); const Utilities_1 = __importDefault(require("../core/Utilities")); // this is a type that is "legacy" version 1.10 definitions of item types class ItemTypeResourceDefinition { wrapper = null; _behaviorPackFile; _id; _isLoaded = false; _loadedWithComments = false; _managed = {}; _data; _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 onComponentAdded() { return this._onComponentAdded.asEvent(); } get onComponentRemoved() { return this._onComponentRemoved.asEvent(); } get onComponentChanged() { return this._onComponentChanged.asEvent(); } get isLoaded() { return this._isLoaded; } get behaviorPackFile() { return this._behaviorPackFile; } get onLoaded() { return this._onLoaded.asEvent(); } set behaviorPackFile(newFile) { this._behaviorPackFile = newFile; } get id() { if (this._id === undefined) { return ""; } return this._id; } set id(newId) { this._id = newId; } get shortId() { if (this._id !== undefined) { if (this._id.startsWith("minecraft:")) { return this._id.substring(10, this._id.length); } return this._id; } return undefined; } async getFormatVersionIsCurrent() { const fv = this.getFormatVersion(); if (fv === undefined || fv.length !== 3) { return false; } return await Database_1.default.isRecentVersionFromVersionArray(fv); } getFormatVersion() { if (!this.wrapper) { return undefined; } return MinecraftUtilities_1.default.getVersionArrayFrom(this.wrapper.format_version); } getComponent(id) { if (this._data === undefined) { return undefined; } if (!this._managed[id]) { const comp = this._data.components[id]; if (comp) { this._managed[id] = new ManagedComponent_1.ManagedComponent(this._data.components, id, comp); } } return this._managed[id]; } notifyComponentUpdated(id) { const component = this.getComponent(id); if (component === undefined) { Log_1.default.unexpectedUndefined("ITRNCU"); } else { this._onComponentChanged.dispatch(this, component); } } getAllComponents() { return this.getComponents(); } getComponents() { const componentSet = []; if (this._data !== undefined) { for (const componentName in this._data.components) { const component = this.getComponent(componentName); if (component !== undefined) { componentSet.push(component); } } } return componentSet; } setResourcePackFormatVersion(versionStr) { this._ensureDataInitialized(); if (this.wrapper) { this.wrapper.format_version = versionStr; } } addComponent(id, componentOrData) { this._ensureDataInitialized(); const bpData = this._data; const mc = componentOrData instanceof ManagedComponent_1.ManagedComponent ? componentOrData : new ManagedComponent_1.ManagedComponent(bpData.components, id, componentOrData); bpData.components[id] = mc.getData(); this._managed[id] = mc; this._onComponentAdded.dispatch(this, mc); return mc; } removeComponent(id) { if (this._data === undefined) { return; } const newBehaviorPacks = {}; const newManagedComponents = {}; for (const name in this._data.components) { if (name !== id && Utilities_1.default.isUsableAsObjectKey(name)) { const component = this._data.components[name]; newBehaviorPacks[name] = component; } } for (const name in this._managed) { if (name !== id && Utilities_1.default.isUsableAsObjectKey(name)) { newManagedComponents[name] = this._managed[name]; } } this._data.components = newBehaviorPacks; this._managed = newManagedComponents; } _ensureDataInitialized() { if (this._data === undefined) { this._data = { description: { identifier: "unknown", }, components: {}, }; } } static async ensureOnFile(file, loadHandler) { let itt; if (file.manager === undefined) { itt = new ItemTypeResourceDefinition(); itt.behaviorPackFile = file; file.manager = itt; } if (file.manager !== undefined && file.manager instanceof ItemTypeResourceDefinition) { itt = file.manager; if (!itt.isLoaded) { if (loadHandler) { itt.onLoaded.subscribe(loadHandler); } await itt.load(); } } return itt; } persist() { if (this._behaviorPackFile === undefined) { return false; } if (!this.wrapper) { Log_1.default.unexpectedUndefined("ITRDP"); return false; } return this._behaviorPackFile.setObjectContentIfSemanticallyDifferent(this.wrapper); } /** * Loads the definition from the file. * @param preserveComments If true, uses comment-preserving JSON parsing for edit/save cycles. * If false (default), uses efficient standard JSON parsing. * Can be called again with true to "upgrade" a read-only load to read/write. */ async load(preserveComments = false) { // If already loaded with comments, we have the "best" version - nothing more to do if (this._isLoaded && this._loadedWithComments) { return; } // If already loaded without comments and caller doesn't need comments, we're done if (this._isLoaded && !preserveComments) { return; } if (this._behaviorPackFile === undefined) { return; } if (!this._behaviorPackFile.isContentLoaded) { await this._behaviorPackFile.loadContent(); } if (this._behaviorPackFile.content === null || this._behaviorPackFile.content instanceof Uint8Array) { this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); return; } // Use comment-preserving parser only when needed for editing this.wrapper = preserveComments ? StorageUtilities_1.default.getJsonObjectWithComments(this._behaviorPackFile) : StorageUtilities_1.default.getJsonObject(this._behaviorPackFile); if (this.wrapper) { const item = this.wrapper["minecraft:item"]; if (item && item.description) { this.id = item.description.identifier; } this._data = item; } this._isLoaded = true; this._loadedWithComments = preserveComments; this._onLoaded.dispatch(this, this); } } exports.default = ItemTypeResourceDefinition;