Solved Adding comments to config via code

Discussion in 'Spigot Plugin Development' started by justin57754, May 30, 2016.

Thread Status:
Not open for further replies.
  1. Hello! I want to add little comments like...

    Code (Text):
    #Enable the message by setting to "enable"
    Message: enable
    I want to do this via code rather than compiling the config in the jar, here is the code for the config right now.


    Code (Text):
        public void onEnable() {
         
            if (!f.exists()) {
                fc.set("message.console.message", "This is a startup message, set it in the config.");
                fc.set("message.console.level", "info");
                fc.set("message.command", "&3[&bSCM&3]&r This is a message, set it in the config.");
                try {
                    fc.save(f);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
     
    #1 justin57754, May 30, 2016
    Last edited: May 30, 2016
  2. As far as I know you can only insert comments when you save the default configuration. I don't thinkt here is a way to insert them dynamically unless you want to parse the file yourself and add it manually.
     
  3. If so, how can I safely delete the config code and input it into the config.yml
     
  4. Where should I input this?

    Code (Text):
    ackage us.justin57754.scm;

    import java.io.File;
    import java.io.IOException;
    import java.util.Random;

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

    import net.md_5.bungee.api.ChatColor;

    public class SimpleConsoleMessage extends JavaPlugin {
       
        File f = new File("plugins/SimpleConsoleMessage/config.yml");
        FileConfiguration fc = new YamlConfiguration();
       
        public void onEnable() {
            if (!f.exists()) {
                fc.set("message.console.message", "This is a startup message, set it in the config.");
                fc.set("message.console.level", "info");
                fc.set("message.command", "&3[&bSCM&3]&r This is a message, set it in the config.");
                try {
                    fc.save(f);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
     
  5. In your onEnable instead of all the stuff you already have...?
     
  6. No, would you like the whole main class?
     
  7. You don't have to create a new file. The same folder where you have plugin.yml, make a config.yml, add as many comments as you want and so. Also set the defaults, like for example "dp-stuff: true", try not to leave them blank if you don't intend to leave them blank.

    After you do that, all you have to do is #saveDefaultConfig() in your onEnable. Keep in mind that if you use getConfig().set() anytime in your code, it will reset the comments. The config.yml should be exactly what it means, "configuring", so try not to store data in it or access it in any way but to obtain information from it. Summary of this paragraph: After you insert comments don't touch the config, but you can only access information from it.
     
  8. I added the methods and edited the config with the commands and it didn't work.


    Main class: http://hastebin.com/axikudidur.avrasm

    Config.yml: http://hastebin.com/urisexixet.cmake
     
  9. Justin, since I wrote the code why didn't you ask me.

    To set a "note" at the beginning of the file. Just use the:
    Code (Text):
    fc.options().header("This is a note at the top of the config.");
    That should work.
     
    • Agree Agree x 1
    • Useful Useful x 1
  10. You don't need to do anything, remove everything you have done with config, and simply do saveDefaultConfig() without anything between the parentheses.

    Remove this:
    Code (Text):
    File f = new File("plugins/SimpleConsoleMessage/config.yml");
        FileConfiguration fc = new YamlConfiguration();
    And this:
    Code (Text):
    private void saveDefaultConfig(String string) {
            // TODO Auto-generated method stub
           
        }
     
    • Agree Agree x 1
  11. or do this @justin57754
     
  12. Choco

    Moderator

    Let me re-summarize @MrWaffleman
    Remove:
    Code (Java):
    File f = new File("plugins/SimpleConsoleMessage/config.yml");
    FileConfiguration fc = new YamlConfiguration();
    Code (Java):
            if (!f.exists()) {          
        fc.set("message.console.message", "This is a startup message, set it in the config.");          
        fc.set("message.console.level", "info");          
        fc.set("message.command", "&3[&bSCM&3]&r This is a message, set it in the config.");          
        try {              
            fc.save(f);          
        }
        catch (IOException e) {              
            // TODO Auto-generated catch block              
            e.printStackTrace();        
        }      
    }              

    try {          
        fc.load(f);      
    } catch (IOException | InvalidConfigurationException e) {          
        // TODO Auto-generated catch block          
        e.printStackTrace();      
    }
    Code (Java):
    private void saveDefaultConfig(String string) {
            // TODO Auto-generated method stub
           
    }

    Add:

    Code (Java):
    public void onEnable(){
        saveDefaultConfig();
        // The rest of your non-config-related onEnable stuff
    }
    Access your configuration file with this.getConfig()
     

  13. LOTS OF ERRORS


    Code: http://hastebin.com/zuqezojuwo.avrasm
     
  14. Yeah, like I said you need to replace "fc" with getConfig().

    Also, put this piece of code in your onEnable between the curly brackets { }:
    Code (Text):
    if (fc.getString("message.console.level").equalsIgnoreCase("info")) {
                Bukkit.getLogger().info("[SimpleConsoleMessage] " + fc.getString("message.console.message"));
            } else if (fc.getString("message.console.level").equalsIgnoreCase("warning")) {
                Bukkit.getLogger().warning("[SimpleConsoleMessage] " + fc.getString("message.console.message"));
            } else if (fc.getString("message.console.level").equalsIgnoreCase("severe")) {
                Bukkit.getLogger().severe("[SimpleConsolegMessage] " + fc.getString("message.console.message"));
            } else if (fc.getString("message.console.level").equalsIgnoreCase("troll")) {
                Bukkit.getLogger().severe("[SimpleConsoleMessage] Packet " + (new Random().nextInt(100-1) + 1) + " threw an illegal exception at line: " + fc.getString("message.console.message") + "\n[SimpleConsoleMessage] Stopping plugin.");
                Bukkit.getPluginManager().disablePlugin(this);
            } else {
                Bukkit.getLogger().info("[SimpleConfigMessage] " + fc.getString("message.console.message"));
            }
    onEnable executes whenever the server enables, so you need to put that there in order for it to run when the server starts.

    As well as replacing this:
    Code (Text):
    import net.md_5.bungee.api.ChatColor;
    With this:
    Code (Text):
    import org.bukkit.ChatColor;
    Bungee API is a whole different thing from Spigot API, importing is getting all the stuff from one class and making it accessible in the current class.

    I'm not going to sound like the everyday rude person on the forums, so I'll reword it. Learning Java before you get to Spigot and all of this will help A TON, by that I mean you won't be needing help on the forums that often, and your code will be much better. This playlist by bucky is a perfect place to start if you're new.

    EDIT TO AVOID DUPLICATE POSTS:

    In your public boolean onCommand(). Delete this portion:

    Code (Text):
    try {
                fc.load(f);
            } catch (IOException | InvalidConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    It doesn't really do anything because your config file is already loaded.

    In addition to this edit, instead of giving you all the code, I explained what it does ;).
     
    #19 MrWaffleman, May 31, 2016
    Last edited: May 31, 2016
  15. First you CAN NOT create comments in the config file except in the header if you create the config file while runtime!

    But what you can do is following:

    Create in your resource folder(in your workingset) your own config file and just copy it out from the jar if the plugin enabled!

    2016-05-31 03_53.png

    I use this function in my xDaily plugin: https://github.com/Xearoxde/Maven/b...a/de/xearox/xdaily/utilz/Utilz.java#L249-L270

    And again. You CAN NOT create comments in the config file except in the header if you create the config file while runtime!
     
    #20 Xearox, May 31, 2016
    Last edited: May 31, 2016
Thread Status:
Not open for further replies.