Saving data to a YAML configuration

Now, we are ready to complete the save method. We want to save data to a YAML file, much like how we did in config.yml. However, we do not want to save it to config.yml, because that serves a different purpose. The first thing that we will need to do is create a new YAML configuration, as follows:

YamlConfiguration config = new YamlConfiguration();

Next, we will store all the information that we wish to save. This is done by setting objects to specific paths, as follows:

config.set(String path, Object value);

The acceptable types for value were mentioned earlier in this chapter. In the teleportation plugin, we have maps, which contain the SerializableLocation method. Maps can be added to a YAML configuration as long as they are a map of strings to an object that is ConfigurationSerializable. Hashmaps are added to a configuration in a different manner. You must create a configuration section using the map.

The following code shows how we will add the teleportation data to the configuration:

config.createSection("homes", homes);
config.createSection("warps", warps);

Once all the data is stored, all that is left to do is write the configuration to the save file. This is done by invoking the save method on config and passing the file that we wish to use. Calling the getDataFolder method of the plugin will give us the directory in which we should store all the plugin data. This is also where config.yml is located. We can use this directory to reference the file in which we will save the data, as follows:

File file = new File(plugin.getDataFolder(), "warps.yml");
config.save(file);

We will put all of these lines of code inside a try block to catch an exception that may occur. If you don't already know about exceptions, they are thrown when there is some sort of error or when something unexpected occurs. A try/catch block can be used to prevent the error from causing your plugin to crash. In this case, an exception is thrown if the specified file cannot be written for some reason. This reason may be that the user has insufficient privileges or the file location cannot be found. Therefore, the save method with the try block is as follows:

/**
 * Saves our HashMaps of warp locations so that they may be loaded
 */
private static void save() {
    try {
        //Create a new YAML configuration
        YamlConfiguration config = new YamlConfiguration();

        //Add each of our hashmaps to the config by creating sections
        config.createSection("homes", homes);
        config.createSection("warps", warps);

        //Write the configuration to our save file
        config.save(new File(plugin.getDataFolder(), "warps.yml"));
    } catch (Exception saveFailed) {
        plugin.getLogger().log(Level.SEVERE, "Save Failed!", saveFailed);
    }
}

The following is a sample warps.yml file that will be created using the Warper plugin:

homes:
  18d6a045-cd24-451b-8e2e-b3fe09df46d3:
    ==: WarperLocation
    pitch: 6.1500483
    world: 89fd34ff-2c01-4d47-91c4-fa5d1e9fdb81
    x: -446.45572804715306
    y: 64.0
    yaw: 273.74963
    z: 224.9827566893271
warps:
  spawn:
    ==: WarperLocation
    pitch: 9.450012
    world: 89fd34ff-2c01-4d47-91c4-fa5d1e9fdb81
    x: -162.47507312961542
    y: 69.0
    yaw: -1.8000238
    z: 259.70096111857805
  Jungle:
    ==: WarperLocation
    pitch: 7.500037
    world: 35dafe89-3451-4c27-a626-3464e3856428
    x: -223.87850735096316
    y: 74.0
    yaw: 87.60001
    z: 382.482006630207
  frozen_lake:
    ==: WarperLocation
    pitch: 16.200054
    world: 53e7fab9-5f95-4e25-99d1-adce40d5447c
    x: -339.3448071127722
    y: 63.0
    yaw: 332.84973
    z: 257.9509874720554
..................Content has been hidden....................

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