@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
171 lines (170 loc) • 6.86 kB
JavaScript
"use strict";
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 WorldTest_1 = __importDefault(require("./../worldtest/WorldTest"));
const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const Structure_1 = __importDefault(require("../minecraft/Structure"));
const BlockVolume_1 = __importDefault(require("../minecraft/BlockVolume"));
const BlockLocation_1 = __importDefault(require("../minecraft/BlockLocation"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
class WorldTestManager {
_jsonFile;
_jsFile;
_functionFile;
_structureFile;
_isLoaded;
_worldTest;
_worldTestData;
get worldTest() {
return this._worldTest;
}
_onLoaded = new ste_events_1.EventDispatcher();
constructor() {
this._isLoaded = false;
}
get isLoaded() {
return this._isLoaded;
}
get jsonFile() {
return this._jsonFile;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
get name() {
if (this._worldTestData === undefined) {
return "";
}
return this._worldTestData.name;
}
set name(newName) {
if (this._worldTestData === undefined) {
return;
}
this._worldTestData.name = newName;
}
set jsonFile(newFile) {
this._jsonFile = newFile;
}
async load() {
if (this._jsonFile === undefined || this._isLoaded) {
return;
}
await this._jsonFile.loadContent();
if (!this._jsonFile.content || this._jsonFile.content instanceof Uint8Array) {
return;
}
try {
const data = JSON.parse(this._jsonFile.content);
this._worldTestData = data;
if (data) {
this._worldTest = new WorldTest_1.default(data);
this._worldTest.ensureLoaded();
this._worldTest.name = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name);
}
}
catch (e) {
Log_1.default.fail("Could not parse world test JSON " + e);
}
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
}
async ensureFunctionFile() {
if (this._jsonFile === undefined || this._functionFile !== undefined) {
return;
}
const newFileName = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name) + ".mcfunction";
let functionFolder = this._jsonFile.parentFolder;
if (functionFolder.name === "scripts" && functionFolder.parentFolder) {
functionFolder = functionFolder.parentFolder.ensureFolder("functions");
}
await functionFolder.ensureExists();
this._functionFile = functionFolder.ensureFile(newFileName);
}
async persist() {
if (this._jsonFile === undefined || this._worldTest === undefined) {
return false;
}
if (!this._worldTest?.data) {
return false;
}
this.ensureFunctionFile();
return this._jsonFile.setObjectContentIfSemanticallyDifferent(this._worldTest.data);
}
async persistSideFiles(project) {
const bpFolder = await project.getDefaultBehaviorPackFolder();
if (!bpFolder || this._jsonFile === undefined) {
Log_1.default.unexpectedUndefined("WTM");
return;
}
if (this._jsFile === undefined) {
const scriptsFolder = await project.ensureBehaviorPackScriptsFolder();
const newFileName = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name) + ".gen.js";
this._jsFile = scriptsFolder.ensureFile(newFileName);
}
if (this._structureFile === undefined) {
const structuresFolder = bpFolder.ensureFolder("structures");
await structuresFolder.ensureExists();
const structuresSubFolder = structuresFolder.ensureFolder("gametest");
await structuresSubFolder.ensureExists();
const newFileName = StorageUtilities_1.default.getBaseFromName(this._jsonFile.name) + "empty.mcstructure";
this._structureFile = structuresSubFolder.ensureFile(newFileName);
}
if (this.worldTest && this._jsFile) {
const jsString = this.worldTest.generateGameTestJavaScript(this._jsonFile.name);
this._jsFile.setContent(jsString);
}
if (this.worldTest) {
const functionsFolder = bpFolder.ensureFolder("functions");
await functionsFolder.ensureExists();
for (let i = 0; i < this.worldTest.areas.length; i++) {
const area = this.worldTest.areas[i];
for (let j = 0; j < area.scripts.length; j++) {
const script = area.scripts[j];
const functionFile = functionsFolder.ensureFile(Utilities_1.default.getSimpleString(area.title + (i + 1).toString() + script.name + (j + 1).toString()) +
".mcfunction");
const functionString = "tp " + area.location.x + " " + area.location.y + " " + area.location.z;
/*"\ngametest run " +
Utilities.getSimpleString(area.title) +
":" +
Utilities.getSimpleString(script.name + (j + 1).toString());*/
functionFile.setContent(functionString);
}
}
}
if (this.worldTest && this._structureFile) {
const structure = new Structure_1.default();
const cube = new BlockVolume_1.default();
cube.setMaxDimensions(8, 8, 8);
structure.cube = cube;
cube.getBlock(new BlockLocation_1.default(0, 0, 0)).typeName = "minecraft:dirt";
const bytes = structure.getMCStructureBytes();
if (bytes) {
this._structureFile.setContent(bytes);
}
}
}
static async ensureOnFile(file, loadHandler) {
let wtm = undefined;
if (file.manager === undefined) {
const wtm = new WorldTestManager();
wtm.jsonFile = file;
file.manager = wtm;
}
if (file.manager !== undefined && file.manager instanceof WorldTestManager) {
wtm = file.manager;
if (!wtm.isLoaded) {
if (loadHandler) {
wtm.onLoaded.subscribe(loadHandler);
}
await wtm.load();
}
}
return wtm;
}
}
exports.default = WorldTestManager;