Meta of an ItemStack clone

Discussion in 'Plugin Development' started by Codisimus, Mar 22, 2013.

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

    Codisimus

    In my testing, I found that modifying the lore of an ItemStack's clone effects the original ItemStack.

    PHP:
    ItemStack clone = item.clone();
    ItemMeta meta = clone.hasItemMeta()
                    ? clone.
    getItemMeta()
                    : 
    Bukkit.getItemFactory().getItemMeta(clone.getType());
    List<
    Stringlore meta.getLore();
    ListIterator<Stringitr lore.listIterator();
        
    //here I iterate through and do either itr.set() or itr.remove()
    meta.setLore(lore); //It is this line that actually changes the lore for both the clone and original
    clone.setItemMeta(meta);
    Is this intended? if so, how do I modify the lore of only the clone?

    EDIT: tried cloning the ItemMeta b4 modifying with the same result
     
  2. Offline

    Nitnelave

    The whole point of clone is to get another instance of the Object, identical in all respects, but independent. So you get a copy of the object, that you can freely manipulate without affecting the original one.
     
  3. Offline

    Codisimus

    Exactly! but that isn't the case. It is odd but I have to clone it several times before it becomes disjointed.
     
  4. Offline

    Nitnelave

    Maybe the metadata is shared. Try setting the metadata of the clone to a clone of the metadata.
     
  5. Offline

    Ivan

    The metadata will point to the same instance of the metadata object. Like Nitnelave said, you want to clone the ItemMeta instead of the ItemStack.
     
  6. Offline

    Codisimus

    Above I mentioned that I cloned both with the same result.
     
  7. Offline

    Ivan

    My bad sorry.
     
  8. Offline

    Nitnelave

    Can I see your code if you updated it? I'd like to try it.
     
  9. Offline

    Codisimus

  10. Offline

    RingOfStorms

    Codisimus

    The clone's item meta still points to the originals item meta. When you do the following on 484-486:
    Code:java
    1.  
    2. meta = clone.getItemMeta();
    3. meta.setLore(lore);
    4. clone.setItemMeta(meta);
    5.  


    You haven't cloned the item meta, you take the same item meta that both the original and the clone point to and edit it. So that makes them both update. If you want to make a new clone of the original item meta then you'll have to construct a new ItemMeta object completely.

    Edit: You can also link to line numbers with github using: #L<line number> at the end of the link, or multiple with #L<line number>-<line number>
     
  11. Offline

    Codisimus

    RingOfStorms Then wut is the purpose of meta.clone()?

    btw it doesn't edit the original with the code on Github. only when I remove the line meta = clone.getItemMeta();
     
  12. Offline

    RingOfStorms

    So put that line in? O_O I don't see it on that Loot.java anywhere :eek:
     
  13. Offline

    Codisimus

    The code on Github works the way that I want it to I just don't understand why the meta is not properly cloned using meta.clone()
     
  14. Offline

    RingOfStorms

    So the code on github w/o the clone works fine? But once you add it, it doesn't work? - I'm a bit confused.
     
  15. Offline

    Codisimus

    the clone method doesn't make a difference either way. I am confused too. I think I should just let it be.
     
  16. Offline

    RingOfStorms

    so with and without the clone method it still edits the original item?

    If that is the case then that is just an API fail and you'll have to do what I said and construct a new object.
     
  17. Offline

    Codisimus

    Can't really construct a new object bc then I would have to copy over enchantments, lore, displayname, color if it is leather armor, pages if it is a book, etc. I am gonna halt this conversation until I can get a small debug method written up to show the code and wut is modified without all the extra.
     
  18. Offline

    RingOfStorms

    Yea, but if the clone method is broken then that would be the only way to go. Maybe they didn't feel like doing all that either, or it isn't updated with the most recent nbt formats.
     
  19. Offline

    Nitnelave

    So if you do the following :
    ItemStack clone = item.clone ();
    MetaData clonedData = item.getMetadata().clone();
    clone.setMetaData (clonedData);
    It will still modify the original item?
     
  20. Offline

    Wolvereness Bukkit Team Member

  21. Offline

    Codisimus

    Wolvereness Thank you for the confirmation that this was actually an error. I thought I was going crazy.
     
Thread Status:
Not open for further replies.

Share This Page