Solved Scheduling Goop (Having trouble cancelling task)

Discussion in 'Plugin Development' started by 0ct0berBkkitPlgins, May 22, 2016.

Thread Status:
Not open for further replies.
  1. I'm getting an IllegalStateException repeatedly stating that a task isn't scheduled yet. This was originally with me using this.cancel() in the task class, and then I read a forum discussion saying that you shouldn't cancel a task within the task's class. So then I replaced it with mainclass.cancel(this.getTaskId()), and had cancel do Bukkit.getScheduler().cancelTask(id) (I don't remember exactly what it was, writing on my phone, but I spelled all the methods correctly). Yet I still got the same error.

    Is there something obvious that I'm missing? How can I avoid cancelling a task before it is scheduled? Also, I'm needing to run conditions that will check wether something at a location is true, and if not, cancel the task. So whatever solution is given would need to work with that.
     
  2. Offline

    WolfMage1

    Need the code to help you.
     
  3. Part of Main class:

    Code:
    @EventHandler
    void someEvent(Event e) {
    if (conditions.isMet()) {
    ItemStack item;
    Block block;
    SecretTask task = new SecretTask(this, item, block);
    getServer().getScheduler().scheduleSyncRepeatingTask(this, (Runnable)task, 1L, 1L);
    }}
    public void cancel(int id) {
    Bukkit.getScheduler().cancelTask(id);
    }
    Part of SecretTask class:

    Code:
    @Override
    void run() {
    if (otherConditions.isMet()) {
    mainclass.cancel(this.getTaskId()); //Tried this.cancel(), got same results
    }
    }
    phone bugging, reply above ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. Offline

    RandomPanda30

    Simply...
    Code:
    int id = new getServer().getScheduler().schedulessometask(plugin, new Runnable() {
    @Override
    public void run() {
    // cancel id if you want
    }
    Running the new task will return a task ID which you can cancel later on. Get creative, enjoy
     
  5. Thanks for trying to help. I'm actually attempting to do this in a separate class that extends BukkitRunnable, and I'm trying to run that task, then in the separate class, cancel it once some conditions are met or a counter reaches zero. I've realized I should get out of the habit of putting all the code in the main class, especially for larger projects, so that's why I'd like to do it this way.

    Edit: Source of the problem explained on the wiki: "BukkitRunnables can schedule and cancel their own execution. However, if the BukkitRunnable did not schedule itself for execution, it cannot cancel itself from execution."
     
    Last edited: May 22, 2016
  6. Offline

    RandomPanda30

    BukkitRunnable allows you to cancel the task. So for example

    Code:
    BukkitTask sometask = new TaskThatExtendsBukkitRunnable();
    
    Then you can run it
    and do .cancel() to stop it 
     
  7. I'm familiar with this, and this is what I originally used, as I had stated in the post... Apparently the cause of the error is because I need to schedule the task within itself if I want to cancel it within itself.
     
Thread Status:
Not open for further replies.

Share This Page