How to Kill to Death Ratio?

Discussion in 'Plugin Development' started by blok601, Nov 14, 2015.

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

    blok601

    Hello!

    I have two integers (kills and deaths) , and I want to make a kill to death ratio with only two decimal places. How would I convert the integer to a double in division?

    Also, when my Kills and Deaths are the same, one of the stats on the scoreboard disappears.

    Here is the code:
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
          
              final Player p = e.getPlayer();
                
    
          
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                public void run(){
                      int tKills = getConfig().getInt(p.getName() + ".kills");
                      int tDeaths = getConfig().getInt(p.getName() + ".deaths");
                     
                       DecimalFormat df = new DecimalFormat("0.00");
                        double ratiom = tKills/tDeaths;
                       final String ratio = df.format(ratiom);
                       final String k = ratio.toString();
    
                    ScoreboardManager manager = Bukkit.getScoreboardManager();
                    final Scoreboard board = manager.getNewScoreboard();
                    Objective objective = board.registerNewObjective("test", "dummmy");
                    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                    objective.setDisplayName(ChatColor.RED + "NightShadePvP");
                  
                    Score score  = objective.getScore(ChatColor.AQUA + "Player name: ");
                    score.setScore(10);
                  
                    Score score1 = objective.getScore(ChatColor.GRAY + p.getName());
                    score1.setScore(9);
                  
                    Score score2 = objective.getScore(ChatColor.AQUA + "Kills: ");
                    score2.setScore(8);
                  
                    long health = Math.round(p.getHealth());
                  
                    Score score3 = objective.getScore(ChatColor.GRAY + "" + getConfig().getInt(p.getName() + ".kills"));
                    score3.setScore(7);
                  
                    Score score4 = objective.getScore(ChatColor.AQUA + "Deaths: ");
                    score4.setScore(6);
                  
                    Score score5 = objective.getScore(ChatColor.GRAY + "" + getConfig().getInt(p.getName() + ".deaths"));
                    score5.setScore(5);
                  
                    Score score6 = objective.getScore(ChatColor.AQUA + "K/D:");
                    score6.setScore(4);
                  
                    Score score7 = objective.getScore(ChatColor.GRAY + "" + k);
                    score7.setScore(3);
                  
                  
                          
                    p.setScoreboard(board);
    
                  
                }
            }, 0, 20);
    Thanks!
     
  2. Offline

    teej107

    cast the dividend or the divisor to a double
    How are you laying out your scoreboard?
     
  3. Offline

    blok601

    Location? SideBar...
     
  4. Offline

    teej107

  5. Offline

    blok601

    Attached Files:

  6. Offline

    teej107

    You can't have 2 of the same scoreboard entries in a scoreboard
     
  7. Offline

    blok601

    Oh, I will change that then.
     
  8. Offline

    Scimiguy

    @teej107 @blok601
    Would you not be better off casting the initial values to a Double prior to dividing?
     
  9. Offline

    blok601

    @teej107 is there a workaround?

     
  10. Offline

    teej107

    Yes.
     
  11. Offline

    blok601

    Maybe use a string or should I just have Kills: and the integer on the same line?

    @teej107
     
  12. Offline

    Scimiguy

    Why dont you give it a try and see what works?
     
    blok601 likes this.
  13. Offline

    teej107

    Use a String that doesn't equal another String in the entries. You can put different ChatColors at the end of entries so you could have "duplicate" entries.
     
Thread Status:
Not open for further replies.

Share This Page