UNPKG

bedrock-development

Version:

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

130 lines (129 loc) 7.21 kB
import { Directories } from "../../file_manager.js"; import { currentFormatVersion } from "../../utils.js"; import { MinecraftDataType } from "../minecraft.js"; export class ClientEntity extends MinecraftDataType { static get DirectoryPath() { return Directories.RESOURCE_PATH + 'entity/'; } constructor(filepath, template) { super(filepath, template); this.format_version = template.format_version; this["minecraft:client_entity"] = template["minecraft:client_entity"]; } static createFromTemplate(nameData) { return new ClientEntity(this.createFilePath(nameData), { format_version: currentFormatVersion, "minecraft:client_entity": { description: { identifier: nameData.fullname, materials: { default: "entity_alphatest", }, geometry: { default: `geometry.${nameData.namespace}.${nameData.shortname}`, }, textures: { default: `textures/${Directories.ADDON_PATH}entity/${nameData.directory}${nameData.shortname}/default`, }, render_controllers: [ "controller.render.default", ], spawn_egg: {} } } }); } static createFilePath(nameData) { return this.DirectoryPath + nameData.directory + nameData.shortname + ".entity.json"; } upgradeFormatVersion() { var _a; if (this.format_version !== '1.8.0') return; this.format_version = currentFormatVersion; if (this["minecraft:client_entity"].description.animation_controllers) { this["minecraft:client_entity"].description.animations = (_a = this["minecraft:client_entity"].description.animations) !== null && _a !== void 0 ? _a : {}; this["minecraft:client_entity"].description.animation_controllers.forEach(controller => { Object.keys(controller).forEach(key => { this["minecraft:client_entity"].description.animations[`ctrl.${key}`] = controller[key]; }); }); delete this["minecraft:client_entity"].description.animation_controllers; } } addInitializeVariable(...variables) { var _a, _b; this["minecraft:client_entity"].description.scripts = (_a = this["minecraft:client_entity"].description.scripts) !== null && _a !== void 0 ? _a : {}; this["minecraft:client_entity"].description.scripts.initialize = (_b = this["minecraft:client_entity"].description.scripts.initialize) !== null && _b !== void 0 ? _b : []; variables.forEach(variable => { var _a, _b; if (!((_a = this["minecraft:client_entity"].description.scripts.initialize) === null || _a === void 0 ? void 0 : _a.includes(variable))) { (_b = this["minecraft:client_entity"].description.scripts.initialize) === null || _b === void 0 ? void 0 : _b.push(variable); } }); } addPreAnimationVariable(...variables) { var _a, _b; this["minecraft:client_entity"].description.scripts = (_a = this["minecraft:client_entity"].description.scripts) !== null && _a !== void 0 ? _a : {}; this["minecraft:client_entity"].description.scripts.pre_animation = (_b = this["minecraft:client_entity"].description.scripts.pre_animation) !== null && _b !== void 0 ? _b : []; variables.forEach(variable => { var _a, _b; if (!((_a = this["minecraft:client_entity"].description.scripts.pre_animation) === null || _a === void 0 ? void 0 : _a.includes(variable))) { (_b = this["minecraft:client_entity"].description.scripts.pre_animation) === null || _b === void 0 ? void 0 : _b.push(variable); } }); } addMaterials(...materials) { var _a; this["minecraft:client_entity"].description.materials = (_a = this["minecraft:client_entity"].description.materials) !== null && _a !== void 0 ? _a : {}; materials.forEach(material => { this["minecraft:client_entity"].description.materials[material.name] = material.reference; }); } addGeometry(...geometry) { var _a; this["minecraft:client_entity"].description.geometry = (_a = this["minecraft:client_entity"].description.geometry) !== null && _a !== void 0 ? _a : {}; geometry.forEach(geometry => { this["minecraft:client_entity"].description.geometry[geometry.name] = geometry.reference; }); } addRenderController(...render_controllers) { var _a; this["minecraft:client_entity"].description.render_controllers = (_a = this["minecraft:client_entity"].description.render_controllers) !== null && _a !== void 0 ? _a : []; render_controllers.forEach(render_controller => { var _a, _b; if (!((_a = this["minecraft:client_entity"].description.render_controllers) === null || _a === void 0 ? void 0 : _a.includes(render_controller))) { (_b = this["minecraft:client_entity"].description.render_controllers) === null || _b === void 0 ? void 0 : _b.push(render_controller); } }); } addAnimation(...animations) { var _a; this.upgradeFormatVersion(); this["minecraft:client_entity"].description.animations = (_a = this["minecraft:client_entity"].description.animations) !== null && _a !== void 0 ? _a : {}; animations.forEach(animation => { this["minecraft:client_entity"].description.animations[animation.name] = animation.reference; }); } addAnimateScript(...animations) { var _a, _b; this.upgradeFormatVersion(); this["minecraft:client_entity"].description.scripts = (_a = this["minecraft:client_entity"].description.scripts) !== null && _a !== void 0 ? _a : {}; this["minecraft:client_entity"].description.scripts.animate = (_b = this["minecraft:client_entity"].description.scripts.animate) !== null && _b !== void 0 ? _b : []; animations.forEach(animation => { var _a, _b; if (!((_a = this["minecraft:client_entity"].description.scripts.animate) === null || _a === void 0 ? void 0 : _a.includes(animation))) { (_b = this["minecraft:client_entity"].description.scripts.animate) === null || _b === void 0 ? void 0 : _b.push(animation); } }); } addPublicVariable(...variables) { var _a, _b; this.upgradeFormatVersion(); this["minecraft:client_entity"].description.scripts = (_a = this["minecraft:client_entity"].description.scripts) !== null && _a !== void 0 ? _a : {}; this["minecraft:client_entity"].description.scripts.variables = (_b = this["minecraft:client_entity"].description.scripts.variables) !== null && _b !== void 0 ? _b : {}; variables.forEach(variable => { this["minecraft:client_entity"].description.scripts.variables[variable] = "public"; }); } }