ELO-System via Config

Discussion in 'Plugin Development' started by Skrarity, Dec 25, 2015.

Thread Status:
Not open for further replies.
  1. So i have a code i made for an elo system with ranks and well my problem is there is no ERROR in console
    but the "default" elo is not what it should be (1500) and well

    Heres the code i made in my made class (and yes the config DOES create itself)
    But my Config only shows "Elo: Players: " but not the name and stuff
    When i add the name manually it won't work either


    Main Class code:
    Code:
        public String checkRankOf(Player p) {
            int elo = this.getConfig().getInt("eloss.players."+p.getName()+".elo");
           
             if(elo < 1500) {
                return "UAV";
            } else if(elo < 3000) {
                return "AV";
            } else if(elo < 5250) {
                return "OAV";
            } else if(elo < 7500) {
                return "OAVP";
            } else if(elo < 10500) {
                return "FOAV";
            } else if(elo < 12750) {
                return "FOAVP";
            } else if(elo < 15000) {
                return "FOAVP+";
            } else if(elo < 15300) {
                return "NAV";
            } else if(elo > 15300) {
                return "NAV+";
            }
            return "RANKERROR";
        }
       
        public void addEloKills(Player p, int anzahl) {
            int elo = this.getConfig().getInt("eloss.players."+p.getName()+".elo");
            elo = +(anzahl*150);
            this.getConfig().set("eloss.players"+p.getName()+".elo",elo);
        }
       
        public void addEloDeaths(Player p,int anzahl) {
            int elo = this.getConfig().getInt("eloss.players."+p.getName()+".elo");
            elo = -(anzahl*75);
            this.getConfig().set("eloss.players"+p.getName()+".elo",elo);
           
        }
    
        public int getEloOf(Player p) {
            return this.getConfig().getInt("eloss.players."+p.getName()+".elo");
        }
    Code of my COmmand (/rankcheck, and well it always shoes 0 as my elo and UAV)
    Code:
        private main Soldiers;
    
        public RankCheckExecutor(main main) {
            this.Soldiers = main;
        }
    
        @Override
        public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
            if(!(cs instanceof Player)) {
                cs.sendMessage("Wrong Command Sender");
               
            }
           
            Player p  = (Player)cs;
            p.sendMessage(ChatColor.DARK_GRAY + "Your Rank is: " + ChatColor.GREEN + Soldiers.checkRankOf(p) + " " + Soldiers.getEloOf(p));
            return true;
        }
    Code of my DeathListener and JoinListener (I want to put the defaults for a player when he joins the
    first time)

    Code:
    private ArrayList<Player> stammis = new ArrayList<Player>();
       
        private main Soldiers;
        public JoinListener(main main) {
            this.Soldiers = main;
            }
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();;
            String emsg = e.getJoinMessage();
            if(!(stammis.contains(p))) {
                stammis.add(p);
                Soldiers.reloadConfig();
    
                String path3 = "eloss.players."+p.getName()+".elo";
                Soldiers.getConfig().addDefault(path3,1700);
                p.sendMessage(ChatColor.GREEN + "You joined the first time and start with an elo of 1700");
            }
            Soldiers.reloadConfig();
            p.sendMessage(ChatColor.RED + "Your Rank is " + ChatColor.GOLD + Soldiers.checkRankOf(p));
        }
       
       
        @EventHandler
        public void onPlayerDead (PlayerDeathEvent e) {
            Player victim = e.getEntity();
           
            if(!(victim == e.getEntity().getKiller())) {
                Player killer = (Player)e.getEntity().getKiller();
                e.setDeathMessage(ChatColor.RED + victim.getName() + ChatColor.DARK_GRAY + " has been killed by " + ChatColor.GOLD + killer);
                if(!(Soldiers.getEloOf(victim) < 0)) {
                    Soldiers.addEloDeaths(victim, 1);
                    Soldiers.checkRankOf(victim);
                    Soldiers.addEloKills(killer, 1);
                    Soldiers.checkRankOf(killer);
                } else {
                    killer.sendMessage(ChatColor.RED + "You didn't received ELO cause " + victim.getName() + " has got below zero ELO.");
                }
    
            }
           
    
        }
        


    So all in all it does create a config.yml , it does create some of the paths, but it doesn't do more, my elo is shown as "0" and i didn't tested if the kill death stuff works but yeah
     
  2. Offline

    Mrs. bwfctower

    First problem: If elo is 15300, it will return "RANKERROR".

    This is also pretty WET code, so I'd work on condensing it.

    You shouldn't be storing the player's name, but rather their UUID. If somebody changes their name, their score would be reset.

    The reason it is always returning 0 is because you never save the config after you set it, and the #getInt method returns 0 if the path is inexistent.
     
    mine-care likes this.
  3. Okay thanks i will look over my code and well this is the first time i use configs cause i just tried to use a good way with storing my ELO's and the players somewhere

    @Mrs. bwfctower
    What method do you recommend for that kind of idea and how would you make it more stable? (Well i think putting the "int elo" only in a config isn't the best idea but i am very new in this and very uncreative. Thanks!


    @Mrs. bwfctower
    Well thank you I will go to sleep and if u want / even if u don't want , i will let you/people now in this thread when it worked and how! Thanks! Have a nice christmas!
     
    Last edited: Dec 25, 2015
  4. Offline

    Mrs. bwfctower

    @Skrarity Configs are fine. Just store the player with their UUID instead of their name, and make sure you save the config after modifying it.
     
  5. Offline

    mine-care

    Also Ontop of what @Mrs. bwfctower has already mentioned, you should not store Player objects when they are not handled properly (remove on disconnect) because they will cause a ram leak. Do what was recommended with UUID's
    Follow Java naming conventions!

    Your command method is ok but you forgot to return a value of the sender is not a player, so the message will be displayed but an error will be thrown.

    First of all, make sure killer is not null and secondly use .equals to compare objects, not ==. The == operator will "return" true only if the two pointers refer to the same object in ram not if the two pointers refer to identical objects.
    Then, why checking if the killer is the victim? Is it even possible to self-kill?

    Lastly constant saving and loading of the config is NOT recommended whatsoever, it is very inefficient.

    Have you gone through the basics of Java? If not please do so before working with bukkit.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page