I'm currently using the following code to get a list of online players but the problem I'm having is the only way I'm able to output the results is in multiple lines. I know there is a way to use a for loop or something to get all the results into one line but I can't seem to get my head around it. Code: Player[] playerlist = plugin.getServer().getOnlinePlayers(); int listsize = plugin.getServer().getOnlinePlayers().length; for(int i = 0; i < listsize; i++) { log.info(playerlist[i].getDisplayName()); } Right now the ouput would be: [INFO] AinSophAur [INFO] Player2 [INFO] Player3 [INFO] ... I would like: [INFO] AinSophAur Player2 Player3 ...
Code: for (Player p : playerList) { dosomething } As for your actual question, add all of the players to a StringBuilder, then put them in the log.
Give this a try Code: Player[] playerlist = plugin.getServer().getOnlinePlayers(); String players = ""; for(int i = 0; i < playerlist.length; i++) { players += playerlist[i].getDisplayName(); if (i < playerlist.length - 1) players += " "; } log.info(players);
You shouldn't use a string like that, it's bad for performance. A StringBuilder object is the correct way to do it. Code: Player[] playerList = plugin.getServer().getOnlinePlayers(); StringBuilder s = new StringBuilder() for (Player p : playerList) { s.append(p.getDisplayName() + " "); // Format this however } log.info(s.toString());
@SpikeMeister I have tried the enhanced for loop and got it to give me the list as well but the problem I had was concatenating the list into one string Thanks for the help this works like a charm
I wanted to keep it simple for him, so not everything will be "overwhelming" It might impact performance when working with pages of text (not with a list of some names which may be at maximum several hundred bytes) and if the method would be called very frequent (but since I guess this will only be run when some command is run once in a while it will make barely any difference)...
You might have missed it because I edited it in, but you should never update a String over and over in a loop. Each time it is making a new string object and copying the contents of the old string into it plus the new stuff, and then disposing of the old String object. It's horribly inefficient. Please use a StringBuilder like in my example Yeah I see your point about simplicity, but it can't hurt to let the OP know that there is a more optimal way to do what he wants The small things like this add up over a project and over multiple plugins, and we don't want to slow down servers any more than we have to! EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Ok I did it using SpikeMeister's way. Thanks for the help Current output looks like this: (Players) AinSophAur Kayla ReZiE Olphia Salvie I will be formatting this so it does look better but I should be able to figure that out on my own