Solved Configuration File Lines

Discussion in 'Spigot Plugin Development' started by MakingTheMagic, Jun 24, 2016.

  1. Hey guys!

    Each time something is added to a config, how would I make it add it onto a new line. I have a method for adding the things so I could just insert that code into the method. So, does anyone know how I could add a new line into the config every time something is added?

    Thanks everyone.
    Any advice is appreciated, as always! :D
     
  2. c.set("list", Arrays.asList("Line0", "Line1", "Line2");
     
  3. Ah. But how would I make it do it automatically. So each time it just adds another line without me specifying which line.

    Thanks for replying.
     
  4. Yes thats easy.
    public void add(){
    List<String> l = c.getStringList("list");
    l.add("Index");

    c.set("list", l);
    c.save(file);
    }
     
  5. http://prntscr.com/bkkr16
    that makes that.

    I want everything to be on the sameline,
    but it to look like this:

    http://prntscr.com/bkkrdb
     
  6. c.getStringList("list").toString()
     
  7. No. Because you have to change the List<String> :p
     
  8. If you don't mind using numbers you can loop through the configuration section and check if the entry exists and if it doesn't you can add it
     
  9. Could you give me some pseudo code, and I take it is using a random number generator?
     
  10. for(int i = 0; true; i++)
    If(config.get("whatever_" + i) == null) do whatever and break
     
  11. It crashes or freezes the server
     
  12. Then you probably forgot to break the loop
     
  13. Okay, and it still just edits the same line, doesn't add any or anything.

    Thanks! :D
     
  14. Please show me your current code :)
     
  15. Code (Text):
        public static void setLink(Player p, String link, String ride) throws IOException
        {
                File f = new File(MainClass.getInstance().getDataFolder() + "/playerdata/photos/" + p.getName() + ".yml");
                  if(!f.exists()) f.createNewFile();
                 YamlConfiguration c = new YamlConfiguration();
                 ConfigurationSection nameOfFile = c.getConfigurationSection(p.getName());
                 for(int i = 0; true; i++)
                 {
                 if(c.get(ride + i) == null)
                 {
                     c.set(p.getName() + "." + ride, link);
                  c.save(f);
                  break;
               
                 }
                 }
    }
     
  16. So you want to add a line at the bottom of the config or in the configuration section?
     
  17. Okay, so:

    Each time that method is run, it adds something to the config file. At the moment it keeps editing the same line, but I want to space itself out over lines. So, each time that method is run, it will make a new line, and add the text to it. It would be:

    Code (Text):
    MiniPlaysX:
       name: google1.com
       name2: google1.com
       name34: google1.com
     
    instead of just editing the same line constantly.

    Hope that explains more.

    Thanks! :D <3
     
  18. The problem with your current code is that you check the whole config instead of the section but then you set the value for the section which results in only editing the line instead of adding a line
     
  19. So, how would I fix that and make it add lines?
     
  20. Check the configuration section instead of the whole config