bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
50 lines (49 loc) • 2.85 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.group", {
parent: (_a = CommandMap.getCommandEntry("root.entity")) === null || _a === void 0 ? void 0 : _a.command,
commandOptions(command) {
command
.name("group")
.description("adds a component group to entities")
.option("-g --group [component group]", 'the component group as a json object {"group_name":{"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 group overwrite the old one rather than merge with it")
.option("--no-add", 'do not add an "add" event')
.option("--no-remove", 'do not add an "remove" event');
},
commandAction: triggerEntityAddGroup,
});
function triggerEntityAddGroup(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
implementConfig();
const group = (_a = options.group) !== 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.setComponentGroups(JSON.parse(group), options.overwrite ? "overwrite" : "merge", { addEvent: options.add, removeEvent: options.remove });
}
catch (error) {
console.error(chalk.red(`Failed to parse ${group} due to ${error}`));
}
files.push(entity.toFile("overwrite"));
});
setFiles(files);
});
}