Saving hashmap on reload

Discussion in 'Spigot Plugin Development' started by dyenxunit, Jun 12, 2015.

  1. So what im trying to do is save the hash map on reload because every time i try to reload my plugin the whole plugin breaks. The current code i have setup doesnt work because it keeps resetting the money value of each player to 0 but every time i do reload the plugin doesnt break.

    Heres my code
    Code (Text):
    private HashMap<UUID, Integer> money = new HashMap<>();
    public void onEnable() {

    if(Bukkit.getOnlinePlayers().size() != 0) {
    for(Player online : Bukkit.getServer().getOnlinePlayers()) {
    if (!money.containsKey(online.getName())){
    money.put(online.getUniqueId(), 0);

    }
    }
    }
     
     
  2. This would save everything inside the hashmap into a single file.
     
  3. Isnt there any way for me to just stop it from clearing when it the server reloads ? insted of generating all these files
     
  4. Nope! You need to either save it or re-instantiate all the entries, your title made it sound like you wanted the former, but I suggest the latter.
     
  5. If you want the data to persist beyond a restart, it needs to be saved somewhere. Either in a file, or saved to a database.
     
  6. Oh ok. But is there a way for me to re add a player to the hash map ?
     
  7. Yes...onEnable() create a hashmap for all players on the server and pass a reference to it to whatever needs to have it. Also have something that handles onPlayerLogin and onPlayerQuit.
     
  8. See the link to SLAPI that I posted. It will allow you to save the map to a file, and reload it later.
     
  9. Is there a reload event?
    So the plugin can do something BEFORE reloading?
    If so, save it into a file then on load check if the files exist and do it.
     
  10. Use of the OOP stream is literally what I've already linked, serialization.
     
  11. Ty for the help i will look into this