UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

146 lines (145 loc) 6.07 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const CommandArgument_1 = __importStar(require("./CommandArgument")); class Command { _commandName; _arguments; constructor(commandText) { const firstSpace = commandText.indexOf(" "); this._arguments = []; let _coordinatePos = -1; if (firstSpace < 0) { this._commandName = commandText; } else { this._commandName = commandText.substring(0, firstSpace); const args = commandText.substring(firstSpace + 1, commandText.length).split(" "); for (let i = 0; i < args.length; i++) { const cmdArg = new CommandArgument_1.default(args[i]); if (cmdArg.type === CommandArgument_1.CommandArgumentType.coordinate) { _coordinatePos++; if (_coordinatePos >= 3) { _coordinatePos = 0; } if (_coordinatePos === 0) { cmdArg.location = CommandArgument_1.CoordinateLocation.x; } else if (_coordinatePos === 1) { cmdArg.location = CommandArgument_1.CoordinateLocation.y; } else if (_coordinatePos === 2) { cmdArg.location = CommandArgument_1.CoordinateLocation.z; } } else { _coordinatePos = -1; } this._arguments.push(cmdArg); } } if (this._commandName.startsWith("/")) { this._commandName = this._commandName.substring(1, this._commandName.length); } } absolutizeCoordinates(absoluteX, absoluteY, absoluteZ) { for (let i = 0; i < this._arguments.length; i++) { const arg = this._arguments[i]; if (arg.type === CommandArgument_1.CommandArgumentType.coordinate) { if (arg.coordinateType === CommandArgument_1.CoordinateType.relativeTilde) { if (arg.location === CommandArgument_1.CoordinateLocation.x) { arg.coordinateType = CommandArgument_1.CoordinateType.absolute; arg.position = absoluteX + arg.position; } else if (arg.location === CommandArgument_1.CoordinateLocation.y) { arg.coordinateType = CommandArgument_1.CoordinateType.absolute; arg.position = absoluteY + arg.position; } else if (arg.location === CommandArgument_1.CoordinateLocation.z) { arg.coordinateType = CommandArgument_1.CoordinateType.absolute; arg.position = absoluteZ + arg.position; } } } } } get hasRelativeOrLocalCoordinates() { for (let i = 0; i < this._arguments.length; i++) { const arg = this._arguments[i]; if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.coordinateType !== CommandArgument_1.CoordinateType.absolute) { return true; } } return false; } get firstX() { for (let i = 0; i < this._arguments.length; i++) { const arg = this._arguments[i]; if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.x) { return arg; } } return undefined; } get firstY() { for (let i = 0; i < this._arguments.length; i++) { const arg = this._arguments[i]; if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.y) { return arg; } } return undefined; } get firstZ() { for (let i = 0; i < this._arguments.length; i++) { const arg = this._arguments[i]; if (arg.type === CommandArgument_1.CommandArgumentType.coordinate && arg.location === CommandArgument_1.CoordinateLocation.z) { return arg; } } return undefined; } toString() { let commandResult = this._commandName; for (let i = 0; i < this._arguments.length; i++) { commandResult += " "; commandResult += this._arguments[i].toString(); } return commandResult; } } exports.default = Command;