Loading a .yml file into memory

Discussion in 'Spigot Plugin Development' started by kinukin, Jun 4, 2017.

  1. How can I load a .yml file into memory?

  2. You can work with HashMaps, HashSets, Set's List's to save it to memory
  3. If you create a new YamlConfiguration object it'll be in memory by default till you do save()
  4. Variables are saved into the memory of the plugin until a reload, restart or they are overwritten with a new entry.

    I setup a class for the .yml file and set each variable in the file to a variable in the class.

    In my example below, the variables in the config file are also the same as the class file.

    Code (Text):
    public static boolean pluginEnabled;
    public static int maximumNumber;
    public static World world;

    public static void setVariables() {
        File configFile = new File(YourPlugin.instance.getDataFolder(), "config.yml");
        FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);

        pluginEnabled = config.getBoolean("pluginEnabled");
        maximumNumber = config.getInt("maximumNumber");
        world = Bukkit.getWorld(config.getString("world"));
  5. Please don't static abuse...
    • Funny Funny x 1
  6. This is fine for this example if those variables are not going to be changed.
  7. not fine it could be a problem you can static or getInstance to that class not the process it may not create the file at some instance.
    • Agree Agree x 1