Minecraft Fabric模组开发教程#9 添加附魔书

创建附魔类

public class FireBoomEnchantment extends Enchantment {
    public FireBoomEnchantment(Rarity rarity, EnchantmentTarget target, EquipmentSlot[] slotTypes) {
        super(rarity, target, slotTypes);
    }
}

如果目标被攻击,目标就会爆炸,这里直接使用FireballEntity类中onCollision方法的部分代码。

@Override
public void onTargetDamaged(LivingEntity user, Entity target, int level) {
    if (target instanceof LivingEntity) {
        boolean bl = user.getWorld().getGameRules().getBoolean(GameRules.DO_MOB_GRIEFING);
        user.getWorld().createExplosion(target, target.getX(), target.getY(), target.getZ(), 1, bl, World.ExplosionSourceType.MOB);
    }
}

注册附魔

创建FireBoomEnchantment对象的时候,需要传入Enchantment.RarityEnchantmentTargetEquipmentSlot[],其中VERY_RARE表示非常稀有,EnchantmentTarget.WEAPON表示只能附魔武器,EquipmentSlot[]表示只能附魔主手。

public static final FireBoomEnchantment FIRE_BOOM = Registry.register(Registries.ENCHANTMENT, new Identifier("awesome", "fire_boom"), new FireBoomEnchantment(Enchantment.Rarity.VERY_RARE, EnchantmentTarget.WEAPON, new EquipmentSlot[]{
        EquipmentSlot.MAINHAND
}));

多语言

{
  "enchantment.awesome.fire_boom": "Fire Boom"
}

附魔书在在原材料的最后

9-1

9-2