bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
307 lines (306 loc) • 14.4 kB
JavaScript
import { Directories } from "../../file_manager.js";
import { mergeDeep, NameData, chalk, currentFormatVersion } from "../../utils.js";
import { MinecraftDataType } from "../minecraft.js";
export class ServerEntity extends MinecraftDataType {
static get DirectoryPath() {
return Directories.BEHAVIOR_PATH + 'entities/';
}
get Identifier() {
return this["minecraft:entity"].description.identifier;
}
get NameData() {
return new NameData(this.Identifier);
}
constructor(filepath, template) {
super(filepath, template);
this.format_version = template.format_version;
this["minecraft:entity"] = template["minecraft:entity"];
}
static createFromTemplate(nameData) {
return new ServerEntity(this.createFilePath(nameData), {
format_version: currentFormatVersion,
"minecraft:entity": {
description: {
identifier: nameData.fullname,
is_spawnable: false,
is_summonable: true,
is_experimental: false,
},
component_groups: {
"instant_despawn": {
"minecraft:instant_despawn": {}
}
},
components: {
"minecraft:type_family": {
family: [
nameData.namespace,
nameData.shortname,
]
},
"minecraft:collision_box": {
height: 0,
width: 0,
},
"minecraft:physics": {
has_collision: false,
has_gravity: false,
},
"minecraft:damage_sensor": {
triggers: [
{
on_damage: {
filters: {
test: "has_damage",
value: "void",
},
event: "despawn"
}
},
{
cause: "all",
deals_damage: false,
}
]
}
},
events: {
"despawn": {
add: {
component_groups: [
"instant_despawn"
]
}
}
}
}
});
}
setComponents(components, handleExisting = 'overwrite') {
Object.keys(components).forEach(key => {
const component = this["minecraft:entity"].components[key];
if (component) {
switch (handleExisting) {
case 'ignore':
console.warn(chalk.yellow(`${this.Identifier} already has component ${key}`));
return;
case 'merge':
mergeDeep(this["minecraft:entity"].components[key], components[key]);
return;
case 'overwrite':
console.log(chalk.green(`Overwriting existing component ${key} on ${this.Identifier}`));
this["minecraft:entity"].components[key] = components[key];
return;
}
}
console.log(chalk.green(`Added component ${key} to ${this.Identifier}`));
this["minecraft:entity"].components[key] = components[key];
});
}
setComponentGroups(groups, handleExisting = 'overwrite', options) {
var _a;
this["minecraft:entity"].component_groups = (_a = this["minecraft:entity"].component_groups) !== null && _a !== void 0 ? _a : {};
Object.keys(groups).forEach(key => {
const group = this["minecraft:entity"].component_groups[key];
if (group) {
switch (handleExisting) {
case 'ignore':
console.warn(chalk.yellow(`${this.Identifier} already has component group ${key}`));
break;
case 'merge':
mergeDeep(this["minecraft:entity"].component_groups[key], groups[key]);
break;
case 'overwrite':
console.log(chalk.green(`Overwriting existing component group ${key} on ${this.Identifier}`));
this["minecraft:entity"].component_groups[key] = groups[key];
break;
}
}
else {
console.log(chalk.green(`Added component group ${key} to ${this.Identifier}`));
this["minecraft:entity"].component_groups[key] = groups[key];
}
if (options === null || options === void 0 ? void 0 : options.addEvent) {
this.setEvents({
[`add_${key}`]: {
add: {
component_groups: [key]
}
}
});
}
if (options === null || options === void 0 ? void 0 : options.removeEvent) {
this.setEvents({
[`remove_${key}`]: {
remove: {
component_groups: [key]
}
}
});
}
});
}
setEvents(events, handleExisting = 'overwrite') {
Object.keys(events).forEach(key => {
const entityEvents = this["minecraft:entity"].events[key];
if (entityEvents) {
switch (handleExisting) {
case 'ignore':
console.warn(chalk.yellow(`${this.Identifier} already has event ${key}`));
return;
case 'merge':
mergeDeep(this["minecraft:entity"].events[key], events[key]);
break;
case 'overwrite':
console.log(chalk.green(`Overwriting existing event ${key} on ${this.Identifier}`));
this["minecraft:entity"].events[key] = events[key];
return;
}
}
console.log(chalk.green(`Added event ${key} to ${this.Identifier}`));
this["minecraft:entity"].events[key] = events[key];
});
}
setProperties(properties, handleExisting = 'overwrite', options) {
var _a;
this["minecraft:entity"].description.properties = (_a = this["minecraft:entity"].description.properties) !== null && _a !== void 0 ? _a : {};
Object.keys(properties).forEach(key => {
var _a, _b, _c;
const idKey = new NameData(key).fullname;
const property = this["minecraft:entity"].description.properties[idKey];
if (property) {
switch (handleExisting) {
case 'ignore':
console.warn(chalk.yellow(`${this.Identifier} already has property ${idKey}`));
break;
case 'merge':
mergeDeep(this["minecraft:entity"].description.properties[idKey], properties[idKey]);
case 'overwrite':
console.log(chalk.green(`Overwriting existing property ${idKey} on ${this.Identifier}`));
this["minecraft:entity"].description.properties[idKey] = properties[idKey];
break;
}
}
else {
console.log(chalk.green(`Added property ${idKey} to ${this.Identifier}`));
this["minecraft:entity"].description.properties[idKey] = properties[idKey];
}
if (options === null || options === void 0 ? void 0 : options.createEvents) {
const keyName = new NameData(idKey);
switch (properties[idKey].type) {
case "bool":
this.setEvents({
[`set_${keyName.shortname}_to_false`]: {
set_property: {
[idKey]: false
}
},
[`set_${keyName.shortname}_to_true`]: {
set_property: {
[idKey]: true
}
},
});
break;
case "enum":
const enum_events = {};
(_a = properties[idKey].values) === null || _a === void 0 ? void 0 : _a.forEach(value => {
enum_events[`set_${keyName.shortname}_to_${value}`] = {
set_property: {
[idKey]: value
}
};
});
this.setEvents(enum_events);
break;
case "float":
case "int":
let start = (_b = properties[idKey].range) === null || _b === void 0 ? void 0 : _b[0];
let end = (_c = properties[idKey].range) === null || _c === void 0 ? void 0 : _c[1];
if (start !== undefined && end !== undefined) {
const number_events = {};
for (let index = start; index <= end; index++) {
number_events[`set_${keyName.shortname}_to_${index}`] = {
set_property: {
[idKey]: index
}
};
}
this.setEvents(number_events);
}
break;
}
}
});
}
setDamageSensor(sensor, options) {
var _a;
this["minecraft:entity"].components["minecraft:damage_sensor"] = (_a = this["minecraft:entity"].components["minecraft:damage_sensor"]) !== null && _a !== void 0 ? _a : { triggers: [] };
if (!Array.isArray(this["minecraft:entity"].components["minecraft:damage_sensor"].triggers)) {
const ogTrigger = this["minecraft:entity"].components["minecraft:damage_sensor"].triggers;
this["minecraft:entity"].components["minecraft:damage_sensor"].triggers = [ogTrigger];
}
if (options === null || options === void 0 ? void 0 : options.prepend) {
this["minecraft:entity"].components["minecraft:damage_sensor"].triggers.splice(0, 0, sensor);
}
else {
this["minecraft:entity"].components["minecraft:damage_sensor"].triggers.push(sensor);
}
}
setAnimations(animations, handleExisting = 'overwrite', options) {
var _a;
this["minecraft:entity"].description.animations = (_a = this["minecraft:entity"].description.animations) !== null && _a !== void 0 ? _a : {};
Object.keys(animations).forEach(key => {
const entityAnimations = this["minecraft:entity"].description.animations[key];
if (entityAnimations) {
switch (handleExisting) {
case 'ignore':
console.warn(chalk.yellow(`${this.Identifier} already has animation reference ${key}`));
break;
case 'merge':
mergeDeep(this["minecraft:entity"].description.animations[key], animations[key]);
break;
case 'overwrite':
console.log(chalk.green(`Overwriting existing animation reference ${key} on ${this.Identifier}`));
this["minecraft:entity"].description.animations[key] = animations[key];
break;
}
}
else {
console.log(chalk.green(`Added animation reference ${key} to ${this.Identifier}`));
this["minecraft:entity"].description.animations[key] = animations[key];
}
if (options === null || options === void 0 ? void 0 : options.createScriptEntry) {
this.setAnimateScripts(key);
}
});
}
setAnimateScripts(...animations) {
var _a, _b;
this["minecraft:entity"].description.scripts = (_a = this["minecraft:entity"].description.scripts) !== null && _a !== void 0 ? _a : {};
this["minecraft:entity"].description.scripts.animate = (_b = this["minecraft:entity"].description.scripts.animate) !== null && _b !== void 0 ? _b : [];
animations.forEach(animation => {
if (!this["minecraft:entity"].description.scripts.animate.includes(animation)) {
this["minecraft:entity"].description.scripts.animate.push(animation);
}
});
}
hasFamilyTypes(...family) {
if (!family.length)
return true;
return family.every(fType => {
var _a, _b;
if ((_a = this["minecraft:entity"].components["minecraft:type_family"]) === null || _a === void 0 ? void 0 : _a.family.includes(fType)) {
return true;
}
if (this["minecraft:entity"].component_groups) {
for (const key of Object.keys(this["minecraft:entity"].component_groups)) {
if ((_b = this["minecraft:entity"].component_groups[key]["minecraft:type_family"]) === null || _b === void 0 ? void 0 : _b.family.includes(fType)) {
return true;
}
}
}
return false;
});
}
}