Solved Best Way to Stop a Task?

Discussion in 'Spigot Plugin Development' started by Username1234, Apr 22, 2017.

Thread Status:
Not open for further replies.
  1. How do I cancel a task with this code?:
    Code (Text):
    ---

    public class Countdown extends BukkitRunnable {
     
        @Override
        public void run() {
         
            if(WaitingPlayers.waitingPlayers.size() < Main.getInstance().getConfig().getInt("min_players")) {
             
                for(String s : WaitingPlayers.waitingPlayers) {
                 
                    Bukkit.getServer().getPlayer(s).sendMessage(Main.getInstance().formatChat(Main.getInstance().getMessagesFile().getString("not_enough_players")));
                }
            }
        }
     
        private void startCountdown() {
         
            new Countdown().runTaskTimer(Main.getInstance(), 20, 20);
        }
    }
    I know I can do
    Code (Text):
    Bukkit.getScheduler().cancelTask(int taskId);
    but where do I get the ID from?
     
    #1 Username1234, Apr 22, 2017
    Last edited: Apr 22, 2017
  2. just store the returned BukkitTask somewhere and use the ( storedTask.cancel(); ) method
     
  3. What Phloxz said or you could just do something like
    Code (Text):
    Bukkit.getScheduler().cancelTask(int TaskName);
    Keep in mind it's an int, so if you wanted to use it, just define the task as an integer
     
  4. Kind of what I was asking- how do I get the task name?
    How?
     
    #4 Username1234, Apr 22, 2017
    Last edited: Apr 22, 2017
  5. You could just define the task as an Integer, for example
    Code (Text):
    task = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    - After you declare it as an integer.
     
  6. But won't this schedule another task? This already implements BukkitRunnable.
     
  7. Use
    Code (Text):
    Countdown countdown = new Countdown();
    countdown.cancel();
    Just store the countdown and cancel it.
     
    • Like Like x 1
  8. What do you mean? If you don't want another task running at the same time. Just implement a sone logic like
    Code (Text):
    if(Bukkit.getScheduler().isCurrentlyRunning(task)) {
    //blah blah
     
  9. -Thread Locked
    @Synapz 's way was the least confusing and it worked :)
     
    • Like Like x 1
Thread Status:
Not open for further replies.