Stats not working?

Discussion in 'Plugin Development' started by lcpvp, Jan 29, 2013.

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

    lcpvp

    So I got some help from ChaseChocolate and Cubix Coders, and have this code, to check player stats.

    I honestly have no idea if this should work or not, but I want it so when players do /stats it shows kills, deaths killstreak and kdr.
    I was just messing around with the message thing.
    Code:java
    1. if(commandLabel.equalsIgnoreCase("stats") && args.length == 0){
    2. int kills = playerKills.get(player.getName());
    3. int deaths = playerDeaths.get(player.getName());
    4. int killStreak = playerKillStreaks.get(player.getName());
    5. double kdr = kills / deaths;
    6. int kdrRound = Math.round(kdr);
    7. player.sendMessage(ChatColor.GOLD + "You have" + playerKills.get(player.getName()) + "total kills");
    8. player.sendMessage(ChatColor.GOLD + "You have" + playerDeaths.get(player.getName()) + "total deaths");
    9. player.sendMessage(ChatColor.GOLD + "You have a killstreak of " + playerKillStreaks.get(player.getName()) + "kills");
    10.  
    11. }
    12.  


    Oops! These are my hash maps and other stuff:
    Code:java
    1. public HashMap<String, Integer> playerKills = new HashMap<String, Integer>();
    2. public HashMap<String, Integer> playerKillStreaks = new HashMap<String, Integer>();
    3. public HashMap<String, Integer> playerDeaths = new HashMap<String, Integer>();
    4.  


    Code:java
    1. @EventHandler
    2. public void onPlayerKill(PlayerDeathEvent event) {
    3. Player victim = event.getEntity();
    4. Player p = event.getEntity();
    5. if (victim.getKiller() instanceof Player) {
    6. Bukkit.broadcastMessage(ChatColor.RED +p.getDisplayName() + ChatColor.WHITE +" Just got "+ ChatColor.BOLD+ "Killed "+ ChatColor.WHITE+ "By "+ ChatColor.RED + victim.getKiller().getName());
    7. playerKills.put(p.getName(), +1);
    8. playerKillStreaks.put(p.getName(), +1);
    9.  
    10. }
    11. }
    12.  


    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Player victim = e.getEntity();
    4. if(victim.getKiller() instanceof Player) {
    5. playerDeaths.put(victim.getName() + 1, null);
    6. playerKillStreaks.remove(victim.getName());
    7.  
    8. }
    9. }
    10.  


    Code:java
    1. @EventHandler
    2. public void onPlayerKillstreak(PlayerDeathEvent event){
    3. if(event.getEntity().getKiller() instanceof Player){
    4. Player killer = event.getEntity().getKiller();
    5. int numKills = kills.get(killer.getName());
    6. kills.put(killer.getName(), +1);
    7. killer.sendMessage(ChatColor.GREEN + "You have a killstreak of " + numKills + " so far!");
    8. killer.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP, numKills * 2));
    9. }
    10. }
    11.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    Rprrr

    lcpvp
    Could you also tell what's the problem?
     
  3. Offline

    lcpvp

    I dont really know, I just know this wont work. only error I get is on this line.

    Code:java
    1. int kdrRound = Math.round(kdr);
    2.  


    It says:
    Type mismatch: Can not convert from long to int.
     
  4. Offline

    Rprrr

    lcpvp
    So at what line would that be? Because I don't see you doing that anywhere in the code you've posted.
     
  5. Offline

    Remi1115

    I'm not sure what you're trying to do with that.
    Converting a Long to Integer and rounding it down?
     
  6. Offline

    lcpvp

    I want it to divide the players kills by deaths, and round up.
     
  7. Offline

    Ne0nx3r0

    lcpvp
    Math.round(Double) returns a long.

    Stack traces are virtually never wrong ; )
     
  8. Offline

    lcpvp

    So should I change to Math.round(Double)?
     
  9. Offline

    chasechocolate

    Whoops, my bad! Change it from an int to a long.
     
  10. Offline

    lcpvp

    Code:java
    1. if(commandLabel.equalsIgnoreCase("stats") && args.length == 0){
    2. int kills = playerKills.get(player.getName());
    3. int deaths = playerDeaths.get(player.getName());
    4. int killStreak = playerKillStreaks.get(player.getName());
    5. double kdr = kills / deaths;
    6. long kdrRound = Math.round(kdr);
    7. player.sendMessage(ChatColor.GOLD + "You have" + playerKills.get(player.getName()) + "total kills");
    8. player.sendMessage(ChatColor.GOLD + "You have" + playerDeaths.get(player.getName()) + "total deaths");
    9. player.sendMessage(ChatColor.GOLD + "You have a killstreak of " + playerKillStreaks.get(player.getName()) + "kills");
    10.  
    11. }
    12.  


    Errors on
    Code:java
    1. int killStreak = playerKillStreaks.get(player.getName());
    2.  


    and
    Code:java
    1. long kdrRound = Math.round(kdr);
    2.  


    Both saying the local variable is not used, how can I add it so it tells the player the KDR?
     
  11. Offline

    Ne0nx3r0

    Take another look at your code. You are doing playerKillStreaks.get(...) in the player.sendMessage() on line 9. So, exactly as your IDE is telling you, you are not using the killStreak variable you created. You could either remove the killStreak variable, or use it in your player.sendMessage() instead of playerKillStreaks.get(...). The same looks to be true for line 6.


    You really need to learn how to troubleshoot and maintain your own code, you are literally giving the answers to your own questions.
     
  12. Offline

    lcpvp

    Can I have an example? Sorry, I figure out most stuff but sometimes just get stuck
     
  13. Offline

    Ne0nx3r0

    You have a fine example already :).

    Code:java
    1. if(commandLabel.equalsIgnoreCase("stats") && args.length == 0){
    2. int kills = playerKills.get(player.getName());
    3. int deaths = playerDeaths.get(player.getName());
    4. int killStreak = playerKillStreaks.get(player.getName());
    5. double kdr = kills / deaths;
    6. long kdrRound = Math.round(kdr);
    7. player.sendMessage(ChatColor.GOLD + "You have" + playerKills.get(player.getName()) + "total kills");
    8. player.sendMessage(ChatColor.GOLD + "You have" + playerDeaths.get(player.getName()) + "total deaths");
    9. player.sendMessage(ChatColor.GOLD + "You have a killstreak of " + playerKillStreaks.get(player.getName()) + "kills");
    10.  
    11. }



    You could simply add:
    Code:java
    1. player.sendMessage(ChatColor.GOLD + "You have a killstreak of " +killStreak+ "kills");

    AND/OR
    Code:java
    1. player.sendMessage(ChatColor.GOLD + "You have a kdrRound of " +kdrRound + "kills");


    Java can get upset about converting number types when doing math (understandably so), but when converting numeric values to strings it's actually pretty easy.
     
Thread Status:
Not open for further replies.

Share This Page