Solved Saving and Retrieving inventory contents

Discussion in 'Plugin Development' started by Loogeh, Nov 9, 2012.

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

    Loogeh

    Okay, so, I made a thread asking how to do this and a guy helped me, I made two methods 'saveContents' and 'retrieveContents' but never used them because I was gonna make it the last thing I do. Now that I actually test them, they don't work.
    They are here:
    Code:
     
    public static HashMap<String, ItemStack[]> invents = new HashMap<String, ItemStack[]>();
    public static void saveContents(Player player) {
            if(!invents.containsKey(player.getName())) {
                invents.put(player.getName(), player.getInventory().getContents());
            } else if(invents.containsKey(player.getName())) {
                return;
            }
        }
     
        public static void retrieveContents(Player player) {
            if(!invents.containsKey(player.getName())) {
                return;
            } else if(invents.containsKey(player.getName())) {
                player.getInventory().setContents(invents.get(player.getName()));
            }
        }
    Any help?
    - Loogeh

    help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. What doesn' t work?
     
  3. Offline

    Loogeh

    That's the thing, I have no idea.
     
  4. Offline

    thehutch

    Loogeh I'm assuming you want to save the inventories so that went the server restarts you still have them?

    If not then this is some clean up code:

    Code:java
    1.  
    2.  
    3. public static Map<String, ItemStack[]> invents = new HashMap<String, ItemStack[]>();
    4.  
    5. public static void saveContents(Player player) {
    6. if(!invents.containsKey(player.getName())) {
    7. invents.put(player.getName(), player.getInventory().getContents());
    8. }
    9. }
    10.  
    11. public static void retrieveContents(Player player) {
    12. if(invents.containsKey(player.getName())) {
    13. player.getInventory().setContents(invents.get(player.getName()));
    14. }
    15. }
    16.  
    17.  


    If it is then you'll need to save all the inventories into a text file in some format
     
  5. Offline

    Loogeh

    thehutch Thanks for your help but I just figured it out, also, it's for spleef (minigame).
     
  6. Offline

    thehutch

    May I ask what you did to fix it and the code ;)
     
  7. Offline

    Loogeh

    All I did was remove the 'if' statements, so it's not that good and I would get NPE's if the player isn't in it lol,

    Code:
        public static void saveContents(Player player) {
            invents.put(player.getName(), player.getInventory().getContents());
            armours.put(player.getName(), player.getInventory().getArmorContents());
        }
       
        public static void retrieveContents(Player player) {
            player.getInventory().setContents(invents.get(player.getName()));
            player.getInventory().setArmorContents(armours.get(player.getName()));
        }
    Code:
        public static HashMap<String, ItemStack[]> invents = new HashMap<String, ItemStack[]>();
        public static HashMap<String, ItemStack[]> armours = new HashMap<String, ItemStack[]>();
     
Thread Status:
Not open for further replies.

Share This Page