How to backup an inventory

Discussion in 'Plugin Development' started by Crosant, Dec 10, 2011.

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

    Crosant

    Hello all,
    I want to write a plugin with that you have a backpack.
    But i have one big problem, I dont know how to backup the inventory of an player.
    And how can i save the inventory and the backpack when the server is offline?
    Must I make files which store the informations?
    Thank you in Advance

    Crosant
     
  2. Offline

    Lolmewn

    Well, for temporarily saving it you can use player.getInventory().getContents();
    Store it somewhere, and when you want to set it back: player.getInventory().setContents(theOldOne);
     
  3. Offline

    Crosant

    Now i have saved the inventorys to a file.
    Now it looks like that:Crosant [Lorg.bukkit.inventory.ItemStack;@2cd5276a
    But How can i now search after the player (in here Crosant) and read the inventory back into an ItemStack[]?
    Thank you
     
  4. Offline

    DomovoiButler

    isnt the getContents an ItemStack?
     
  5. Offline

    Crosant

    Yes it is an ItemStack but i saved it into a file and i must search and read it out.
     
  6. Offline

    CptSausage

    How do you save it to a file? You need to read out the ItemStack and save in a String (e.g.), because you need everything in a primitive data type to save it.
     
  7. Offline

    Crosant

    @CptSausage im verry silly i have saved it without reading it out ...
    can u tell me how i can read it out?
     
  8. Offline

    CptSausage

    You could do something like:
    Code:java
    1. String itemStackString = "";
    2. itemStackString = itemStack.getTypeId();
    3. + ";" + itemStack.getAmount()
    4. + ":" + itemStack.getDurability();
    5.  

    The itemStackString holds now every detail you need to know about this ItemStack[]. And a String is something you can save.

    But maybe @Kierrow 's idea works as well (In this thread).
     
  9. Offline

    halley

    Just wanted to point out that the contents of the inventory is not an ItemStack. It is an array of ItemStack instances. Each cell in the inventory is an ItemStack. Each specific index into the array is a specific slot in the client's GUI-- such as armor slots, crafting slots, and the bottom row of fast access slots. To properly save/restore, you need to save each of those slots. When calling .getInventory().setContents(newArray), you need to give an array of the correct size.
     
Thread Status:
Not open for further replies.

Share This Page