ソースを参照

For each

I finally learned how to use "for each" in Java. Remade some of the
methods to use this.
Tankernn 9 年 前
コミット
a5eca1082b
3 ファイル変更14 行追加14 行削除
  1. 6 6
      src/server/ClientCollection.java
  2. 5 5
      src/server/CommandHandler.java
  3. 3 3
      src/server/Server.java

+ 6 - 6
src/server/ClientCollection.java

@@ -22,17 +22,17 @@ public class ClientCollection extends ArrayList<Client>{
 	}
 	
 	public Client getClientByName(String name) throws NullPointerException {
-		for (int i = 0; i < size(); i++) {
-			if (get(i).username.equals(name))
-				return get(i);
+		for (Client c: this) {
+			if (c.username.equals(name))
+				return c;
 		}
 		return null;
 	}
 	
 	void broadcast(Message mess) { //Broadcast to all
 		 if (mess.validate()) {
-			for (int i = 0; i < size(); i++)
-				get(i).send(mess);
+			for (Client c: this)
+				c.send(mess);
 			Server.OPClient.send(mess.toString());
 		}
 	}
@@ -56,7 +56,7 @@ public class ClientCollection extends ArrayList<Client>{
 	public void remove(Client user, boolean disconnect) { //Remove and disconnect if needed
 		if (disconnect)
 			user.disconnect();
-		remove(user);
+		super.remove(user);
 	}
 	
 	public String listClients() { //String from array

+ 5 - 5
src/server/CommandHandler.java

@@ -18,12 +18,12 @@ public class CommandHandler {
 	}
 	
 	public static void executeCommand(String[] command, Client caller) {
-		for (int i = 0; i < commands.length; i++) { //Go through all commands
-			if ((commands[i].name).equals(command[0])) { //Look for command with correct name
-				if (caller.hasPermission(commands[i].permission)) //Check if the client has permission
-					if (command.length -1 >= commands[i].argNumber) { //Check the number of arguments
+		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
 						try {
-							commands[i].execute(removeFirst(command), caller); //Execute command
+							comm.execute(removeFirst(command), caller); //Execute command
 						} catch (Exception e) {
 							caller.send("Error while executing command!");
 							e.printStackTrace();

+ 3 - 3
src/server/Server.java

@@ -102,9 +102,9 @@ public class Server {
 	
 	public static void cleanUp() { //Makes sure the client gets removed from all arrays
 		clients.cleanUp();
-		for (int i = 0; i < channels.length; i++)
-			if (channels[i] != null)
-				channels[i].cleanUp();
+		for (Channel c: channels)
+			if (c != null)
+				c.cleanUp();
 	}
 	
 	public static void exit() {