Save HashMap

Discussion in 'Plugin Development' started by shohouku, Oct 6, 2014.

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

    shohouku

    So I'm trying to save HashMaps into a file this is what I got so far:

    This is how I'm saving it: (I put this in OnDisable)
    Code:java
    1. public void savehashmapfile() {
    2. try {
    3. final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getDataFolder() + File.separator + "AccessoryInv.bin"));
    4. oos.writeObject(accessoryinv);
    5. oos.flush();
    6. oos.close();
    7. } catch (final Exception e) {
    8. e.printStackTrace();
    9. }
    10. }

    This is how I'm loading it: (I put this in OnEnable)
    Code:java
    1. final File f = getDataFolder();
    2. if (!f.exists()) {
    3. f.mkdirs();
    4. }
    5. File hashmapfile = new File(getDataFolder() + File.separator + "AccessoryInv.bin");
    6. if (!hashmapfile.exists()) {
    7. savehashmapfile();
    8. } else if (hashmapfile.exists()) {
    9. try {
    10. final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(getDataFolder() + File.separator + "AccessoryInv.bin"));
    11. final Object result = ois.readObject();
    12. ois.close();
    13. accessoryinv = (HashMap<UUID, Inventory>) result;
    14.  
    15.  
    16. } catch (final Exception e) {
    17. e.printStackTrace();
    18. }
    19. }

    My Hashmap:

    Code:java
    1. private HashMap<UUID, Inventory> accessoryinv = new HashMap<UUID, Inventory>(); //Last inventory of the player


    And the rest of the code I'm just saving the UUId of the player and the inventory to the hash map.

    My file isn't reading it correctly. Because my hashmap still gets removed when I restart my server.

    A little help would be appreciated! :)
     
  2. Offline

    fireblast709

    shohouku I hope you are using 'onDisable' and 'onEnable' (note the casing of the 'o'). That aside, I am not sure how serializable Inventory is ;3
     
  3. Offline

    shohouku

    Yeah I noticed you cant save objects...so I kinda gave up lol.
     
  4. Offline

    TheOddPuff

    shohouku You could make a controller that reads and saves inventories to files.
     
Thread Status:
Not open for further replies.

Share This Page