Saving HashMaps that contain inventories.

Discussion in 'Plugin Development' started by HCMatt, Feb 22, 2015.

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

    HCMatt

    So i have 4 hashmaps that contain player inventories:

    Code:
        public static HashMap<String, ItemStack[]> armor1 = new HashMap<String, ItemStack[]>();
        public static HashMap<String, ItemStack[]> inv1 = new HashMap<String, ItemStack[]>();
        public static HashMap<String, ItemStack[]> armor2 = new HashMap<String, ItemStack[]>();
        public static HashMap<String, ItemStack[]> inv2 = new HashMap<String, ItemStack[]>();
    The inventories are put into and taken from the hash maps in different methods:

    Code:
        public static void saveKit1(Player player) {
            armor1.put(player.getName(), player.getInventory().getArmorContents());
            inv1.put(player.getName(), player.getInventory().getContents());
            player.sendMessage(tag + ChatColor.GREEN + " Saved Kit: 1");
        }
    
        public static void loadKit1(Player player) {
            player.getInventory().setContents(inv1.get(player.getName()));
            player.getInventory().setArmorContents(armor1.get(player.getName()));
            player.sendMessage(tag + ChatColor.GREEN + " Loaded Kit: 1");
            player.updateInventory();
        }
    
        public static void saveKit2(Player player) {
            armor2.put(player.getName(), player.getInventory().getArmorContents());
            inv2.put(player.getName(), player.getInventory().getContents());
            player.sendMessage(tag + ChatColor.GREEN + " Saved Kit: 2");
        }
    
        public static void loadKit2(Player player) {
            player.getInventory().setContents(inv2.get(player.getName()));
            player.getInventory().setArmorContents(armor2.get(player.getName()));
            player.sendMessage(tag + ChatColor.GREEN + " Loaded Kit: 2");
            player.updateInventory();
        }
    However, when the server is reloaded or someone quits and re-joins, they have to create the kit and save it all over again. How do i save the hashmaps and load them in my onEnable() and onDisable() ?
    Thanks
     
  2. Offline

    1Rogue

  3. Offline

    HCMatt

    @1Rogue I am not very experienced in Bukkit coding and i am still just starting out, could you please help with showing me how i would implement it into my code?
     
  4. Offline

    1Rogue

    https://github.com/CodeLanx/Codelan...delanxlib/serialize/SPlayerInventory.java#L59
    Code:java
    1. Player p = /* your player */;
    2. SPlayerInventory inv = new SPlayerInventory(p.getInventory());
    3. //plugin being your main class instance
    4. plugin.getConfig().set("some-player-inv", inv);


    Keep in mind you have to register the class to ConfigurationSerialization first before you can use it: http://docs.codelanx.com/Bukkit/1.8...alization.html#registerClass-java.lang.Class-
     
  5. Offline

    HCMatt

    Im going to be honest.... I have no idea what you are talking about and do not know how to do it :I.. @1Rogue
     
  6. Offline

    SuperOriginal

  7. Offline

    HCMatt

    @SuperOriginal like i said to @1Rogue , i have no clue how to do any of that, even with reading that page... could you please help a little more by showing me an example?
     
  8. Offline

    MajorSkillage

    You could always clear the inventory and make a for loop to re add the items using
    HashMap<String, ArrayList<ItemStack>> and create a section in configuration for it so it can be modified easily and the ArrayList in the plugin can be modified easier to remove unwanted items and such :) Hope this helps!
     
  9. Offline

    SuperOriginal

    If you can't follow the official page for configuration, which provides LOTS of examples, then I suggest learning more Java. Then you can fix your static as well.
     
Thread Status:
Not open for further replies.

Share This Page