Chat System - Admin Command Kick - Client Side

Introduction: This tutorial is the eighteenth in my Java Network Programming using KryoNet series, or thirteenth in creating a chat client and server system, in which we are going to be giving clients a command to request actions from an admin. Previous: In the previous tutorial we added private message features to the client. The System: We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat. When a client connects, add them to a list. Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation. This Tutorial: This tutorial we are going to be making admin requests from a client application. The main focus is going to be the request of kicking another client. We'll also add a parameter to allow whether the client wants the request broadcast or not, the default will be private. The Packet: First we need to create a new request packet, we'll create this named Packet7AdminKick, it contains three strings and one boolean used as properties later on...
  1. package Packets;
  2.  
  3. public class Packet7AdminKick extends Packet{
  4. public String userFrom, userKick, reason;
  5. public boolean broadcast;
  6. }
Ensure you have this Packet in both Java Projects. Client Command: Now we need to add the command to the client, you should know that this goes where we send the message and check if it is a command (starting with a forward slash(/))...
  1. else if(entered.toLowerCase().startsWith("/admin")) {
  2. System.out.println("Warning, For kick please enter the command as follows...");
  3. System.out.println("/admin kick {username to kick} {reason code, type /admin reasons for a list} {public/yes/y/true or private/no/n/false}");
  4. String args[] = entered.split(" ");
  5. if (args.length >= 2 && args[1].equalsIgnoreCase("kick")) {
  6. boolean isBroadcasting = false;
  7. if (args.length == 5) {
  8. if (args[4].equalsIgnoreCase("public") || args[4].equalsIgnoreCase("yes") || args[4].equalsIgnoreCase("y") || args[4].equalsIgnoreCase("true")){
  9. isBroadcasting = true;
  10. }else if(args[4].equalsIgnoreCase("private") || args[4].equalsIgnoreCase("no") || args[4].equalsIgnoreCase("n") || args[4].equalsIgnoreCase("false")) {
  11. isBroadcasting = false;
  12. }else{
  13. System.out.println("Global status not found or set properly, setting broadcast to false/private.");
  14. isBroadcasting = false;
  15. }
  16. }
  17. if (args.length == 4 || args.length == 5) {
  18. Packet7AdminKick packet = new Packet7AdminKick();
  19. packet.userFrom = ClientCustom.username;
  20. packet.userKick = args[1].toLowerCase();
  21. packet.reason = args[2].toLowerCase();
  22. packet.broadcast = isBroadcasting;
  23. client.sendTCP(packet);
  24. area1.append("Request sent.");
  25. }else
  26. System.out.println("Incorrect specified parameters. /admin kick {user} {reason code} {public/private broadcast status}");
  27. }else if(args.length < 1 && args[0].equalsIgnoreCase("/admin")) {
  28. System.out.println("Incorrect specified parameters. /admin kick {user} {reason code} {public/private broadcast status}");
  29. }else if(args.length >= 2 && args[1].equalsIgnoreCase("reasons")) {
  30. String reasons[] = {"Spamming Chat", "Abusing Other Members", "Abusing Me", "Sending False Information", "Making Threats", "Other"};
  31. for (int i=0;i<reasons.length;i++) {
  32. System.out.println("[" + String.valueOf(i) + "] " + reasons[i]);
  33. }
  34. }
  35. area2.setText("");
  36. }
As you can see, we check if the command is /admin, then we check if there is a parameter value as kick. If there is, we check if there is another two parameters, or three (the first is the username to request a kick to, the second is the reason code, and the final third parameter is to check whether the kick request should be globally broadcast or not). If the first option is kick, we check if there are full parameters, if so, we set the value of isBroadcasting (whether the request should be broadcast or not) to the final parameter. If we received 4 or 5 parameters (since the broadcasting boolean variable parameter is optional and not required) we create the packet, set the contents and send it on its way. As you can see, we also have a reasons option instead of kick to pair with the admin command, this gives a list of reasons as to why they are requesting the kick action to another user through the admin. The reasons sub-command simply contains a list of reasons, iterates through them and outputs each ones index followed by the reason, the user uses the code associated with the given reason, then it will be converted back on the server. -I have not split up the giant method above, simply because it would be more confusing if you had to piece it all together. If you require any help, leave a comment or send me a message, thanks.- As you may of noticed, we are using a variable which does not currently exist, 'username' within the setting variables/properties of the new Packet7AdminKick packet, lets change our Client ClientCustom (main) class a little bit. First create a new static String username variable to contain the logged in username of the client...
  1. static String username;
Then we want to set the value of the new variable once we are logged in...
  1. username = loggedInUsername;
...
  1. public static boolean connect(Client client, String loggedInUsername) {
  2. Packet3UsernameReq req = new Packet3UsernameReq();
  3. req.username = loggedInUsername;
  4. client.sendTCP(req);
  5. while (!userServerRes) {
  6. try {
  7. System.out.println("Sleeping...");
  8. Thread.sleep(500);
  9. }catch (Exception ex) {}
  10. }
  11. if (userServerAvailable) {
  12. System.out.println("That username is already taken. Please enter a new one... ");
  13. userServerRes = false; //Reset global booleans used for server username response
  14. return false;
  15. }else{ //false=available
  16. System.out.println("Connecting...");
  17. Packet1Connect con = new Packet1Connect();
  18. con.name = loggedInUsername;
  19. client.sendTCP(con);
  20. System.out.println("Connected as " + loggedInUsername);
  21. userServerRes = false; //Reset global booleans used for server username response
  22. username = loggedInUsername;
  23. return true;
  24. }
  25. }
Next Tutorial: In the next tutorial we are going to be making the server part of this packet request since the way I'm thinking of doing it right now will require quite a few new methods. Thanks for reading!

Add new comment