The Soul
Moderator
Number of posts : 8
Points :
Registration date : 2009-12-31
|
Subject: Handling Infractions via ArrayList Thu Dec 31, 2009 12:55 am |
|
|
Hello! Welcome to my tutorial about handling infractions through ArrayLists. 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: - Code:
-
ArrayList<Integer> arraylist = new ArrayList<Integer>(); ArrayList - the class used to instantiate the arraylist object - used for generics, and it's a Type-Wrapper. 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. - generics.
For adding anything to the list, you use the add method like so: - Code:
-
ArrayList<Integer> elements = new ArrayList<Integer>(); 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. - Code:
-
ArrayList<Integer> elements = new ArrayList<Integer>(); elements.remove((Integer) 2); And to clear all elements within your list, you use the clear() method. - Code:
-
ArrayList<Integer> elements = new ArrayList<Integer>(); 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: - Code:
-
ArrayList<String> elements = new ArrayList<String>() {{ add("one"); add("two"); add("three"); }}; This doesn't change at all from what we were doing before, it's just an alternative method.
For more information, visit: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
--------
Alright, now to handling banning.
To start, you'll need to add this ArrayList: - Code:
-
public ArrayList<String> bannedplayers = new ArrayList<String>(); Import these packages: - Code:
-
import java.io.FileOutputStream; import java.io.DataOutputStream; import java.io.DataInputStream Add this method: - Code:
-
public void update() { DataOuputStream out = new DataOutputStream(new FileOutputStream("bans.dat")); String[] bans = new String[bannedplayers.size()]; bannedplayers.toArray(bans); for (String s : bans) out.writeUTF(s); }
Add this under the class initialization: - Code:
-
DataInputStream dataIn = new DataInputStream(new FileInputStream("bans.dat")); Add this 'ban' command (or replace if you already have one): - Code:
-
if(command.startsWith("ban")) { String name = command.substring(4); int pID = PlayerHandler.getPlayerID(name); if(pID != null) { Client p = (Client) server.playerHandler.players[name]; if(bannedplayers.contains(name)) sendMessage("You've already banned this player!"); else bannedplayers.add(name); p.disconnect = true; update(); } else { sendMessage("User: "+name+" does not exist."); } } Add this to the run() method in the client class: - Code:
-
if(playerName.equalsIgnoreCase(bannedplayers)) return; Add this to the run() method as well: - Code:
-
while(true) { try { bannedusers.add(dataIn.readUTF()); } catch(EOFException e) { break; } } And for unbanning the player... Add this command: - Code:
-
if(command.startsWith("unban")) { String name = command.substring(6); bannedplayers.remove((String) name); update(); } Finally, for clearing the list of banned players, add this command as well: - Code:
-
if(command.equalsIgnoreCase("unbanall")) { bannedplayers.clear(); update(); } ----
Now to handling mutes.
Start by adding this ArrayList: - Code:
-
public ArrayList<String> mutedplayers = new ArrayList<String>(); We'll have to still import the FileOutputStream and DataInputStream classes: - Code:
-
import java.io.FileOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; Next, we'll have to add another method that will be invoked whenever a new user is added/removed from the list: - Code:
-
public void updateMute() { DataOuputStream out = new DataOutputStream(new FileOutputStream("mutes.dat")); // creates an object as a new instance of the DataOutputStream class String[] mute = new String[mutes.size()]; // takes the 'mutes' ArrayList and alters it into an array mutedplayers.toArray(mute); // returns an array containing all of the elements for (String s : bans) // loops through the array out.writeUTF(s); // uses the instance of the DataOutputStream class to call the method that will write all of the elements in the bans array } Add this under the client class' initialization: - Code:
-
DataInputStream dataInput = new DataInputStream(new FileInputStream("mutes.dat")); Add this 'mute' command (or replace if you already have one): - Code:
-
if(command.startsWith("mute")) { String name = command.substring(4); int pID = PlayerHandler.getPlayerID(name); if(pID != null) { Client p = (Client) server.playerHandler.players[name]; if(mutes.contains(name)) sendMessage("This player is already muted."); else mutes.add(name); update(); } else sendMessage("User: "+name+" does not exist."); } Under packet 4, the chat packet, add this: - Code:
-
if(playerName.equalsIgnoreCase(mutes)) { sendMessage("You're muted and cannot talk!"); return; Under the run() method in the client class, add this while loop: - Code:
-
while(true) { try { mutes.add(dataInput.readUTF()); } catch(EOFException e) { break; } } Add this under the 'mute' command: - Code:
-
if(command.startsWith("unban")) { String name = command.substring(6); mutes.remove((String) name); update(); }
|
|