The Soul
08-27-2009, 06:06 AM
Hello! Welcome to my tutorial about handling player banning. First, you'll need to know about a bit about ArrayLists before we start. An ArrayList is a resizeable array.
The initialization of an ArrayList object:
ArrayList<int> arraylist = new ArrayList<int>();
ArrayList - the class
<int> - used for generics. It assigns an int to the list, meaning that no other datatype can be used.
arraylist - the object's name.
new - the keyword is a Java operator that creates the object.
ArrayList - the class.
<int> - generics.
For adding anything to the list, you use the add method like so:
ArrayList<int> elements = new ArrayList<int>();
elements.add(1);
elements.add(2);
elements.add(3);
Now the ArrayList contains the values 1, 2 and 3.
You can also remove an element by using the Remove() method.
ArrayList<int> elements = new ArrayList<int>();
elements.Remove(2);
And to clear all elements within your list, you use the clear() method.
ArrayList<int> elements = new ArrayList<int>();
elements.clear();
Double Bracing
Instead of calling the object everytime you add an element to your ArrayList, you can use the double brace.
For example:
ArrayList elements = new ArrayList() {{
add("one");
add("two");
add("three");
}};
This doesn't change at all from what we were doing before, it's just an alternative method. :)
--------
Alright, now to to the main part of the tutorial.
To start, you'll need to import this package:
import java.util.*;
Add this ArrayList:
public ArrayList<String> bannedplayers = new ArrayList<String>();
Add this under the class initialization:
DataInputStream dataIn = new DataInputStream(new FileInputStream("bans.dat"));
Add this 'ban' command (or replace if you already have one):
if(command.startsWith("ban")) {
String args = command.split(" ");
String name = args[1];
if(bannedplayers.contains(name))
sendMessage("You've already banned this player!");
else
bannedplayers.add(name);
}
Add this to the initialize() method:
if(playerName.equals(bannedplayers)) disconnect = true;
Add this to the initialize() method:
while(true) {
try {
bannedusers.add(dataIn.readUTF());
} catch(EOFException e) {
break;
}
}
And for unbanning the player...
Add this command:
if(command.startsWith("unban")) {
String args = command.split(" ");
name = args[1];
bannedplayers.remove(name);
}
The initialization of an ArrayList object:
ArrayList<int> arraylist = new ArrayList<int>();
ArrayList - the class
<int> - used for generics. It assigns an int to the list, meaning that no other datatype can be used.
arraylist - the object's name.
new - the keyword is a Java operator that creates the object.
ArrayList - the class.
<int> - generics.
For adding anything to the list, you use the add method like so:
ArrayList<int> elements = new ArrayList<int>();
elements.add(1);
elements.add(2);
elements.add(3);
Now the ArrayList contains the values 1, 2 and 3.
You can also remove an element by using the Remove() method.
ArrayList<int> elements = new ArrayList<int>();
elements.Remove(2);
And to clear all elements within your list, you use the clear() method.
ArrayList<int> elements = new ArrayList<int>();
elements.clear();
Double Bracing
Instead of calling the object everytime you add an element to your ArrayList, you can use the double brace.
For example:
ArrayList elements = new ArrayList() {{
add("one");
add("two");
add("three");
}};
This doesn't change at all from what we were doing before, it's just an alternative method. :)
--------
Alright, now to to the main part of the tutorial.
To start, you'll need to import this package:
import java.util.*;
Add this ArrayList:
public ArrayList<String> bannedplayers = new ArrayList<String>();
Add this under the class initialization:
DataInputStream dataIn = new DataInputStream(new FileInputStream("bans.dat"));
Add this 'ban' command (or replace if you already have one):
if(command.startsWith("ban")) {
String args = command.split(" ");
String name = args[1];
if(bannedplayers.contains(name))
sendMessage("You've already banned this player!");
else
bannedplayers.add(name);
}
Add this to the initialize() method:
if(playerName.equals(bannedplayers)) disconnect = true;
Add this to the initialize() method:
while(true) {
try {
bannedusers.add(dataIn.readUTF());
} catch(EOFException e) {
break;
}
}
And for unbanning the player...
Add this command:
if(command.startsWith("unban")) {
String args = command.split(" ");
name = args[1];
bannedplayers.remove(name);
}