Solved Cancel a repeating event?

Discussion in 'Plugin Development' started by chrisman0091, Dec 14, 2013.

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

    chrisman0091

    How would I go about canceling my scheduled repeating task?

    I would use something like
    Code:java
    1. Bukkit.getScheduler().cancelTasks(<MainClassInstance>);

    but the problem with that is I have more than one task and I only need to cancel one of them. I also tried giving the task an ID
    Code:java
    1. int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(..);

    and then figured I would use
    Code:java
    1. Bukkit.getScheduler().cancelTask(taskID);

    to cancel it. Only problem is I need to make it a final int to use it in cancelTask but after making it a final upn using the ID in cancelTask I get a "The local variable CT may not have been initialized" error.

    Basically, how would I go about canceling this task within the task.
     
  2. Offline

    amhokies

    Something like this:

    The class that extends BukkitRunnable.
    Code:java
    1. private class ScoreboardRunnable extends BukkitRunnable {
    2. private int id;
    3.  
    4. public void setId(int id) {
    5. this.id = id;
    6. }
    7.  
    8. @Override
    9. public void run() {
    10. // Whatever you want to do.
    11. Bukkit.getScheduler().cancelTask(id);
    12. }
    13. }


    Where you're running the task
    Code:java
    1. ScoreboardRunnable scoreboardTask = new ScoreboardRunnable(zPlayer);
    2. scoreboardTask.setId(Bukkit.getScheduler().runTaskTimer(plugin, scoreboardTask, 20L, 20L).getTaskId());
     
  3. Offline

    chrisman0091

    I think that'll work, thanks!
     
Thread Status:
Not open for further replies.

Share This Page