Executing code asynchronously
We can improve the Warper
plugin by writing its data to a file asynchronously. This will help keep the main thread of the server running smoothly with no lag.
Take a look at the current save
method. We will add the data to a YamlConfiguration
file and then write the configuration to the file. This entire method cannot be run asynchronously. Adding the data to the configuration must be done synchronously to ensure that it is not modified while it is being added. However, the save
method call on the configuration can be called asynchronously. We will place the entire try/catch
block within a new BukkitRunnable class
. We will then run it asynchronously as a task. This task will be stored as a static variable in the Warper
class. This is shown in the following code:
BukkitRunnable saveRunnable = new BukkitRunnable() { @Override public void run() { try { //Write the configuration to our save file config.save(new File(plugin.getDataFolder(), "warps...