YAML Configuration, Remove Item From List

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

YAML Configuration, Remove Item From List

Post by offluffy »

Sorry, another stupid question: How can I remove a single node from a YAML file?
i.e.:
FirstTier:
  SecondTier:
  - TierItem1
  - TierItem2
  - TierItem3

How might I go about removing one of those TierItems and leave the rest?

While I'm at it, I suppose I should also ask how to add a tier item as well. Imma want to ask it soon anyways.

p.s. Let me know if I need to move this to a new thread!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: How can I use the getHostAddress() correctly?

Post by jacek »

A list like that is basically a LinkedArrayList (maybe not linked)

I think the method you want is something like getStringList() So somethign along the lines of
ArrayList<String> items = comfig.getStringList("FirstTier.SecondTier");

items.remove("TierItem2")

config.set("FirstTier.SecondTier", items);

config.save();
To add one you replace remove with add.

And this should be in a new topic but don't worry I did it for you :D
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: YAML Configuration, Remove Item From List

Post by offluffy »

lmao, thanks a lot XD

May end up asking even more questions. So far I like this forum better than Google as far as that is concerned @.@

edit: I actually already have a rather related question! XP Given that example above, how could I retrieve a list of of second-tier level items without their associated values and in the same format (List<String>) as plugin.config.getStringList(path) ? I've tried using the exact same method ie plugin.config.getStringList("FirstTier") and for some reason the .size() returns a 0 or a null or something along those lines. Which kinda throws a monkey wrench in my while or for loop. T.T In fact, I'll throw this in here to show you about what I'm doing, probably some very menial, stupid problem.
if (args[0].equalsIgnoreCase("aip")){
	if (sender.isOp()){
		plugin.log.info("Step 1");
		List<String> userList = plugin.user.createSection("Users").getStringList("Users");
		plugin.log.info("Step 2");
		int i = 0;
		while (i < plugin.user.getStringList("Users").size()){
			plugin.log.info("Step 3");
			List<String> ipList = plugin.user.getStringList("Users." + userList.size());
			sender.sendMessage(ChatColor.AQUA + "IPs for " + userList.get(i) + ":");
			for (int x = 0; x < ipList.size(); ++x){
				plugin.log.info("Step 4");
				sender.sendMessage(ChatColor.BLUE + ipList.get(x));							
			}
			sender.sendMessage(ChatColor.AQUA + "-----------------");
			++i;
		}
		
		return true;
	}
	sender.sendMessage(ChatColor.RED + "You must be OP to view others' IPs!");
	return true;				
}
Anyways, you can see I had it log Step # as a way to see how much of the code works. Step 1 and 2 works, doesn't get to 3. You can probably guess, but plugin.user is structured like:
User:
  administrator:
  - 127.0.0.1
  herobrine:
  - 192.168.0.2
So on and so forth.

There is probably so much wrong with just this chunk .-.
Last edited by offluffy on Sat Mar 24, 2012 7:36 am, edited 1 time in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: YAML Configuration, Remove Item From List

Post by jacek »

So your trying to get the section names, not somethign I have done before but I think it should be something like this
Set<String> users = this.config.getConfigurationSection("users").getKeys(false);
Also you can remove the while look and replace it with a foreach
for(String user : this.config.getConfigurationSection("users").getKeys(false)){
	// user should be each players name here
}
so the full thing could look something like this
for(String user : this.config.getConfigurationSection("users").getKeys(false)){
	sender.sendMessage("IPs for " + user);
	
	for (String ip : this.config.getStringList("users." + user)){
		sender.sendMessage(ip);
	}
}
Completely untested of course ;)
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: YAML Configuration, Remove Item From List

Post by offluffy »

So when I'm setting up the path for a YAML file (such as how I have "Users" in that example), would I consider capitalization when using any of those methods? Should it be getStringList("Users") or just leave the nodes all lower case i.e., getStringList("users")? I have actually tried the getConfigurationSection() before, but I'll probably try it again all kinda ways with different capitalizations and such. XD

Thanks for the tips. Love foreach loops, but wasn't sure how to set them up before.

EDIT:
So here's where I'm at now, using some of the tips you gave me:
if (args[0].equalsIgnoreCase("aip")){
	sender.sendMessage("Step 1"); // STEP 1
	if (sender.isOp()){
		sender.sendMessage("Step 2"); // STEP 2
		for (String user : plugin.user.getConfigurationSection("Users").getKeys(false)){
			sender.sendMessage("Step 3"); // STEP 3
			sender.sendMessage(ChatColor.AQUA + "IPs for " + user + ": ");						
			for (String ip : plugin.user.getStringList("users." + user)){
				sender.sendMessage("Step 4"); // STEP 4
				sender.sendMessage(ChatColor.BLUE + ip);	
			}
			sender.sendMessage(ChatColor.AQUA + "-----------------");
		}					
		return true;
	}
	sender.sendMessage(ChatColor.RED + "You must be OP to view others' IPs!");
	return true;				
}
It's still stopping after Step 2. If I use "Users" when I'm getting info from the YAML doc, no errors pop up, but it doesn't seem to initiate the foreach loop. If I use "users" instead, get a few errors on the line where the first foreach loop starts. I should also mention that this part is in it's own class and not in the main class as well. Why I'm using plugin.user instead of this.user.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: YAML Configuration, Remove Item From List

Post by jacek »

I think it's case sensitive so you would need to use the upper-case U on both lines.

So are you getting no users ? Try
sender.sendMessage(plugin.user.getConfigurationSection("Users").getKeys(false).size());
And see if it is more an 0 :)

Or if it was the IPs loop that was not working, you need an upper-case U :)
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Re: YAML Configuration, Remove Item From List

Post by offluffy »

In the process of trying that idea, I think I may have found the problem. While I told the plugin to get the node "Users", the node in the YML file was actually labeled "User". Prolly the issue. But at any rate, I'll try that again. Also have to add a string to that since the sendMessage() can't handle the int data type apparently. Results pending!

That was indeed the issue. So now I have to figure out why it's no posting any IPs under that. Thanks for the help so far!

EDIT:
Scratch all that! Got everything working as it should be so far. Now on to my next idea. Which I suppose I'll go ahead and start a new topic for this XD Before long, you'll have helped me with the bulk of this plugin XP
Post Reply