Solved ScoreBoard countdown

Discussion in 'Plugin Development' started by galaipa, Jun 5, 2015.

Thread Status:
Not open for further replies.
  1. Hi!
    I am coding the countdown timer of a minigame plugin using the scoreboard, but I have a problem. Instead of replacing the old score with the new time, it creates a new one. So this is the result:

    [​IMG]
    Here you have the code:

    Code:
              public void ScoreBoard(final String gaia,Player p){
                  final Objective objective = board.registerNewObjective(ChatColor.DARK_GREEN.BOLD + "B", "dummy");
                  objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                    int seconds = 0;
                    int minutes = 10;
                    @Override
                    public void run() {
                        if (seconds == 0){
                            seconds = 60;
                            minutes =minutes  - 1;
                        }
                        else{
                            seconds = seconds - 1;
                        }
                        Score gaiaa = objective.getScore(ChatColor.GREEN + "Gaia: " + ChatColor.YELLOW + gaia);
                        gaiaa.setScore(6);
                        Score web = objective.getScore(ChatColor.GREEN + "gamerauntsia.eus");
                        web.setScore(1);
                        Score denbora = objective.getScore(ChatColor.GREEN + "Denbora: " + minutes  + ":" + seconds);
                        denbora.setScore(3);
                        for(Player online : Bukkit.getOnlinePlayers()){
                            online.setScoreboard(board);
                    }
    
                    }
                }, 0, 20);
        }
    I have been googleing but I can't find a solutionl. I guess it is very simple but... I hope you can help me
     
  2. Offline

    Timbals

    You have to reset the score. The best way to do this to store the entry in a string and then reset it when you create the next entry. Scoreboard#resetScores
     
    galaipa likes this.
  3. Sorry, I didn't understand you very well. Can you put me a example?

    Thanks
     
  4. Offline

    Zombie_Striker

    @galaipa
    We do not spoonfeed, because only people who do not know Java or Bukkit will ask for it. That is the best you're going to get here.
     
  5. @Zombie_Striker If you don't want to help just don't post anything. I am LEARNING and I need some help, this forum is not only for professional developers like you.

    I tryed to do what he told me, but I got errors. I understood that I had to put "ChatColor.GREEN + "Denbora: " + minutes + ":" + seconds " in a String and then change it any every loop, but I didn't succes, I guess I didn't understand correctly .

    PD: My english is not very good :(
     
  6. Offline

    Zombie_Striker

    @galaipa
    Then you should have posted what you did, not just ask for the answer. If we just told people the answers to all their problems, they would still come back with another set of problems because they would not know how to solve their own problems. This forum is supposed to be for problems that are not answered by adding a single statement.
    I wouldn't go as far as saying i am a professional, I just now how to look things up.

    Please post what you have done, and look through all of ScoreBoard's methods. This is the easiest way to see if there is already a method for what you want.
     
  7. :D Finaly I understood what he meant with the Strings. Maybie its true that I replyed him to quickly, sorry.

    Here is the final code:
    Code:
                Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                    int seconds = 0;
                    int minutes = 10;
                    @Override
                    public void run() {
                        if (minutes == 0){
                            Bukkit.getServer().getScheduler().cancelAllTasks();
                        }
                        else if (seconds == 0){
                            seconds = 60;
                            minutes =minutes  - 1;
                        }
                        else{
                            seconds = seconds - 1;
                        }
                        Score gaiaa = objective.getScore(ChatColor.GREEN + "Gaia: " + ChatColor.YELLOW + gaia);
                        gaiaa.setScore(6);
                        Score web = objective.getScore(ChatColor.GREEN + "gamerauntsia.eus");
                        web.setScore(1);
                        board.resetScores(timer);
                        timer = ChatColor.GREEN + "Denbora: " + minutes  + ":" + seconds;
                        Score denbora = objective.getScore(ChatColor.GREEN + "Denbora: " + minutes  + ":" + seconds);
                        denbora.setScore(3);
                       
                        for(Player online : Bukkit.getOnlinePlayers()){
                            online.setScoreboard(board);
                    }
                    }
                }, 0, 20);
    Just one last perfomance question. If I change the scheduler from 1 second to 5, would the perfomance be improved?

    Thanks
     
  8. Offline

    Zombie_Striker

    @galaipa
    Yes, What you would be doing would be instead of using memory every seconds like what you would have now, you would be using that memory every 5 seconds (only using a fifth of the memory you would have been). The only thing is, it is only using a little amount of memory. Though there will be memory saved, its small and will not make that much of a difference. It's just a matter of choice.
     
    galaipa likes this.
Thread Status:
Not open for further replies.

Share This Page