内置附魔效果组件

内置附魔效果组件

原版提供多种组件用于附魔定义,本文介绍其用途和代码表示。

数值效果组件

另见 Minecraft Wiki 的数值效果组件。

数值效果组件用于修改游戏中的数值,由 EnchantmentValueEffect 实现。同一数值被多个组件或附魔修改时,所有效果都会应用。

可用运算包括:

  • minecraft:set:用指定的随等级变化的值覆盖原值。
  • minecraft:add:将指定等级值加到原值。
  • minecraft:multiply:用指定等级因子乘原值。
  • minecraft:remove_binomial:使用二项分布按给定等级概率抽取,成功时减 1。许多数值实际是标志,1 为完全启用、0 为关闭。
  • minecraft:all_of:按顺序应用一组其他数值效果。

锋利附魔用 minecraft:damage 实现效果:

JSON

"effects": {
    // The type of this effect component is "minecraft:damage".
    // This means that the effect will modify weapon damage.
    // See below for a list of more effect component types.
    "minecraft:damage": [
        {
            // A value effect that should be applied.
            // In this case, since there's only one, this value effect is just named "effect".
            "effect": {
                // The type of value effect to use. In this case, it is "minecraft:add", so the value (given below) will be added 
                // to the weapon damage value.
                "type": "minecraft:add",

                // The value block. In this case, the value is a LevelBasedValue that starts at 1 and increases by 0.5 every enchantment level.
                "value": {
                    "type": "minecraft:linear",
                    "base": 1.0,
                    "per_level_above_first": 0.5
                }
            }
        }
    ]
}

数据生成

// Passed into 'effects' in an Enchantment during data generation
// See the Data Generation section of the Enchantments entry to learn more
DataComponentMap.builder().set(
    // Selects the "minecraft:damage" component.
    EnchantmentEffectComponents.DAMAGE,

    // Constructs a list of one conditional AddValue without any requirements.
    List.of(new ConditionalEffect<>(
        new AddValue(LevelBasedValue.perLevel(1.0F, 0.5F)),
        Optional.empty()))
).build()

value 内的对象是 LevelBasedValue,可让效果强度随附魔等级变化。

EnchantmentValueEffect#process 按指定数值操作调整值:

// `valueEffect` is an EnchantmentValueEffect instance.
// `enchantLevel` is an integer representing the level of the enchantment
float baseValue = 1.0;
float modifiedValue = valueEffect.process(enchantLevel, server.random, baseValue);

原版数值效果组件类型

DataComponentType<EnchantmentValueEffect>

  • minecraft:crossbow_charge_time:修改弩蓄力秒数,用于快速装填。
  • minecraft:trident_spin_attack_strength:修改三叉戟旋转攻击强度(见 TridentItem#releaseUsing),用于激流。

DataComponentType<List<ConditionalEffect<EnchantmentValueEffect>>>

盔甲相关:

  • minecraft:armor_effectiveness:决定盔甲对该武器的防护有效性,0 为无防护,1 为正常防护,用于突破。
  • minecraft:damage_protection:每点使持有该物品时所受伤害减少 4%,最多 80%,用于爆炸保护、摔落缓冲、火焰保护、保护和弹射物保护。

攻击相关:

  • minecraft:damage:修改武器伤害,用于锋利、穿刺、节肢杀手、力量和亡灵杀手。
  • minecraft:smash_damage_per_fallen_block:为重锤每格下落增加伤害,用于致密。
  • minecraft:knockback:修改击退量,以游戏单位计,用于击退和冲击。
  • minecraft:mob_experience:修改生物掉落经验,原版未使用。

耐久相关:

  • minecraft:item_damage:修改物品耐久损耗,低于 1 时表示损耗概率,用于耐久。
  • minecraft:repair_with_xp:使用获得的经验修复物品,并确定效率,用于经验修补。

弹射物相关:

  • minecraft:ammo_use:修改弓或弩的弹药消耗,值转为整数,因此小于 1 时消耗为 0,用于无限。
  • minecraft:projectile_piercing:修改穿透实体数量,用于穿透。
  • minecraft:projectile_count:修改射出的弹射物数量,用于多重射击。
  • minecraft:projectile_spread:修改相对于发射方向的最大散布角度,用于多重射击。
  • minecraft:trident_return_acceleration:让三叉戟返回主人并修改其加速度,用于忠诚。

其他:

  • minecraft:block_experience:修改破坏方块的经验,用于精准采集。
  • minecraft:fishing_time_reduction:减少浮漂下沉前的等待秒数,用于饵钓。
  • minecraft:fishing_luck_bonus:修改钓鱼战利品表使用的幸运,用于海之眷顾。

DataComponentType<List<TargetedConditionalEffect<EnchantmentValueEffect>>>

  • minecraft:equipment_drops:修改被该武器击败的实体掉落装备的概率,用于抢夺。

基于位置的效果组件

另见 Minecraft Wiki 的位置效果组件。

这类组件实现 EnchantmentLocationBasedEffect,定义需要知道附魔持有者位置的操作。装备物品及持有者 BlockPos 改变时调用 EnchantmentEntityEffect#onChangedBlock,卸下物品时调用 onDeactivate。

下面通过 minecraft:attributes 修改实体尺寸:

JSON

// The type is "minecraft:attributes" (described below).
// In a nutshell, this applies an attribute modifier.
"minecraft:attributes": [
    {
        // This "amount" block is a LevelBasedValue.
        "amount": {
            "type": "minecraft:linear",
            "base": 1,
            "per_level_above_first": 1
        },

        // Which attribute to modify. In this case, modifies "minecraft:scale"
        "attribute": "minecraft:generic.scale",
        // The unique identifier for this attribute modifier. Should not overlap with others, but doesn't need to be registered.
        "id": "examplemod:enchantment.size_change",
        // What operation to use on the attribute. Can be "add_value", "add_multiplied_base", or "add_multiplied_total".
        "operation": "add_value"
    }
],

数据生成

// Passed into the effects of an Enchantment during data generation
DataComponentMap.builder().set(
    // Specifies the "minecraft:attributes" component type.
    EnchantmentEffectComponents.ATTRIBUTES,

    // This component takes a list of these EnchantmentAttributeEffect objects.
    List.of(new EnchantmentAttributeEffect(
        ResourceLocation.fromNamespaceAndPath("examplemod", "enchantment.size_change"),
        Attributes.SCALE,
        LevelBasedValue.perLevel(1F, 1F),
        AttributeModifier.Operation.ADD_VALUE
    ))
).build()

原版提供以下位置效果:

  • minecraft:all_of:依次执行实体效果列表。
  • minecraft:apply_mob_effect:对目标应用状态效果。
  • minecraft:attribute:对持有者应用属性修饰符。
  • minecraft:damage_entity:对目标造成伤害;攻击场景中会与攻击伤害叠加。
  • minecraft:damage_item:减少物品耐久。
  • minecraft:explode:产生爆炸。
  • minecraft:ignite:点燃实体。
  • minecraft:play_sound:播放指定声音。
  • minecraft:replace_block:替换指定偏移处的方块。
  • minecraft:replace_disk:替换圆盘状范围的方块。
  • minecraft:run_function:运行指定数据包函数。
  • minecraft:set_block_properies:修改指定方块的状态属性。
  • minecraft:spawn_particles:生成粒子。
  • minecraft:summon_entity:生成实体。

原版位置效果组件类型

DataComponentType<List<ConditionalEffect<EnchantmentLocationBasedEffect>>>

  • minecraft:location_changed:持有者方块位置变化及装备物品时执行,用于冰霜行者和灵魂疾行。

DataComponentType<List<EnchantmentAttributeEffect>>

  • minecraft:attributes:对持有者应用属性修饰符,卸下物品时移除。

实体效果组件

另见 Minecraft Wiki 的实体效果组件。

实体效果实现 EnchantmentEntityEffect,它是 EnchantmentLocationBasedEffect 的子类型,重写 onChangedBlock 以执行 EnchantmentEntityEffect#apply。根据具体组件类型,代码其他位置也会直接调用 apply,因此无需等待持有者方块位置变化。

除仅注册为位置效果的 minecraft:attribute 外,全部位置效果类型也可作为实体效果使用。

下面是火焰附加的 JSON 示例:

JSON

// This component's type is "minecraft:post_attack" (see below).
"minecraft:post_attack": [
    {
        // Decides whether the "victim" of the attack, the "attacker", or the "damaging entity" (the projectile if there is one, attacker if not) recieves the effect.
        "affected": "victim",
        
        // Decides which enchantment entity effect to apply.
        "effect": {
            // The type of this effect is "minecraft:ignite".
            "type": "minecraft:ignite",
            // "minecraft:ignite" requires a LevelBasedValue as a duration for how long the entity will be ignited.
            "duration": {
                "type": "minecraft:linear",
                "base": 4.0,
                "per_level_above_first": 4.0
            }
        },

        // Decides who (the "victim", "attacker", or "damaging entity") must have the enchantment for it to take effect.
        "enchanted": "attacker",

        // An optional predicate which controls whether the effect applies.
        "requirements": {
            "condition": "minecraft:damage_source_properties",
            "predicate": {
                "is_direct": true
            }
        }
    }
]

数据生成

// Passed into the effects of an Enchantment during data generation
DataComponentMap.builder().set(
    // Specifies the "minecraft:post_attack" component type.
    EnchantmentEffectComponents.POST_ATTACK,

    // Defines the data for this component. In this case, a list of one TargetedConditionalEffect.
    List.of(
        new TargetedConditionalEffect<>(

            // Determines the "enchanted" field.
            EnchantmentTarget.ATTACKER,

            // Determines the "affected" field.
            EnchantmentTarget.VICTIM,

            // The enchantment entity effect.
            new Ignite(LevelBasedValue.perLevel(4.0F, 4.0F)),

            // The "requirements" clause. 
            // In this case, the only optional part activated is the isDirect boolean flag.
            Optional.of(
                new DamageSourceCondition(
                    Optional.of(
                        new DamageSourcePredicate(
                            List.of(),
                            Optional.empty(),
                            Optional.empty(),
                            Optional.of(true)
                        )
                    )
                )
            )
        )
    )
).build()

这里的组件为 minecraft:post_attack,效果为 minecraft:ignite,由 Ignite record 实现,其 apply 方法会点燃目标实体。

原版实体效果组件类型

DataComponentType<List<ConditionalEffect<EnchantmentEntityEffect>>>

  • minecraft:hit_block:实体(例如弹射物)命中方块时执行,用于引雷。
  • minecraft:tick:每刻执行,用于灵魂疾行。
  • minecraft:projectile_spawned:弓或弩生成弹射物后执行,用于火矢。

DataComponentType<List<TargetedConditionalEffect<EnchantmentEntityEffect>>>

  • minecraft:post_attack:攻击造成伤害后执行,用于节肢杀手、引雷、火焰附加、荆棘和风爆。

详细说明见对应 Minecraft Wiki 页面。

其他原版组件类型

DataComponentType<List<ConditionalEffect<DamageImmunity>>>

  • minecraft:damage_immunity:免疫指定伤害类型,用于冰霜行者。

DataComponentType<Unit>

  • minecraft:prevent_equipment_drop:防止玩家死亡时掉落该物品,用于消失诅咒。
  • minecraft:prevent_armor_change:防止从盔甲槽卸下该物品,用于绑定诅咒。

DataComponentType<List<CrossbowItem.ChargingSounds>>

  • minecraft:crossbow_charge_sounds:决定弩蓄力的声音,每项对应一个附魔等级。

DataComponentType<List<Holder<SoundEvent>>>

  • minecraft:trident_sound:决定使用三叉戟时的声音,每项对应一个附魔等级。

来源: NeoForged/Documentation(1.21.1)。中文翻译依据提交 4a680cdd4d15。