Countdown Help

Discussion in 'Spigot Plugin Development' started by Swedz, Jun 14, 2016.

  1. Currently, I'm using
    Code (Text):
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                            int countdownNum = 5;
                            @Override
                            public void run() {
                                if(countdownNum > 0) {
                                    API.sendTitle(player1, "title", "§b" + countdownNum, 0, 2 * 20, 0);
                                    API.sendTitle(player1, "subtitle", "§aPrepare to fight!", 0, 2 * 20, 0);
                                    API.sendTitle(player2, "title", "§b" + countdownNum, 0, 2 * 20, 0);
                                    API.sendTitle(player2, "subtitle", "§aPrepare to fight!", 0, 2 * 20, 0);
                                    player1.playSound(player1.getLocation(), Sound.CLICK, (float) 0.5, (float) 1.0);
                                    player2.playSound(player2.getLocation(), Sound.CLICK, (float) 0.5, (float) 1.0);
                                    countdownNum++;
                                } else {
                                    //Cancel code
                                }
                            }
                        }, 0, 20);
    This doesn't even work at all, but I also have two more questions.

    - How do I cancel the loop/countdown?
    - Does the code outside of the scheduler run during this, or wait until it is over to be run?

    Thanks!
     
  2. How do I cancel?
    I recommend you use 'runTaskTimer' instead of scheduleSyncRepeatingTask.
    Code (Text):
    BukkitTask task = Bukkit.getScheduler().runTaskTimer....

    task.cancel();
    Does the code outside run during this?
    If you put code after the scheduler, it will run on the same tick as when you schedule the scheduler.
     
  3. How do I make it so the code after it doesn't run until this is over?
     
  4. A better way of running a countdown would be to have a for loop 1-5 and use runTaskLater (set for 20 * loop) instead of a repeating task.
     
    • Funny Funny x 1
  5. Could you give me an example?
     
  6. Code (Text):
    for (int i = 1; i <= 3; i++) {
         Bukkit.getScheduler().runTaskLater(this, () -> {
              Bukkit.broadcastMessage(i + " Mississippi...");
              if (i == 3) {
                   Bukkit.broadcastMessage("WE COUNTED TO THREE YAY!");
              }
         }, 20 * i);
    }
     
  7. And outside the 'for' I'd put the code after? Which will cause it to be delayed, right?
     
  8. No. Like I showed with the "WE COUNTED TO THREE!", you would put it inside based on the loop number.
     
    • Like Like x 1
  9. If I do this:
    Code (Text):
    for (int i = 1; i <= 6; i++) {
        Bukkit.getScheduler().runTaskLater(this, () -> {
            if(i < 6) {
                            //do stuff
                    }
             }
    }
    I get an error in Eclipse:
    Local variable i defined in an enclosing scope must be final or effectively final

    EDIT: Ignore the weird formatting :p
     
  10. Oh whoops. Inside the for loop do this

    Code (Text):
    for (...) {
    final int n = i;
    }
    and then use 'n' instead of 'i'
     
  11. Okay nice, thanks! That clears up the Eclipse errors. How would I do a scheduler to run repetitively until some line of code is run?

    Thanks so much again. :)
     
  12. Scheduler#run_____ methods return a 'BukkitTask' object. BukkitTask#cancel cancels the task
     
  13. Can you give me an example? I'm stupid I know :p
     
  14. Code (Text):
    BukkitTask task;

    task = Bukkit.getScheduler().runTaskTimer....

    if (task != null)
         task.cancel();
     
  15. How would I format this exactly?
    Just:
    Code (Text):
    task = Bukkit.getScheduler().runTaskTimer {

    }
    ?
     
  16. You have to define BukkitTask task; before that, then yes. If you just do BukkitTask task = .... it won't let you cancel the task from inside itself because the object doesn't exist until the scheduler method has completely been run.
    (NOTE: this is probably for a good reason, make sure to do proper null checks and such before cancelling it)
     
  17. I get 'runTaskTimer cannot be resolved or is not a field' in Eclipse with
    Code (Text):
    BukkitTask task;
    task = Bukkit.getScheduler().runTaskTimer {
                                       
    }
     
  18. You have to put the arguments. runTaskTimer(plugin, runnable, time, time)
     
  19. Wait.. do I do this?
    Code (Text):
    BukkitTask task;
                                    task = Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                                        @Override
                                        public void run() {
                                         
                                        }
                                    }, 0, 5);
    EDIT: This will make it run every 5 ticks immediately from the activation of the code, right?
     
  20. Indeed.
    Then do task.cancel() to cancel it.