From d00c0574cdfff2e4e330c683565e7e59d09ee852 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Fri, 28 Jul 2017 17:47:59 +1000
Subject: [PATCH] SPIGOT-3403: Add an EntityPickupItemEvent

---
 nms-patches/EntityInsentient.patch | 41 +++++++++++++++++++++--------
 nms-patches/EntityItem.patch       | 42 +++++++++++++++++++-----------
 2 files changed, 57 insertions(+), 26 deletions(-)

diff --git a/nms-patches/EntityInsentient.patch b/nms-patches/EntityInsentient.patch
index 8098e9a795..ef6aadb7aa 100644
--- a/nms-patches/EntityInsentient.patch
+++ b/nms-patches/EntityInsentient.patch
@@ -1,12 +1,14 @@
 --- a/net/minecraft/server/EntityInsentient.java
 +++ b/net/minecraft/server/EntityInsentient.java
-@@ -9,6 +9,15 @@
+@@ -9,6 +9,17 @@
  import java.util.UUID;
  import javax.annotation.Nullable;
  
 +// CraftBukkit start
 +import org.bukkit.craftbukkit.event.CraftEventFactory;
 +import org.bukkit.craftbukkit.entity.CraftLivingEntity;
++import org.bukkit.entity.LivingEntity;
++import org.bukkit.event.entity.EntityPickupItemEvent;
 +import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
 +import org.bukkit.event.entity.EntityTargetEvent;
 +import org.bukkit.event.entity.EntityUnleashEvent;
@@ -16,7 +18,7 @@
  public abstract class EntityInsentient extends EntityLiving {
  
      private static final DataWatcherObject<Byte> a = DataWatcher.a(EntityInsentient.class, DataWatcherRegistry.a);
-@@ -27,7 +36,7 @@
+@@ -27,7 +38,7 @@
      public float[] dropChanceHand;
      private final NonNullList<ItemStack> by;
      public float[] dropChanceArmor;
@@ -25,7 +27,7 @@
      public boolean persistent;
      private final Map<PathType, Float> bB;
      private MinecraftKey bC;
-@@ -57,6 +66,9 @@
+@@ -57,6 +68,9 @@
              this.r();
          }
  
@@ -35,7 +37,7 @@
      }
  
      protected void r() {}
-@@ -110,7 +122,38 @@
+@@ -110,7 +124,38 @@
      }
  
      public void setGoalTarget(@Nullable EntityLiving entityliving) {
@@ -74,7 +76,7 @@
      }
  
      public boolean d(Class<? extends EntityLiving> oclass) {
-@@ -340,11 +383,20 @@
+@@ -340,11 +385,20 @@
  
      public void a(NBTTagCompound nbttagcompound) {
          super.a(nbttagcompound);
@@ -97,7 +99,24 @@
          NBTTagList nbttaglist;
          int i;
  
-@@ -525,7 +577,9 @@
+@@ -508,7 +562,15 @@
+             }
+         }
+ 
+-        if (flag && this.c(itemstack)) {
++        // CraftBukkit start
++        boolean canPickup = flag && this.c(itemstack);
++
++        EntityPickupItemEvent entityEvent = new EntityPickupItemEvent((LivingEntity) getBukkitEntity(), (org.bukkit.entity.Item) entityitem.getBukkitEntity(), 0);
++        entityEvent.setCancelled(!canPickup);
++        this.world.getServer().getPluginManager().callEvent(entityEvent);
++        canPickup = !entityEvent.isCancelled();
++        if (canPickup) {
++            // CraftBukkit end
+             double d0;
+ 
+             switch (enumitemslot.a()) {
+@@ -525,7 +587,9 @@
              }
  
              if (!itemstack1.isEmpty() && (double) (this.random.nextFloat() - 0.1F) < d0) {
@@ -107,7 +126,7 @@
              }
  
              this.setSlot(enumitemslot, itemstack);
-@@ -565,11 +619,11 @@
+@@ -565,11 +629,11 @@
                  double d2 = entityhuman.locZ - this.locZ;
                  double d3 = d0 * d0 + d1 * d1 + d2 * d2;
  
@@ -121,7 +140,7 @@
                      this.die();
                  } else if (d3 < 1024.0D) {
                      this.ticksFarFromPlayer = 0;
-@@ -938,12 +992,24 @@
+@@ -938,12 +1002,24 @@
  
      public final boolean b(EntityHuman entityhuman, EnumHand enumhand) {
          if (this.isLeashed() && this.getLeashHolder() == entityhuman) {
@@ -146,7 +165,7 @@
                  this.setLeashHolder(entityhuman, true);
                  itemstack.subtract(1);
                  return true;
-@@ -964,10 +1030,12 @@
+@@ -964,10 +1040,12 @@
  
          if (this.bE) {
              if (!this.isAlive()) {
@@ -159,7 +178,7 @@
                  this.unleash(true, true);
              }
          }
-@@ -978,7 +1046,9 @@
+@@ -978,7 +1056,9 @@
              this.bE = false;
              this.leashHolder = null;
              if (!this.world.isClientSide && flag1) {
@@ -169,7 +188,7 @@
              }
  
              if (!this.world.isClientSide && flag && this.world instanceof WorldServer) {
-@@ -1048,6 +1118,7 @@
+@@ -1048,6 +1128,7 @@
  
                  this.setLeashHolder(entityleash, true);
              } else {
diff --git a/nms-patches/EntityItem.patch b/nms-patches/EntityItem.patch
index f34b0b8996..0cb43b4b72 100644
--- a/nms-patches/EntityItem.patch
+++ b/nms-patches/EntityItem.patch
@@ -1,14 +1,17 @@
 --- a/net/minecraft/server/EntityItem.java
 +++ b/net/minecraft/server/EntityItem.java
-@@ -4,6 +4,7 @@
+@@ -4,6 +4,10 @@
  import javax.annotation.Nullable;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
-+import org.bukkit.event.player.PlayerPickupItemEvent; // CraftBukkit
++// CraftBukkit start
++import org.bukkit.event.entity.EntityPickupItemEvent;
++import org.bukkit.event.player.PlayerPickupItemEvent;
++// CraftBukkit end
  
  public class EntityItem extends Entity {
  
-@@ -15,6 +16,7 @@
+@@ -15,6 +19,7 @@
      private String g;
      private String h;
      public float a;
@@ -16,7 +19,7 @@
  
      public EntityItem(World world, double d0, double d1, double d2) {
          super(world);
-@@ -54,9 +56,12 @@
+@@ -54,9 +59,12 @@
              this.die();
          } else {
              super.B_();
@@ -32,7 +35,7 @@
  
              this.lastX = this.locX;
              this.lastY = this.locY;
-@@ -104,9 +109,11 @@
+@@ -104,9 +112,11 @@
                  this.motY *= -0.5D;
              }
  
@@ -44,7 +47,7 @@
  
              this.aq();
              if (!this.world.isClientSide) {
-@@ -121,6 +128,12 @@
+@@ -121,6 +131,12 @@
              }
  
              if (!this.world.isClientSide && this.age >= 6000) {
@@ -57,7 +60,7 @@
                  this.die();
              }
  
-@@ -162,6 +175,7 @@
+@@ -162,6 +178,7 @@
                      } else if (itemstack1.getCount() + itemstack.getCount() > itemstack1.getMaxStackSize()) {
                          return false;
                      } else {
@@ -65,7 +68,7 @@
                          itemstack1.add(itemstack.getCount());
                          entityitem.pickupDelay = Math.max(entityitem.pickupDelay, this.pickupDelay);
                          entityitem.age = Math.min(entityitem.age, this.age);
-@@ -208,6 +222,11 @@
+@@ -208,6 +225,11 @@
          } else if (!this.getItemStack().isEmpty() && this.getItemStack().getItem() == Items.NETHER_STAR && damagesource.isExplosion()) {
              return false;
          } else {
@@ -77,7 +80,7 @@
              this.ax();
              this.f = (int) ((float) this.f - f);
              if (this.f <= 0) {
-@@ -270,6 +289,26 @@
+@@ -270,6 +292,35 @@
              Item item = itemstack.getItem();
              int i = itemstack.getCount();
  
@@ -87,15 +90,24 @@
 +
 +            if (this.pickupDelay <= 0 && canHold > 0) {
 +                itemstack.setCount(canHold);
-+                PlayerPickupItemEvent event = new PlayerPickupItemEvent((org.bukkit.entity.Player) entityhuman.getBukkitEntity(), (org.bukkit.entity.Item) this.getBukkitEntity(), remaining);
-+                event.setCancelled(!entityhuman.canPickUpLoot);
-+                this.world.getServer().getPluginManager().callEvent(event);
-+                itemstack.setCount(canHold + remaining);
-+
-+                if (event.isCancelled()) {
++                // Call legacy event
++                PlayerPickupItemEvent playerEvent = new PlayerPickupItemEvent((org.bukkit.entity.Player) entityhuman.getBukkitEntity(), (org.bukkit.entity.Item) this.getBukkitEntity(), remaining);
++                playerEvent.setCancelled(!entityhuman.canPickUpLoot);
++                this.world.getServer().getPluginManager().callEvent(playerEvent);
++                if (playerEvent.isCancelled()) {
 +                    return;
 +                }
 +
++                // Call newer event afterwards
++                EntityPickupItemEvent entityEvent = new EntityPickupItemEvent((org.bukkit.entity.Player) entityhuman.getBukkitEntity(), (org.bukkit.entity.Item) this.getBukkitEntity(), remaining);
++                entityEvent.setCancelled(!entityhuman.canPickUpLoot);
++                this.world.getServer().getPluginManager().callEvent(entityEvent);
++                if (entityEvent.isCancelled()) {
++                    return;
++                }
++
++                itemstack.setCount(canHold + remaining);
++
 +                // Possibly < 0; fix here so we do not have to modify code below
 +                this.pickupDelay = 0;
 +            }