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

Any help topics that don't fit in the current categories can go here.
Post Reply
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post by offluffy »

I have a yml file here:
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
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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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
this.config.getConfigurationSection("messages").getKeys(true).remove("1");
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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 !
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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:
Borders:
  test:
    Owner: OffLuffy
    Position: 0,64,0
    Radius: 100
    Shape: round
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:
Borders: !!set
  test: null
  test.Owner: null
  test.Position: null
  test.Radius: null
  test.Shape: null
Any clue why it's doing this or how to fix this?
This is the code I'm using to remove the node:
// 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();
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post by jacek »

The config section should not be a set of strings. It should be a ConfigurationSection

So change
Set<String> cfs = plugin.borders.getConfigurationSection("Borders").getKeys(true);
To
ConfigurationSection cfs = plugin.borders.getConfigurationSection("Borders").getKeys(true);
and you should be okay.
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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
plugin.borders.set("Borders." + world, null);
?
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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:
Borders: {}
Which works well enough for me. Doesn't spit back errors or anything.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post by jacek »

Looks good, I think {} just indicates an empty node.
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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 @.@
Post Reply