Solved Hashmaps and configs

Discussion in 'Plugin Development' started by Funergy, Apr 28, 2014.

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

    Funergy

    Hey there! :),

    I'm making a FFA minigame and when I do /kill it shows how many deaths and kills:0 I have but when I do /reload and I do /ffa stats again it says kills:null deaths:0 but for the reload it said deaths:1

    This is my code:
    Code:
    //SLAPI
        public void savekills()
        {
            for(String p : getkillsMap().keySet())
            {
                getConfig().set("deaths."+p, getkillsMap().get(p));
            }
            this.saveConfig();
        }
     
        public void loadkills()
        {
            if(!getConfig().contains("kills")) return;
            for(String s : getConfig().getConfigurationSection("kills").getKeys(false))
            {
                setkills(s, getConfig().getInt("kills."+s));
            }
        }
            public void savedeaths()
            {
                for(String p : getdeathsMap().keySet())
                {
                    getConfig().set("deaths."+p, getdeathsMap().get(p));
                }
                this.saveConfig();
            }
     
            public void loaddeaths()
            {
                if(!getConfig().contains("deaths")) return;
                for(String s : getConfig().getConfigurationSection("deaths").getKeys(false))
                {
                    setdeaths(s, getConfig().getInt("deaths."+s));
                }
            }
            //manager KILLS
           
            public static HashMap<String, Integer> kills = new HashMap<>(); // {PlayerName, kills}
            public static HashMap<String, Integer> deaths = new HashMap<>(); // {PlayerName, deaths}
     
            public static void setkills(String player, Integer amount)
            {
                kills.put(player, amount);
            }
     
            public static Integer getkills(String player)
            {
                return kills.get(player);
            }
     
            public static boolean haskillsAccount(String player)
            {
                return kills.containsKey(player);
            }
     
            public static HashMap<String, Integer> getkillsMap()
            {
                return  kills;
            }
            //Mangager deaths
            public static void setdeaths(String player, Integer amount)
            {
                deaths.put(player, amount);
            }
     
            public static Integer getdeaths(String player)
            {
                return deaths.get(player);
            }
     
            public static boolean hasdeathsAccount(String player)
            {
                return deaths.containsKey(player);
            }
     
            public static HashMap<String, Integer> getdeathsMap()
            {
                return  deaths;
            }
     
  2. Offline

    clmcd42

    So when you do /kill, it shows you one thing, and when you do /ffa stats it shows you something else? Could you show us the actual commands?
     
  3. Offline

    Funergy

    So if I do /kill
    and I do then /ffa stats
    it shows me:
    Kills:0
    Deaths:1
    But when I do /reload
    and do /ffa stats again
    is says:
    Kills:null
    Deaths:0
    ////////////////
    The hashmap works but
    just after the reload it messes up.
    and If I look after the reload at the config.yml it only shows me:
    Deaths:
    Funergy: 0
    and also not:
    Kills:
    Funergy:0

    Garris0n

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    unforgiven5232

    Why is the number being called by a string? Funergy
     
  5. Offline

    Funergy

    Where
     
  6. Offline

    unforgiven5232

    Funergy for loop in the loadKills method
     
  7. Offline

    Funergy

    What do you mean
    What I have written is right
     
  8. Offline

    clmcd42

    Is the config ok before the reload? Or is it not saving properly?
     
  9. Offline

    mrgreen33gamer

    I know were you got this code from...... And I use it, it works perfectly. If you want to know how to put this in a Scoreboard, PM me.

    Oh and you should now what is going on and how to fix this.
     
  10. Offline

    Funergy

  11. Funergy Show us the command code please
     
  12. Offline

    Funergy


    Code:
        /*
        * COMMANDS
        */
       
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(command.getName().equalsIgnoreCase("ffa")){
                if(args.length == 0){
                sender.sendMessage("§cUsage: /ffa join || /ffa leave || /ffa stats");
                return true;
                }
                if(args[0].equalsIgnoreCase("join")){
                Player p = (Player) sender;
                if(!list.contains(p.getName())){
                join(p);
                }else{
                    p.sendMessage("§cYou are already playing FFA!");
                }
                }
                if(args[0].equalsIgnoreCase("leave")){
                    Player p = (Player) sender;
                    if(list.contains(p.getName())){
                        if(respawned.contains(p.getName())){
                            respawned.remove(p.getName());
                        }
                            list.remove(p.getName());
                            p.getInventory().clear();
                            Bukkit.dispatchCommand(sender, "spawn");
                       
                        }else{
                            p.sendMessage("§cYou are not playing FFA!");
                        }
               
            }
                if(args[0].equalsIgnoreCase("stats")){
                    Player p = (Player) sender;
                    p.sendMessage("§cKills: " + getkills(p.getName()));
                    p.sendMessage("§cDeaths: " + getdeaths(p.getName()));
               
            }
            }
           
            return false;
        }
        /*
        * Stats:
        */
        //SLAPI
        public void savekills()
        {
            for(String p : getkillsMap().keySet())
            {
                getConfig().set("deaths."+p, getkillsMap().get(p));
            }
            this.saveConfig();
        }
     
        public void loadkills()
        {
            if(!getConfig().contains("kills")) return;
            for(String s : getConfig().getConfigurationSection("kills").getKeys(false))
            {
                setkills(s, getConfig().getInt("kills."+s));
            }
        }
            public void savedeaths()
            {
                for(String p : getdeathsMap().keySet())
                {
                    getConfig().set("deaths."+p, getdeathsMap().get(p));
                }
                this.saveConfig();
            }
     
            public void loaddeaths()
            {
                if(!getConfig().contains("deaths")) return;
                for(String s : getConfig().getConfigurationSection("deaths").getKeys(false))
                {
                    setdeaths(s, getConfig().getInt("deaths."+s));
                }
            }
            //manager KILLS
           
            public HashMap<String, Integer> kills = new HashMap<>(); // {PlayerName, kills}
            public HashMap<String, Integer> deaths = new HashMap<>(); // {PlayerName, deaths}
     
            public void setkills(String player, Integer amount)
            {
                kills.put(player, amount);
            }
     
            public Integer getkills(String player)
            {
                return kills.get(player);
            }
     
            public boolean haskillsAccount(String player)
            {
                return kills.containsKey(player);
            }
     
            public HashMap<String, Integer> getkillsMap()
            {
                return  kills;
            }
            //Mangager deaths
            public void setdeaths(String player, Integer amount)
            {
                deaths.put(player, amount);
            }
     
            public Integer getdeaths(String player)
            {
                return deaths.get(player);
            }
     
            public boolean hasdeathsAccount(String player)
            {
                return deaths.containsKey(player);
            }
     
            public HashMap<String, Integer> getdeathsMap()
            {
                return  deaths;
            }
            /*
            * Chat Listener
            */
            @EventHandler
            public void onchat(AsyncPlayerChatEvent e){
                if(list.contains(e.getPlayer().getName())){
                    Bukkit.broadcastMessage("§c[§e"+ getkills(e.getPlayer().getName()) + "§c] " + ChatColor.RESET + e.getPlayer().getDisplayName() + "> "+ ChatColor.GRAY + e.getMessage());
                    e.setCancelled(true);
                }
               
            }
            /*
            * Join Listener
            */
            @EventHandler
            public void onJoin(PlayerJoinEvent event){
                if(haskillsAccount(event.getPlayer().getName())) return;
                setkills(event.getPlayer().getName(), (int)0);
                if(hasdeathsAccount(event.getPlayer().getName())) return;
                setdeaths(event.getPlayer().getName(), (int)0);
            }
    }
     
  13. Funergy Do you ever call loadKills() or loadDeaths() anywhere?
     
  14. Offline

    Funergy

    Yes at the on enable and the on disable
     
  15. Funergy can I see that too please?
     
  16. Offline

    Funergy

    Code:
    public void onEnable() {
            Bukkit.getLogger().info("[FFA] Plugin Enabled!");
            Bukkit.getPluginManager().registerEvents(this, this);
            loadkills();
            loaddeaths();
            Bukkit.getLogger().info("[FFA] Loading stats...");
        }
     
        public void onDisable() {
            savedeaths();
            savekills();
            Bukkit.getLogger().info("[FFA] Saving stats...");
        }
     
  17. Funergy Hmm. And you're sure that it saves the config correctly?
     
  18. Offline

    Funergy

    idk look at my code
    it saves nothing
    so what do I need to do to fix this

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  19. Funergy If you have no config file then do the following:
    1. In the same place as your plugin.yml, create a config.yml file. This file can be blank, I suppose.

    2. Do saveDefaultConfig(); as the first thing inside your onEnable() (i.e. before the first getLogger() thing - which, by the way, shouldn't be there)
     
  20. Offline

    Funergy

    This is what I have now
    Code:
        public void onEnable() {
            saveDefaultConfig();
            Bukkit.getLogger().info("[FFA] Plugin Enabled!");
            Bukkit.getPluginManager().registerEvents(this, this);
            loadkills();
            loaddeaths();
            Bukkit.getLogger().info("[FFA] Loading stats...");
        }
     
        public void onDisable() {
            savedeaths();
            savekills();
            Bukkit.getLogger().info("[FFA] Saving stats...");
        }
     
            /*
        * COMMANDS
        */
       
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(command.getName().equalsIgnoreCase("ffa")){
                if(args.length == 0){
                sender.sendMessage("§cUsage: /ffa join || /ffa leave || /ffa stats");
                return true;
                }
                if(args[0].equalsIgnoreCase("join")){
                Player p = (Player) sender;
                if(!list.contains(p.getName())){
                join(p);
                }else{
                    p.sendMessage("§cYou are already playing FFA!");
                }
                }
                if(args[0].equalsIgnoreCase("leave")){
                    Player p = (Player) sender;
                    if(list.contains(p.getName())){
                        if(respawned.contains(p.getName())){
                            respawned.remove(p.getName());
                        }
                            list.remove(p.getName());
                            p.getInventory().clear();
                            Bukkit.dispatchCommand(sender, "spawn");
                       
                        }else{
                            p.sendMessage("§cYou are not playing FFA!");
                        }
               
            }
                if(args[0].equalsIgnoreCase("stats")){
                    Player p = (Player) sender;
                    p.sendMessage("§cKills: " + getkills(p.getName()));
                    p.sendMessage("§cDeaths: " + getdeaths(p.getName()));
               
            }
            }
           
            return false;
        }
        /*
        * Stats:
        */
        //SLAPI
        public void savekills()
        {
            for(String p : getkillsMap().keySet())
            {
                getConfig().set("deaths."+p, getkillsMap().get(p));
            }
            this.saveConfig();
        }
     
        public void loadkills()
        {
            if(!getConfig().contains("kills")) return;
            for(String s : getConfig().getConfigurationSection("kills").getKeys(false))
            {
                setkills(s, getConfig().getInt("kills."+s));
            }
        }
            public void savedeaths()
            {
                for(String p : getdeathsMap().keySet())
                {
                    getConfig().set("deaths."+p, getdeathsMap().get(p));
                }
                this.saveConfig();
            }
     
            public void loaddeaths()
            {
                if(!getConfig().contains("deaths")) return;
                for(String s : getConfig().getConfigurationSection("deaths").getKeys(false))
                {
                    setdeaths(s, getConfig().getInt("deaths."+s));
                }
            }
            //manager KILLS
           
            public HashMap<String, Integer> kills = new HashMap<>(); // {PlayerName, kills}
            public HashMap<String, Integer> deaths = new HashMap<>(); // {PlayerName, deaths}
     
            public void setkills(String player, Integer amount)
            {
                kills.put(player, amount);
            }
     
            public Integer getkills(String player)
            {
                return kills.get(player);
            }
     
            public boolean haskillsAccount(String player)
            {
                return kills.containsKey(player);
            }
     
            public HashMap<String, Integer> getkillsMap()
            {
                return  kills;
            }
            //Mangager deaths
            public void setdeaths(String player, Integer amount)
            {
                deaths.put(player, amount);
            }
     
            public Integer getdeaths(String player)
            {
                return deaths.get(player);
            }
     
            public boolean hasdeathsAccount(String player)
            {
                return deaths.containsKey(player);
            }
     
            public HashMap<String, Integer> getdeathsMap()
            {
                return  deaths;
            }
            /*
            * Chat Listener
            */
            @EventHandler
            public void onchat(AsyncPlayerChatEvent e){
                if(list.contains(e.getPlayer().getName())){
                    Bukkit.broadcastMessage("§c[§e"+ getkills(e.getPlayer().getName()) + "§c] " + ChatColor.RESET + e.getPlayer().getDisplayName() + "> "+ ChatColor.GRAY + e.getMessage());
                    e.setCancelled(true);
                }
               
            }
            /*
            * Join Listener
            */
            @EventHandler
            public void onJoin(PlayerJoinEvent event){
                if(haskillsAccount(event.getPlayer().getName())) return;
                setkills(event.getPlayer().getName(), 0);
                if(hasdeathsAccount(event.getPlayer().getName())) return;
                setdeaths(event.getPlayer().getName(), 0);
            }
     
  21. Offline

    Funergy

    nope
    it does only add deaths to the config and when I do /reload it says kills null and deaths 0
    but when I restart the server and rejoin it says kills:0 and deaths:1 so the deaths save but are buggy

    Its fixed Thanks for all your help guys! love ya

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page