.getItemMeta() weird behaviour when iterated

Discussion in 'Plugin Development' started by tfacchini, Jul 1, 2015.

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

    tfacchini

    Hello Guys,

    I'm trying to prevent a specific item to be dropped when the event PlayerDeathEvent is fired, though I'm can't understand the results .getItemMeta() is returning.

    My code:
    Code:
        
    public void preventSkillBookDropOnDeath( PlayerDeathEvent event ) {
            Player                 player          = event.getEntity().getPlayer();
            List<ItemStack>     itemsDropped     = event.getDrops();
            Iterator<ItemStack> iterator         = itemsDropped.iterator();
           
            while ( iterator.hasNext() ) {
               
                if ( iterator.next().getType().equals(Material.WRITTEN_BOOK ) ) {
                   
                    player.sendMessage(" item meta : " + iterator.next().getItemMeta() );
                    BookMeta bookmeta = (BookMeta) iterator.next().getItemMeta();
                   
                    if ( bookmeta.getTitle().equalsIgnoreCase( player.getName() + " skill book") ) {
                        iterator.remove();
                    }           
                }     
            }
        }
    
    The event identify the Material is a WRITTEN_BOOK, but when I try .getItemMeta, to check the "book contents" I get: "UNSPECIFIC_META:{META-TYPE-unspecific} also it throws
    java.lang.NullPointerException.

    Any ideas?

    Thanks in advance,
    TF
     
  2. Offline

    mine-care

    Can we see the npe? And that's what it would return if you invoke toString on ItemMeta object.
     
    tfacchini likes this.
  3. Offline

    tfacchini

    Sure...

    Yes mine-care you right about what would return, but the thing is:
    By the time the condition WRITTEN_BOOK is true, it always should return a bookMeta obj

    If I remove the iteration and use a loop, it works, but them the method .remove() dont :(

    There you go:
    [​IMG]
     
  4. Offline

    mine-care

    I missed something... What doesn't work again? Also try using a for each loop to simplify and optimize your code.
     
    tfacchini likes this.
  5. You access iterator.next() 2 times. Why make it so commplicated? Just use this instead of the iterator and while-loop:
    Code:
    for (ItemStack item : event.getDrops()) {
     
    mine-care likes this.
  6. Offline

    tfacchini

    No worries,

    Well actually the goal of this script is:

    OnPlayerDeathEvent: Prevent an item called "<playername> skill book" to drop. It happens that this item is a book.

    So what I'm doing:
    using .getDrops() to check if there is any book in the list, and in case of yes, check if the book is named "<playername> skill book", if it is, remove from getDrops() list.

    Pretty much it.

    BTW: Thanks for the help :)
     
Thread Status:
Not open for further replies.

Share This Page