构建器模式 - Junye-666/RideBattleLib_NeoForge_1.21.1 GitHub Wiki
1.3.3 推荐使用 Builder 模式。
所有示例优先参考 ExampleBasicPack 与 ExampleDynamicPack。
RiderBuilder 核心方法
RiderBuilder.create(riderId)
.driver(item)
.driver(item, EquipmentSlot.CHEST)
.auxDriver(item)
.auxDriver(item, EquipmentSlot.FEET)
.slot(slotId, allowedItems, required, replace)
.auxSlot(slotId, allowedItems, required, replace)
.form(formId)
.form(formConfig)
.baseForm(formId)
.allowDynamicForms(true)
.triggerItem(item)
.baseAttribute(attribute, amount)
.baseEffect(effect, amplifier)
.buildAndRegister();
buildAndRegister() 会自动执行:
RiderRegistry.registerRider(config);
如果你只需要构建、不立即注册:
RiderConfig config = RiderBuilder.create(riderId)
// ...
.build(); // 仅构建
// 后续需commonSetup中手动注册
RiderRegistry.registerRider(config);
FormBuilder 两种用法
独立构建
public static final FormConfig BASE_FORM = FormBuilder.create(FORM_BASE)
.armor(Items.IRON_HELMET, Items.IRON_CHESTPLATE, null, Items.IRON_BOOTS)
.requiredItem(CORE_SLOT, Items.IRON_INGOT)
.triggerType(TriggerType.KEY)
.attribute(Attributes.MAX_HEALTH, 8.0, AttributeModifier.Operation.ADD_VALUE)
.effect(MobEffects.NIGHT_VISION, 0)
.grantedItem(Items.IRON_SWORD)
.shouldPause(true)
.build();
然后传入 RiderBuilder:
RiderBuilder.create(RIDER_ID)
.driver(Items.IRON_LEGGINGS)
.slot(CORE_SLOT, List.of(Items.IRON_INGOT), true, true)
.form(BASE_FORM)
.baseForm(FORM_BASE)
.buildAndRegister();
RiderBuilder 内部构建
RiderBuilder.create(RIDER_ID)
.driver(Items.NETHERITE_LEGGINGS)
.slot(SLOT_1, List.of(Items.EMERALD, Items.DIAMOND), true, true)
.form(BASE_FORM)
.armor(Items.LEATHER_HELMET, Items.LEATHER_CHESTPLATE, null, Items.LEATHER_BOOTS)
.triggerType(TriggerType.KEY)
.requiredItem(SLOT_1, Items.AIR)
.attribute(Attributes.MAX_HEALTH, 8.0, AttributeModifier.Operation.ADD_VALUE)
.allowsEmptyDriver(true)
.end()
.baseForm(BASE_FORM)
.allowDynamicForms(true)
.buildAndRegister();
技能绑定
// 自动注册技能,并生成翻译键 skill.namespace.path
.skill(SKILL_ID, 20)
// 自动拼接形态路径 + skillPath
.skill("rider_kick", 30)
// 使用已注册技能 ID
.skill(ResourceLocation.fromNamespaceAndPath("mymod", "pre_registered_skill"))
详细可查看 技能系统