How to add long to scoreboard

Discussion in 'Plugin Development' started by MarioNoobProgrammer, Mar 8, 2017.

Thread Status:
Not open for further replies.
  1. I've seen people do it. I just cant do it because it only allows int.

    btw this is the code im trying to add:
    Code:
    kdrTitle.setScore( plugin.getConfig().getLong(p.getUniqueId() + ".kdr"));
     
  2. Online

    timtower Administrator Administrator Moderator

    @MarioNoobProgrammer I doubt it that a kdr will pass the max of an int.
    So in my opinion you should switch to int and not use longs for this.
     
  3. Offline

    Zombie_Striker

  4. @timtower @Zombie_Striker

    Ok, I just read this when I was looking into my error: An integer divided by an integer returns an integer, so it will truncate to 0.


    So if I can use an int, why dont this work?:
    Code:
    String kills1 = Main.plugin.getConfig().getString(killer.getUniqueId() + ".kills");
                String deaths1 = Main.plugin.getConfig().getString(killed.getUniqueId() + ".deaths");
                int newKills1 = Integer.parseInt(kills1);
                int newDeaths1 = Integer.parseInt(deaths1);
                if(!(newDeaths1 >= 0)) {
                    /*
                    double newKdr1 = newKills1 / 1;
                    main.plugin.getConfig().set(killer.getUniqueId() + ".kdr", Double.valueOf(newKdr1));
                    main.plugin.saveConfig();
                    */
                    return;
                }
                int newKdr = newKills1 / newDeaths1;
                main.plugin.getConfig().set(killer.getUniqueId() + ".kdr", Double.valueOf(newKdr));
                main.plugin.saveConfig();
     
  5. Online

    timtower Administrator Administrator Moderator

    @MarioNoobProgrammer Why not use getInt instead of parsing it?
    What is the error that you are getting?
     
  6. There wasnt any actual error, I just meant that I researched it a little and someone stated that I couldnt use Integers. I dont know why I didnt use getInt, I didnt really think about it.

    The kdr still wont change, it only changes to 1 then it stops. here is my code:

    I made it very easy to read, and I found a way to use Long by adding it into the title instead. Yet it wont update. any clues?

    Code:
    package com.creativecode.enjin.BeastCraft3.Listener;
    
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    import com.creativecode.enjin.BeastCraft3.Main;
    
    public class DeathEvent implements Listener
    {
       
        Main main;
    
    
        public DeathEvent(Main main) {
            this.main = main;
        }
       
        @EventHandler
        public void onDeath(PlayerDeathEvent e){
            Player killed = e.getEntity().getPlayer();
            Player killer = e.getEntity().getPlayer().getKiller();
           
            e.setDeathMessage(null);
           
            Main.plugin.getConfig().set(killed.getUniqueId() + ".killstreak", Integer.valueOf(0));
            Main.plugin.saveConfig();
           
           
            if (((killer instanceof Player)) || ((killer instanceof Snowball)))
              {
               
                //Kills
                String kills = Main.plugin.getConfig().getString(killer.getUniqueId() + ".kills");
                int newKills = Integer.parseInt(kills);
                newKills++;
                Main.plugin.getConfig().set(killer.getUniqueId() + ".kills", Integer.valueOf(newKills));
                Main.plugin.saveConfig();
               
                //KillStreak
                String killstreak = Main.plugin.getConfig().getString(killer.getUniqueId() + ".killstreak");
                int newKillstreak = Integer.parseInt(killstreak);
                newKillstreak++;
                Main.plugin.getConfig().set(killer.getUniqueId() + ".killstreak", Integer.valueOf(newKillstreak));
                Main.plugin.saveConfig();
               
              //Highest KillStreak
                String highKillstreak = Main.plugin.getConfig().getString(killer.getUniqueId() + ".highest_killstreak");
                int newhighKillStreak = Integer.parseInt(highKillstreak);
                if(newKillstreak > newhighKillStreak) {
                    Main.plugin.getConfig().set(killer.getUniqueId() + ".highest_killstreak", Integer.valueOf(newKillstreak));
                    Main.plugin.saveConfig();
                   
                } else {
                    return;
                }
               
               
                //Deaths
                String deaths = Main.plugin.getConfig().getString(killed.getUniqueId() + ".deaths");
                int newDeaths = Integer.parseInt(deaths);
                newDeaths++;
                Main.plugin.getConfig().set(killed.getUniqueId() + ".deaths", Integer.valueOf(newDeaths));
                Main.plugin.saveConfig();
               
                //KDR
                String kills1 = Main.plugin.getConfig().getString(killer.getUniqueId() + ".kills");
                String deaths1 = Main.plugin.getConfig().getString(killed.getUniqueId() + ".deaths");
                int newKills1 = Integer.parseInt(kills1);
                int newDeaths1 = Integer.parseInt(deaths1);
                if(!(newDeaths1 >= 0)) {
                    /*
                    double newKdr1 = newKills1 / 1;
                    main.plugin.getConfig().set(killer.getUniqueId() + ".kdr", Double.valueOf(newKdr1));
                    main.plugin.saveConfig();
                    */
                    return;
                }
                int newKdr = newKills1 / newDeaths1;
                main.plugin.getConfig().set(killer.getUniqueId() + ".kdr", Double.valueOf(newKdr));
                main.plugin.saveConfig();
              
               
                //Setup ScoreBoard
                Main.setupScoreboard(killer);
                Main.setupScoreboard(killed);
               
               
              } else {
                  return;
              }
           
           
           
           
        }
    
    
    }
    
     
    Last edited: Mar 8, 2017
  7. Offline

    raunak114

    Public int a;
    If ((int) <longname> = <longname> // checks if the int which is casted is equal to the original
    a =<longname>}// you just converted the int into a long


    I know there are methods that help accomplish this easily, but I just wanted to tell you another way, with this, you can even check if a string or a double is an int etc..
     
Thread Status:
Not open for further replies.

Share This Page