UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

272 lines (271 loc) 11.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const CreatorTools_1 = require("../app/CreatorTools"); const IMinecraft_1 = require("../app/IMinecraft"); const AppServiceProxy_1 = __importStar(require("../core/AppServiceProxy")); const ste_events_1 = require("ste-events"); const Utilities_1 = __importDefault(require("../core/Utilities")); const GameStateManager_1 = __importDefault(require("../minecraft/GameStateManager")); const Log_1 = __importDefault(require("../core/Log")); const ICreatorToolsData_1 = require("../app/ICreatorToolsData"); const ProjectExporter_1 = __importDefault(require("../app/ProjectExporter")); class MinecraftGameProxyMinecraft { _creatorTools; state; _onStateChanged = new ste_events_1.EventDispatcher(); _onRefreshed = new ste_events_1.EventDispatcher(); _project; _gameStateManager; errorStatus; errorMessage; worldFolder; projectFolder; worldContentStorage; worldProject; _onWorldFolderReady = new ste_events_1.EventDispatcher(); _onProjectFolderReady = new ste_events_1.EventDispatcher(); _onMessage = new ste_events_1.EventDispatcher(); get onWorldFolderReady() { return this._onWorldFolderReady.asEvent(); } get onProjectFolderReady() { return this._onProjectFolderReady.asEvent(); } get onMessage() { return this._onMessage.asEvent(); } get canDeployFiles() { return this._creatorTools.minecraftGameMode !== ICreatorToolsData_1.MinecraftGameConnectionMode.remoteMinecraft; } get onRefreshed() { return this._onRefreshed.asEvent(); } get onStateChanged() { return this._onStateChanged.asEvent(); } constructor(creatorTools) { this._creatorTools = creatorTools; this._gameStateManager = new GameStateManager_1.default(this._creatorTools); this.state = CreatorTools_1.CreatorToolsMinecraftState.none; } async updateStatus() { return this.state; } get activeProject() { return this._project; } set activeProject(newProject) { this._project = newProject; } get gameStateManager() { return this._gameStateManager; } notifyStateChanged(newVal) { this.state = newVal; this._onStateChanged.dispatch(this, newVal); } async initialize() { await this.start(); } async prepare(force) { } async pushWorld() { if (!this._creatorTools || this._creatorTools.deploymentStorage === null || !this._project) { return; } const worldFolderName = this._project.deployWorldId; const worldDisplayName = this._project.name + " _mct"; const worldsFolder = await ProjectExporter_1.default.ensureMinecraftWorldsFolder(this._creatorTools); if (!worldsFolder) { Log_1.default.debug("Could not find a Minecraft world."); return; } const worldFolder = worldsFolder.ensureFolder(worldFolderName); await worldFolder.ensureExists(); await ProjectExporter_1.default.syncFlatPackRefWorldTo(this._creatorTools, this._project, worldFolder, worldDisplayName); await worldFolder.saveAll(); await this._project.save(); return worldDisplayName; } async syncWithDeployment() { const dt = this._creatorTools.defaultDeploymentTarget; if (dt == null || dt.deployBehaviorPacksFolder == null || this._creatorTools.minecraftGameMode === ICreatorToolsData_1.MinecraftGameConnectionMode.remoteMinecraft) { throw new Error("This instance doesn't support deployment"); } if (!this._project) { return; } let isAvailable = dt.storage.available; if (isAvailable === undefined) { isAvailable = await dt.storage.getAvailable(); } if (!isAvailable) { return; } const deployFolderExists = await dt.deployBehaviorPacksFolder.exists(); if (deployFolderExists) { await ProjectExporter_1.default.deployProject(this._creatorTools, this._project, dt.storage.rootFolder); } } processExternalMessage(command, data) { switch (command) { case "wsevent": let obj = undefined; try { obj = JSON.parse(data); } catch (e) { } if (obj) { this._gameStateManager.handleEvent(obj); } break; case "webSocketCommandComplete": Log_1.default.message(data); break; case "webSocketConnected": this.notifyStateChanged(CreatorTools_1.CreatorToolsMinecraftState.started); if (!this._creatorTools.successfullyConnectedWebSocketToMinecraft) { this._creatorTools.successfullyConnectedWebSocketToMinecraft = true; this._creatorTools.save(); } break; case "webSocketDisconnected": this.notifyStateChanged(CreatorTools_1.CreatorToolsMinecraftState.disconnected); break; } } async runActionSet(actionSet) { return undefined; } async runCommand(command) { if (!AppServiceProxy_1.default.hasAppService && Utilities_1.default.isDebug) { // simulate a command in debug Log_1.default.debugAlert("Simulating command to Minecraft:\n\n" + command + "\n\n"); if (command.indexOf("fail") >= 0) { return JSON.stringify({ statusCode: -2147483648, statusMessage: "Failed command " + command, }); } else { return JSON.stringify({ statusCode: 0, statusMessage: "Success command " + command, position: { x: -100, y: 100, z: -100, }, }); } } else { let result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.webSocketCommand, command); if (result !== undefined) { this.logCommandResult(result); } return result; } } logCommandResult(result) { if (result) { if (result.indexOf("{") >= 0) { try { const commandResponse = JSON.parse(result); if (commandResponse.statusMessage) { this._creatorTools.notifyStatusUpdate("Minecraft Game: " + commandResponse.statusMessage); return; } } catch (e) { Log_1.default.fail(e); } } } this._creatorTools.notifyStatusUpdate("Minecraft Game: " + result); } canPrepare() { return true; } async prepareAndStart(push) { let worldName = undefined; if (push.project) { this._project = push.project; await this.syncWithDeployment(); } if (push.worldType) { worldName = await this.pushWorld(); } await this.start(); return { type: IMinecraft_1.PrepareAndStartResultType.started, worldName: worldName, }; } async stop() { } async start() { if (!AppServiceProxy_1.default.hasAppService && Utilities_1.default.isDebug) { this.notifyStateChanged(CreatorTools_1.CreatorToolsMinecraftState.stopped); } else if (this.state === CreatorTools_1.CreatorToolsMinecraftState.none || this.state === CreatorTools_1.CreatorToolsMinecraftState.error || this.state === CreatorTools_1.CreatorToolsMinecraftState.disconnected) { this.notifyStateChanged(CreatorTools_1.CreatorToolsMinecraftState.initializing); AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.startWebSocketServer, "").then(async (result) => { const getMinecraftGameProjectDeployDir = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.getMinecraftGameProjectDeployDir, this._creatorTools.minecraftGameMode); const getMinecraftGameWorldDeployDir = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.getMinecraftGameWorldDeployDir, this._creatorTools.minecraftGameMode); if (getMinecraftGameWorldDeployDir && getMinecraftGameWorldDeployDir.length > 0 && this._creatorTools.ensureLocalFolder) { this.worldFolder = this._creatorTools.ensureLocalFolder(getMinecraftGameWorldDeployDir); this._onWorldFolderReady.dispatch(this, this.worldFolder); } if (getMinecraftGameProjectDeployDir && getMinecraftGameProjectDeployDir.length > 0 && this._creatorTools.ensureLocalFolder) { this.projectFolder = this._creatorTools.ensureLocalFolder(getMinecraftGameProjectDeployDir); this._onProjectFolderReady.dispatch(this, this.projectFolder); } this.notifyStateChanged(CreatorTools_1.CreatorToolsMinecraftState.initialized); }); } } } exports.default = MinecraftGameProxyMinecraft;