Is there a scheduleSyncRepeatingTask alternative

Discussion in 'Plugin Development' started by CoderJim, Mar 28, 2015.

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

    CoderJim

    Hi there,
    I am making a plugin and in the main class i am calling an runnable in the onEnable, well this was until i realised that scheduleSyncRepeatingTask is deprecated. I know i could use it but i would prefer not to use deprecated methods. So the question i am asking is is there an alternative to scheduleSyncRepeatingTask
    Thanks in advance
    CoderJim

    Here is my current code with only the affected parts:
    Code:
    public class Main extends JavaPlugin {
       
        @Override
        public void onEnable() {
            registerTimers();
        }
        private void registerTimers(){
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new ClassName(), 0L, 100L);
        }
    }
     
  2. @CoderJim Make a class extend Bukkit Runnable.
     
  3. Offline

    CoderJim

    @bwfcwalshy I have already made a class that extends BukkitRunnable but that still doesn't change the fact that the ScheduleSyncRepeatingTask is deprecated :(
     
  4. Offline

    timtower Administrator Administrator Moderator

    1Rogue likes this.
  5. Offline

    CoderJim

    @timtower hmm i tried this
    Code:
    Bukkit.getScheduler().runTaskTimer(this, new Bc(), 0L, 100L);
    
    But that method is deprecated as well,
     
  6. new BukkitRunnable() {
    public void run(){
    //Code
    }
    }.runTaskTimer(plugin, 0, 20);
     
  7. Offline

    MexMaster

    Or use Bukkit.getScheduler().runTaskLater(plugin, runnable, 10L);
    (Bukkit.getScheduler().runTaskLaterAsynchronously(...);)
     
  8. He wants a repeatingTask, not a delayedTask
     
  9. Offline

    CoderJim

    @MexMaster Thanks for the suggestion but that would not work for me because it is for a broadcaster which needs a repeating task :)
     
  10. Offline

    MexMaster

    I used delayed tasks like this:

    private void execute(){
    //Do whatever here
    Bukkit.getScheduler().runTaskLater(plugin, new Runnable(){
    execute();
    }, interval);
    }

    Because then you can change the interval on every run. Let's say we have a broadcaster. And you have 3 messages that are broadcasted at different time intervals. So you can calculate the time to the next broadcast and just schedule this time. But if you just need a simple repeating task this would be too complicated.
    (The code looks somehow messy because I'm only on my phone, sorry)
     
  11. Offline

    CoderJim

    @FisheyLP How would i register this in main class because i want it to be registered in the main class and the code be in a seperate class eg..

    In the main class:
    Code:
        public void onEnable() {
            registerTimers();
        }
        private void registerTimers(){
            Bukkit.getServer().getScheduler().runTaskTimer(this, new Bc(), 0L, 100L);
        }
    }
    And in a seperate class:
    Code:
    public class Bc extends BukkitRunnable {
    
        @Override
        public void run() {
            // Method here!!
        }
    }
    Because i have tried this and the tasktimer is deprecated :(
     
  12. Offline

    1Rogue

    The scheduler deprecates any calls when you pass a BukkitRunnable. Pass a normal runnable or use BukkitRunnable's methods. You should always use .runTask* methods, not .schedule* methods
     
    Last edited: Mar 28, 2015
  13. Offline

    BTTFHamsterZK

    Have you tried using scheduleAsyncRepeatingTask?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @BTTFHamsterZK There are enough things that you don't want to do async though
     
    BTTFHamsterZK likes this.
  15. Offline

    BTTFHamsterZK

    I have no problems using Async, but that's my personal opinion.
     
  16. Offline

    dlange

  17. Offline

    BTTFHamsterZK

    I guess, That explains that, because I'm using the Async in the core class. ;-;
     
  18. Offline

    1Rogue

    Just because you don't see errors in console, doesn't mean there aren't problems.
     
  19. Offline

    CoderJim

    @1Rogue Thanks got rid of the deprecation now, just one more question
    What method do you think is better/ use more often
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new ClassName(), 0L, 100L);
    
    Or
    Code:
    Bukkit.getServer().getScheduler().runTaskTimer(this, new ClassName(), 0L, 100L);
    
     
  20. Offline

    1Rogue

    Doesn't matter, #scheduleSyncRepeatingTask is deprecated and just directly calls #runTaskTimer:

    Code:java
    1. public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) {
    2. return runTaskTimer(plugin, runnable, delay, period).getTaskId();
    3. }
     
  21. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page