Game Ranks

Discussion in 'Plugin Development' started by Luflexed, Dec 29, 2014.

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

    Luflexed

    I am developing a stats plugin for my server, there is a feature called game ranks that I want. Game ranks simply just sorts the kills in order from player to player, and displays your number in the stats!


    Like: Game Rank: 115
    Game Rank: 114

    I do use configuration and I do not really want to use mysql because I do not know it too well.

    Here is what my stats plugin looks like:
    Code:
    package me.Alex.EnderStats;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class EnderStats extends JavaPlugin implements Listener {
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            }
       
        @SuppressWarnings("unused")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("stats")){
                if(args.length == 0){
                Player dp = (Player) sender;
                String p = sender.getName();
                String amount = getConfig().getString(p + ".Kills");
                String amount2 = getConfig().getString(p + ".Deaths");
                String amount3 = getConfig().getString(p + ".Killstreak");
                int kills = Integer.parseInt(amount);
                int deaths = Integer.parseInt(amount2);
                dp.sendMessage("§5§lEnder§d§lCube§7> " + dp.getDisplayName() + "§d's Stats");
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Game Rank§7: §b#0");
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Kills§7: §b" + amount);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Deaths§7: §b" + amount2);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Killstreak§7: §b" + amount3);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Ratio§7: §b" + kills/deaths);
                return true;
                }
    
                @SuppressWarnings("deprecation")
                Player target = Bukkit.getServer().getPlayer(args[0]);
                Player dp = (Player) sender;
                String p = target.getName();
                String amount = getConfig().getString(p + ".Kills");
                String amount2 = getConfig().getString(p + ".Deaths");
                String amount3 = getConfig().getString(p + ".Killstreak");
                double kills = Integer.parseInt(amount);
                double deaths = Integer.parseInt(amount2);
                dp.sendMessage("§5§lEnder§d§lCube§7> " + target.getDisplayName() + "§d's Stats");
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Game Rank§7: §b#0");
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Kills§7: §b" + amount);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Deaths§7: §b" + amount2);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Killstreak§7: §b" + amount3);
                dp.sendMessage("§5§lEnder§d§lCube§7> §d Ratio§7: §b" + kills/deaths);
                if(target == null){
                    sender.sendMessage("§8[§6SGFFA§8] §cPlayer is not online, or does not have any stats tracked!");
                return true;
                }
            }
            return true;
    }
       
        @EventHandler
        public void onKill(EntityDeathEvent e1){
            if (e1.getEntity() instanceof Player){
                Player p2 = (Player) e1.getEntity();
                if(p2.getKiller() instanceof Player){
                    Player p = p2.getKiller();
                    giveKills(p, 1);
            }
        }
    }
        @EventHandler
        public void onKll(PlayerDeathEvent e1){
            if (e1.getEntity() instanceof Player){
                    Player p = e1.getEntity();
                    giveDeaths(p, 1);
            }
        }
       
        @EventHandler
        public void Killstreak(PlayerDeathEvent e){
            if (e.getEntity() instanceof Player){
                Player p = e.getEntity();
                Player k = p.getKiller();
                giveKs(k, 1);
            }
        }
       
        @EventHandler
        public void KillstreakTake(PlayerDeathEvent e){
            if (e.getEntity() instanceof Player){
                Player p = e.getEntity();
                getConfig().set(p.getName() + ".Killstreak", 0);
            }
        }
       
        @EventHandler
        public void onLeave(PlayerQuitEvent e){
            saveConfig();
        }
    
    
        public void giveKills(Player p, int i){
            getConfig().set(p.getName() + ".Kills", getConfig().getInt(p.getName() + ".Kills", 0) + i);
            saveConfig();
        }
       
        public void giveDeaths(Player p5, int i){
            getConfig().set(p5.getName() + ".Deaths", getConfig().getInt(p5.getName() + ".Deaths", 0) + i);
            saveConfig();
        }
       
        public void giveKs(Player p, int i){
            getConfig().set(p.getName() + ".Killstreak", getConfig().getInt(p.getName() + ".Killstreak", 0) + i);
            saveConfig();
        }
       
        public void takeKs(Player p, int i){
            getConfig().set(p.getName() + ".Killstreak", getConfig().getInt(p.getName()+ ".Killstreak", 0) - i);
            saveConfig();
        }
    }
    Here is what the config looks like:
    Code:
    Luflexed:
      Killstreak: 0
      Kills: 20
      Deaths: 1
    PowderedBiscuit:
      Killstreak: 1
      Kills: 21
      Deaths: 1
    
    How would I add game ranks, help is really appreciated.
     
  2. Offline

    1Rogue

    You could define a comparable class:

    Code:java
    1. public class KillInfo implements Comparable<KillInfo> {
    2.  
    3. //Store Player UUID, kills, killstreak, deaths
    4.  
    5. //methods for incrementing deaths and kills, internally modify killstreak accordingly
    6.  
    7. //getter methods
    8.  
    9. //prerequisite: "kills" is a field in this class denoting number of total kills
    10. public int compareTo(KillInfo other) {
    11. return Integer.compare(this.kills, other.kills);
    12. }
    13.  
    14. }


    Add a player's KillInfo to a TreeSet, and then their rank is merely the position in the set.
     
  3. Offline

    Luflexed

    I understand comparing the integers, just not the Store player kills, methods for incrediment, and getter methods. Please explain better? @1Rogue
     
  4. Offline

    mythbusterma

    @Luflexed

    He's saying create a new class that stores this data about a player, and have it implement Comparable, and compare upon the number of kills. That way you can store it in a Java data structure that automatically sorts the data, like a Tree.
     
  5. Offline

    Luflexed

    More help? I have never worked with anything of this nature. Maybe help on skype, or on here? Anything. @mythbusterma @1Rogue
     
  6. Offline

    API_Tutorials

    @Luflexed
    Keep all discussions on the forums.

    Also, something align the lines of:
    Code:
    public class KillInfo implements Comparable<KillInfo>{
    
    private int kills;
    
    public int getKills(){
    return kills;
    }
    
    public int compareTo(KillInfo other){
    return Integer.compare(this.kills, other.getKills());
    }
    should work.
     
Thread Status:
Not open for further replies.

Share This Page