Resetting an async repeating task

Discussion in 'Plugin Development' started by mollekake, Oct 19, 2012.

Thread Status:
Not open for further replies.
  1. So i use an async repeating task for my game countdown. and it works great.
    it gets cancelled at the end which it's supposed to.
    BUT when i start the second match, i want the countdown to start all over again, but it just resumes the countdown and start going down with minus, -1, -2, -3 and so on.
    here's the counter:
    Code:
    public static int Countdown;
        int count = 20;//181 for 3 min
        public void startGameCountdown(final Player p) {
            Countdown = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
                public void run() {
                    count--;
                   
                    if(count == 180){
                        Bukkit.broadcastMessage("3 minutes until next game!");
                    }
                   
                    if(count == 60){
                        Bukkit.broadcastMessage("1 minute until game starts!");
                    }
           
                    if(count == 30 || count == 20 || count < 11){
                        Bukkit.broadcastMessage(count + " seconds until game starts!");
                    }
           
                    if(count == 0){
                        Bukkit.broadcastMessage("Game starts NOW!");
                        startGame(p);
                        Bukkit.getScheduler().cancelTask(Countdown);
                    }
                }
            }, 0L, 20L);
        }
    
     
  2. Offline

    CorrieKay

    either create a method in the runnable that sets the counter integer, or create a new instance of the runnable and run it.
     
  3. Offline

    LucasEmanuel

    You dont need an async task for that, and im not sure if all API calls you are using are threadsafe.
     
    CorrieKay likes this.
  4. Offline

    CorrieKay

    This is also true. when you call startGame(p);
    Make sure if you absolutely require to be async, that it only calls threadsafe methods (fairly dificult to do much without utilizing non-thread-safe methods)
     
  5. hmm ok, i've never used async tasks before. could this be done with a delayed task?
     
  6. Offline

    CorrieKay

    theres a difference between Async and Sync. use a Sync repeating task
     
  7. looks like it works now, thanks! :D
     
Thread Status:
Not open for further replies.

Share This Page