Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-248272

Enchantment::doPostHurt and Enchantment::doPostAttack are called twice for players

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 1.18.1, 22w03a, 22w05a, 1.18.2, 1.19.2, 1.19.4 Pre-release 3
    • None
    • Confirmed
    • Enchantments
    • Normal

    Description

      Enchantment::doPostHurt and Enchantment::doPostAttack are called twice for the player when they should not.

      Due to this bug, the slow effect from the Bane of Arthropods enchantment is applied twice. The Thorns enchantment may also work incorrectly, causing damage to the attacker twice.

      The problem is that EnchantmentHelper::doPostHurtEffects and EnchantmentHelper::doPostDamageEffects first check all items of the entity, and then if that entity is a player, these methods check the item in the main hand.

      Vanilla code 1.18.1:

      public static void doPostHurtEffects(LivingEntity livingEntity, Entity entity) {
          EnchantmentVisitor enchantmentVisitor = (enchantment, i) -> enchantment.doPostHurt(livingEntity, entity, i);
          if (livingEntity != null) {
              EnchantmentHelper.runIterationOnInventory(enchantmentVisitor, livingEntity.getAllSlots());
          }
          if (entity instanceof Player) {
              EnchantmentHelper.runIterationOnItem(enchantmentVisitor, livingEntity.getMainHandItem());
          }
      }
      public static void doPostDamageEffects(LivingEntity livingEntity, Entity entity) {
          EnchantmentVisitor enchantmentVisitor = (enchantment, i) -> enchantment.doPostAttack(livingEntity, entity, i);
          if (livingEntity != null) {
              EnchantmentHelper.runIterationOnInventory(enchantmentVisitor, livingEntity.getAllSlots());
          }
          if (livingEntity instanceof Player) {
              EnchantmentHelper.runIterationOnItem(enchantmentVisitor, livingEntity.getMainHandItem());
          }
      }

      Fixed code:

      public static void doPostHurtEffects(LivingEntity livingEntity, Entity entity) {
          EnchantmentVisitor enchantmentVisitor = (enchantment, i) -> enchantment.doPostHurt(livingEntity, entity, i);
          if (entity instanceof Player) {
              EnchantmentHelper.runIterationOnItem(enchantmentVisitor, livingEntity.getMainHandItem());
          } else if (livingEntity != null) {
              EnchantmentHelper.runIterationOnInventory(enchantmentVisitor, livingEntity.getAllSlots());
          }
      }
      public static void doPostDamageEffects(LivingEntity livingEntity, Entity entity) {
          EnchantmentVisitor enchantmentVisitor = (enchantment, i) -> enchantment.doPostAttack(livingEntity, entity, i);
          if (livingEntity instanceof Player) {
              EnchantmentHelper.runIterationOnItem(enchantmentVisitor, livingEntity.getMainHandItem());
          } else if (livingEntity != null) {
              EnchantmentHelper.runIterationOnInventory(enchantmentVisitor, livingEntity.getAllSlots());
          }
      } 

      Attachments

        Activity

          People

            Unassigned Unassigned
            Maity Maity
            Votes:
            8 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              CHK: