-
Getting 2 variables.
Hello everyone, I want to fetch 2 player variables from all players that are currently online and then put them in a SendFrame126, i've tried the following code, but it crashes me. Can anyone help me with this?
And I also would like it that the highest scores come in first with the right usernames, but it really goes to far for me.
I've tried, but I can't do it.
I've started programming with Java just 3 days ago.
Code:
public void scoreMenu() {
clearQuestInterface();
int[] scores = new int[256];
String[] names = new String[256];
for(int i = 0; i < server.playerHandler.maxPlayers; i++) {
if(server.playerHandler.players[i] != null) {
scores[i] = server.playerHandler.players[i].playerScore;
names[i] = server.playerHandler.players[i].playerName;
if(i >= PlayerHandler.getPlayerCount()) {
for(int a = 8720;a<8799;a++) {
sendFrame126("",a);
}
for(int s = 0; s<PlayerHandler.getPlayerCount();s++) {
sendFrame126(""+Integer.toString(scores[s]),8720+s);
}
for(int p = 0; p<PlayerHandler.getPlayerCount();p++){
sendFrame126(""+names[p],8760+p);
}
}
}
}
sendQuestSomething(8713);
showInterface(8714);
flushOutStream();
}
Thanks in advance,
DaMaGeX
-
Re: Getting 2 variables.
Code:
public void scoreMenu() {
clearQuestInterface();
int[] scores = new int[256];
String[] names = new String[256];
int[] tmpScores = new int[256];
String[] tmpNames = new String[256];
System.out.println(PlayerHandler.getPlayerCount());
for(int i = 1; i <= PlayerHandler.getPlayerCount(); i++) {
if(server.playerHandler.players[i] != null) {
scores[i] = server.playerHandler.players[i].playerScore;
names[i] = server.playerHandler.players[i].playerName;
}
}
int n = PlayerHandler.getPlayerCount();
for (int pass=1; pass <= n; pass++) {
for (int i=1; i <= n-pass; i++) {
if (scores[i] < scores[i+1]) {
// exchange elements
int temp = scores[i];
scores[i] = scores[i+1];
scores[i+1] = temp;
String tempS = names[i];
names[i] = names[i+1];
names[i+1] = tempS;
}
}
}
for(int s = 1; s <= PlayerHandler.getPlayerCount();s++) {
sendFrame126(""+Integer.toString(scores[s]),8720+s);
sendFrame126(""+names[s],8760+s);
}
sendQuestSomething(8713);
showInterface(8714);
flushOutStream();
}
Your original reason for crashing was a nullPointerException, which meant you were trying to point to something that didnt exist.
You'll notice I removed both for loops and made it one, there's no point looping twice. The new for loop block is a bubble sort in java, this orders the highscores, with the highest score first.
I also changed it so that the printing out only happened once all of the players scores/names have been assigned, not as it happens, as this means you're doing multiple print loops for each time you loop around a person, very inefficient, so this is slightly more efficient.
All I can suggest is reading through books as you program your server, best way to learn, practice!
Enjoy, I'm going to bed, 4:30am :thumbdown:
-fedexer-