A task can be run either synchronously or asynchronously. Simply put, when a synchronous task is executed, it must be completed before the server can continue running normally. An asynchronous task can run in the background while the server continues to function. If a task accesses the Bukkit API in any way, then it should be run synchronously. For this reason, you will rarely run a task asynchronously. An advantage that an asynchronous task gives is that it can be completed without causing your server to lag. For example, writing data to a save file can be done asynchronously. Later in this chapter, we will modify the Warper
plugin to save its data asynchronously. As for the AlwaysDay
plugin, we must run the task synchronously because it accesses the Minecraft server.
Calling runTask
or runTaskAsynchronously
on a BukkitRunnable
class will cause the task to run immediately. The only time you are likely to use this is to run a synchronous task from an asynchronous context or vice versa.
Calling runTaskLater
or runTaskLaterAsynchronously
on a BukkitRunnable
class will delay the task from being executed for a specific amount of time. The amount of time is measured in ticks. Remember that there are 20 ticks in every second. In the Warper
plugin, we will add a warp delay so that the player is teleported 5 seconds after running the warp command. We will accomplish this by running the task later.
Calling runTaskTimer
or runTaskTimerAsynchronously
on a BukkitRunnable
class will repeat the task every specified number of ticks. The task will repeat until it is canceled. Task timers can also be delayed to offset the initial run of the task. A task timer can be used to periodically save data, but for now, we will use this type of repeating task to complete the AlwaysDay
plugin.
18.226.251.68