Using Bukkit Schedulers

Discussion in 'Spigot Plugin Development' started by RiotShielder, May 2, 2015.

  1. Hey all,

    I've been having a bit of trouble wrapping my head around Schedulers.

    From my understanding, you can define work and schedule work. What throws me off is the BukkitScheduler section of the wiki. Doesn't that do the same thing as the above, but with less code? Or does it just depend on what you want to do with it?

    Lastly, should I use sync or async if I want to send a message to players in chat every so often?

    Thanks, I appreciate any help!
  2. Chat is the special case with sync vs async tasks as chat is async. So yes for chat use an async task. Any other time you call a method from the bukkit/spigot api it should be synchronous.

    Basically there are two types of tasks. Delayed and repeating.
    1. A delayed is when you tell the scheduler to run the run () method in the Runnable you give it x ticks later.
    2. A repeating task is also a delayed task in that you can tell the scheduler to dun it x ticks later. But in addition after it is run that first time it will run again however many ticks later you put in the period.

    Also try to use the runTaskLater and runTaskTimer method over the scheduleRepeating etc as those method will return a BukkitTask that you can directly cancel () rather that an int that you need to ask the scheduler to find and cancel the task with that id. Just in general its nicer to have the BulkitTask to work with over an int :p
  3. There are delayed and repeating schedulars as MrBlobman said. Delayed will run once after a certain amount of ticks (20 ticks = 1 second). Repeating with have a delay at first, and then keep running every time at that delay, until cancelled. Sync and Async took me a while to understand, and I still don't completely get it, but I believe that 'sync' is syncronized, which means it runs off the main Bukkit thread. So if you are calling Bukkit methods, use a sync schedular. Asyncronous has it's own thread, so it won't lag the server. So if you're connecting to a MySQL database, run an Async task, so it won't lag the server.

    Hope I helped :)
    Please someone correct me if I am wrong.
    • Like Like x 2
    • Creative Creative x 1
  4. What MrBlobman and BetaNyan said are both correct. You can use a delayed task if you want to do something 'x' ticks after you call it. A repeating task can also have a delay but once called, it will run again and again every 'x' number of ticks. As for the sync vs async, chat is on async as it won't lag the server. I was able to notice this difference once how when the actual server crashed, people will still able to chat as the chat and the server processes are not run on the same thread.