HashMap Saving/Loading Issue

Discussion in 'Plugin Development' started by isamgray, Jul 20, 2011.

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

    isamgray

    I'm trying to save two HashMaps to files, so I can load them after the server has been restarted. However I'm finding this very difficult. Let me know if you need anymore information or my code. Thanks in advance for all the help...

    The HashMaps I'm trying to save:
    Code:
    public static Map<Player, Boolean> breathers = new HashMap<Player, Boolean>();
        public static Map<Player, Boolean> instaminers = new HashMap<Player, Boolean>();
    Here is the class that I have to save the HashMaps to a file I specify.
    Code:
    package me.sam.first;
    
    import java.io.*;
    import java.util.HashMap;
    
    import org.bukkit.entity.Player;
    
    public class HashTagController
    {
        public static void save(Object obj,String path) throws Exception
        {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(obj);
            oos.flush();
            oos.close();
    
        }
        public static Object load(String path) throws Exception
        {
            System.out.println("Loading information!");
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            Object result = ois.readObject();
            ois.close();
            return result;
        }
    }
    Here is my code to save the HashMaps when a player quits.
    Code:
    package me.sam.first;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class BasicPlayerListener extends PlayerListener
    {
         public static First plugin;
          public BasicPlayerListener(First instance) {
                plugin = instance;
            }
    
          public void onPlayerQuit(PlayerQuitEvent event)
          {
              HashTagController saver = new HashTagController();
              //saver.save(plugin.breathers, plugin.mainDirectory + File.separator + "breathers.dat");
             try{
                 HashTagController.save(plugin.breathers, plugin.mainDirectory + File.separator + "breathers.bin");
                 HashTagController.save(plugin.instaminers, plugin.mainDirectory + File.separator + "instaminers.bin");
             }
             catch(Exception e){
                 System.out.println("EXPECTION 2!" + e.getMessage());
             }
          }
    }
    Here is the code I use on onEnable to try and load the HashMap data into my HashMap.
    Code:
            try{
                this.breathers = (HashMap<Player, Boolean>)HashTagController.load(mainDirectory + File.separator + "breathers.bin");
            }
            catch(Exception e)
            {
                System.out.println("EXPECTION 1! " + e.getMessage());
    
            }
    And here are the messages I get from the catches:
     
  2. Offline

    Mixcoatl

    The keys of your maps are players and players are not serializable. If you use player names instead and this will work.
     
    isamgray likes this.
  3. Offline

    isamgray

    Thank you SO much. That makes perfect sense to me now. My code works perfectly now that I've changed it to player names instead of the whole player class object.
     
  4. Offline

    Mixcoatl

    Glad I could help.
    Congratulations! :D
     
Thread Status:
Not open for further replies.

Share This Page