1.14.4 Setting persistent data to ItemStack without an ItemMeta

Discussion in 'Spigot Plugin Development' started by DavidMa, Feb 25, 2020.

  1. Hi.
    I am trying to put persistent data on an ItemStack.
    However, ItemStack#getItemMeta is nullable. How would I set the persistent data if the ItemMeta is null?
  2. If the ItemMeta is null, you can‘t do that. However, getItemMeta only returns null, if there is no sensible ItemMeta for the given stack available, for example if the ItemStack represents Air (Material.AIR)
    • Like Like x 2
  3. getItemMeta() never returns null, if the item didn't have a meta then the method would apply a new one for it and return it. so it's absolutely safe to save your data on any item.
    • Optimistic Optimistic x 1
  4. No.
  5. Perhaps the worst way possible to express that you don't agree with someone, you're a troll.
  6. There are worse ways, my intention was never to troll thou. Read the post above, and you‘ll understand
    #6 Schottky, Feb 25, 2020
    Last edited: Feb 25, 2020
  7. okay my entire comment was wrong, but the general conclusion is still right though.. it's safe if you can guarantee that the type isn't AIR which usually never happens.. but good to know!
    • Agree Agree x 1
  8. I’m reasonably sure Player#getItemInHand() returns AIR if nothing is held, so this is not as uncommon as you think. But again, as long as you make sure that the item isn’t null and that it isn’t AIR you’re safe.