UNPKG

pack-mc-2

Version:
127 lines (106 loc) 3.83 kB
var {ActivePlugin} = require("../Plugin"); var {MinecraftObject} = require("../Util"); class Entity { constructor(plugin, id, displayName) { this.plugin = plugin; this.id = id; this.subId = plugin.subId(id); this.displayName = displayName; this.behaviorComponents = {}; this.resourceComponents = {}; this.texturePath = `textures/entities/${this.subId}`; // 存储需要额外设置的字段 this.extraBehaviorFields = {}; this.extraResourceFields = {}; } addBehaviorComponent(key, value, namespace = "minecraft") { this.behaviorComponents[`${namespace}:${key}`] = value; return this; } addResourceComponent(key, value, namespace = "minecraft") { this.resourceComponents[`${namespace}:${key}`] = value; return this; } // 添加行为包额外字段 setBehaviorField(key, value) { this.extraBehaviorFields[key] = value; return this; } // 添加资源包额外字段 setResourceField(key, value) { this.extraResourceFields[key] = value; return this; } generateFiles() { // 生成行为包实体 const behaviorEntity = new MinecraftObject("entity", this.id) .initComponent(this.behaviorComponents) // 通过onGenerateJSON设置额外字段 behaviorEntity.onGenerateJSON = (json) => { json[`minecraft:entity`].description = { identifier: this.id, is_experimental: false }; Object.assign(json[`minecraft:entity`], this.extraBehaviorFields); }; this.plugin.json(`data/entities/${this.subId}.json`, (json) => (Object.assign( json, behaviorEntity.json()))); // 生成资源包实体 const resourceEntity = new MinecraftObject("entity", this.id) .initComponent(this.resourceComponents) .set("render_controllers", ["controller.render.item_sprite"]) .set("geometry", "geometry.item_sprite") .set("textures", { default: this.texturePath }); // 通过onGenerateJSON设置额外字段 resourceEntity.onGenerateJSON = (json) => { json[`minecraft:entity`].description = { identifier: this.id, is_experimental: false }; Object.assign(json[`minecraft:entity`], this.extraResourceFields); }; this.plugin.json(`resources/entities/${this.subId}.json`, (json) => (Object.assign( json, resourceEntity.json()))); } } class EntityPlugin extends ActivePlugin { constructor(ctx) { super("Entity", ctx); this.entities = []; this.lang = null; if (ctx.plugins.i18n) { this.lang = ctx.plugins.i18n.new('en_US'); } } onGenerate() { this.entities.forEach(entity => { console.log( "TIP: 生物 " + entity.displayName + " 贴图在 " + entity.texturePath ) if (this.lang) { const nameKey = `entity.${entity.subId}.name`; this.lang.t(nameKey, entity.displayName); // 将显示名称添加到实体的额外字段中 entity.setBehaviorField("description", { identifier: entity.id, is_experimental: false, // 如果API支持,可以在这里添加其他描述字段 is_spawnable: true, is_summonable: true }); } entity.generateFiles(); }); } createEntity(displayName = "未知生物") { const entityId = `${this.ctx.namespace}:entity${this.entities.length + 1}`; const entity = new Entity(this, entityId, displayName) .addBehaviorComponent("health", { value: 10, max: 10 }) .addBehaviorComponent("collision_box", { width: 0.5, height: 0.5 }); this.entities.push(entity); return entity; } createSimpleEntity(displayName = "未知生物") { return this.createEntity(displayName) .addResourceComponent("scale", { value: 1.0 }); } } module.exports = EntityPlugin;