Solved PlayerPickupItemEvent

Discussion in 'Plugin Development' started by KeybordPiano459, Dec 22, 2012.

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

    KeybordPiano459

    What's wrong with my code? I'm still picking up poisonous potatoes, and my variable in the HashMap isn't being incremented.
    Code:java
    1. @EventHandler
    2. public void onPickup(PlayerPickupItemEvent event) {
    3. Player player = event.getPlayer();
    4. if (player.getWorld() == Bukkit.getServer().getWorld("battlefield")) {
    5. if (event.getItem() == new ItemStack(Material.POISONOUS_POTATO)) {
    6. event.setCancelled(true);
    7. Coins.addCoins(player.getName(), 1);
    8. event.getItem().remove();
    9. }
    10. }
    11. }
     
  2. Offline

    RealDope

    KeybordPiano459
    Try replacing the if(event.getWorld()) blah blah part to this:
    Code:JAVA
    1.  
    2. if(player.getWorld().getName().equalsIgnoreCase("battlefield")) {
    3. // Other stuff
    4. }
    5.  


    Also I don't think you have to make a new itemstack, you could just do:
    Code:JAVA
    1.  
    2. if(event.getItem().getType() == Material.POISONOUS_POTATO)) {
    3. // Other stuff
    4. }
    5.  
     
  3. Offline

    KeybordPiano459

    First of all, in PlayerPickupItemEvent, event.getItem().getType() returns an EntityType, not a Material. Second of all, that didn't solve it...
     
  4. Offline

    Polaris29

    Do what RealDope suggested for the event.getWorld() part

    And it's not working is because getItem() returns an Item, not an ItemStack
    To get the ItemStack from an Item, use event.getItem().getItemStack(), then with the ItemStack you can use .getType() to get the Material

    Code:
    if (event.getItem().getItemStack().getType() == Material.POISONOUS_POTATO) {
        //Do other stuff
    }
    
     
  5. Offline

    KeybordPiano459

    Thanks, that worked. Also, for anyone wondering how to do this with the new items combining into one entity (added in 1.4 or something) do it like this to add the amount of items in the single entity:
    Code:java
    1. if (player.getWorld().getName().equalsIgnoreCase("battlefield")) {
    2. if (event.getItem().getItemStack().getType() == Material.POISONOUS_POTATO) {
    3. event.setCancelled(true);
    4. Coins.addCoins(player.getName(), event.getItem().getItemStack().getAmount());
    5. event.getItem().remove();
    6. }
    7. }
     
  6. Offline

    fireblast709

    Note that
    Code:java
    1. if(player.getWorld() == Bukkit.getWorld("someworld"))
    is just perfectly fine, as player.getWorld() and Bukkit.getWorld("someworld") are references of the same object (if they are in the same world)
     
Thread Status:
Not open for further replies.

Share This Page