Scheduler time configurable ?

Discussion in 'Plugin Development' started by recon88, May 26, 2012.

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

    recon88

    What I got:
    - It starts from 10 seconds.
    - At 5 seconds it starts to send the countdown to the player.
    - After that it puts the player on a hashmap and stops/kills the task.

    What I need:
    - I want to make the timer/cooldown configurable.


    Code:
    private int warmup = 10;
    int stopper;
     
        public void startCountdown(final Player player) {
            stopper = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                  public void run() {
                      if (warmup == 5) {
                    player.sendMessage(ChatColor.RED + "5");
                      }
                      if (warmup == 4) {
                    player.sendMessage(ChatColor.RED + "4");
                      }
                      if (warmup == 3) {
                    player.sendMessage(ChatColor.RED + "3");
                      }
                      if (warmup == 2) {
                    player.sendMessage(ChatColor.RED + "2");
                      }
                      if (warmup == 1) {
                    player.sendMessage(ChatColor.RED + "1");
                      }
                      if (warmup == 0) {
                     trash.put(player, true);
                      player.sendMessage(ChatColor.GREEN + "Timer is up.");
                     stop();
                      }            
                      warmup--;
                  }
              }, 200L, 20L);
      }
     
      public void stop() {
          this.getServer().getScheduler().cancelTask(this.stopper);
          warmup = 10;
      }
     
  2. Offline

    Orcem12

    @recon88
    on your "200L" for example make it so:
    Code:
    this.getConfig().getLong("My_Long_Variable");
    or Same thing for your repeating value:
    Code:
    }, this.getConfig().getLong("My_Long_Variable"), this.getConfig().getLong("My_Other_Long_Variable"); 
    Full example:
    Code:java
    1.  
    2. plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    3. public void run() {
    4.  
    5. }
    6. }, plugin.getConfig().getLong("Long_Value"), plugin.getConfig().getLong("Other_Long_Value"));
    7.  
     
  3. Offline

    recon88

    I still don't get it.

    This happens now:
    - Using command
    - 10 seconds timer starts
    - After that the 5 seconds countdown starts

    Expected
    - Using command
    - 10 seconds timer starts
    - 5 seconds later the countdown starts


    Code:
    private int warmup = 10;
    int stopper;
        public void startCountdown(final Player player) {
            Long timer = this.getConfig().getLong("Warmup Time") * 20; // Simple maths to set seconds in the config.
            stopper = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                  public void run() {
                      if (warmup == 5) {
                          player.sendMessage(ChatColor.RED + "5");
                      }
                      if (warmup == 4) {
                          player.sendMessage(ChatColor.RED + "4");
                      }
                      if (warmup == 3) {
                          player.sendMessage(ChatColor.RED + "3");
                      }
                      if (warmup == 2) {
                          player.sendMessage(ChatColor.RED + "2");
                      }
                      if (warmup == 1) {
                          player.sendMessage(ChatColor.RED + "1");
                      }
                      if (warmup == 0) {
                          trash.put(player, true);
                        player.sendMessage(ChatColor.GREEN + "Timer is up.");
                          stop();
                      }   
                      warmup--;
                  }
              }, timer, 20L);
      }
     
      public void stop() {
          this.getServer().getScheduler().cancelTask(this.stopper);
          warmup = 10;
      }
    Still can't find the solution.

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

    Coryf88

    Set warmup to 5 and your "Warmup Time" config to 5.
     
  5. Offline

    recon88

    That would not fix it... I don't want a static 10seconds timer.. I want it to be configurable as I already said.
     
  6. Offline

    Coryf88

    Code:java
    1. private int warmup = 5;
    2. int stopper;
    3. public void startCountdown(final Player player) {
    4. Long timer = (this.getConfig().getLong("Warmup Time") - 5) * 20; // Simple maths to set seconds in the config.
    5. stopper = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    6. public void run() {
    7. if (warmup == 5) {
    8. player.sendMessage(ChatColor.RED + "5");
    9. }
    10. if (warmup == 4) {
    11. player.sendMessage(ChatColor.RED + "4");
    12. }
    13. if (warmup == 3) {
    14. player.sendMessage(ChatColor.RED + "3");
    15. }
    16. if (warmup == 2) {
    17. player.sendMessage(ChatColor.RED + "2");
    18. }
    19. if (warmup == 1) {
    20. player.sendMessage(ChatColor.RED + "1");
    21. }
    22. if (warmup == 0) {
    23. trash.put(player, true);
    24. player.sendMessage(ChatColor.GREEN + "Timer is up.");
    25. stop();
    26. }
    27. warmup--;
    28. }
    29. }, timer, 20L);
    30. }
    31.  
    32. public void stop() {
    33. this.getServer().getScheduler().cancelTask(this.stopper);
    34. warmup = 6;
    35. }
     
  7. Offline

    recon88

    Silly me! :/

    Thanks.
     
  8. Offline

    Coryf88

    Can clean that up a bit...

    Code:java
    1. private int warmup = 5;
    2. int stopper;
    3.  
    4. public void startCountdown(final Player player) {
    5. Long timer = (this.getConfig().getLong("Warmup Time") - 5) * 20; // Simple maths to set seconds in the config.
    6. stopper = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    7. public void run() {
    8. if (warmup > 0) {
    9. player.sendMessage(ChatColor.RED + Integer.toString(warmup));
    10. warmup--;
    11. } else {
    12. trash.put(player, true);
    13. player.sendMessage(ChatColor.GREEN + "Timer is up.");
    14. stop();
    15. }
    16. }
    17. }, timer, 20L);
    18. }
    19.  
    20. public void stop() {
    21. this.getServer().getScheduler().cancelTask(this.stopper);
    22. warmup = 5;
    23. }
     
  9. Offline

    the_merciless

    Is Interger.toString neccesary if you have no other string in your message.
    I have this code in 1 of my plugins and it works just fine.

    Code:
    int cd = getConfig().getInt("Countdown");
     
    if ( cd % 30 == 0 && cd > 0 ){
                        Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED + "THE GAME WILL BEGIN IN " + cd + " SECONDS!");
                    }
     
  10. Offline

    Coryf88

    the_merciless Yes, the Integer.toString is necessary in the "clean that up a bit" code, otherwise you'd get an error along the lines of operator + isn't defined for ChatColor and int. Using + on a string and an int is fine since Java will compile + to use a StringBuilder.

    E.g.
    System.out.println("Hello, we have " + currentPlayers + " players online. Max seen is " + maxSeenPlayers);
    would compile to something like...
    System.out.println(new StringBuilder("Hello, we have ").append(currentPlayers).append(" players online. Max seen is ").append(maxSeenPlayers).toString());

    Also, instead of doing player.sendMessage(ChatColor.RED + Integer.toString(warmup)); could do player.sendMessage(ChatColor.RED.toString() + warmup); or even player.sendMessage(ChatColor.RED.toString() + Integer.toString(warmup));
     
  11. Offline

    recon88

    Code:
                      if (warmup > 0) {
                          player.sendMessage(ChatColor.RED + Integer.toString(warmup));
                          warmup--;
    Output:
    5
    3
    1

    Instead of:
    5
    4
    3
    2
    1
     
  12. Offline

    the_merciless

    Understood, thanks for the info :)

    (sorry to hijack the thread i just had to ask out of interest)
     
  13. Offline

    Coryf88

    Are you decrementing warmup elsewhere?
     
  14. Offline

    recon88

    Code:
        private int warmup = 5;
        int stopper;
     
        public void startCountdown(final Player player) {
            Long timer = (this.getConfig().getLong("Warmup Time") - 5) * 20;
            stopper = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                  public void run() {
                      if (warmup > 0) {
                          player.sendMessage(ChatColor.RED + Integer.toString(warmup));
                          warmup--;
                      } else {
                          trash.put(player, true);
                        player.sendMessage(ChatColor.GREEN + "Timer is up.");
                          stop();
                      }         
                      warmup--;
                  }
              }, timer, 20L);
        }
     
      public void stop() {
          this.getServer().getScheduler().cancelTask(this.stopper);
          warmup = 5;
      }
     
  15. Yes, you're decrementing it twice. Once inside the if and another time outside of the if.
     
  16. Offline

    recon88

    I see.. My mistake.
    Works now. Thanks =)
     
Thread Status:
Not open for further replies.

Share This Page