当我装备盔甲时激活的方法

A method that activates when I equip armor

我正在为 Minecraft 制作一个 mod,现在我正在制作可以为您提供特殊药水效果的魔法。我一直在研究 Health Boost 附魔,并从每次更新激活效果开始。当你这样做时,每次更新都会造成伤害凸轮效果,所以我切换到每次设备更换。这效果更好,但每次我有更多的半心和 switched/dropped 一个项目时,它又会造成伤害。我想使用某种 onArmorEquip 事件,但我没有看到一个选项。我正在为 Minecraft 1.12.2 开发 Eclipse,有人有什么建议吗?这是我的代码:

@SubscribeEvent
public static void enchantTankFunction(LivingEquipmentChangeEvent event)
{
    Object player = event.getEntityLiving();
    if(player instanceof EntityLivingBase)
    {
        EntityLivingBase entityPlayer = (EntityLivingBase)player;
        int levelFeet = EnchantmentHelper.getEnchantmentLevel(ENCHANT_TANK, entityPlayer.getItemStackFromSlot(EntityEquipmentSlot.FEET));
        int levelLegs = EnchantmentHelper.getEnchantmentLevel(ENCHANT_TANK, entityPlayer.getItemStackFromSlot(EntityEquipmentSlot.LEGS));
        int levelChest = EnchantmentHelper.getEnchantmentLevel(ENCHANT_TANK, entityPlayer.getItemStackFromSlot(EntityEquipmentSlot.CHEST));
        int levelHead = EnchantmentHelper.getEnchantmentLevel(ENCHANT_TANK, entityPlayer.getItemStackFromSlot(EntityEquipmentSlot.HEAD));
        if(!entityPlayer.getEntityWorld().isRemote && (levelFeet > 0 || levelLegs > 0 || levelChest > 0 || levelHead > 0))
        {
            entityPlayer.removePotionEffect(MobEffects.HEALTH_BOOST);
            entityPlayer.addPotionEffect(new PotionEffect(MobEffects.HEALTH_BOOST, (24 * 60 * 60 * 20), (levelFeet) + (levelLegs) + (levelChest) + (levelHead) - 1, false, false));
        }
        else
        {
            entityPlayer.removePotionEffect(MobEffects.HEALTH_BOOST);
        }
    }
}

这是我会做的。您可以获得更改的设备插槽,然后是更改的项目。这是未经测试的,但类似的东西应该可以工作。

这会检查该实体是否为玩家以及装备更换是否发生在装甲槽中(您可能只需要这部分的帮助)。

@SubscribeEvent
public static void enchantSaturationFunction(LivingEquipmentChangeEvent event)
{
    if (event.getEntityLiving() instanceof EntityPlayer) // Remove this if you want other mobs to get the benefits
    {
        EntityEquipmentSlot slotChanged = event.getSlot();
        if (slotChanged.getSlotIndex() > 0 && slotChanged.getSlotIndex() < 5) // Check out the class EntityEquipmentSlot
        {

然后,为了完成它,这将获得新盔甲的 ItemStack 并检查附魔。我从 ItemStack::getTooltip.

偷了一些代码
            ItemStack previousArmorPiece = event.getFrom();
            ItemStack newArmorPiece = event.getTo();

            NBTTagList nbtTagList = newArmorPiece.getEnchantmentTagList();

            for (int k1 = 0; k1 < nbtTagList.tagCount(); ++k1)
            {
                NBTTagCompound nbttagcompound = nbtTagList.getCompoundTagAt(k1);
                int enchantmentID = nbttagcompound.getShort("id");
                int enchantmentLevel = nbttagcompound.getShort("lvl");
                Enchantment enchantment = Enchantment.getEnchantmentByID(enchantmentID);
                if (enchantment instanceof MY_ENCHANTMENT)
                {
                    // They equipped armor with the necessary enchantment, so do what you want.
                }
            }

        }
    }
}