[TUT] Bukkit's new FileConfiguration API! Create a YAML configuration!

Discussion in 'Resources' started by Windwaker, Oct 23, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Zolli

    Add value as string.

    Code:
    this.getConfig().set("reload", "true");
     
  2. Offline

    dakoslug

    Tried that didn't work. Any suggestions?

    Edit: Nvm fixed it. Got rid of reloadConfig() and replaced it with saveConfig()
     
  3. Offline

    Lius

    I had the same problem as cat24max, but how do I change the compiler to java6 or my bukkit to java 7?
     
  4. Offline

    dsmyth1915

    If you compile in java7 you can run bukkit in java7 as well. Changing the compiler to java6 I'm not entirely sure how to explain right now, I'm in school. Try searching around plugindev. There's alot of threads and comments about it already.
     
  5. Offline

    Unknowncmbk

    Reloading your config:
    At times it is going to seem necessary to reload your plugin, perhaps to get a fields value. You can simply create this method to reload your configuration.
    Code:java
    1. plugin.reloadConfig()


    Great! Now we have the config loaded! +[diamond] (It should still be blank)

    I don't understand where the line 'plugin.reloadConfig()' goes in my file? Does it go with onEnable or outside that method or in the method that initializes a reload (in which that method needs created). And the point of the 'plugin' part is what? You commented in the very first method that you don't need the 'plugin' part if your plugin extends JavaPlugin.

    I'm new to java, and any detailed info will help me much!
     
  6. Offline

    phondeux

    Code:
    plugin.getConfig().getStringList("gates.names").add(args[1]);
    plugin.saveConfig();
    Why doesn't this save anything?

    Basically what i'm trying to do is add to a list another string. I've confirmed that the String args[1] is correct by echoing it back to me, but I can't use 'getList' as I get an error. I use 'getStringList' instead ... but following with add and then saveConfig does nothing, the config file is never updated.

    Just to make sure it wasn't some kind of file permission issue i changed it a bit to just a value:name pair and it updates just fine.
     
  7. Offline

    evilmidget38

    phondeux The returned list is a clone of the list that's actually in the config.
    The code you want should be more like this:
    Code:
    List<String> list=plugin.getConfig().getStringList("gates.names");
    list.add(args[1]);
    plugin.getConfig().set("gates.names", list);
    plugin.saveConfig();
    
     
    phondeux likes this.
  8. Offline

    phondeux

    That worked flawlessly, thank you! Some of Java is very easy for me but some of it just goes right over my head.

    How do you remove an entry? For example, I have this;
    Code:
    gates:
      frontier:
        X:
        Y:
        Z:
      smoke:
        X:
        Y:
        Z:
    How would I remove either one of those? Am I doing it all wrong? Should I have a yml file for each gate instead?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  9. Offline

    gomeow

    To remove, set the path to null
     
    phondeux likes this.
  10. Offline

    phondeux

    Ahh, thanks, worked perfectly!
     
  11. Offline

    puyttre

    I'm trying to add to a list by doing getConfig().getList("commands").add("String");
    It gives me an error saying
    Code:
    no suitable method found for add()
    method List.add(int, CAP#1) is not applicable
      (actual and formal argument lists differ in length)
    method List.add(CAP#1) is not applicable
      (actual and formal argument lists differ in length)
    where CAP#1 is a fresh type-variable:
      Cap#1 extends Object from capture of ?
     
  12. Offline

    gomeow

    To use lists in the config, say you want to add something:
    Code:
    getConfig().set("path", getConfig().getStringList("path").add(Object o));
     
  13. Offline

    puyttre

    Thanks :)
     
  14. Offline

    bfgbfggf

    hyyyym. bla bla bla... "Making use of the config.yml (optional but better)" but if I use saveConfig();. all comments disappear.
     
  15. Offline

    cdsboy2000

    Love the tutorial, very helpful for a noob like me. I do have one question, though.
    When you say "get" true, would all of the plugin.getConfig.blah.blah translate into true?

    For example, could I do this?
    Code:
    if (plugin.getConfig().getBoolean("path.to.boolean") = true) {
     
    do stuff;
     
    }
    ...or is that not the right usage?

    Thanks,
    -cdsboy2000
     
  16. Offline

    chasechocolate

    cdsboy2000 it is == true as == is for comparisons. Also that is unnecessary as if you don't have an == check Java will automatically think that you are checking if it is true.
     
  17. Offline

    gooding300

    it is not up to date.
    because you use higher api.

    Can you put the full code here?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  18. Offline

    peti446

    in my config i have this '' in every string!!
    Example:
    PHP:
    Config:
      
    Example String'Example'
    My loadconfiguration:
    Code:java
    1. private void loadConfig(){
    2. String phat1 = "Config.Check Updates";
    3. this.getConfig().set(phat1, true);
    4. String phat2 = "Config.Input Line 0";
    5. this.getConfig().set(phat2, "[Link]");
    6. String path3 = "Config.Output Line 0";
    7. this.getConfig().set(path3, "&9[Link]");
    8. String path4 = "Config.Color Line 1";
    9. this.getConfig().set(path4, "&f");
    10. String path5 = "Config.Output Line 3";
    11. this.getConfig().set(path5, "&5Right Click!");
    12. String path6 = "Config.Messages.Url ColorCode";
    13. this.getConfig().set(path6, "&6");
    14. String path7 = "Config.Messages.After BroadCast Url 1";
    15. this.getConfig().set(path7, "&bThis link bring your to -->");
    16. String path8 = "Config.Messages.After BroadCast Url 2";
    17. this.getConfig().set(path8, "&5Click the url from the chat!");
    18.  
    19. this.getConfig().options().copyDefaults(true);
    20. this.saveConfig();
    21. }

    Why i have this '' in all my string ?
     
  19. Windwaker I created the list with this method:
    Code:java
    1. int x = attachedBlock.getLocation().getBlockX();
    2. int y = attachedBlock.getLocation().getBlockY();
    3. int z = attachedBlock.getLocation().getBlockZ();
    4.  
    5. String list = attachedBlock.getLocation().getWorld().getName() + "," + x + "," + y + "," + z;
    6.  
    7. bl = YamlConfiguration.loadConfiguration(blocked);
    8.  
    9. bl.set("blocked", Arrays.asList(list));

    And I tried to add a String in this list:
    Code:java
    1. int x = attachedBlock.getLocation().getBlockX();
    2. int y = attachedBlock.getLocation().getBlockY();
    3. int z = attachedBlock.getLocation().getBlockZ();
    4.  
    5. String list = attachedBlock.getLocation().getWorld().getName() + "," + x + "," + y + "," + z;
    6.  
    7. bl = YamlConfiguration.loadConfiguration(blocked);
    8.  
    9. bl.getList("blocked".add(list));

    but Eclipse reported me an error: "The method add(capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (String)"
    Why? I have to add strings in a list, how can I do this?
     
  20. Offline

    MrVerece

    Windwaker Thanks for this helpful tutorial :)
    But I have got a question: I want my Plugin to save the default config.yml to the plugins folder when the plugin is started for the first time. After that, I would like to change the config.yml from the plugins folder manually, but with this
    Code:java
    1. public void loadConfiguration() {
    2. getConfig().options().copyDefaults( true );
    3. saveConfig();
    4. }

    in my onEnable() method, it keeps overwriting the changes that I have made. What do I have to do so that manual changes to the config.yml are no longer deleted when reloading?

    EDIT: Found the Problem. I had a saveConfig()-Method in the onDisable() that i completely forgot about >.<
     
  21. Offline

    Tudedude

    Windwaker Great tutorial, so helpful in many ways! And, BTW, in "Setting a Field's Value" for the list one,
    Code:java
    1. String[] list = {"This", "Is", "A", "List"}
    2. plugin.getConfig().set("path.to.list", Arrays.asList(list));

    should be:
    Code:
    String[] list = {"This", "Is", "A", "List"};
    plugin.getConfig().set("path.to.list", Arrays.asList(list));
    Just forgot a semicolon, though it shouldn't matter, as people shouldn't be using your EXACT code, as spoonfeeding.
     
  22. This gives me an error:
    Code:java
    1. this.getConfig().getList("path.to.list").add("A new string");

    Code:
    The method add(capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (String) 
     
  23. bluegru
    Use getStringList() instead. Also, your code won't work, use this
     
  24. Code:java
    1. String pathlist = "Warp.List";
    2. getConfig().set(pathlist, getConfig().getStringList(pathlist).add(name));

    I get:
    Code:
    List: true

    Edit:Ok, got it,
    thx anyway
     
  25. Offline

    Exoaria

    This should be stickied somewhere.
     
  26. Offline

    MrAw3omeZz

    I cant add to a list..? it doesnt work no matter which add i use..?
     
Thread Status:
Not open for further replies.

Share This Page