Solved Reloading server adds hidden values from default config to edited config

Discussion in 'Spigot Plugin Development' started by xUltraGaming, Mar 26, 2020.

  1. Hi everyone, in my plugin's onenable, I call a method that chooses which config to use by getting if the config is already existing or not. If the config exists, it uses the existing config, but if it doesn't, it makes a new one.

    Code (Java):
    File file = new File(this.getDataFolder() + "/config.yml");
            if (file.exists()) {
                this.getLogger().info("Using the existing config.yml!");
            } else {
                try {
                    this.getLogger().info("Creating a new config.yml!");
                    file.createNewFile();
                    this.getConfig().options().copyDefaults(true);
                    this.saveConfig();
                } catch (IOException e) {
                 
                }
            }

    When I load the server with the plugin installed for the first time, everything works and a new config is created. Also, when I load the server after the first time with the plugin installed, it uses the existing config.

    Here is the default config:
    Code (YAML):
    # Note: To avoid errors, follow the syntax provided in the config.
    # To learn how to use the config, read the documentation on the
    # resource page.

    messages
    :
      send-update-message-to-ops
    : true
      use-error-prefix
    : true
      kit-claimed
    : '&7Successfully claimed kit &b{kit}&7!'
      kit-does-not-exist
    : '&cThat kit does not exist!'
      no-items-defined-ops
    : '&cThere are no items defined for that kit in the config!'
      no-items-defined-player
    : '&cThis kit is currently not available. Ask a staff member for support!'
      player-not-found
    : '&cPlayer &4{player} &cnot found!'
      player-given-kit-from-op
    : '&7You have been given kit &b{kit}&7!'
      no-kits-in-gui
    : '&cThere are no kits in the GUI!'
      invalid-item-config-format-ops
    : '&cInvalid item config format. Refer to the plugin documentation for details!'
      invalid-item-config-format-players
    : '&cThis kit is currently not available. Ask a staff member for support!'
      insufficient-permission
    : '&cYou do not have permission to do this!'
      insufficient-permission-ops-only
    : '&cOnly OPs can do this!'
      op-given-kit-to-player
    : '&7Given kit {kit} to &a{player}&7!'
      console-given-kit-to-player
    : 'Given kit {kit} to {player}!'
      kit-cooldown-active
    : '&cYou cannot use this kit for another {time}!'
      unknown-error-ops
    : '&cUnknown error. Contact the plugin developer for support!'
      unknown-error-player
    : '&cUnknown error. Ask a staff member for help!'
      kit-sent-to-enderchest
    : '&bYour inventory is full so the kit was sent to your ender chest!'
      invalid-material
    : '&cCould not add item "{item}" to the kit! Reason: INVALID_MATERIAL'
     

    kits
    :
      test
    : # -> Name of the kit.
        items
    :
       # <material> <amount> <durability> <name> <enchants[enchant:level]>
        # This item is a diamond sword with the name &aTest_&7Sword and it has Sharpness 5 and Unbreaking 3
        # Names can use "&" color code symbols
        # To add spaces in the name of the item, use "_"
     
          - diamond_sword 1 0 &aTest_&7Sword sharpness:5,unbreaking:3
          - cooked_beef 1 0
       
          # Some items have custom formats
          # Potions:
          # <material> <potion type> <extended> <upgraded>
          # Leather Armor:
          #    For Solid Color:
          #       <material> <amount> <durability> <color> <name> <enchants[enchant:level]>
          #    For RGB Color:
          #       <material> <amount> <durability> <red:green:blue> <name> <enchants[enchant:level]>
          #       Note: red, green and blue must be replaced with numbers.
          # Note: If extended and upgraded are both set to true, the potion will only be extended since
          # potions cannot be both extended and upgraded. Also, the only acceptable material types for potions
          # are "potion", "splash_potion", and "lingering_potion". Otherwise, the potion will be a regular awkward potion.
       
          - potion instant_healing false true
     
        delay
    : 43200 # -> This is the cooldown for the kit in seconds.
        gui
    :
          displayname
    : '&aTest &7Kit' # -> Use this to set the display name for the kit's GUI display item.
          # Note: You don't need to use "_" to create spaces in the name.
          material
    : chest # -> Use this to set the material for the kit's GUI display item.
          lore
    : # -> Use this to set the lore for the kit's GUI display item.
          - '&bClick me to claim'
          - '&ba &aTest &bKit!'

      # Here is another example.
     
      starter
    :
        items
    :
       - stone_pickaxe 1 0 &cStarter_&7Pickaxe
        - stone_sword 1 0 &cStarter_&7Sword
        - stone_axe 1 0 &cStone_&7Axe
        - cooked_beef 16 0
        - torch 32 0
        - cobblestone 32 0
        - oak_log 16 0
        - potion instant_healing true false
     
        cooldown
    : 7200
        gui
    :
          displayname
    : '&cStarter &7Kit'
          material
    : chest
          lore
    :
         - '&7Useful tools for'
          - '&7new players!'
     

    gui
    :
      displayname
    : '&0Select a Kit!' # -> Use this to change the title of the GUI.

    Everything loads properly, but when I delete the configuration sections "kits.starter" and "kits.test" then save the config and reload the server, these configuration sections aren't shown in the config but I can still get their kits even though every kit is entirely config-based. If anyone knows what I'm doing wrong, I would greatly appreciate the help. Thanks!

    EDIT:

    I forgot to mention that when I am making these changes to the config, I am MANUALLY editing the config via an editor (ex: Notepad++).

    This thread was the only thread that I found helpful in this situation.
    I decided to call the method reloadEditedConfig() in the ondisable.


    Code (Java):
    @Override
        public void onDisable() {
            reloadEditedConfig();
            String name = this.getName();
            String version = this.getDescription().getVersion();
            this.getLogger().info(name + " version " + version + " has been disabled!");
        }
     
    public void reloadEditedConfig() {
            FileConfiguration config;
            File cfile = new File(this.getDataFolder() + "/config.yml");
            config = YamlConfiguration.loadConfiguration(cfile);
            try {
                config.save(cfile);
            } catch (IOException e) {
                this.getLogger().info("Could not reload the config! Reason: " + DeveloperErrorType.IO_EXCEPTION);
                e.printStackTrace();
            }
       }
    However, when I MANUALLY edit a value in the config.yml, save the config.yml via the editor and reload the server, the values are still there but hidden.
     
    #1 xUltraGaming, Mar 26, 2020
    Last edited: Mar 26, 2020
  2. BUMP - Please check the edit on the post above :)
     
  3. I suggest just creating a resource named 'config.yml' and then calling the method 'JavaPlugin#saveDefaultConfig'. This overrides non-existing values and keeps existing ones. Then you shouldn't get in any problems with that.
     
    • Like Like x 1
  4. How exactly would this work and how would I have the two config.yml's link and update each others data? I'm open to suggestions but I don't think I need another resource because people in the thread I previously linked have claimed that it works for them.
     
  5. Well, if you create a file named 'config.yml' with the default values inside your resources directory, you can just use the method 'JavaPlugin#saveDefaultConfig' to save it. You do not have to check if it already exists or not, because Spigot does that for you. It also updates only values inside the config, which are defined in the resource file, but not in the config itself. I don't see any sense in making things so difficult like the code you shared above.

    For example you can look at one of my plugins:
    This is where and how the config is saved
    And this is how I initialize it
     
    • Like Like x 1
  6. In my project's resource directory I already have a config.yml.
    The only difference from what you said is that I use JavaPlugin#getConfig()#options()#copyDefaults(true) in the onenable instead of the saveDefaultConfig.

    I want to be able to edit values MANUALLY using Notepad++ in the config.yml located in the plugin's data folder (/plugins/projectname/config.yml). Would JavaPlugin#getConfig()#options()#copyDefaults(true) or JavaPlugin#saveDefaultConfig be better for doing this?
     
  7. As I mentioned above, saveDefaultConfig will not override any changed data. It only adds undeclared values, if they are definded in your resource.
     
    • Like Like x 1
  8. Just tried using saveDefaultConfig() on the onenable after I deleted the exisiting config.yml from the data folder.
    Code (Java):
    File file = new File(this.getDataFolder() + "/config.yml");
            if (file.exists()) {
                this.getLogger().info("Using the existing config.yml!");
            } else {
                try {
                    this.getLogger().info("Creating a new config.yml!");
                    file.createNewFile();
                    this.saveDefaultConfig();
                } catch (IOException e) {
                   
                }
            }
    ^ this creates a new config.yml, but the values from the resource config are not written. However, the config still appears to have loaded in-game.

    After this, I tried saveDefaultConfig() and then saveConfig().
    Code (Java):
    File file = new File(this.getDataFolder() + "/config.yml");
            if (file.exists()) {
                this.getLogger().info("Using the existing config.yml!");
            } else {
                try {
                    this.getLogger().info("Creating a new config.yml!");
                    file.createNewFile();
                    this.saveDefaultConfig();
                    saveConfig();
                } catch (IOException e) {
                   
                }
            }
    Code (YAML):
    # Note: To avoid errors, follow the syntax provided in the config.
    # To learn how to use the config, read the documentation on the
    # resource page.

     
    ^ this creates a new config.yml, but only the header of the resource config is written. However, the config still appears to have loaded in-game.
     
  9. You do not need to check if the config exists. Just drop the whole code and just use the bare saveDefaultConfig() method.
     
    • Like Like x 1
  10. This works, but when I make a change to the config.yml in the data folder (I deleted two configuration sections: kits.starter and kits.test which are both in the resource config.yml) and I reload the server, making sure I call reloadConfig() in my ondisable, the kits still exist but they are not in the datafolder's config.yml. Do I have to delete them somehow in the resource config.yml and if so, how would I go about doing that?
     
  11. The default behavior in this case would be that the config just reloads if you reload the plugin. The method I use in the linked plugin works fine for me, so it has to be something with your code.
     
    • Like Like x 1
  12. I use JavaPlugin#getConfig() to set and grab most of my values, including those two configuration sections that I talked about.
    Is there a chance that it's grabbing the deleted configuration sections from the resource folder's config.yml?

    EDIT: I didn't find what was causing everything to happen, so I decided to make a new config file in the plugin's data folder called kits.yml and I updated all of the methods accordingly. Everything works as intended now. I'm going to mark the thread as solved. Thanks so much for the help! :D
     
    #12 xUltraGaming, Mar 27, 2020
    Last edited: Mar 27, 2020
    • Like Like x 1