Solved Hashmap(s) clearing on reload

Discussion in 'Plugin Development' started by AmberOfSickness, Feb 20, 2013.

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

    AmberOfSickness

    Hello, everytime i reload my server, a hashmap gets re-initializated. It gets keys when a player joins and looses keys when they leave. But when i reload the server, there are still players online, and the hashmap doesn't have any key. I tried to re-put all the keys with the "onReload" event, but for some reasons the FOR_EACH construct doesn't work properly. Here's the code, re-adapted for you:
    Code:
    import java.util.HashMap;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import com.hotmail.amberofsickness.main;
     
    public class main extends JavaPlugin {
        HashMap<String, Boolean> HM = new HashMap<String, Boolean>();
       
       
        Player[] Players() {
            return getServer().getOnlinePlayers();
        }
       
        boolean ThisIsABoolean() {
            //Input from somewhere
        }
       
        @Override
        public void onEnable() {
            for(Player player:Players) {
                HM.put(player.getName(), ThisIsABoolean());
            }
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player Joiner = event.getPlayer():
            HM.put(Joiner.getName(), ThisIsABoolean());
        }
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            Player Leaver = event.getPlayer();
            HM.remove(event.getPlayer().getName());
        }
       
    }

    The problem, as i said, is in onEnable(), as the console reports no errors and the hashmap doesn't get any key, or at least not the player's name.
    Can anyone help me out? Thanks in advance!
     
  2. Offline

    iBawb

    Maybe you could log the hashmap into a text file on disable, then when it enables again, you could retrieve the hashmap from this file on enable, then delete the file. You would have to find a method to save hashmaps into a text file though.
     
  3. Offline

    Ne0nx3r0

  4. Offline

    iBawb

  5. Offline

    RealDope

    No, he's saying that when he reloads and calls getOnlinePlayers() in onEnable(), it returns nothing.
     
  6. Offline

    Ne0nx3r0

    That should only be the case if no players are online when the plugin is reloaded, ie. the server just started up.
     
  7. Offline

    AmberOfSickness

    No, i tried it with two accounts. /reload, and the hashmap goes to hell. Players still online.

    Anyway i tried to open a file to get the data, it's just not written on this thread. The only problem is the FOR_EACH construct that doesn't work. I'll try to get the data from the HashMap, as iBawb suggested.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    Geekola

    Your plugin will go out of scope during a reload and you will lose the data in your HashMap.

    You need to save (persist) the map during onDisable()
     
  9. Offline

    AmberOfSickness

    I'm pretty new at Bukkit programming, how do i do that?
     
  10. Offline

    ZachBora

Thread Status:
Not open for further replies.

Share This Page