Welcome to Jc - Scape!

Please log-in if you already have a JCscape forum account,

If you don't, please register!

Best regards, JCscape staff.
Welcome to Jc - Scape!

Please log-in if you already have a JCscape forum account,

If you don't, please register!

Best regards, JCscape staff.
Would you like to react to this message? Create an account in a few clicks or log in to continue.


.
 
HomeHome  Portal*Portal*  GalleryGallery  SearchSearch  Latest imagesLatest images  RegisterRegister  Log inLog in  
Navigation
 Index
 FinalX
 Server Status
Who is online
In total there are 2 users online :: 0 Registered, 0 Hidden and 2 Guests

None

Most users ever online was 167 on Mon May 18, 2009 10:33 pm
Top posters
♫ Steven ♫ (1243)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Jamie (1112)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Norway (1110)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Immense Jelly <3 Obliv (1072)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Mod Robbie (984)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
J A M I E (832)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Oblivious (780)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
Disturb3d (760)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
ErenGurkan ツ (740)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 
James (710)
Handling Infractions via ArrayList I_vote_lcapHandling Infractions via ArrayList I_voting_barHandling Infractions via ArrayList I_vote_rcap 

Advert

Interested to advertise at jcscape.org? Contact Evanna@graphicscene.net

 

Handling Infractions via ArrayList

View previous topic View next topic Go down 
Author Message
The Soul
Moderator
Moderator
The Soul

Number of posts : 8
Points :
Handling Infractions via ArrayList Left_bar_bleue0 / 1000 / 100Handling Infractions via ArrayList Right_bar_bleue

Registration date : 2009-12-31

Handling Infractions via ArrayList Vide
PostSubject: Handling Infractions via ArrayList   Handling Infractions via ArrayList EmptyThu 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. Smile

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();
}
Back to top Go down

Handling Infractions via ArrayList

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum: You cannot reply to topics in this forum
 :: Main Rsps :: Tutorial's / Config's Ect. -