ItemFlag Problem

Discussion in 'Spigot Plugin Development' started by Serializator, May 9, 2015.

  1. Serializator



    I need to check if two items are the same, these items have both an ItemFlag in there ItemMeta. I'm 100% sure they are the same, but when I do:

    Code (Text):
    It will return false, when I removed the ItemFlag from both items it returns true. Why?

    With regards,
    Julian v.d Berkmortel
  2. Try to use ==
  3. == only works on primitives and Objects which use the same memory space.
  4. Add the same random int value as a metadata value on the item, and then check if that is the same?
  5. Serializator


    Isn't working, the weird thing is when I do equals(); or isSimilar(); in the onEnable(); with as argument the direct getter of the ItemStack it will return true, but when I do equals(); or isSimilar(); on the player#getItemInHand(); method in the PlayerInteractEvent it will return false.
  6. Maybe getting the item meta?
  7. Serializator


    Not working.
  8. You may have already done this, but I tried digging through the code to see how the comparison was being accomplished. I may be taking a shot in the dark, but this is what I've seen.

    The equals() method of ItemStack appears to also call isSimilar(), which appears to compare the ItemMeta using ItemFactory. ItemFactory compares the metadata using the methods equalsCommon() and notUncommon(). equalsCommon() appears to compare numerous items. Is it possible that any of these numerous items are changing for the ItemStack from the time you use the method in your onEnable() and when you use it again in PlayerInteractEvent?

    Alternatively, perhaps you could write your own comparison method that only compares those attributes in which you are interested? It might be an easier solution than trying to navigate the provided comparison methods for ItemStacks.