@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
128 lines (127 loc) • 4.37 kB
JavaScript
"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 ste_events_1 = require("ste-events");
const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const Log_1 = __importDefault(require("../core/Log"));
class Dialogue {
_file;
_id;
_isLoaded = false;
_loadedWithComments = false;
definition;
_onLoaded = new ste_events_1.EventDispatcher();
get isLoaded() {
return this._isLoaded;
}
get file() {
return this._file;
}
set file(newFile) {
this._file = newFile;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
static async ensureOnFile(file, loadHandler) {
let dia;
if (file.manager === undefined) {
dia = new Dialogue();
dia.file = file;
file.manager = dia;
}
if (file.manager !== undefined && file.manager instanceof Dialogue) {
dia = file.manager;
if (!dia.isLoaded) {
if (loadHandler) {
dia.onLoaded.subscribe(loadHandler);
}
await dia.load();
}
}
return dia;
}
getAllButtons() {
const buttons = [];
if (this.definition &&
this.definition["minecraft:npc_dialogue"] &&
Array.isArray(this.definition["minecraft:npc_dialogue"].scenes)) {
for (const scene of this.definition["minecraft:npc_dialogue"].scenes) {
if (scene && scene.buttons && Array.isArray(scene.buttons)) {
for (const button of scene.buttons) {
buttons.push(button);
}
}
}
}
return buttons;
}
persist() {
if (this._file === undefined) {
return false;
}
Log_1.default.assert(this.definition !== null, "DGUEP");
if (!this.definition) {
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this.definition);
}
ensureDefinition(name, description) {
if (!this.definition) {
this.definition = {
format_version: "1.12.0",
"minecraft:npc_dialogue": {
scenes: [],
},
};
}
}
async save() {
if (this._file === undefined) {
return;
}
if (this.persist()) {
await this._file.saveContent(false);
}
}
/**
* 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._file === undefined) {
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
if (this._file.content === null || this._file.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.definition = preserveComments
? StorageUtilities_1.default.getJsonObjectWithComments(this._file)
: StorageUtilities_1.default.getJsonObject(this._file);
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
}
}
exports.default = Dialogue;