Page 1 of 1

How to delete a node and all sub values? (Bukkit of course)

Posted: Mon Apr 23, 2012 3:23 pm
by offluffy
I have a yml file here:
[syntax=text]messages:
'0':
task: Example Normal Task
done: false
urgency: '1'
added-by: CONSOLE
'1':
task: Example Important Task
done: false
urgency: '2'
added-by: CONSOLE[/syntax]
This is for a new plugin I'm writing called BukkitList, and I have a command to remove a task, and to do that, I have it set to fetch a configuration section ("messages") & getKeys(false), and remove the specified node, then to set that conf. section back to "messages", but as I've realized, this will empty all the nodes under the remaining nodes. How can I get rid of one node under "messages" and all it's sub-nodes, and leave the others intact? Apparently I have fascination with using conf. sections @.@ I tried it without the getKeys(false) part, but that changes the data type of the variable, then I can't remove items from it. Or I should say I don't know how. T.T

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Tue Apr 24, 2012 9:22 pm
by jacek
Well what you tried sounds right, I think the argument to getKeys() controls if the child nodes should also be returned, so if you set that to true it should keep the rest of the data in place.

You might not need to set it back to the main config too, if the list is live you won't need too. I don't know if it is or not but it might be worth trying since you can do it in a one-liner
[syntax=java]this.config.getConfigurationSection("messages").getKeys(true).remove("1");[/syntax]

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Wed Apr 25, 2012 4:48 am
by offluffy
Jeeze, never thought to try getKeys(true), I always set it false or left it out entirely (which changed the varible data type), and the .remove() wasn't supported for that data type. But yes, I'll try it. I do somewhat need to save back to the file though. I like to save the file after every edit so that they can reload (which won't save the file since it's saved during every edit, but reloads the files). Somewhat of a method I prefer. I hate it when I edit a file, reload it, then it rewrites the old file back. Just dreadful annoying. Always hafta shut down the server to edit it.

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Thu Apr 26, 2012 12:27 pm
by jacek
offluffy wrote:I hate it when I edit a file, reload it, then it rewrites the old file back. Just dreadful annoying. Always hafta shut down the server to edit it.

Good point, I hate that too. I sometime open the file just before the server shutdown fully too. That's much more irritting !

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Tue May 08, 2012 9:19 am
by offluffy
Sorry to revive this, but I'm having some issues with this. So I start with something like this for a yml file:
[syntax=text]Borders:
test:
Owner: OffLuffy
Position: 0,64,0
Radius: 100
Shape: round[/syntax]
Then I can add to the file fine and it works perfectly, but when I go to remove a node like test, it turns into this:
[syntax=text]Borders: !!set
test: null
test.Owner: null
test.Position: null
test.Radius: null
test.Shape: null[/syntax]
Any clue why it's doing this or how to fix this?
This is the code I'm using to remove the node:
[syntax=java]// For reference:
String world = "test";
Set<String> cfs = plugin.borders.getConfigurationSection("Borders").getKeys(true);
cfs.remove(world);
plugin.borders.set("Borders", cfs);
try{
plugin.borders.save(plugin.bordersFile);
}catch (IOException e){
e.printStackTrace();
}[/syntax]

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Thu May 10, 2012 12:39 am
by jacek
The config section should not be a set of strings. It should be a ConfigurationSection

So change

[syntax=java]Set<String> cfs = plugin.borders.getConfigurationSection("Borders").getKeys(true);[/syntax]
To

[syntax=java]ConfigurationSection cfs = plugin.borders.getConfigurationSection("Borders").getKeys(true);[/syntax]
and you should be okay.

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Thu May 10, 2012 4:06 pm
by offluffy
Not entirely sure how that'd work O.o I can set it to a ConfigurationSection if I cast it from a Set<String>, but then the ConfigurationSection doesn't have a .remove() method. So I guess in the mean time, I'll just remove the getKeys(true), and work from there.

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Fri May 11, 2012 7:00 pm
by jacek
Oh sorry I misread it.

I checked the javadoc and found
If value is null, the value will be removed from the default Configuration source.

So maybe try

[syntax=java]plugin.borders.set("Borders." + world, null);[/syntax]
?

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Fri May 11, 2012 11:10 pm
by offluffy
I tried it right quick and it seems to do the same thing. Just splits all the nodes into a single-tier level list with null values.

EDIT:

Scratch that, kinda sorta changed the wrong line of code. Will post again later with the right result I guess XDD

MORE EDITS:

Alright, that seems to work XD After using that, it sets it to this if there's no more sub-nodes:
[syntax=text]Borders: {}[/syntax]
Which works well enough for me. Doesn't spit back errors or anything.

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Sun May 13, 2012 12:51 pm
by jacek
Looks good, I think {} just indicates an empty node.

Re: How to delete a node and all sub values? (Bukkit of cour

Posted: Sun May 13, 2012 6:13 pm
by offluffy
Seems so. More-so I think it indicates an empty multi-tier node. And empty single-tier node usually has a "[]" after it. And thanks! This slight issue with node removal has been hindering me from updating about 3 of my plugins in the works XDD Great help, appreciate it @.@