Writing a repeating task for a plugin
We already have a BukkitRunnable
class. Therefore, in order to run a task timer, we just need to determine the delay and the period by which the task is delayed. We want the delay to be 0. That way, if it is night when the plugin is enabled, the time will be set to noon right away. As for the period, we can repeat the task every second if we want to keep the sun always directly above. The task only contains one simple line of code. Repeating it often will not cause the server to lag. However, repeating the task every minute will still prevent the world from ever growing dark and will be less of a strain on the computer. Therefore, we will delay the task by 0 ticks and repeat it every 1,200 ticks. This results in the following line of code:
runnable.runTaskTimer(this, 0, 1200);
With this, we started a repeating task. It is good practice to cancel repeating tasks when the plugin is disabled. To accomplish this, we will store the BukkitTask
as a class variable...