Help with loading/saving hashmap

Discussion in 'Spigot Plugin Development' started by SammySpicer, Jul 10, 2015.

  1. I am having trouble with loading and saving to and from a data file. Here is my code, any help would be much appreciated.

    Code (Text):
    package coding;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public HashMap<String, Integer> Rating = new HashMap<String, Integer>();
        @SuppressWarnings("unchecked")
        @Override
        public void onEnable() {

               File f = new File(getDataFolder() + File.separator, "Data.yml");


                    if (!f.exists()) {


                        return;


                    }


                   try {


                       FileInputStream fis = new FileInputStream(f);


                        ObjectInputStream ois = new ObjectInputStream(fis);


                      @SuppressWarnings("unused")
                    HashMap<String, Integer> Rating = (HashMap<String, Integer>) ois.readObject();


                       ois.close();


                       fis.close();


                   } catch (Exception e) {


                       e.printStackTrace();


                 }

        }
    @Override
    public void onDisable() {
        File f = new File(getDataFolder() + File.separator, "Data.yml");


            if (!f.exists()) {


                f.getParentFile().mkdirs();
                System.out.println("Making a new directory.");


           }


           try {


                FileOutputStream fos = new FileOutputStream(f);


              ObjectOutputStream oos = new ObjectOutputStream(fos);


              oos.writeObject(Rating);


               oos.close();


               fos.close();


           } catch (IOException ex) {


               ex.printStackTrace();


            }
         
         
         
    }
    @EventHandler
    public void onEntityDeath(PlayerDeathEvent e) {
        if (e.getEntity() instanceof Player) {
            if (e.getEntity().getKiller() instanceof Player) {
                String p = e.getEntity().getKiller().toString();
                String d = e.getEntity().toString();
                    Rating.put(p, Rating.get(p) + 2);
                    Rating.put(d, Rating.get(d) - 1);
       
    }



    }
        }

        @EventHandler
        public void onLogin(PlayerLoginEvent event) {
            if (event.getPlayer() instanceof Player) {
                String person = event.getPlayer().toString();
                Player persan = event.getPlayer();
                if (!persan.hasPlayedBefore()) {
                    Rating.put(person, 100);
                }
                if (!Rating.containsKey(person)) {
                    Rating.put(person, 100);
                   
                   

                   
                }
            }
           
           
        }
    }
     
  2. Hashsets will reset when the server resets or reloads. so its your best bet to put them in the config. and then remove them from the config when you want them removed. Hopes that helps
     
  3. You can check Pogostick29's video on saving hashmap. It is useful tho.
     
  4. I have always recommended BCBroz, He is more advanced I believe. But both work!