Saving config

Discussion in 'Spigot Plugin Development' started by dexelOK, May 4, 2017.

  1. When I read some data from config, it saves to default. So I can't change anything in my configuration file. Here is my config:
    Code (Text):
      - '1:3:5'
      - '2:4:6'
      anotherInteger: 10
      anotherString: '&c&lTest ;)'
      anotherBoolean: true
    And here is my code for reading anotherInteger value:
    Code (Text):
    static void initializeAnotherInteger() {
            try {
                anotherInteger = Integer.parseInt(Main.plugin.getConfig().getString("config.anotherInteger"));
            catch (Exception e) {
                Bukkit.getLogger().info("Something went wrong");
    Why when I change the value and just read it (without saving the previous value), anotherInteger always changes to 10?

    P.S. 10 is default value.
  2. How are you loading your config on startup?
  3. Code (Text):
    public static void load() {
            try {
                // Other code
            catch (Exception e) {
                Main.plugin.getLogger().warning("Couldn't load data.");
    I call load() method. Here I load some values and anotherInteger too.
  4. Wait, this seems pointlessly overcomplicated. Why don't you just use something like this?
    Code (Text):
    int i = Main.plugin().getConfig().getInt(String path);
  5. @dexelOK By loading it, I ment like in your OnEnable function and then the function you used
  6. Code (Text):
        public void onEnable() {
            try {
                if (!getDataFolder().exists()) {
                File file = new File(getDataFolder(), "config.yml");
                if (!file.exists()) {
                    getLogger().info("Creating config file...");
                } else {
                    getLogger().info("Successfully found config! Loading...");
            } catch (Exception e) {

            getServer().getPluginManager().registerEvents(new Listener1(), this);
            getServer().getPluginManager().registerEvents(new Listener2(), this);
            wg = getWorldGuard();
            plugin = this;
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    getLogger().info("Saving data automatically...");
            }, 0, 12000);
  7. Look, it seems that once you register the Config onEnable, you set it like this:
    Code (Java):
    Change it to:
    Code (Java):
    or Maybe you did, saveDefaultConfig();
    or SaveConfig();
    Those are things that you need to take care of once you code :D
    • Agree Agree x 1
  8. Hey, how you are saving something that is not there?
    Code (Java):
    if (!file.exists()) {
                    getLogger().info("Creating config file...");
    Remove saveDefaultConfig();
    • Like Like x 1
    • Optimistic Optimistic x 1
  9. And what to write instead of?..
  10. that saves the default every time the plugin loads, that's not something you want if you're wanting to change it
  11. I think it will work only if config does not exist.
  12. Theme is still unclosed.
  13. I will try to give you better code, I hope that it is not considered as a spoon-feeding:
    1. Use methods rather than writing everything in onEnable Method.
    - Make registerConfig Method
    - Make getConfig Method
    - Make saveConfig Method
    2. This makes life easier!
  14. There are 3 config files
    The resource: Also called "Default Config", Located in your plugin's internal resource folder
    The config: Stored in memory after you load it with getConfig();
    The config file: Located in the plugins external root directory if you created it from the resource

    getConfig() will load your config file from your plugins external root directory into memory if it exists.
    reloadConfig(); obvious
    saveConfig(); saves any changes which were made to the config in memory, to config.yml in your plugins external root directory

    saveDefaultConfig(); saves the RAW contents of the default config located in your plugins intenal resource directory to config.yml in your plugins external root directory but only if it does not already exist. Otherwise fails silently

    getConfig.options.copyDefualts(true); If this is true, all values in the default Configuration will be directly copied, making it impossible to distinguish between values that were set and values that are provided by default.

    saveResource saves the RAW contents of ANY resource located in your plugins internal resource directory to specified .yml file in your plugins external root directory. This one is useful if you want custom .yml files

    Also, this link comes in handy sometimes:
    #14 Stuperfied, May 5, 2017
    Last edited: May 5, 2017
    • Like Like x 1
  15. Thank you VERY MUCH for such a good information, but can you just tell me, how to read from config without returning to default value?
  16. Theme is still unclosed.
  17. if you want, I can send you the code I use to load my config
  18. I don't think spoon feed is the choice to take!
  19. yeah, i know. I just want to make sure he knows how to load the yml correctly