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

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

      The Enchantment::doPostHurt and Enchantment::doPostAttack functions are called twice for the player causing some bugs.

      • The Bane of Arthropods enchantment gives the Slowness effect twice
      • The Thorns enchantment deals damage to the attacker and the durability of the item with this enchantment twice.

      The problem is that the EnchantmentHelper::doPostHurtEffects and EnchantmentHelper::doPostDamageEffects actually call these functions twice:

      Vanilla code 1.18.1-1.20.1

      public static void doPostHurtEffects(LivingEntity defender, Entity attacker) {
          EnchantmentVisitor consumer = (enchantment, lvl) -> enchantment.doPostHurt(defender, attacker, lvl);
      
          if (defender != null) {
              EnchantmentHelper.runIterationOnInventory(consumer, defender.getAllSlots());
          }
          if (attacker instanceof Player) {
              EnchantmentHelper.runIterationOnItem(consumer, defender.getMainHandItem());
          }
      } 
      public static void doPostDamageEffects(LivingEntity attacker, Entity target) {
          EnchantmentVisitor consumer = (enchantment, lvl) -> enchantment.doPostAttack(attacker, target, lvl);
      
          if (attacker != null) {
              EnchantmentHelper.runIterationOnInventory(consumer, attacker.getAllSlots());
          }
          if (attacker instanceof Player) {
              EnchantmentHelper.runIterationOnItem(consumer, attacker.getMainHandItem());
          }
      }

      Fix suggestion

      public static void doPostHurtEffects(LivingEntity defender, Entity attacker) {
          EnchantmentVisitor consumer = (enchantment, lvl) -> enchantment.doPostHurt(defender, attacker, lvl);
      
          if (defender != null) {
              EnchantmentHelper.runIterationOnInventory(consumer, defender.getAllSlots());
          } else if (attacker instanceof Player) {
              EnchantmentHelper.runIterationOnItem(consumer, defender.getMainHandItem());
          }
      }
      public static void doPostDamageEffects(LivingEntity attacker, Entity target) {
          EnchantmentVisitor consumer = (enchantment, lvl) -> enchantment.doPostAttack(attacker, target, lvl);
      
          if (attacker != null) {
              EnchantmentHelper.runIterationOnInventory(consumer, attacker.getAllSlots());
          } else if (attacker instanceof Player) {
              EnchantmentHelper.runIterationOnItem(consumer, attacker.getMainHandItem());
          }
      }

            slicedlime [Mojang] slicedlime
            Maity Maity
            Votes:
            9 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              CHK: