bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
48 lines (47 loc) • 2.66 kB
JavaScript
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.component", {
parent: (_a = CommandMap.getCommandEntry("root.entity")) === null || _a === void 0 ? void 0 : _a.command,
commandOptions(command) {
command
.name("component")
.description("adds a component to entities")
.option("-c --component [component]", 'the component as a json object {"minecraft:is_baby":{}}')
.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("-o, --overwrite", "should the new component overwrite the old one rather than merge with it");
},
commandAction: triggerEntityAddComponent,
});
function triggerEntityAddComponent(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
implementConfig();
const component = (_a = options.component) !== 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.setComponents(JSON.parse(component), options.overwrite ? "overwrite" : "merge");
}
catch (error) {
console.error(chalk.red(`Failed to parse ${component} due to ${error}`));
}
files.push(entity.toFile("overwrite"));
});
setFiles(files);
});
}