Help with hashmap on disable

Discussion in 'Spigot Plugin Development' started by noobykazu, Jun 29, 2018.

  1. Hi, I am making kill counter plugin and I got a problem. (Please look at the second code)How my kill counter works is 1.onJoin get data from the player data file and make hashmap that track player's kill.2,OnKill,add 1 to the player's hashmap 3,When I stop/reload the server, It says the hashmap is null.(I am trying to put hashmap to the player data config). I know when the plugin disabled. Hashmap will be null. Does anyone know other methods that put hashmap on the config on disable the plugin?
    Code (Text):
        private KillandDeath kad;
    public void onDisable() {;
            kad = new KillandDeath();
    Code (Text):
            public void leave() {
                for(Player online : Bukkit.getOnlinePlayers()){
                    String uuid = online.getUniqueId().toString();
                    playerfile = new File(plugin.getDataFolder() + File.separator + "PlayerDatas", uuid+".yml");
                    PlayerStatscfg = YamlConfiguration.loadConfiguration(playerfile);
                    PlayerStatscfg.set("User." + uuid + ".Kills", Kills.get(online.getUniqueId()));
                    PlayerStatscfg.set("User." + uuid + ".Deaths", Deaths.get(online.getUniqueId()));
                    System.out.println("[KitPVP] => Updated " + online.getName() + "'s DataFile");
                    try {
                    } catch (IOException e2) {
                        Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not save the PlayerStats.yml file");
                    online.kickPlayer("Server is restarting or stopped");
    No errors on console.
  2. What about just saving it in a yml file like this :
    Code (YAML):
    player1 : 1
    : 47
    : 8
  3. But the hashmap is null on disabled.
  4. You have to load the hashmap again in the onEnable. When you reload/restart your server it clears the ram on disc so you have to load everything back in. Try initializing the hashmap onEnable.
  5. You don't have to use a hashmap for this, after each kill just save it in your yml file, with something like :
    Code (Text):
    getConfig().set(e.getKiller().toString(), getConfig().getString(e.getKiller().toString) + 1);
  6. But I don't want to load the yml every time.
  7. Ow, well then what @Perotin said
  8. Post your main class. Also, try not to use variables like "p, k" and other 1 letters; casing Goes Like This As Well For All Variables.

    Code (Text):
     HashMap<UUID, Integer> Kills = new HashMap<UUID, Integer>();
    Is this how to initializing the hashmap?
    Should I initializing the hashmap on another class?
  10. You do realize the config is in memory, right? Calling getConfig() doesnt load it from disk.
  11. Is the kill and death class store the hashmaps? If so, you need to assign that to a new instance in your onEnable method and then populate it with previous data from your yaml file.

    Code (Java):
    kad = new KillandDeath();
  12. You're creating a new instance of the class in your onDisable which creates a new hashmap. So when you're setting the values in the file, you're trying to get something from the map that doesn't exist. Don't make a new instance.
  13. #14 noobykazu, Jun 30, 2018
    Last edited: Jun 30, 2018
  14. Just set the "kad" variable in your onEnable method then onDisable call the same variable.
  15. What you're doing is creating a new plugin instance when the plugin is disabled. This creates 2 issues:
    1. When the server is stopped, the server disables all plugins that are running, hence you're doing nothing by creating a new instance of a disabled plugin
    2. You should not be reloading your server because most plugins don't properly handle, or more accurately, don't account for reloads at all. It is more reliable to completely stop then start your server again.

    In your case, when you stop the server, the plugin is disabled and eventually your server, the thread that minecraft is running on will be stopped and hence will no longer store any relevant instances. You're relying on a Map<K, V> to store kills, but it is unreliable as the server can crash in any reasonable future of the runtime. As suggested, write on read i.e. writing to the configuration every time a kill is registered is a better solution.
  16. Thank you for your advice! I appreciate it!
    • Friendly Friendly x 1
  17. Thank you. You made my life easier :p
    • Friendly Friendly x 2
  18. Thanks for your help!
    • Friendly Friendly x 1