[SPIGOT-3403] Add an EntityPickupItemEvent Created: 07/Jul/17  Updated: 06/Aug/17  Resolved: 28/Jul/17

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: iPyronic Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: EntityPickupItemEvent, entity, event, pickup


 Description   

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.



 Comments   
Comment by md_5 [ 06/Aug/17 ]

And what do you suggest be done about this?
It's an unavoidable side effect, read the javadocs and you're fine.

There is no "new event" for arrows because afaik only players can pick them up.

Comment by xGhOsTkiLLeRx [ 06/Aug/17 ]

Having this being merged in the master branch, it deprecated the good old PlayerPickupArrowEvent from here: https://hub.spigotmc.org/jira/browse/SPIGOT-1733 Is there any particular reason?

I have to deal with deprecation now in order to get the arrow entity, not the item. Besides that the "new" event does not fire when a player picks up an arrow. Can you clarify please @md_5?

Comment by iPyronic [ 07/Jul/17 ]

Woops, I totally meant to put event.setCancelled(!this.canPickUpLoot);
I forgot the !, but I don't see a edit button to fix it
I'm not entirely sure that line is even relevant for entities though. It was there for the player event so I copied it over :/

Generated at Sat Dec 13 15:19:45 UTC 2025 using Jira 10.3.13#10030013-sha1:56dd970ae30ebfeda3a697d25be1f6388b68a422.