Serializing Inventory to an ArrayList<String>

Discussion in 'Plugin Development' started by CorrieKay, Oct 21, 2012.

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

    CorrieKay

    With the new stuff happening in 1.4 regarding item stacks, and the addition of written books in 1.3, i have come to the conclusion that my serialization methods regarding inventories are quickly becoming outdated.

    Instead of continually updating my code, I'm going to look for a simpler way to serialize and deserialize my item stacks into an array list.

    Its either that or something else. I am completely open to suggestions! Whaddya think?
     
  2. Offline

    hakelimopu

    I've had similar thoughts regarding my own code.

    However, I think there is an answer.

    Through use of a JSON library, it should be possible to have a single maintenance point for ItemStack->String and String->ItemStack serialization.
     
  3. Offline

    CevinWa

    You could just use the omc-lib as a reference and use the serializeableinventory class.
     
  4. Offline

    CorrieKay

    I just checked out the source for that, it looks like it does the same exact thing as mine :p
     
  5. Offline

    CevinWa

    Aha ok
     
  6. Offline

    CorrieKay

    mmh, the problem is, if you used this with a written book, the book data would be erased.

    And when 1.4 hits, it will also erase weapon lore data :\
     
  7. Offline

    CevinWa

    ohh yes. :D didn't tought of that. Lucky for me that i just use it for locations. :D
     
  8. Offline

    Ammar Askar

    CorrieKay likes this.
  9. cant you just store the inventory data to an config file, as bukkit designed the code, I would accept its updated with updates
     
  10. Offline

    CorrieKay

    Awesome! this will probably work the best.

    Interesting, i saved the inventory directly to a node, and it seemed to work. Then again, how would i deserialize it?
     
  11. Offline

    Ammar Askar

  12. (too bad the javadocs are down now, there they said how to desiralize it, let me look into my cache)

    an item stack has the method static ItemStack deserialize(Map<String,Object> args) , so I guess you should use that
     
  13. Offline

    CorrieKay

    So... This is weird. After utilizing the Multiverse Inventory api, it was unable to serialize books and named items at all :\

    edit: whatevs, ill make my own serialization method based on the NBT tags idea.

    That said, does anyone know how i can load a player.dat file, so i can get to their inventory?
    edit2: if you respond, please do so in this thread.
     
  14. Offline

    urm

    As soon it's the only forum thread about storing inventory data which can be easily googled, I'll post best (IMAO) way to store custom inventories. Idea of using serialize() was adviced to use by ferrybig (big thx), but there was no code.
    Code:java
    1. class InventorySaver {
    2. public static ConfigurationSection serialize(ItemStack[] content) {
    3. ConfigurationSection section=new MemoryConfiguration();
    4. Map<String,Object> air=(new ItemStack(Material.AIR)).serialize();
    5. section.set("length",content.length);
    6. for(int i=0;i<content.length;i++) {
    7. if(content[x]==null) section.set(String.format("is%s",i),air);
    8. else section.set(String.format("is%s",i),content[x].serialize());
    9. }
    10. }
    11.  
    12. public static ItemStack[] deserialize(ConfigurationSection section) {
    13. int n=section.get("length");
    14. ItemStack[] content=new ItemStack[n];
    15. Map<String,Object> temp;
    16. ConfigurationSection stack;
    17. for(int i=0;i<n;i++) {
    18. stack=section.getConfigurationSection(String.format("is%s",i));
    19. content[i]=ItemStack.deserialize(stack.getValues());
    20. }
    21. return content;
    22. }
    23. }[/i]
     
Thread Status:
Not open for further replies.

Share This Page