Save Inventory

Discussion in 'Plugin Development' started by Aragone, Aug 7, 2019.

  1. Offline

    Aragone

    Hello,

    I'm posting a thread here because I would like to save the player inventory when he goes to a specific world, and then when he recomes in the true world, the inventory is returned.

    The problem is : I don't know how to save it, more precisly where. I heared about a file (config.yml) but I don"t have it in my server folder. I can't keep the inventory in a ItemStack[] variable because if the server restart or reload, it will just lose the items.

    Thanks for your help and have a good day
     
  2. Offline

    maxwell71

  3. Offline

    KarimAKL

    @Aragone
    1. Create the file to save it in.
    2. Get the contents using Inventory#getContents()
    3. Save the contents to the file
    4. Load the contents from the file
    5. Set the contents using Inventory#setContents(ItemStack[])
     
  4. Offline

    Aragone

    @KarimAKL I suspected that but How to put the data in the file ?
     
  5. Offline

    KarimAKL

    @Aragone Use the ConfigurationSection#set(String, Object) method. Make a list containing all the ItemStacks in the array, then add that to the specified path.
     
  6. Offline

    Aragone

    Ok thanks I will try that
     
  7. Offline

    KarimAKL

    @Aragone If that doesn't work, try looping the array and then just set the ItemStack at the specified path, then use the ConfigurationSection#getItemStack(String) method.
     
  8. Offline

    maxwell71

    please mark this as solved, @Aragone.
     
  9. Offline

    Aragone

    @maxwell71 This is not solved. I really don't know how to transform the inventory to a String to save it in a file
     
  10. Offline

    maxwell71

    Oh okay, it seemed like it was solved.
    We wrote a couple of solutions, try them.
     
  11. Offline

    Aragone

    Last edited: Aug 9, 2019
  12. Offline

    KarimAKL

    @Aragone
    1. Put the variables 'i' and 'invToString' into the method.
    2. Where do you get the item from the file?
    3. You can also save the ItemStack object, and then use ConfigurationSection#getItemStack(String path).
     
  13. Offline

    Aragone

    Finally I don't want to save the string into a file but a database (it will be more simple for me who is a beginner).

    So the difficulty is only to convert the complete item (durability, amount, type and enchantment) to a string. It works for the amount, durability and type but I don't know how to save the enchantment.

    Thx for your help
     
  14. Offline

    KarimAKL

    @Aragone Try saving the map gotten from ItemStack#serialize() and then get the item using ItemStack#deserialize(Map<String, Object>)
     
  15. Offline

    Aragone

  16. Offline

    KarimAKL

    What is line 19 of your 'ComTest' class?
     
  17. Offline

    Aragone

  18. Offline

    KarimAKL

    @Aragone 'inv' is null, you haven't initialized it.
     
  19. Offline

    Aragone

    @KarimAKL what ? Inv is InventorySerializer no ?
     
  20. Offline

    CraftCreeper6

    @Aragone
    Yes but you haven't initialized it. You need to create an instance of it in your class.
     
  21. Offline

    Aragone

    Hum... Yes, I really don't know how to do that.

    You mean inv = new InventorySerializer(); ?
     
  22. Offline

    CraftCreeper6

  23. Offline

    Aragone

    Ok I tried I have an error like that*

    upload_2019-8-12_19-21-8.png

    The string is running because I tested it. And for the database, it's just a string which I have to put it in.
     
  24. Offline

    CraftCreeper6

    @Aragone
    Can I see the InventorySerializer class.
     
  25. Offline

    Aragone

  26. Offline

    KarimAKL

    @Aragone 'sql' is null as well. You also need to initialize that.
     
  27. Offline

    Aragone

  28. Offline

    KarimAKL

    @Aragone In your InventorySerializer class.
     
  29. Offline

    Aragone

  30. Offline

    KarimAKL

    @Aragone Show me your SqlDatabase class, something in the 'setInventory(Player, String)' method is throwing a NullPointerException.
    Btw, if you don't know how to read a stacktrace, here is a couple of links:
    https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/
    https://www.spigotmc.org/wiki/fixing-plugin-problems-easily-reading-stacktraces/

    EDIT: Btw, i need the whole class, or at least the line numbers in the method. (so i can tell what is throwing the exception)
     

Share This Page