Calling a scheduled task to run

Discussion in 'Plugin Development' started by Betagear, Sep 21, 2015.

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

    Betagear

    Hi.
    I'm currently trying to run a scheduled task instantaneously, instead of waiting for it to happen.

    It's for when my plugin is disabled. I have scheduled block reloading animation, and so I would like that when you /reload or whatever, it forces the scheduled tasks to be run instantaneously.
    I already looked a bit, but this code doesn't works :
    Code:
    for (BukkitTask task : Bukkit.getServer().getScheduler().getPendingTasks()) {
                if (task.getOwner() == plugin) {
                    task.notify();
                }
            }
    So, how can we use task.notify() or is there another way ?
    Thanks for reading.
     
  2. Offline

    Zombie_Striker

    @Betagear
    What do you mean doesn't work? Did you Debug? Do you know what line is causing this to "not work"?
     
  3. Offline

    Betagear

    It's just doing nothing. I doesn't runs the scheduled tasks.
     
  4. Offline

    Zombie_Striker

    @Betagear
    Maybe you can show me the schedulers that have been issued to .wait()? Does the server even recognized that you told some tasks to stop?
     
  5. Offline

    mythbusterma

    @Betagear

    When your plugin is disabled, you can't run any tasks. Don't try to.
     
  6. Offline

    Betagear

    I don't used any wait().
    I schedule my task like so :
    Code:
    new BukkitRunnable() {
                       
                        @Override
                        public void run() {
                             //Andhere I put my stuff
                           
                        }
                    }.runTaskLater(plugin, delay);
     
  7. @Betagear The tasks wont run when the plugin is disabled.
     
  8. Offline

    Betagear

    I know, and this is why I want them to be run right before the plugin the plugin disables.
     
  9. Offline

    RoboticPlayer

    You could try listening for the stop or reload command, and when it is executed, start your runnable.
     
  10. Offline

    Betagear

    I tried and It didn't worked. The only way would be to accelerate the ticking of the scheduler, or do something so.
     
  11. Offline

    mythbusterma

    You could just put it in your onDisable() method, it's there for a reason, you know. No need to schedule anything.
     
  12. Offline

    Betagear

    This is what I did. I think I need to know how to use wait().
     
  13. Offline

    mythbusterma

  14. Offline

    Betagear

    Because I use a system that does a reloading animation for the blocks by switching their colors and adding blocks.
    Since this is an animation, this is not instantaneous, and so if the admin reloads or stops the server, everything will be displaced and then they will need to replace all the blocks as they were in their initial phase. My plugin is StompArena, so if you want to know more about why, go there : http://dev.bukkit.org/bukkit-plugins/stomparena/
     
  15. @Betagear
    What you could do is store the runnable(s) in a list/field, and when the plugin disables just call Runnable#run() so they run.
     
  16. Offline

    Betagear

    Nah, it's not possible, my plugin would use a lot more RAM and CPU as before. I just can't.
     
  17. Offline

    mythbusterma

    @Betagear

    No, it wouldn't. Why do you think this is the case? It would take marginally more RAM. Don't optimise unless you can prove what you're doing is unoptimised.
     
  18. Offline

    Betagear

    With all the parameters that I use in my funtions, it would take a lot of power to stor it, because it run every 2 ticks and on a lot of blocks.
     
  19. Offline

    mythbusterma

    @Betagear

    I think you're going to need to improve your program if that's the case. Also, it doesn't matter how often it runs when we're talking about running them onDisable?
     
  20. Offline

    Betagear

    But I was wondering if there was a command like task.forceRun() or something like that to make the scheduled tasks to run. Anyways, I think i've got the solution, how do we use wait(long timeout) ?
     
  21. Offline

    teej107

    You can just call #run() directly if you have an instance of the (Bukkit)Runnable.
     
  22. Offline

    Betagear

    What do you mean ? The only thing I have is the BukkitTask.
     
  23. @Betagear
    When you schedule a task you have an instance of the runnable, just save that.
     
  24. Offline

    Betagear

    How can I do with
    Code:
                new BukkitRunnable() {
                   
                    @Override
                    public void run() {
                    }
                }.runTaskLater(plugin, delay);
    ?
     
  25.  
  26. Offline

    mythbusterma

    @Betagear

    Just store a reference to the runnable. It's really not that hard.
     
  27. Offline

    Betagear

    How ?
     
  28. Offline

    mythbusterma

    @Betagear

    A Collection of some sort. Alternatively, iterate through the return values from BukkitScheduler#getPendingTasks, and look for the ones that belong to your plugin.
     
  29. Offline

    Betagear

    I already did it on onDisable, but can't run them. Code :
    Code:
            for (BukkitTask task : Bukkit.getServer().getScheduler().getPendingTasks()) {
                if (task.getOwner() == plugin) {
                    task.notify();
                    }
            }
     
  30. Offline

    mythbusterma

    @Betagear

    1. Why are you comparing Objects using ==?
    2. BukkitTask#notify() does nothing.
     
Thread Status:
Not open for further replies.

Share This Page