Need Help with PlayerItemPickupEvent

Discussion in 'Plugin Development' started by Lanuk, Oct 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Lanuk

    Hello,

    Basically I am trying to make a simple plugin that makes it so if you pick up an item (in this case, a sword), and if that sword has a specific enchantment on it (in this case, an unsafe enchantment, power), you will be sent a message.

    I know the error is somewhere in this onPlayerPickupItemEvent method because all my other methods work just fine.

    Here is my code:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerPickupItem(PlayerPickupItemEvent event) {
    4. Player p = event.getPlayer();
    5.  
    6. if(p.getItemInHand() != null){
    7.  
    8. //If the item is a sword
    9. if(p.getItemInHand().getTypeId() == 267
    10. || p.getItemInHand().getTypeId() == 268
    11. || p.getItemInHand().getTypeId() == 276
    12. || p.getItemInHand().getTypeId() == 272
    13. || p.getItemInHand().getTypeId() == 283){
    14.  
    15. Map<Enchantment, Integer> Enchants = p.getItemInHand().getEnchantments();
    16. Integer level = Enchants.get(Enchantment.ARROW_DAMAGE);
    17.  
    18. //If the enchantment is power
    19. if (Enchants.containsKey(Enchantment.ARROW_DAMAGE)) {
    20.  
    21. //Send player a message
    22. p.sendMessage("Yay, there you go");
    23. }
    24. }


    I am not getting the message sent to me--is it because it is trying to get the item in the player's hand before the item is actually picked up? Help would be appreciated, thanks!

    Oh, also: I know there aren't a correct amount of curly braces and all--I have more code under that.
     
  2. Offline

    Builder4Free

    Okay, after looking through it I found the problem. PlayerItemPickupEvent is called before the item is actually picked up. So, you can't use:
    Code:
    p.getItemInHand();
    I believe you have to use:

    Code:
    event.getItem();
    I hope this helps; glad to help you anytime :D!!
     
  3. Offline

    Lanuk

    Thanks, but I've already tried this as well, and got the same result. What I did was:

    Player p = event.getPlayer();
    Item i = event.getItem();

    And then checked if the player's iteminhand was equal to i.
     
  4. Offline

    Woobie

    I have code for something like this somewhere, but i cant access my files atm.
    Probably not the right code, but try
    Code:
     if(event.getEntity().getItem( == Item.SOMETHING){ 
    or
    Code:
     if(event.getEntity() instanceof Diamond_sword){
     
  5. Offline

    Lanuk

    The getEntity method does not apply to this event. I have tried
    Code:
    if(event.getItem() == p.getItemInHand()
    and stuff like that, but nothing has worked.
     
  6. Offline

    Builder4Free

    The p.getItemInHand() doesn't go into effect until the event is fired, then ended... Only event.getItem() will work.
     
  7. Offline

    Lanuk

    Ah thanks, that makes sense. I wonder how I will go about doing this then... :p
     
  8. Offline

    Tirelessly

    just use e.getItem() instead of p.getItemInHand()......
     
  9. Offline

    EnvisionRed

    I'm not sure why you assumed that the picked-up item will automatically go into the player's hand. Why not just do something along these lines:
    Code:
    ItemStack i = event.getItem().getItemStack();
    int id = i.getTypeId();
    Player p = event.getPlayer();
    if (id == 268 || id == 272 || id == 276 || id == 283 || id == 267){
    p.sendMessage("You have picked up a " + i.getType());
    }
    
     
  10. Offline

    Lanuk

    I tried that and it didn't work. Ultimately I do want to find the item in the player's hand as well, so somewhere along the line I would have to check whether e.getItem() is equal to p.getItemInHand() which obviously wont work... I think o.0

    Yes, thank you, but I do want to find if the item ends up in the player's hand on pickup.
     
  11. Offline

    Tirelessly

    You need to make sure the item in hand isnt null..
     
  12. Offline

    hockeygoalie5

    I'm using the same system in my code. Here you go:
    Code:
    @EventHandler
    public void onPlayerPickupItem(PlayerPickupItemEvent e) {
        ItemStack item = e.getItem().getItemStack();
        if(item != null) {
            // I gave the items I wanted to check for a durability of at least 9000
            if(item.getEnchantmentLevel(Enchantment.DURABILITY) > 8999) {
                // Send message
            }
        }
    }
     
    
     
Thread Status:
Not open for further replies.

Share This Page