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

问题描述 投票:0回答:1

我正在为Minecraft制作一个mod,现在我正在努力制作能给你带来特殊药水效果的enchantments。我一直在研究一个健康提升的附魔,开始时每次更新都会激活该效果。当你这么做的时候,每次更新都会使伤害凸轮效果,所以我改成了每次更换装备。这样做的效果更好,但每次我有多出的半颗心并换掉一个物品时,它又会产生伤害凸轮的效果。我想使用某种 onArmorEquip 事件,但我没有看到一个选项。我正在使用Eclipse for Minecraft 1.12.2,有人有建议吗?这是我的代码。

@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);
        }
    }
}
java eclipse minecraft minecraft-forge
1个回答
0
投票

这是我要做的。你可以得到哪个装备槽被改变了,然后是被改变的物品。这是未经测试的,但类似于这个的东西应该可以用。

这将检查实体是一个玩家,并且装备的改变发生在一个盔甲槽中(你可能只需要帮助这部分)。

@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.
                }
            }

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.