Solved Check for item and his lore in inv. and modify it on event.

Discussion in 'Plugin Development' started by BioBG, Apr 17, 2016.

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

    BioBG

    Hi
    I'm trying to check for item in player inventory on event with loop.
    I can get the item and based on specified lore i want to do something with it.

    here a example code, how to modify only that item have this lore and name?
    Code:
            for (ItemStack i : e.getInventory().getContents())
            {
                if (i.getType() == Material.<MyItemILookFor>)
                {
                    if (i.getItemMeta().getLore() != null && i.getItemMeta().getDisplayName().equals("<myitemname>"))
                    {
                        List<String> l = i.getItemMeta().getLore();
                        if (l.get(0).equals("<loreilookfor>"))
                        {
                            // do something with that item: delete/rename/change/drop/eat/enchant
                        }
                    }
                }
            }
    everything i try do modification on all items, not only on that specified one i want.
     
  2. You need to edit your loop so you won't get a ConcurrentModificationException when editing the contents:
    Code:
    for (int i = 0; i < e.getInventory().getSize(); i++) {
    ItemStack item = e.getInventory().getItem(i);
    //your code
    
    //update item:
    e.getInventory().setItem(i, item);
    }
    And also check if the item has ItemMeta before accessing it!
     
  3. Offline

    BioBG

    Every time when IF return false i get NullPointerException
    when i check IF the item is that i look for, is item have meta, is have lore, is have specified lore .... NullPointerException

    and if fail on some checks, breaks, and did not continue to check other slots for that item ..
     
  4. Offline

    mcdorli

    You can't assume the inventory has something at a specific place, check if the item is null or not. Same for ItemMeta, lore and displayname
     
  5. Offline

    BioBG

    how can i check is that item is a that i look for and is not null :)
    (item.getType() != null && item.getType().compareTo(Material.<myItem>) < 0) ?
     
  6. Offline

    mcdorli

    item != null, also, check out this: https://docs.oracle.com/javase/tutorial/
     
  7. Offline

    BioBG

    ok, I think i solved it :)

    but i found a bug, if the item is in offhand, cannot be modified, will check if the item in offhand is that i look for and will try to delete it or move it.

    Thanks for help guys!

    How can i modify the new 40 slot in inventory(Offhand)
    i want to move or delete item if is in that slot but i can't.

    i solved that too :)
     
    Last edited: Apr 17, 2016
Thread Status:
Not open for further replies.

Share This Page