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

Add an EntityPickupItemEvent

XMLWordPrintable

      Right now there is no event for when a zombie/skeleton picks an item up off the ground. It would be useful for allowing some items to be picked up while denying others, instead of the all-or-nothing behavior of CanPickUpLoot. Another use would be to make something happen when an item is picked up. For example: giving the mob levitation when it picks up a feather, or exploding when it picks up TNT.

       
      I was able to throw together the functionality on my end using the following code...

       A simple copy/paste of PlayerPickupItemEvent with the getRemaining() bit removed:
      EntityPickupItemEvent.java - https://pastebin.com/raw/G9sWpgBb

      Calling the event in what I think is the appropriate location in EntityInsentient.java (replicating how PlayerPickupItemEvent was implemented):

      protected void a(EntityItem entityitem) {
          // New stuff start - Fire EntityPickupItemEvent
          EntityPickupItemEvent event = new EntityPickupItemEvent(this.getBukkitEntity(), (org.bukkit.entity.Item) entityitem.getBukkitEntity());
          event.setCancelled(this.canPickUpLoot);//should cX() be preferred since it returns canPickUpLoot?
          this.world.getServer().getPluginManager().callEvent(event);
          if (event.isCancelled()) {
              return;
          }
          // New stuff end
          ItemStack itemstack = entityitem.getItemStack();
          EnumItemSlot enumitemslot = d(itemstack);
          boolean flag = true;
          ItemStack itemstack1 = this.getEquipment(enumitemslot);
      
          if (!itemstack1.isEmpty()) {
      

       I was able to test that it does work for Zombies/Skeletons. Cancelling the event left the item on the ground and it was not picked up. Setting a new ItemStack to the Item entity caused the mob to pick up the new item instead of what was there before.

      The reason I removed the getRemaining() stuff that the player event had was because the mobs seem to just pick up the entire thing.

      I mean... it looks like it works to me, but just because it works doesn't mean it is right o.o Idk, you'll probably know better than I do.

            Unassigned Unassigned
            iPyronic iPyronic
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: