Solved PlayerPickupItemEvent with pickup custom items

Discussion in 'Plugin Development' started by GRayzerG, Jun 30, 2015.

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

    GRayzerG

    Hi, i need help with PlayerPickupItemEvent with pickup custom items i have, don't send message and give rupias
    Code:
    public void onPickup(PlayerPickupItemEvent event) {
            Player player = event.getPlayer();
            ItemStack rp = new ItemStack(Material.EMERALD);
            ItemMeta sl = rp.getItemMeta();
            sl.setDisplayName(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "Rupia");
            rp.setItemMeta(sl);
            if(event.getItem().equals(rp)){
                EconomyAPI.giveRupia(player, 10);
                player.sendMessage("Received 10 Rupias");
            }
        }
     
  2. Offline

    bwfcwalshy Retired Staff

    @GRayzerG Have you debugged it? Print out what the item and rp is and print out to see if they match.
     
  3. Offline

    567legodude

    @GRayzerG Try adding ".getItemStack()" after event.getItem() and replace .equals(rp) with .isSimilar(rp)
    I'm not sure if that would fix it but it might help.

    The reason it's not working right now is because you are comparing an Item to an ItemStack. So of course they won't be equal. So you get the ItemStack from the Item, and use the isSimilar method to see if they are the same item.
     
  4. Offline

    terturl890

    Wouldnt it be easier if you changed ".equals()" to "=="? so getItemStack() == rp? Just went through a recent post that had an error using .equals() but worked when they used ==.
     
  5. Offline

    tytwining

    Put the whole class to help us debug it. If you can't do it yourself then please give us all of your code.

    Sorry for how bitter I may have sounded there, but seriously -- if you put all of the code then we would be able to rule out a ton of things.

    But remember:
    - Is there @EventHandler above the method?
    - Does the class implement Listener?
    - Are you registering the events?
     
  6. Offline

    567legodude

    @terturl890 I found that when you compare items, it's easier to use the built-in method made to compare items. ItemStack.isSimilar() and on the javadocs they say "This method is the same as equals, but does not consider stack size (amount)."
     
  7. Offline

    terturl890

    @567legodude
    Thanks for the new insight on some new code I could use ;)
     
  8. Offline

    GRayzerG

Thread Status:
Not open for further replies.

Share This Page