Chapter 10. The Bukkit Scheduler

The Bukkit scheduler is a very powerful tool, and it is easy to learn how to use it. It allows you to create repetitive tasks such as saving data. It also allows you to delay how long until a block of code is executed. The Bukkit scheduler can also be used to asynchronously compute lengthy tasks. This means that a task such as writing data to a file or downloading a file to the server can be scheduled to run on a separate thread to prevent the main thread, and thus the game, from lagging. In this chapter, you will learn how to do each of these by continuing to work on the Warper teleportation plugin, as well as creating a new plugin called AlwaysDay. This new plugin will ensure that it is always daytime on the server by repeatedly setting the time to noon. This chapter will cover the following topics:

  • Creating a BukkitRunnable class
  • Understanding synchronous and asynchronous tasks and when they should be used
  • Running a task from a BukkitRunnable class
  • Scheduling a delayed task from a BukkitRunnable class
  • Scheduling a repeating task from a BukkitRunnable class
  • Writing a plugin called AlwaysDay that uses a repeating task
  • Adding a delayed task to the Warper plugin
  • Asynchronously executing code

Creating a BukkitRunnable class

We will start by creating the AlwaysDay plugin. The code that we will write for this plugin will be put inside the onEnable method. The first step to creating a scheduled task is to create a BukkitRunnable class. This class will comprise very few lines of code. Therefore, it is not necessary to create a whole new Java file for it. For this reason, we will create a class within the onEnable method. This can be done using the following line of code:

BukkitRunnable runnable = new BukkitRunnable();

Normally, this code would be valid since you are constructing a new instance of a class. However, BukkitRunnable is an abstract class, which means that it cannot be instantiated. The purpose of an abstract class is to provide some base code that other classes can extend and build on top of. An example of this is the JavaPlugin class. For each plugin that you created, you started with a class that extends JavaPlugin. This allows you to override methods, such as onEnable, while keeping the current code of other methods, such as getConfig. This is similar to implementing an Interface, such as Listener. The difference between an abstract class and an interface is its purpose. As mentioned earlier, an abstract class is a base for other classes to extend. An interface is more of an outline within which classes can implement. Interfaces do not include code within any of their methods and therefore, all methods within an interface must have an implementation. For an abstract class, only the methods defined as abstract must be overridden because they do not include code within them. Therefore, because BukkitRunnable is an abstract class, you will be given a warning that will ask you to implement all the abstract methods. NetBeans can automatically add the needed methods for you. The new method that is added for you is run. This method will be called when the scheduler runs your task. For the new AlwaysDay plugin, we want the task to set each world's time to noon, as follows:

BukkitRunnable runnable = new BukkitRunnable() {
  @Override
  public void run() {
    for (World world : Bukkit.getWorlds()) {
      //Set the time to noon
      world.setTime(6000);
    }
  }
};

Note

Remember that time on a Minecraft server is measured in ticks. 20 ticks is equivalent to 1 second. The measurement of ticks is as follows:

0 ticks: Dawn

6,000 ticks: Noon

12,000 ticks: Dusk

18,000 ticks: Midnight

Take a look at the API documentation for the BukkitRunnable class at https://hub.spigotmc.org/javadocs/spigot/org/bukkit/scheduler/BukkitRunnable.html. Note that there are six ways to run this task, which are as follows:

  • runTask
  • runTaskAsynchronously
  • runTaskLater
  • runTaskLaterAsynchronously
  • runTaskTimer
  • runTaskTimerAsynchronously
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.20.56