Store data in ItemStacks

Discussion in 'Plugin Development' started by OrangeGuy, May 27, 2014.

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

    OrangeGuy

    Hi everyone! I made a class called "SurpriseItem" that extends ItemStack, and add two vars in it.

    Then I added it into an inventory, and then try to get it back in InventoryClickEvent. However, when I'm trying to use SurpriseItem surpriseItem = (SurpriseItem) event.getCurrentItem(), it throws ClassCastException. Is there anyway to do that?

    (I want to share some data between my configloader and eventlisteners)

    Currently I am storing it by item lore, and hide it by using ProtocolLib. I want to find a way that needs not ProtocolLib...

    ;)My friend told me to use Custom Event and Decorator Patter.. but I have no idea how to use them..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  2. Offline

    desht

    At this time, the only ways of storing custom data on an ItemStack are:
    Subclassing the ItemStack class certainly won't work.
    There is an open PR right now thanks to NathanWolf to add Bukkit API to manage custom data - let's hope it gets pulled at some point :)
     
    NathanWolf and OrangeGuy like this.
  3. Offline

    OrangeGuy

    I have took a look of AttributeStorage classes, however, it still displays "empty" lore. I guess that I still need ProtocolLib with those classes?
     
  4. Offline

    desht

    It shouldn't display any lore at all (I'm using AttributeStorage heavily in this plugin and I can confirm it works) - item attributes are separate from the lore. Are you sure you're not still setting some empty lore?
     
  5. Offline

    OrangeGuy

    OK. And does it supports multi-version of CraftBukkit?
     
  6. Offline

    desht

    Yes, it should do, although I'll leave it to Comphenix to confirm that. The current version works with all 1.7.x releases, at least.
     
    OrangeGuy likes this.
  7. Offline

    OrangeGuy

    Thanks, I think I should try it again.. It is so annoying to use lore.
     
  8. Offline

    OrangeGuy

    I tried it again. However, there still have invisible lore. After I removed "storage.setData("a");", the lore removed.

    Tested with CB 1.7.9 R0.1 and Minecraft 1.7.2.

    Any ideas?

    Before I removed setData("a"):
    [​IMG]

    After I removed setData("a"):
    [​IMG]

    :( desht

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    bensku

    It actually isn't lore line, but AttributeStorage always creates invisible attribute which causes small emphty space under lore. As I know it may removed with some ProtocolLib hacking, but if you can use ProtocolLib there isn't any point to use AttributeStorage (invisible lore is easier).
     
    OrangeGuy likes this.
  10. Offline

    OrangeGuy

    Even if I can't use ProtocolLib, theres still no point to use AttributeStorage. Remove invisible lore is much easier than remove invisible attributes..

    desht Any ideas?

    And I don't think it is "small empty space", it just likes another empty line of lore.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. Offline

    OrangeGuy

  12. Offline

    metalhedd

    OrangeGuy you already got the only answer there are. AttributeStorage, or hidden Lore. I use the hidden lore technique in Portable Horses, and it works great.
     
    OrangeGuy likes this.
  13. Offline

    desht

    Yeah, you're right, I'm afraid, there is a blank line. So your options are, basically:
    1. Put the data in the item's lore and optionally use ProtocolLib to hide it
    2. Use an item attribute and optionally use ProtocolLib to hide it (possibly more suitable for storing larger chunks of data, up to 64KB)
    3. Direct messing with item NBT data - a lot of work
    4. Wait for Bukkit to support custom itemstack metadata (the holy grail)
     
    OrangeGuy and NathanWolf like this.
  14. Offline

    NathanWolf

    OrangeGuy and desht like this.
  15. Offline

    OrangeGuy

    OK.. I decided to choose #2, so OPs can modify the item's lore freely, without breaking the information inside it...

    and less laggy if some crazy guys named it to "Type: LOLOLOLOL (* 1000)"

    Hope it will come before I stop playing MineCraft :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  16. Offline

    NathanWolf

    Well obviously I can make no guarantees, but from the sound of it, supporting custom item data may be required, in some form, for vanilla functionality in 1.8. So really, I have hope!
     
    Zettelkasten and OrangeGuy like this.
  17. Offline

    OrangeGuy

    That sounds good! Don't want to mess with Attributes and reflections.. They can be broken at any time...
     
Thread Status:
Not open for further replies.

Share This Page