No repeat nicknames

For real usernames, remove already used nicknames from the list once chosen and update linesSize. To prevent IndexOutOfBoundsException from multi-threading make nextReal method synchronized.
This commit is contained in:
Bas Themen committed 2025-06-07 04:07:24 +02:00
1 parent 5e109efc56
commit 3811004fda
1 file changed
+6 -3
@@ -78,10 +78,13 @@ public class NickGenerator {
return prefix + generateRandom(nickLen);
}
public String nextReal() {
String nick = prefix + lines.get(random.nextInt(linesSize));
return nick.length() <= 16 ? nick : nick.substring(0, 15);
public synchronized String nextReal() {
int index = random.nextInt(linesSize);
String nick = prefix + lines.remove(index); // Remove chosen nicknames
linesSize = lines.size();
return nick.length() <= 16 ? nick : nick.substring(0, 15);
}
/**