UNPKG

bedrock-development

Version:

APIs for creating and editing files related to Minecraft Bedrock development.

48 lines (47 loc) 2.65 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var _a; import { Option } from "commander"; import { getFiles, getStringFromTemporaryFile, setFiles } from "../../file_manager.js"; import { ServerEntity } from "../../types/index.js"; import { chalk, implementConfig } from "../../utils.js"; import { CommandMap } from "../command_map.js"; CommandMap.addCommand("root.entity.sensor", { parent: (_a = CommandMap.getCommandEntry("root.entity")) === null || _a === void 0 ? void 0 : _a.command, commandOptions(command) { command .name("sensor") .description("adds a damage sensor to entities") .option("-d, --damage_sensor [damage sensor]", 'the damage sensor as a json object {"cause": "all", "deals_damage": false}') .option("-t, --type <family type...>", "filter entities by family type") .addOption(new Option("-f, --file [file]", "the entity files that should be modified").makeOptionMandatory().preset("**/*.json")) .option("-s, --start", "adds the new sensor to the start of the array, rather than the end"); }, commandAction: triggerEntityAddDamageSensor, }); function triggerEntityAddDamageSensor(options) { var _a; return __awaiter(this, void 0, void 0, function* () { implementConfig(); const sensor = (_a = options.sensor) !== null && _a !== void 0 ? _a : yield getStringFromTemporaryFile(); const files = []; const entities = getFiles(ServerEntity.DirectoryPath + options.file).map(file => ServerEntity.fromFile(ServerEntity, file)).filter(entity => entity.hasFamilyTypes(...options.type)); entities.forEach(entity => { try { entity.setDamageSensor(JSON.parse(sensor), { prepend: options.start }); } catch (error) { console.error(chalk.red(`Failed to parse ${sensor} due to ${error}`)); } files.push(entity.toFile("overwrite")); }); setFiles(files); }); }