Config doesn't load new state.

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

  1. Hello,
    I'm creating a game with a config file.

    Now I just have:
    Code (Text):
    this.getConfig().get("general.tokens");
    It returns a message (I removed the sendMessage()) from the amount of the tokens in the config.

    But when I change it in the config to like 100, and I run this again I get still the previous amount of int. Only after a reload the new int will return in the message.

    How do I fix this?
     
  2. electronicboy

    IRC Staff

    you'd need to reload the config from the disk, the configuration is saved in memory until stored on the disk, should be a reloadConfig method exposed by JavaPlugin
     
    • Like Like x 1
  3. but when I do this.getConfig().set something
    and then run the command, it returns the new message.
     
  4. Yes, because the Object is stored in the memory as electronicboy already said.
    You have to differ between disk and memory. The config is saved on the disk and your plugin automatically loads the config into the memory.
    From there you can access the config with this.getConfig(). and do something with it. All changes would not automatically get saved, so you have to do this.saveConfig().

    Edit: I recommend to read this tutorial:
    https://www.spigotmc.org/wiki/config-files/
     
    • Like Like x 1
  5. WAS

    WAS

    Something like this in your Main should accomplish the saving and reloading. Just call it after you get done editing your config.

    Code (Java):
      private JavaPlugin plugin;

       // Your main class constructor
       public YourPluginMain() {
         this.plugin = this;
       }

      public void reloadConfiguration() {
         plugin.saveConfig();
         plugin.reloadConfig();
      }
    You can supply a getter for your plugin instance if you want too...

    Code (Java):
      private static JavaPlugin plugin;

       // Your main class constructor
       public YourPluginMain() {
         plugin = this;
       }

      public static JavaPlugin getPlugin() {
         return plugin;
       }

      public void reloadConfiguration() {
         plugin.saveConfig();
         plugin.reloadConfig();
      }
    Which you can use elsewhere like

    Code (Java):
    JavaPlugin plugin = YourPluginMain.getPlugin();
    // Do stuff to config
    plugin.reloadConfiguration();