Canceling a runTaskTimer fails

Discussion in 'Spigot Plugin Development' started by titivermeesch, Jun 11, 2019.

  1. Hello there,

    So I have a runTaskTimer that will run some code, and there are conditions that will cancel the runner itself, but it cancels it and when I try to restart it by doing
    Code (Text):
    this.runTaskTimer(Main.getPlugin(), 0L, 20L);
    It says that's it's already scheduled, while I did cancel()

    Can someone please say me why it's doing so?
     
  2. Send the whole code, please.
     
  3. Code (Java):
    this.time = time;
            this.runTaskTimer(Main.getPlugin(), 0L, 20L);
        }

        @Override
        public void run() {
            if (arena.getPlayers().size() < arena.getMinPlayers()) {
                cancel();
                arena.setState(GameState.WAITING);
                arena.broadcast(ChatColor.RED + "There are too few players. Countdown stopped.");
                return;
            }
            time--;
        }
     
  4. You can't schedule a task that has been cancelled. Create a new one instead (or don't cancel it in the first place?).
     
  5. It's for a timer, so like at some times it will cancel itself, but like how do I create a new one then? It's instantiated from a class. Maybe BukkitRunnable then?
     
  6. You initiate it again or don't cancel it at all.
     
  7. But I'm initiating it again and it gives me that error
     
  8. share some code?
     
  9. It's just above as you can see. If you do cancel() it should delete that instance right? Or is there something else?
     
  10. It doens't delete the instance, it just cancels the task. Show us where you initiate the class.
     
  11. Cancelling a BukkitRunnable doesn't fail. Once you cancel it, you cannot re-start it, you need to create a new instance of it.
     
  12. This is how I instatiate it :
    Code (Text):
    countdown.start(15);
    And this is the start function
    Code (Text):
    public void start(int time) {
         
            this.time = time;
            this.runTaskTimer(Main.getPlugin(), 0L, 20L);
    }
     
  13. This is not initializing. You're just calling a method. As people have said before, you cannot restart a previously cancelled task. You will need to create a new instance. That means using the constructor to get the new instance.
     
  14. If you want a timer, store the initial time (System.currentTimeInMillis) and let the tasktimer run each second and calculate with the duration of the timer how many seconds left
     
  15. That's already done but I need to cancel that
     
  16. what's your countdown variable exactly?
     
  17. It's time
     
  18. Share the code maby?
     
  19. Everything is already in a post above
     
  20. Why do you want to cancel it? A simple boolean check if the timer has been < 0 and remove the player from the timer hashmap?
     

Share This Page