Loading data from a YAML configuration

Now that the save method is complete, we are ready to write the load method. You are already familiar with loading data using the Bukkit configuration API. What we'll do now is similar to retrieving values from config.yml, as discussed in the previous chapter. However, we must first manually load the configuration using the following code, which will be different. We should only do this if the file actually exists. The file will not exist the first time the plugin is used. Therefore, we do not want an error to occur in that situation:

File file = new File(plugin.getDataFolder(), "warps.yml");
if (file.exists()) {
    YamlConfiguration config = new YamlConfiguration();
    config.load(file);

Now that we have the YAML configuration loaded, we can get values from it. The data has been placed into two unique configuration sections. We will loop through each key of both the sections in order to load all the locations. To get a specific object from a section, all that we need to do is call the get method and cast it to the correct object. You can see how this is done in the completed load method:

/**
 * Loads warp names/locations from warps.yml
 * 'warp' refers to both homes and public warps
 */
private static void load() {
    try {
        //Ensure that the file exists before attempting to load it
        File file = new File(plugin.getDataFolder(), "warps.yml");
        if (file.exists()) {
            //Load the file as a YAML Configuration
            YamlConfiguration config = new YamlConfiguration();
            config.load(file);

            //Get the homes section which is our saved hash map
            //Each key is the uuid of the Player
            //Each value is the location of their home
            ConfigurationSection section = config.getConfigurationSection("homes");
            for (String key: section.getKeys(false)) {
                //Get the location for each key
                SerializableLocation loc = (SerializableLocation)section.get(key);
                //Only add the warp location if it is valid
                if (loc.getLocation() != null) {
                    homes.put(key, loc);
                }
            }

            //Get the warps section which is our saved hash map
            //Each key is the name of the warp
            //Each value is the warp location
            section = config.getConfigurationSection("warps");
            for (String key: section.getKeys(false)) {
                //Get the location for each key
                SerializableLocation loc = (SerializableLocation) section.get(key);
                //Only add the warp location if it is valid
                if (loc.getLocation() != null) {
                    warps.put(key, loc);
                }
            }
        }
    } catch (Exception loadFailed) {
        plugin.getLogger().log(Level.SEVERE, "Load Failed!",loadFailed);
    }
}

Now that the plugin is complete, you can test it on your server. Set a home location as well as some warp locations and then view the save file. Stop and then start the server again to verify that the plugin does indeed load the correct data.

..................Content has been hidden....................

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