PlayerPickupItemEvent check for custom items

Discussion in 'Plugin Development' started by shadow5353, Jun 25, 2014.

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

    shadow5353

    How can I check for items that have a specific name and a lore?

    Heres is my code:
    Code:
    @EventHandler
        public void onPlayerPick(PlayerPickupItemEvent e){
            ItemStack tent = new Wool(DyeColor.GREEN).toItemStack(1);
            ItemMeta im = tent.getItemMeta();
            im.setDisplayName(ChatColor.DARK_GREEN + "Tent");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_PURPLE + "Rightclick with this on the ground to place a tent");
            im.setLore(lore);
            tent.setItemMeta(im);
           
            if(e.getItem().getType().equals(tent)){
                Bukkit.getServer().broadcastMessage("test");
            }
        }
     
  2. Offline

    Avery246813579

    Instead of doing e.getItem().getType().equals(tent), try e.getItem().getType().isSimilar(tent). This happens because once your custom item is given to a player, it gets changed very slightly so that the item in game is not equal to the original. Hope this help!
     
  3. Offline

    Konkz

    PHP:
    public void onPlayerPickUpItem(PlayerPickupItemEvent event) {
    if(
    event.getItem().getItemMeta().getDisplayName().equals(getTent().getItemMeta().getDisplayName())) {
      
    //it's a tent!
    }
    }
     
    public static 
    ItemStack getTent() {
    ItemStack is = new Wool(DyeColor.GREEN).toItemStack(1);
    ItemMeta im is.geItemMeta();
    im.setDisplayName("Tent");
    im.setLore("My lore");
    is.setItemMeta(im);
    return 
    is;
    }
     
  4. Offline

    shadow5353

    Avery246813579 I get a red line under .isSimilar(tent).

    Konkz I get this error under .getItemMeta() "The method getItemMeta() is undefined for the type Item"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    Konkz

    Oops my mistake, I forgot .getType() I think - did not play around with the API for some time. Just experiment for yourself. :)
     
  6. Offline

    shadow5353

    I changed it to if(e.getItem().getType().getName().equals(getTent().getItemMeta().getDisplayName())) { but it still do not work :(
     
  7. Offline

    MineCrashKoen

    Correct me if I'm wrong:

    An Item is the class for items that lay on the ground I'm pretty sure.
    If you are working with items in any type inventory, use ItemStacks

    Hope it helps :)
     
    shadow5353 likes this.
  8. Offline

    shadow5353

    MineCrashKoen Do you mean like this, e.getItem().getItemStack() etc.

    Because I have tried with ItemStacks, did not work

    Edit: Nevermind, your method worked, I used e.getItem().getItemStack().isSimilar(tent)
     
  9. Offline

    MineCrashKoen

    shadow5353 Yeah, if you want to get the ItemStack of the item picked up, you can use item.getItemStack()

    Glad I could help :)
     
    shadow5353 likes this.
  10. Offline

    Avery246813579

    Never use equals. Use is similar.
     
    MineCrashKoen and shadow5353 like this.
Thread Status:
Not open for further replies.

Share This Page