UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

107 lines (106 loc) 5.15 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 Utilities_1 = __importDefault(require("../core/Utilities")); const Command_1 = __importDefault(require("../minecraft/Command")); const CreatorTools_1 = require("./CreatorTools"); class CommandRunner { static async runCustomTool(creatorTools, commandNumber) { if (creatorTools.activeMinecraftState !== CreatorTools_1.CreatorToolsMinecraftState.started) { Log_1.default.debug("Cannot run command " + commandNumber + "; not connected to Minecraft."); return; } const cartoCommand = creatorTools.getCustomTool(commandNumber - 1); let commandName = commandNumber + ""; if (cartoCommand.name !== undefined && cartoCommand.name.length > 0) { commandName = cartoCommand.name; } const operId = await creatorTools.notifyOperationStarted("Running command " + commandName); if (cartoCommand.text !== undefined) { await CommandRunner.runCommandText(creatorTools, cartoCommand.text); } else { // alert("This command is not defined, yet."); } await creatorTools.notifyOperationEnded(operId, "Command " + commandName + " complete."); } static async runCommandText(creatorTools, commandText) { const commandItems = commandText.split("\n"); await this.runCommandList(creatorTools, commandItems); } static async runCommandList(creatorTools, commandItems) { const resultItems = []; let lastStoredBlockX = -1; let lastStoredBlockY = -1; let lastStoredBlockZ = -1; let firstRelativeX; let firstRelativeY; let firstRelativeZ; let setFirstPosition = false; for (let i = 0; i < commandItems.length; i++) { let commandText = commandItems[i].trim(); if (commandText.length > 4 && !commandText.startsWith("#")) { if (!commandText.startsWith("/")) { commandText = "/" + commandText; } const commandStart = new Date(); const command = new Command_1.default(commandText); // fix up a command to use absolute positions based on the position of our first command. if (command.hasRelativeOrLocalCoordinates && setFirstPosition && firstRelativeX !== undefined && firstRelativeY !== undefined && firstRelativeZ !== undefined) { command.absolutizeCoordinates(lastStoredBlockX - firstRelativeX, lastStoredBlockY - firstRelativeY, lastStoredBlockZ - firstRelativeZ); } commandText = command.toString(); await creatorTools.notifyStatusUpdate("Running command '" + commandText + "'"); const commandResult = await creatorTools.runCommand(commandText); const result = commandResult?.data; let resultData; if (result !== undefined && result !== null && result.indexOf("{") >= 0) { try { resultData = Utilities_1.default.parseJson(result); } catch (e) { Log_1.default.debug("Failed to parse command result: " + e); } if (resultData !== undefined) { // store a position so that we can absolutize future commands if (resultData.position !== undefined && !setFirstPosition) { lastStoredBlockX = resultData.position.x; lastStoredBlockY = resultData.position.y; lastStoredBlockZ = resultData.position.z; const argX = command.firstX; if (argX !== undefined) { firstRelativeX = argX.position; } const argY = command.firstY; if (argY !== undefined) { firstRelativeY = argY.position; } const argZ = command.firstZ; if (argZ !== undefined) { firstRelativeZ = argZ.position; } setFirstPosition = true; } } } const resultItem = { lineNumber: i, dateTime: commandStart, response: resultData, }; resultItems.push(resultItem); } } return resultItems; } } exports.default = CommandRunner;