Serializing ItemStacks

Discussion in 'Plugin Development' started by randomizer1234, Aug 5, 2014.

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

    randomizer1234

    Hello.
    I've found this solution to serialize ItemStacks.
    But I was wondering if I will be able to deserialize ItemStacks even when CraftBukkit gets updated (and therefore the CraftItemStack class or any of its attribute classes possibly get changed).
    If the answer is yes, I would be curious to know how this is technically possible (sadly I don't have the time to read the source code of CraftBukkit :)).
    If the answer is no, I would then like to understand what are the uses of BukkitObjectOutputStream and BukkitObjectInputStream.
    Thank you very much!

    EDIT: Just noticed that it is implemented in Bukkit, not CraftBukkit. Just unsee all the "Craft" prefixes from my post :p
     
  2. Offline

    Gater12

    randomizer1234
    ItemStack is ConfigirationSerializable so you can save it directly to the config and invoke getItemStack(String s) to get it from the config.
     
    NathanWolf likes this.
  3. Offline

    randomizer1234

    Thank you for your answer.
    I need to be able to directly handle all the data streams for my plugin to work, I would prefer not to use YAML formatted data.
    I just found this comment in a Bukkit commit:
    Apparently, those Stream functions provide a wrapper to serialize ConfigurationSerializable objects as if they are Serializable objects.
    If I understand well, when a object goes through BukkitObjectOutputStream what happens is [Object implementing ConfigurationSerializable] -> [Bukkit's internal config representation, which should be forward-compatible] -> [Object implementing Serializable, through a Bukkit class called Wrapper] -> [Bytes written to a Stream].
    If a Bukkit developer could actually confirm this, it would be really nice, as I'm not really at the right level to fully understand what's happening here.

    Wolvereness sorry to bother you, but you could be the only one which is able to help me.
     
  4. Offline

    Wolvereness Bukkit Team Member

    BOOS, and BOIS work together. When you serialize something with BOOS, it substitutes any object that implements ConfigurationSerializable with Wrapper, which wraps a Map<String, Object>, just like our YAML does. The actual map serialized can consist of primitives, strings, nested 'ConfigurationSerializable', or other maps. Coincidentally, all of these things implement Serializable, or ConfigurationSerializable (which gets substituted recursively with Wrapper).

    It is NOT a primitive data stream! It is an ObjectOutputStream, as implemented by Java specs - with the Wrapper class carrying a special meaning, unable to be serialized directly itself.
     
    randomizer1234 and NathanWolf like this.
  5. Offline

    randomizer1234

    I think I got it. Thank you!

    One more question. I'm implementing my own Inventory class for a plugin I'm writing, and I'm comparing my method implementation with the CraftBukkit ones.
    In the method public HashMap<Integer,ItemStack> addItem(ItemStack...items) (click on the method signature to open the source code), why do you call getMaxItemStack(), which calls getMaxStackSize() from the Minecraft IInventory instead of calling item.getMaxStackSize(), or even better Math.min(getMaxItemStack(), item.getMaxStackSize())?
    I'm probably missing something here... If someone could make that clearer for me it would be nice :)

    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page