Page 1 of 1

How do I add a YAML node without overwriting the others?

Posted: Sat Mar 24, 2012 5:32 am
by offluffy
I have this YAML file:
User:
  root:
  - 127.0.0.1
And I want to be able to add a node under the "User" node (at the level where "root" is at), but when I do that, it overwrites previous nodes. I'm about 90% sure this is the wrong way to do it because I tried the handle the Set<String> just like a List<String>, but here's the code I'm using (in the LoginListener):
Set<String> userList = plugin.users.getConfigurationSection("User").getKeys(false);

	if (!userList.contains(playerName.getName().toLowerCase())){
		userList.add(playerName.getName().toLowerCase());
		plugin.users.set("User", userList);
	
		try{
			plugin.users.save(plugin.usersFile);
		}catch (IOException e){
			e.printStackTrace();
		}
	}
This is basically what I want the YAML file to look like afterwards, short of the specific IP addresses and/or usernames of course:
User:
  root:
  - 127.0.0.1
  offluffy:
  - 192.168.2.3
Unless another problem shows up, this is likely the last question you'll hear from me concerning this plugin XD Which maybe a relief to you since I started the last 4 or 5 threads in this section prior to this. .-. Hope that's not considered spamming XP

Re: How do I add a YAML node without overwriting the others?

Posted: Sat Mar 24, 2012 6:43 pm
by jacek
I think it's because you are adding to the list of keys directly and it is not meant to be used like that. You could try adding an empty list at the position instead. Like this
Set<String> userList = plugin.users.getConfigurationSection("User").getKeys(false);

if (!userList.contains(playerName.getName().toLowerCase())){
    plugin.users.set("User." + playerName.getName().toLowerCase(), new ArrayList<String>());
    
    try{
        plugin.users.save(plugin.usersFile);
    }catch (IOException e){
        e.printStackTrace();
    }
}

Re: How do I add a YAML node without overwriting the others?

Posted: Sat Mar 24, 2012 8:57 pm
by offluffy
Well, that works well enough XD Between that and the other post about YAML nodes with periods, I think the plugin is about ready O.o I reckon I might upload this to bukkit somewhere soon XD Might test it out a bit with more users first though. And before I do all that, mind if I credit you and/or the forum here in the config.yml? O.o

I made a shiny picture for the plugin already:
Image

Re: How do I add a YAML node without overwriting the others?

Posted: Tue Mar 27, 2012 5:25 pm
by jacek
Looks good :)

I can never be bothered to do graphics like that, You can always release it in the work in progress section people are less likely to comment "it not work, plz update" in there ;)

And credit is always appreciated :) But obviously not at all necessary.

Re: How do I add a YAML node without overwriting the others?

Posted: Wed Mar 28, 2012 6:50 am
by offluffy
I posted it up on dev Bukkit XD Some people seem to think it's a good idea. And only one problem so far, which I suspect is a problem with someone editing the YML file. Anywho, here's a link if you want to take a gander => blip!

And hell, I might have time to do ya some graphics. Just ask XD I have lotz of free time and apparently not much to do with it.