Repeating task not cancelling

Discussion in 'Plugin Development' started by teunie75, Apr 30, 2013.

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

    teunie75

    I've made a repeating event, but when is executed multiple times at the same time it continues without stopping.
    Code:
    int arrows, task;
                     arrows = 0;
                     task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                         public void run() {
                             arrows++;
                             if(arrows > 6){
                                Bukkit.getScheduler().cancelTask(task);
                             }
                                 spawnArrow(damager, hitted);
                         }
                 }, 10L, 10L);
    
     
  2. Offline

    CubieX

    How is this embedded in your event handler?

    Probably your "task" variable is overwritten by successive calls of your handler method.
    So the already running schedulers do not call the .cancel() method with their suiting taskID.

    You need to make sure your taskIDs will persist until the task is actually canceled.
     
  3. Offline

    catageek

    The variable thread is not available when you execute the task, so it fails.

    You should create a BukkitRunnable() instead of a Runnable(), and replace the line
    Code:
    Bukkit.getScheduler().cancelTask(task);
    with
    Code:
    this.cancel();
     
  4. Offline

    Zach_1919

    teunie75 I've been having the same problem, and catageek when I do that it gives me this big error about the task not being scheduled yet on the line that is this.cancel();
     
  5. Offline

    CluelessDev

    I guess there would be different ways of approaching it. Does each task cancel in the order it was created? That is, would the first one scheduled be the first to be canceled?

    If so, then you could add the task id's to an arraylist, and then pull them out of the array list as you cancel them.

    Code:
    ArrayList<Integer> taskIDs;// - This definition should be an attribute of the class you're working with.
    taskIDs = new ArrayList<Integer>();//initialize the arraylist somewhere
     
    taskIDs.add(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
    {
        @Override
        public void run()
        {
            //stuff
            if(CancelCondition == true)
            {
                cancelTask();
            }
        }
    }, 0, 20));
     
    private void cancelTask()
    {
        plugin.getServer().getScheduler().cancelTask(taskIDs[0]);
        taskIDs.remove(0);
    }
    (I haven't actually tested this code, since it's just a modification of something I wrote up for someone a bit ago. If it doesn't work, let me know.)

    Using a queue would be more 'proper' I guess... But since I haven't tested the code I figured it would be best to stick with something whose syntax I am familiar with.

    If the problem is that your multiple repeating tasks are overwriting the task id you have stored and therefore not knowing what task to cancel, taking an approach like this might help.

    If your tasks can be canceled in a different order than they've started in, this wouldn't work and a different approach would be necessary.
     
    TigerHix likes this.
  6. Offline

    TheUpdater

    do
    if(arrows == 6){
     
  7. Offline

    teunie75

    I always used a HashMap to store the playernames with a task id, but that fails sometimes too.
     
  8. Offline

    Zach_1919

    Just bumpin this to make sure it gets a reply, as I really want this fixed too
     
Thread Status:
Not open for further replies.

Share This Page