Solved [1.13] Event when a trident is picked up

Discussion in 'Spigot Plugin Development' started by Gadse, Dec 31, 2018.

  1. Good day everyone,

    I want to prevent players from picking up tridents, that they didn't throw.

    Now, I know that there is the PlayerPickupArrowEvent, but since that extends from the PlayerPickupItemEvent, it's indirectly deprecated.

    I noticed that by trying to use
    Code (Text):
    Player player = (Player) event.getArrow().getShooter();
    if(player != event.getPlayer())
        event.setCancelled(true);

    I also know, that the intended Event is EntityPickupItemEvent, but as far as I can see, it's not possible to get an arrow or trident entity from there.

    My current solution for this would be to write the owner in the lore or name of the trident and catch that in the PickupItem event, but I don't think that's very clean.

    Is there any intended method that I'm just blatantly missing?
     
  2. Eh? Can't you just use #getItem then item#getType and see if its a trident?
     
    • Like Like x 1
  3. Thanks, I overlooked that :)

    In case anyone else has the problem, this is the code I'm using now


    Code (Text):
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Trident;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityPickupItemEvent;

    public class PlayerPickupArrowEventHandler implements Listener {
        @EventHandler
        public void onPlayerPickupArrow(EntityPickupItemEvent event) {
            if(event.getItem().getType().equals(EntityType.TRIDENT)) {
                Trident trident = (Trident) event.getItem();
                if(event.getEntity() instanceof Player) {
                    Player player = (Player) event.getEntity();
                    if(trident.getShooter() instanceof Player) {
                        Player shooter = (Player) trident.getShooter();
                        if(player != shooter)
                            event.setCancelled(true);
                    }
                }
            }
        }
    }
     
    • Like Like x 1
  4. Remember to use == to compare enums instead of #equals
     
    • Informative Informative x 1