From 7055c93117b800cda3d2cb8c89588f8e79c3ad31 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Sat, 24 Aug 2019 19:06:17 +1000
Subject: [PATCH] SPIGOT-5264: Call event for experience orbs losing their
 target

---
 nms-patches/EntityExperienceOrb.patch         | 44 +++++++++----------
 .../craftbukkit/event/CraftEventFactory.java  |  4 +-
 2 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/nms-patches/EntityExperienceOrb.patch b/nms-patches/EntityExperienceOrb.patch
index 6f0228ccb2..de5e127e15 100644
--- a/nms-patches/EntityExperienceOrb.patch
+++ b/nms-patches/EntityExperienceOrb.patch
@@ -20,33 +20,31 @@
          if (this.d > 0) {
              --this.d;
          }
-@@ -73,6 +79,16 @@
+@@ -72,7 +78,22 @@
+             this.targetPlayer = null;
          }
  
-         if (this.targetPlayer != null) {
-+            // CraftBukkit start
-+            boolean cancelled = false;
-+            if (this.targetPlayer != prevTarget) {
-+                EntityTargetLivingEntityEvent event = CraftEventFactory.callEntityTargetLivingEvent(this, targetPlayer, EntityTargetEvent.TargetReason.CLOSEST_PLAYER);
-+                EntityLiving target = event.getTarget() == null ? null : ((org.bukkit.craftbukkit.entity.CraftLivingEntity) event.getTarget()).getHandle();
-+                targetPlayer = target instanceof EntityHuman ? (EntityHuman) target : null;
-+                cancelled = event.isCancelled();
-+            }
+-        if (this.targetPlayer != null) {
++        // CraftBukkit start
++        boolean cancelled = false;
++        if (this.targetPlayer != prevTarget) {
++            EntityTargetLivingEntityEvent event = CraftEventFactory.callEntityTargetLivingEvent(this, targetPlayer, (targetPlayer != null) ? EntityTargetEvent.TargetReason.CLOSEST_PLAYER : EntityTargetEvent.TargetReason.FORGOT_TARGET);
++            EntityLiving target = (event.getTarget() == null) ? null : ((org.bukkit.craftbukkit.entity.CraftLivingEntity) event.getTarget()).getHandle();
++            cancelled = event.isCancelled();
 +
-+            if (!cancelled && targetPlayer != null) {
++            if (cancelled) {
++                targetPlayer = prevTarget;
++            } else {
++                targetPlayer = (target instanceof EntityHuman) ? (EntityHuman) target : null;
++            }
++        }
++
++        if (this.targetPlayer != null && !cancelled) {
++            // CraftBukkit end
              Vec3D vec3d = new Vec3D(this.targetPlayer.locX - this.locX, this.targetPlayer.locY + (double) this.targetPlayer.getHeadHeight() / 2.0D - this.locY, this.targetPlayer.locZ - this.locZ);
              double d1 = vec3d.g();
  
-@@ -81,6 +97,8 @@
- 
-                 this.setMot(this.getMot().e(vec3d.d().a(d2 * d2 * 0.1D)));
-             }
-+            }
-+            // CraftBukkit end
-         }
- 
-         this.move(EnumMoveType.SELF, this.getMot());
-@@ -113,7 +131,7 @@
+@@ -113,7 +134,7 @@
      protected void az() {}
  
      @Override
@@ -55,7 +53,7 @@
          this.damageEntity(DamageSource.FIRE, (float) i);
      }
  
-@@ -160,13 +178,19 @@
+@@ -160,13 +181,19 @@
                      if (!itemstack.isEmpty() && itemstack.f()) {
                          int i = Math.min(this.c(this.value), itemstack.getDamage());
  
@@ -78,7 +76,7 @@
                  }
  
                  this.die();
-@@ -188,6 +212,24 @@
+@@ -188,6 +215,24 @@
      }
  
      public static int getOrbValue(int i) {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 2ed41a874a..9331952385 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1016,13 +1016,13 @@ public class CraftEventFactory {
     }
 
     public static EntityTargetEvent callEntityTargetEvent(Entity entity, Entity target, EntityTargetEvent.TargetReason reason) {
-        EntityTargetEvent event = new EntityTargetEvent(entity.getBukkitEntity(), target == null ? null : target.getBukkitEntity(), reason);
+        EntityTargetEvent event = new EntityTargetEvent(entity.getBukkitEntity(), (target == null) ? null : target.getBukkitEntity(), reason);
         entity.getBukkitEntity().getServer().getPluginManager().callEvent(event);
         return event;
     }
 
     public static EntityTargetLivingEntityEvent callEntityTargetLivingEvent(Entity entity, EntityLiving target, EntityTargetEvent.TargetReason reason) {
-        EntityTargetLivingEntityEvent event = new EntityTargetLivingEntityEvent(entity.getBukkitEntity(), (LivingEntity) target.getBukkitEntity(), reason);
+        EntityTargetLivingEntityEvent event = new EntityTargetLivingEntityEvent(entity.getBukkitEntity(), (target == null) ? null : (LivingEntity) target.getBukkitEntity(), reason);
         entity.getBukkitEntity().getServer().getPluginManager().callEvent(event);
         return event;
     }