Преглед изворни кода

Updated command handling to be a bit cleaner

Removed the variables in the Command class, get values from the methods
instead.
Moved and rewrote arrayToString method from Command class to
CommandHandler.
Frans пре 9 година
родитељ
комит
c0cdcbb640

+ 5 - 5
src/command/Ban.java

@@ -35,11 +35,11 @@ public class Ban extends Command {
 				duration = new Scanner(args[1]).nextInt();
 				
 				if (args.length >= 3)
-					bn = new BanNote(IP, duration, this.stringArrayToString(CommandHandler.removeFirst(CommandHandler.removeFirst(args))));
+					bn = new BanNote(IP, duration, CommandHandler.stringArrayToString(CommandHandler.removeFirst(CommandHandler.removeFirst(args))));
 				else
 					bn = new BanNote(IP, duration);
 			} catch (InputMismatchException ime) {
-				bn = new BanNote(IP, this.stringArrayToString(CommandHandler.removeFirst(args)));
+				bn = new BanNote(IP, CommandHandler.stringArrayToString(CommandHandler.removeFirst(args)));
 			}
 		
 		Server.banNotes.add(bn);
@@ -47,12 +47,12 @@ public class Ban extends Command {
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "ban";
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "server.ban";
 	}
 
@@ -62,7 +62,7 @@ public class Ban extends Command {
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 1;
 	}
 

+ 3 - 20
src/command/Command.java

@@ -3,26 +3,9 @@ package command;
 import server.Client;
 
 public abstract class Command {
-	public String name, permission = "*";
-	public int argNumber;
-	
-	public Command () {
-		name = setName();
-		argNumber = setMinArgNumber();
-		permission = setPermission();
-	}
-
 	public abstract void execute (String[] args, Client caller) throws Exception;
-	public abstract String setName ();
-	public abstract String setPermission ();
+	public abstract String getName ();
+	public abstract String getPermission ();
 	public abstract String writeDescription ();
-	public abstract int setMinArgNumber ();
-	
-	public String stringArrayToString(String[] strArr) {
-		String content = "";
-		for (int i = 1; i < strArr.length -1; i++) {
-			content += strArr[i] + " ";
-		}
-		return content;
-	}
+	public abstract int getMinArgNumber ();
 }

+ 3 - 3
src/command/CreateChannel.java

@@ -15,12 +15,12 @@ public class CreateChannel extends Command{
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "createchannel";
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "server.createchannel";
 	}
 
@@ -30,7 +30,7 @@ public class CreateChannel extends Command{
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 1;
 	}
 

+ 3 - 3
src/command/Exit.java

@@ -11,12 +11,12 @@ public class Exit extends Command{
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "exit";
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 0;
 	}
 
@@ -26,7 +26,7 @@ public class Exit extends Command{
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "server.exit";
 	}
 

+ 4 - 4
src/command/Help.java

@@ -9,7 +9,7 @@ public class Help extends Command {
 	public void execute(String[] args, Client caller) {
 		String help = "Help for all commands:" + "\n";
 		for (int i = 0; i < CommandHandler.commands.length; i++) {
-			help += CommandHandler.commands[i].name + ": ";
+			help += CommandHandler.commands[i].getName() + ": ";
 			help += "\t";
 			help += CommandHandler.commands[i].writeDescription();
 			if (i + 1 < CommandHandler.commands.length)
@@ -19,12 +19,12 @@ public class Help extends Command {
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "help";
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 0;
 	}
 
@@ -34,7 +34,7 @@ public class Help extends Command {
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "noob.help";
 	}
 

+ 3 - 3
src/command/JoinChannel.java

@@ -22,12 +22,12 @@ public class JoinChannel extends Command {
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "join";
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "noob.channel";
 	}
 
@@ -37,7 +37,7 @@ public class JoinChannel extends Command {
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 1;
 	}
 

+ 3 - 3
src/command/Kick.java

@@ -15,12 +15,12 @@ public class Kick extends Command {
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "kick";
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 1;
 	}
 
@@ -30,7 +30,7 @@ public class Kick extends Command {
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "server.kick";
 	}
 }

+ 3 - 3
src/command/LeaveChannel.java

@@ -25,12 +25,12 @@ public class LeaveChannel extends Command{
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "leave";
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "noob.leave";
 	}
 
@@ -40,7 +40,7 @@ public class LeaveChannel extends Command{
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 1;
 	}
 

+ 3 - 3
src/command/List.java

@@ -27,12 +27,12 @@ public class List extends Command {
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "list";
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 0;
 	}
 
@@ -42,7 +42,7 @@ public class List extends Command {
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "noob.list";
 	}
 

+ 4 - 4
src/command/PrivateMessage.java

@@ -19,19 +19,19 @@ public class PrivateMessage extends Command {
 			return;
 		}
 		
-		Message mess = new Message("PM", caller.username, this.stringArrayToString(CommandHandler.removeFirst(args)));
+		Message mess = new Message("PM", caller.username, CommandHandler.stringArrayToString(CommandHandler.removeFirst(args)));
 		
 		reciever.send(mess); caller.send(mess);
 		
 	}
 
 	@Override
-	public String setName() {
+	public String getName() {
 		return "pm";
 	}
 
 	@Override
-	public String setPermission() {
+	public String getPermission() {
 		return "noob.pm";
 	}
 
@@ -41,7 +41,7 @@ public class PrivateMessage extends Command {
 	}
 
 	@Override
-	public int setMinArgNumber() {
+	public int getMinArgNumber() {
 		return 2;
 	}
 

+ 7 - 3
src/server/CommandHandler.java

@@ -21,9 +21,9 @@ public class CommandHandler {
 	
 	public static void executeCommand(String[] command, Client caller) {
 		for (Command comm: commands) { //Go through all commands
-			if ((comm.name).equals(command[0])) { //Look for command with correct name
-				if (caller.hasPermission(comm.permission)) //Check if the client has permission
-					if (command.length -1 >= comm.argNumber) { //Check the number of arguments
+			if ((comm.getName()).equals(command[0])) { //Look for command with correct name
+				if (caller.hasPermission(comm.getPermission())) //Check if the client has permission
+					if (command.length -1 >= comm.getMinArgNumber()) { //Check the number of arguments
 						try {
 							comm.execute(removeFirst(command), caller); //Execute command
 						} catch (Exception e) {
@@ -51,4 +51,8 @@ public class CommandHandler {
 		}
 		return newCommand;
 	}
+	
+	public static String stringArrayToString(String[] strArr) {
+		return strArr.toString().replace("[", "").replace("]", "").replace(", ", " ");
+	}
 }