我们本次对于模组中自定义附魔进行讲解
1. init 包中新建 ModEnchantment 类
![cr1.jpg](https://i-blog.csdnimg.cn/blog_migrate/6662c1cfd801e75018ca52e9ffc487d0.jpeg)
在 ModEnchantment.java 中编写:
package com.Joy187.newmod.init;
import java.util.ArrayList;
import java.util.List;
import com.Joy187.newmod.enchantments.EnchantmentYourEnchantment;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import com.Joy187.newmod.util.Reference;
@Mod.EventBusSubscriber(modid=Reference.Mod_ID)
public class ModEnchantment {
public static final List<Enchantment> ENCHANTNENTS = new ArrayList<Enchantment>();
public static final Enchantment MY_ENCHANTMENT = new EnchantmentYourEnchantment();
@SubscribeEvent
public static void yourEnchantmentsFunction(LivingUpdateEvent event)
{
EntityLivingBase living = event.getEntityLiving();
int level = EnchantmentHelper.getMaxEnchantmentLevel(MY_ENCHANTMENT, living);
BlockPos pos = living.getPosition();
World world = event.getEntity().world;
}
}
2. 新建 enchantment 包
![cr3.jpg](https://i-blog.csdnimg.cn/blog_migrate/47dfdf45f2016c75ddd42832128e19ba.jpeg)
enchantment 包中新建EnchantmentYourEnchantment 类:
![cr4.jpg](https://i-blog.csdnimg.cn/blog_migrate/17d3e334bde30419aa7f3e46d7d84030.jpeg)
在EnchantmentYourEnchantment.java 中编写:
package com.Joy187.newmod.enchantments;
import java.util.Random;
import com.Joy187.newmod.init.ModEnchantment;
import com.Joy187.newmod.util.Reference;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.EnumEnchantmentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.init.Enchantments;
import net.minecraft.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
public class EnchantmentYourEnchantment extends Enchantment{
public EnchantmentYourEnchantment()
{
//设定穿戴的位置(头盔、胸甲、护腿、鞋子)
super(Rarity.RARE, EnumEnchantmentType.ARMOR_CHEST, new EntityEquipmentSlot[] {EntityEquipmentSlot.CHEST});
this.setName("my_enchantment");
this.setRegistryName(new ResourceLocation(Reference.Mod_ID + ":my_enchantment"));
ModEnchantment.ENCHANTNENTS.add(this);
}
@Override
public int getMinEnchantability(int enchantmentLevel)
{
return 20 * enchantmentLevel;
}
@Override
public int getMaxEnchantability(int enchantmentLevel) {
return this.getMinEnchantability(enchantmentLevel) + 10;
}
@Override
public int getMaxLevel() {
//你想拥有的最大等级
return 5;
}
protected boolean canApplyTogether(Enchantment ench) {
//不能和哪个魔法同时附魔
return super.canApplyTogether(ench) && ench != Enchantments.THORNS;
}
//设置一个与荆棘效果类似的附魔效果
public static boolean shouldHit(int level, Random rnd)
{
if (level <= 0)
{
return false;
}
else
{
return rnd.nextFloat() < 0.15F * (float)level;
}
}
//设置一个与荆棘效果类似的附魔效果,同时附带生命回复效果
public void onUserHurt(EntityLivingBase user, Entity attacker, int level)
{
Random random = user.getRNG();
ItemStack itemstack = EnchantmentHelper.getEnchantedItem(ModEnchantment.MY_ENCHANTMENT, user);
if (shouldHit(level, random))
{
//如果被攻击了,就反弹伤害
if (attacker != null)
{
attacker.attackEntityFrom(DamageSource.causeThornsDamage(user), (float)getDamage(level, random)*5);
}
if (!itemstack.isEmpty())
{
damageArmor(itemstack, 2, user);
}
// user.removeActivePotionEffect(MobEffects.WITHER);
user.addPotionEffect(new PotionEffect(MobEffects.RESISTANCE, 20 * 10, 3, true, true));
}
else if (!itemstack.isEmpty())
{
//如果是用武器攻击的,给自己增加一个生命恢复的效果,持续5s
damageArmor(itemstack, 1, user);
user.addPotionEffect(new PotionEffect(MobEffects.REGENERATION, 20 * 5, 2, true, true));
}
else{
user.removeActivePotionEffect(MobEffects.WITHER);
user.addPotionEffect(new PotionEffect(MobEffects.REGENERATION, 20 * 5, 2, true, true));
}
}
public static int getDamage(int level, Random rnd)
{
return level > 10 ? level - 10 : 1 + rnd.nextInt(2);
}
//装备遭到攻击时的伤害与耐久损耗设置
private void damageArmor(ItemStack stack, int amount, EntityLivingBase entity)
{
int slot = -1;
int x = 0;
for (ItemStack i : entity.getArmorInventoryList())
{
if (i == stack){
slot = x;
break;
}
x++;
}
if (slot == -1 || !(stack.getItem() instanceof net.minecraftforge.common.ISpecialArmor))
{
stack.damageItem(1, entity);
return;
}
net.minecraftforge.common.ISpecialArmor armor = (net.minecraftforge.common.ISpecialArmor)stack.getItem();
armor.damageArmor(entity, stack, DamageSource.causeThornsDamage(entity), amount, slot);
}
}
3.在util.handlers 中添加附魔的注册信息:
@SubscribeEvent
public static void onEnchantmentRegister(RegistryEvent.Register<Enchantment> event)
{
event.getRegistry().registerAll(ModEnchantment.ENCHANTNENTS.toArray(new Enchantment[0]));
}
![cr5.jpg](https://i-blog.csdnimg.cn/blog_migrate/77798d97f80e329498fc1b68a1a526f0.jpeg)
4.在resources的语言表(en_us.lang)中添加你的附魔书的游戏名称
//enchantment
enchantment.my_enchantment=My Enchantment
5.保存操作 -> 运行游戏
![cr2.jpg](https://i-blog.csdnimg.cn/blog_migrate/6813e7b89b5cae27b65dcb9f00ca0276.jpeg)
在游戏中成功找到我们的附魔书!
攻击回血效果函数(可附魔在武器上面)
public void onEntityDamaged(EntityLivingBase user, Entity target, int level)
{
if (target instanceof EntityLivingBase)
{
EntityLivingBase entitylivingbase = (EntityLivingBase)target;
int i = 20 + user.getRNG().nextInt(10 * level);
//被攻击的减速3效果
entitylivingbase.addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, i, 3));
//给自己生命恢复5效果
user.addPotionEffect(new PotionEffect(MobEffects.HEALTH_BOOST, i, 5));
}
}