Solved Config Path Names Changing

Discussion in 'Spigot Plugin Development' started by AeriusMC, Mar 15, 2017.

  1. I've made a TestPlugin to get back in shape for spigot development again and to refresh my memory in configuration. So I created a class that has commands to save, reload, and edit the config. However, the 'test editConfig' command is suppose to set a new value of an existing path into "stringtest" but when it does that it also inverted the names of the existing paths to 'true' and 'false' somehow.

    I double checked my code already and looked for somewhere where I could have set my path names to 'test.true' and 'test.false' but there is no place where I have done that. I only have one class too so it can't be another class interfering with the commands.

    Here is the default config file that is embedded in the jar:
    Code (Text):
    test:
      on: stringgenerated
      off: test
    Here is the specific code causing the path names to be changed:
    Code (Text):
                    //Edits value in config -- this is used first
                    case "editConfig":
                    {
                        System.out.println("BEFORE: " + this.getConfig().get("test.on"));
                        this.getConfig().set("test.on", "stringtest");
                        System.out.println("AFTER: " + this.getConfig().get("test.on"));
                        System.out.println("Check your disk now (file) and see if it is edited (hypo. : probably not");
                        return true;
                    }
    Here is the result of the config file using that command and saving the config with the 'test save' command (as you can see somehow my original paths become 'true' and 'false' without me setting anything:
    Code (Text):
    test:
      'true': stringgenerated
      'false': test
      'on': stringtest
     
    Here is the full code incase you want to triple check that I didn't do anything wrong (I only have 1 class too):
    Code (Text):
    package jy.mc.testplugin.main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.List;

    public class TestPlugin extends JavaPlugin implements CommandExecutor
    {
        @Override
        public void onEnable()
        {
            saveDefaultConfig();
            getConfig().options().copyDefaults(true);

            System.out.println("BEFORE: " + this.getConfig().get("test.on"));
            System.out.println("BEFORE: " + this.getConfig().get("test.off"));

            getCommand("test").setExecutor(this);
        }

        @Override
        public void onDisable()
        {

        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[])
        {
            if(args.length == 1)
            {
                switch(args[0])
                {
                    case "reload":
                    {
                        this.reloadConfig();
                        System.out.println("Takes what is in the config and can now be used on memory!");
                        return true;
                    }
                    //This is used second
                    case "save":
                    {
                        this.saveConfig();
                        System.out.println("Take what is on the memory and puts it into disk (the file)");
                        return true;
                    }
                    //Edits value in config -- this is used first
                    case "editConfig":
                    {
                        System.out.println("BEFORE: " + this.getConfig().get("test.on"));
                        this.getConfig().set("test.on", "stringtest");
                        System.out.println("AFTER: " + this.getConfig().get("test.on"));
                        System.out.println("Check your disk now (file) and see if it is edited (hypo. : probably not");
                        return true;
                    }
                    case "printout":
                    {
                        System.out.println("test.on value: " + this.getConfig().get("test.on"));
                    }
                    default:
                    {
                        System.out.println("Make sure you don't have any uppercase letters in your commands");
                        return true;
                    }

                }
            }

            return false;
        }


    }
     
     
  2. While I have no idea regarding your issue, I don't think you need "getConfig().options().copyDefaults(true);", the default values are copied into the file, that's the point of saveDefaultConfig.
     
  3. I happen to have noticed a similar problem earlier regarding a user of one of my resources, where they used "yes" and "no" without quotes as keys. It turns out that SnakeYAML reads those, like on and off, as booleans so the keys will turn into "true" and "false" which are the string representations of the booleans (in Java). In the config, you want to surround the keys with single quotation marks, so:
    Code (YAML):
    test:
      'on'
    : ...
      'off'
    : ...
     
    • Useful Useful x 2
  4. Oh yes, I almost forgot to mention in my quote that surrounding the paths with keys works...

    And huh, I didn't know that, I'll try testing it out. Man... if you hadn't posted I probably would have been at this for days, thanks!
     
  5. I think copyDefaults does more than just that, I forgot what else it does but I will be testing it out later, but here's the docs link:
    https://hub.spigotmc.org/javadocs/s...nfigurationOptions.html#copyDefaults(boolean)

    "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. As a result,ConfigurationSection.contains(java.lang.String)will always return the same value as ConfigurationSection.isSet(java.lang.String). The default value is false."
     
  6. As far as I know copyDefaults has to be used when you add the default values using the addDefault method.
    I am not using the copyDefaults method and everything works fine (I have a config.yml in the the jar).
     
  7. I'll test it out later, I remember using it for something I can't remember for.
     

Share This Page