Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-557

EntityTargetEvent has incorrect TargetReason when directly attacking a mob

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None

      When directly attacking a mob, EntityTargetEvent will fire with TargetReason.TARGET_ATTACKED_NEARBY_ENTITY.

      Based on the API docs, the correct result for this case should be TargetReason.TARGET_ATTACKED_ENTITY.

      Steps:
      1) Run fresh Spigot install with one plugin, using the listener below
      2) Log in and use /summon Zombie
      3) Punch the zombie

      Result: EntityTargetEvent fires with TargetReason.TARGET_ATTACKED_NEARBY_ENTITY.

          @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
          public void onEntityTargetEvent(EntityTargetEvent event) {
      
          	if (event.getReason() == TargetReason.CLOSEST_PLAYER) {
          		event.setCancelled(true);
          	}
          	
          	getLogger().info("EntityTargetEvent: " + event.getReason() + " " + "Cancelled: " + event.isCancelled());
          }
      

      [SpigotTest] EntityTargetEvent: CLOSEST_PLAYER Cancelled: true
      [SpigotTest] EntityTargetEvent: CLOSEST_PLAYER Cancelled: true
      [SpigotTest] EntityTargetEvent: TARGET_ATTACKED_NEARBY_ENTITY Cancelled: false

      
      

      It is necessary to cancel TargetReason.CLOSEST_PLAYER so that you can hit the mob before it has already targeted you.

            Thinkofdeath Thinkofname
            octagami Octagami MC
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: