Hey. It's me again mates I got another problem with some strings. Since I made a /pn command and tried to optimize my /players command, which shows a playerlist to all players, i have some problems with adding strings together. i have something like that: Code: if (cmd.getName().toLowerCase().equals("players")) { // ...Some stuff // Variables int sent = 0; String[] players = new String[PlayerList]; String printlist = ""; String potential = ""; // Some testnames... players[0] = "dukkdehwzvgbbvsdoinsd"; players[1] = "sollbruchstellenverursacher"; players[2] = "ichhabekeinehobbysunddenkemirnamenaus"; players[3] = "vandwanaöcvanfcöanfcawdmadökmkmcvfneanfn"; players[4] = "hahadukleinesopferkopfgesichtsgrätschenkopf"; for (int i = 0; i < PlayerList; i++) { potential += players[i]; // I don't exactly know, but i think this is the reason. And i can't solve it :S if (potential.length() >= ChatLength) { if (sent == 0) { p.sendMessage(ChatColor.YELLOW + "Spieler: " + ChatColor.WHITE + printlist); } else { p.sendMessage(ChatColor.WHITE + printlist); } sent++; printlist = ""; } // Some Code.... } return true; } I really don't know why it won't work. Hope you can help me and sorry for all my problems^^ Greets
It would be nice if you could post the error. Also i found this when taking a quick look. Code: for (int i = 0; i < PlayerList; i++) Replace that with Code: for (int i = 0; i < players ; i++) Also is this function supposed to print all connected players?
Before tryin' to figure-out the problem, What kind of playerlist you want to show players ? because I can't see the way you are coding being very useful in most situations.
Sorry guys. Forgot to post all the code so you can see what is wrong. But i figured out the problem Code: int PlayerList = this.getServer().getOnlinePlayers().length; // This is the length String[] players = new String[PlayerList]; //Here i made the mistake // If not enough players are connected, there can't be for example players[4] and stuff :P players[0] = "adsngvbrenvn jsafemsn"; players[1] = "sollbruchstellenverursacher"; players[2] = "ichhabekeinehobbysunddenkemirnamenaus"; players[3] = "vandwanaöcvanfcöanfcawdmadökmkmcvfneanfn"; players[4] = "hahadukleinesopferkopfgesichtsgrätschenkopf"; Anyway thanks for trying to help me out
GetOnlinePlayers is alreay an array so just do Code: Player[] PlayerList = this.getServer().getOnlinePlayers(); after that just iterate all the array like this: Code: for(int x=0);x<PlayerList.length;x++){ } Inside you can just do a PlayerList.toString() and get a string representation of all the array or iterate with [x]
Replace your loop with this: Code: final StringBuilder stringBuilder = new StringBuilder(); for (Player p: getServer().getOnlinePlayers()) { // Replace ... with whatever you want appended for each player that is online. stringBuilder.append(...); } // Convert the contents of the string builder to a string. final String message = stringBuilder.toString();