Changing config.yml with command

Discussion in 'Spigot Plugin Development' started by arsneg, Jul 10, 2015.

  1. Hello, I've some trouble when I try to change config.yml with in game command. Plugin load on server properly, no warnings and errors and in /pl output it's green. When I use command it give me proper output in chat ("Test Message"), but config.yml doesn't change. I guess something with saveConfig(); but I don't know what.

    Code (Text):

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class TestPlugin extends JavaPlugin {
        FileConfiguration config;
        @Override
        public void onEnable() {
            config = getConfig();
            saveConfig();
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("test")) {
                config.set("SomeNode", "SomeValue");
                sender.sendMessage("Test Message");
                return true;
            }
            return false;
        }
    }

     
    Here is plugin.yml
    Code (Text):

    name: TestPlugin
    main: com.arsneg.testplugin.TestPlugin
    version: 1.0
    commands:
        test:
            description: Description
            usage: /<command>
     
    Please, help me with it.
     
  2. JustThiemo

    JustThiemo Previously TrinityDEV

    Put saveConfig(); after you set the config variables, should work. And a tip, add @Override above your command boolean, and change commandLabel.equalsIgnoreCase to cmd.getName("command"), its better to use if you're going to use command aliases
     
  3. Or save it in the onDisable() function in the end.
     
  4. Thank you, it work! But I still have one questions, should it be a problem if I'll use saveConfig() a few times in my code?
    Thanks you too, I get how to use onDisable now.
     
  5. JustThiemo

    JustThiemo Previously TrinityDEV

    Well, saveConfig(); sometimes resets config back to default (happend to me) for no reason.
     
  6. Yeah this sometimes happens to me, best thing in my opinion is saving the config anywhere apart from onDisable.