Purpose: To filter any usernames containing symbols of any sort.
Difficulty:1/10.
Effectiveness: 100%.
Source used: PVPScape
Welcome all, to my tutorial on how to implement username filtering. First of all, why would we want to do this? Well there are a couple of reasons. First off, is to prevent people playing with names such as:
Code:
~HEY~
^S^U^P
Your G@Y
You could also modify this tutorial a little bit to filter out any bad words, but that isn't what this is about. Now on to the code!
Step one.
First of all, open up your client.java. If you know of any existing ways that this is implemented, you should remove it to prevent lagging (doing the code twice over) and/or possible problems arising from accessing the same file. If you don't know, you should probably assume that you don't have any.
Step two.
Go to the very top of your client.java file, and search for this:
Code:
public class client extends Player implements Runnable {
If you can't find that, I can't help you sorry, your on your own .
Directly underneath that line of code, insert this line of code:
Code:
public String IllegalUsernameChars[] = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", ";", "'", ",", ".", "/", "?", ":", "[", "{", "]", "}", "|", "admin", "mod"};
Step three.
Go to the top of your client.java again, and search for this code:
Code:
playerLastConnect = connectedFrom;
Right underneath that line of code, add this code:
Code:
for (int j = 0; j < IllegalUsernameChars.length; j++) {
if (playerName.toLowerCase().contains(IllegalUsernameChars[j])) {
if (checkbannedusers() != 5) { //You may need to modify this line!
server.banHandler.appendToBanned(playerName);//You may need to modify this line!
server.playerHandler.kickNick = playerName;
}
}
}
In the above code I have commented on two lines of code that you may need to modify, depending on what source you are using. The first is the method that checks to see if your username is on the banned username list, and the second line is to add the current username to the banned username list.
Well done! You now have a username protection mechanism that disallows usernames with symbols in them! Congratulations.
Afternote:
If you want to add more symbols to the IllegalUserNameChars list, or if you wanted to add protection from 'bad' or 'rude' words in the username, you can do that by simply adding to the list. No other changes are nescessary!
Cheers everyone who read this,
~Pancakes.
I appreciate rëp