1.8.8 how to save a variable through reload or restart

Discussion in 'Spigot Plugin Development' started by catssss, Apr 12, 2020.

  1. how do you save variables through reloads I know of PersistentDataContainer but that is for 1.15.2.
    I am running 1.8 not 1.8.8
     
  2. I think your best bet is writing to a yaml file, the Spigot API gives you some tools to do this.
     
    • Agree Agree x 2
  3. How do I make a .yml file that isn't config in fact how do I make a config file
     
  4. It would just be a config but I think admins would be smart enough not to edit a file like that.
    The link below will take you to the YamlConfiguration javadoc page. Definitely take a look there.
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/file/YamlConfiguration.html

    Maybe even more interesting, there is a full guide as well:
    https://www.spigotmc.org/wiki/config-files/
     
  5. I know if is possible to make a different .yml file because pex and groupmanager and even essentials makes more than just the config
     
  6. Yup, that's definitely possible. This is how I handle files in my BetterSleeping plugin. I make an instance of that class for each config file I need.
    EDIT #2: This is how I do it now, which is a big improvement.

    Edit: I also provide several configuration files to make configuration more straight forward.
     
    #6 vallas, Apr 12, 2020
    Last edited: Aug 17, 2020
  7. Ok thank will look into it more in the morning
     
  8. No problem, and good luck!
     
  9. what does this in your code do?

    Code (Java):
    Reader defaultStream = null;
            try {
                defaultStream = new InputStreamReader(plugin.getResource(fileName), "UTF8");
                if (defaultStream != null) {
                    this.defaultConfig = YamlConfiguration.loadConfiguration(defaultStream);
                }
            } catch (Exception ex) {}
     
  10. That piece of code gets the default configuration from within the jar file, to later find missing options in the server's config. That class is far from perfect by the way, take what you want from it but you can do better than that old class:p

    The reason:
    I always missed an option in the Spigot API that a missing option wasn't automatically set to its provided default (meaning that it exists in the default config you provide, but not on the server's config because they downloaded a new version of your plugin with new options or deleted something). Though I must admit, Spigot shouldn't handle this, but basically I needed a configuration with all default values to automatically fill in the missing options or report them to the console. The plugin would still work, but would use the default settings for the missing ones.