[HELP] Save & load inventory !

Discussion in 'Plugin Development' started by Hester, Aug 10, 2012.

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

    Hester

    Hey, i have big problem and i haven't any good ideas how i can do it..
    so i want save & load player inventory for my minigames
    this code must save enchanting items and their current status

    Maybe anyone know how do that ?
     
  2. Offline

    Courier

    Code:java
    1. private HashMap<String, ItemStack[]> mySavedItems = new HashMap<String, ItemStack[]>();
    2.  
    3. public void saveInventory(Player player)
    4. {
    5. this.mySavedItems.put(player.getName(), copyInventory(player.getInventory()));
    6. }
    7.  
    8. /**
    9. * This removes the saved inventory from our HashMap, and restores it to the player if it existed.
    10. * @return true iff success
    11. */
    12. public boolean restoreInventory(Player player)
    13. {
    14. ItemStack[] savedInventory = this.mySavedItems.remove(player.getName());
    15. if(savedInventory == null)
    16. return false;
    17. restoreInventory(player, savedInventory);
    18. return true;
    19. }
    20.  
    21. private ItemStack[] copyInventory(Inventory inv)
    22. {
    23. ItemStack[] original = inv.getContents();
    24. ItemStack[] copy = new ItemStack[original.length];
    25. for(int i = 0; i < original.length; ++i)
    26. if(original[I] !=[/I] null)
    27. copy = new ItemStack(original);
    28. return copy;
    29. }
    30.  
    31. private void restoreInventory(Player p, ItemStack[] inventory)
    32. {
    33. p.getInventory().setContents(inventory);
    34. }


    Like that?
     
    Chief_Sunboyz, OHQCraft and Hester like this.
  3. Offline

    Hester

    So, for example.

    Player player = (Player)sender;
    if ((args.length == 1) && (args[0].equalsIgnoreCase("save"))){
    inv.saveInventory(player);
    return true;
    }

    if ((args.length == 1) && (args[0].equalsIgnoreCase("restore"))){
    inv.restoreInventory(player);
    return true;
    }

    and now, save = remove my item only for strip of used items but not with eq
    restore = no restore items

    maybe I do something wrong ?

    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    Hester

    It's ok but why i can't restore my item :( i don't have any ideas how i can save and restore inventory and i need this fast :) can u tell my why i can't restore my inventory ?
     
  5. Offline

    Courier

    Uhh... I just showed you. Remove the "[ I]" and "[ /I]", forums added those. Does it work? Do you get errors?
     
  6. Offline

    Hester


    Eh... I am not so stupid :D all is ok but when i try restore item i can't . I haven't any error but "restoreInventory(Player player)" return me hymmm... only air ? I have only one f***ing problem - with saving and load inventory
     
  7. Offline

    whitehooder

    First of all CALM DOWN... Courier has showed you how to do this with brilliant code, he has put everything up for you.
    The next step would be to find where the error is and fix it. To find where the error is you should add multiple debugging lines, like system.out.println("Restoring inventory...") and such so that you can see what happens and where its stopping. Try adding print lines in the saveintentory method to see how far it comes and then you could check what it stores. Without knowing which method that is malfunctioning there is nothing to do about it.
     
    Hester likes this.
  8. Offline

    devilquak

    Sorry if I'm off-topic, but wow, thanks for that, Courier. I made an inventory-saver myself and it was pretty crappily-coded, like 5x longer than this. Thank you so much, you just saved me a lot of space and confusion.
     
  9. Offline

    Hester

    Okey, i found this :) Yeaaa very very thanks :)
     
  10. Offline

    whitehooder

    See now Hester? This is good code. Just need the right way to use it.
     
  11. Offline

    Hester

    Yes, i know but i forgot to add the code to clear inventory ;) sorry for the confusion and Thank you(Courier) again
     
  12. Offline

    whitehooder

    You should thank Courier for this ;)
     
  13. Offline

    Hester

    I thanks for help you and Courier -.-
     
  14. Offline

    whitehooder

    Lol, glad I could help :cool:
     
  15. Offline

    sety

    Hi. I'm trying to use this code but I'm getting a type mismatch with the line:

    copy = new ItemStack (original);

    Any ideas?
     
  16. Offline

    whitehooder

    What about \/ ?
    ItemStack copy = new ItemStack(original);
     
  17. Offline

    sety

    Nup.

    With: copy = new ItemStack (original); I get "Cannot convert from ItemStack to ItemStack[]"
    With: ItemStack copy = ItemStack (original); I get "ItemStack cannot be resolved to a variable" as well as the previous error.
     
  18. Offline

    whitehooder

    Looks like you try to convert from an ItemStack array into an ItemStack (multiple items into one item variable).
    If you put up some more coffee wee can probably solve the issue for you.
    Why don't just try
    ItemStack[] = original;
     
  19. Offline

    Janmm14

    sety
    do
    copy = new ItemStack(original);
     
Thread Status:
Not open for further replies.

Share This Page