Browse Source

Made Command system use Deque instead of array

Tankernn 8 years ago
parent
commit
264c55faa2

+ 27 - 28
src/main/java/eu/tankernn/chat/server/CommandRegistry.java

@@ -1,6 +1,8 @@
 package eu.tankernn.chat.server;
 
+import java.util.ArrayDeque;
 import java.util.Arrays;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -14,71 +16,68 @@ import eu.tankernn.chat.common.MessagePacket;
 import eu.tankernn.chat.common.MessagePacket.MessageType;
 import eu.tankernn.chat.server.command.Command;
 import eu.tankernn.chat.server.command.CommandInfo;
-import eu.tankernn.chat.util.ArrayUtil;
 
-public class CommandRegistry  {
+public class CommandRegistry {
 	private static final Logger LOG = Logger.getLogger(CommandRegistry.class.getName());
 	private Map<String, Command> commands = new HashMap<>();
-	
+
 	private static final String COMMAND_PACKAGE = "eu.tankernn.chat.server.command";
-	
+
 	public CommandRegistry() {
 		Reflections reflections = new Reflections(COMMAND_PACKAGE);
 		Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(CommandInfo.class);
-		
-		for (Class<?> comm: annotated) {
+
+		for (Class<?> comm : annotated) {
 			try {
 				CommandInfo c = comm.getAnnotation(CommandInfo.class);
-				
+
 				if (!Arrays.asList(comm.getInterfaces()).contains(Command.class)) {
-					LOG.warning(comm.getName() + " is annoteded with " + CommandInfo.class.getName() + ", but does not implement" + Command.class.getName());
+					LOG.warning(comm.getName() + " is annoteded with " + CommandInfo.class.getName()
+							+ ", but does not implement" + Command.class.getName());
 					continue;
 				}
-				
+
 				commands.put(c.name(), (Command) comm.newInstance());
 			} catch (ClassCastException | InstantiationException | IllegalAccessException ex) {
 				ex.printStackTrace();
 			}
 		}
 	}
-	
+
 	public void executeCommand(String commandStr, Client caller) {
-		String[] command = commandStr.substring(1).split(" ");
+		Deque<String> args = new ArrayDeque<String>(Arrays.asList(commandStr.substring(1).split(" ")));
 		
 		Command comm;
-		if ((comm = commands.get(command[0])) != null) { // Get the command
+		if ((comm = commands.get(args.pop())) != null) { // Get the command
 			CommandInfo info = comm.getClass().getAnnotation(CommandInfo.class);
-			if (caller.hasPermission(info.permission())) { // Check if the client has permission
-				String[] args = ArrayUtil.removeFirst(command);
-				if (args.length >= info.minArg()) { // Check the number of arguments
+			// Check if the client has permission
+			if (caller.hasPermission(info.permission())) {
+				// Check the number of arguments
+				if (args.size() >= info.minArg()) {
 					try {
 						comm.execute(args, caller); // Execute command
 					} catch (Exception e) {
 						caller.send(new MessagePacket("Error while executing command!", MessageType.ERROR));
 						e.printStackTrace();
 					}
-					return;
-				} else {
+				} else
 					caller.send("More arguments required!");
-					return;
-				}
-			} else {
+			} else
 				caller.send("Not enough permissions!");
-				return;
-			}
 		} else
 			caller.send("No such command! Type '/help' for a list of commands.");
 	}
-	
+
 	public String getHelp() {
 		StringBuilder help = new StringBuilder();
 		help.append("Help for all commands:" + "\n");
-		
+
 		Stream<Entry<String, Command>> stream = commands.entrySet().stream();
-		
-		stream.map(c -> c.getValue().getClass().getAnnotation(CommandInfo.class)).map((a) -> a.name() + ": " + "\t" + a.desc() + "\n").forEach(help::append);
-		
+
+		stream.map(c -> c.getValue().getClass().getAnnotation(CommandInfo.class))
+				.map((a) -> a.name() + ": " + "\t" + a.desc() + "\n").forEach(help::append);
+
 		return help.toString();
 	}
-	
+
 }

+ 10 - 10
src/main/java/eu/tankernn/chat/server/command/Ban.java

@@ -1,7 +1,7 @@
 package eu.tankernn.chat.server.command;
 
 import java.io.IOException;
-import java.util.Arrays;
+import java.util.Deque;
 import java.util.InputMismatchException;
 import java.util.Optional;
 
@@ -10,23 +10,23 @@ import eu.tankernn.chat.common.MessagePacket.MessageType;
 import eu.tankernn.chat.server.BanNote;
 import eu.tankernn.chat.server.Client;
 import eu.tankernn.chat.server.Server;
-import eu.tankernn.chat.util.ArrayUtil;
 
 @CommandInfo(desc = "Bans a user. (/ban <username> [seconds] [reason])", name = "ban", permission = "admin.ban", minArg = 1)
 public class Ban implements Command {
 	
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		String IP = null;
 		int duration = -1;
 		Client victim;
+		String name = args.pop();
 		
-		Optional<Client> maybeVictim = Server.getUserByName(args[0]);
+		Optional<Client> maybeVictim = Server.getUserByName(name);
 		
 		if (maybeVictim.isPresent())
 			victim = maybeVictim.get();
 		else {
-			caller.send(new MessagePacket("No user called " + args[0] + ".", MessageType.ERROR));
+			caller.send(new MessagePacket("No user called " + name + ".", MessageType.ERROR));
 			return;
 		}
 		
@@ -40,16 +40,16 @@ public class Ban implements Command {
 		
 		BanNote bn = new BanNote(IP);
 		
-		if (args.length != 1)
+		if (!args.isEmpty())
 			try {
-				duration = Integer.parseInt(args[1]);
+				duration = Integer.parseInt(args.pop());
 				
-				if (args.length >= 3)
-					bn = new BanNote(IP, duration, Arrays.toString(ArrayUtil.removeFirst(ArrayUtil.removeFirst(args))));
+				if (!args.isEmpty())
+					bn = new BanNote(IP, duration, String.join(" ", args));
 				else
 					bn = new BanNote(IP, duration);
 			} catch (InputMismatchException ime) {
-				bn = new BanNote(IP, Arrays.toString(ArrayUtil.removeFirst(args)));
+				bn = new BanNote(IP, String.join(" ", args));
 			}
 		
 		Server.ban(bn);

+ 3 - 1
src/main/java/eu/tankernn/chat/server/command/Command.java

@@ -1,7 +1,9 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
+
 import eu.tankernn.chat.server.Client;
 
 public interface Command {
-	public void execute(String[] args, Client caller) throws Exception;
+	public void execute(Deque<String> args, Client caller) throws Exception;
 }

+ 5 - 3
src/main/java/eu/tankernn/chat/server/command/CreateChannel.java

@@ -1,5 +1,7 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
+
 import eu.tankernn.chat.common.MessagePacket;
 import eu.tankernn.chat.server.Channel;
 import eu.tankernn.chat.server.Client;
@@ -9,10 +11,10 @@ import eu.tankernn.chat.server.Server;
 public class CreateChannel implements Command {
 	
 	@Override
-	public void execute(String[] args, Client caller) throws Exception {
-		Server.getChannels().add(new Channel(args[0]));
+	public void execute(Deque<String> args, Client caller) throws Exception {
+		Server.getChannels().add(new Channel(args.peek()));
 		
-		Server.wideBroadcast(new MessagePacket("Channel " + args[0] + " is now available. Use '/join " + args[0] + "' to join."));
+		Server.wideBroadcast(new MessagePacket("Channel " + args.peek() + " is now available. Use '/join " + args.peek() + "' to join."));
 	}
 	
 }

+ 3 - 1
src/main/java/eu/tankernn/chat/server/command/Exit.java

@@ -1,5 +1,7 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
+
 import eu.tankernn.chat.server.Client;
 import eu.tankernn.chat.server.Server;
 
@@ -7,7 +9,7 @@ import eu.tankernn.chat.server.Server;
 public class Exit implements Command {
 	
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		Server.exit();
 	}
 	

+ 0 - 22
src/main/java/eu/tankernn/chat/server/command/GiveOP.java

@@ -1,22 +0,0 @@
-package eu.tankernn.chat.server.command;
-
-import eu.tankernn.chat.common.MessagePacket;
-import eu.tankernn.chat.common.MessagePacket.MessageType;
-import eu.tankernn.chat.server.Client;
-import eu.tankernn.chat.server.Server;
-
-@CommandInfo(desc = "Give a client OP permissions.", name = "op", permission = "admin.op", minArg = 1)
-public class GiveOP implements Command {
-
-	@Override
-	public void execute(String[] args, Client caller) throws Exception {
-		try {
-			Client target = Server.getUserByName(args[0]).get();
-			target.addPermission("*");
-			target.send(new MessagePacket("You are now OP.", MessageType.INFO));
-		} catch (NullPointerException ex) {
-			caller.send(new MessagePacket("No such user: " + args[0], MessageType.WARNING));
-		}
-	}
-
-}

+ 25 - 0
src/main/java/eu/tankernn/chat/server/command/GivePermission.java

@@ -0,0 +1,25 @@
+package eu.tankernn.chat.server.command;
+
+import java.util.Deque;
+
+import eu.tankernn.chat.common.MessagePacket;
+import eu.tankernn.chat.common.MessagePacket.MessageType;
+import eu.tankernn.chat.server.Client;
+import eu.tankernn.chat.server.Server;
+
+@CommandInfo(desc = "Give a client specified permissions.", name = "perm", permission = "admin.permission", minArg = 2)
+public class GivePermission implements Command {
+
+	@Override
+	public void execute(Deque<String> args, Client caller) throws Exception {
+		String name = args.pop();
+		try {
+			Client target = Server.getUserByName(name).get();
+			target.addPermission(args.pop());
+			target.send(new MessagePacket("You are now OP.", MessageType.INFO));
+		} catch (NullPointerException ex) {
+			caller.send(new MessagePacket("No such user: " + name, MessageType.WARNING));
+		}
+	}
+
+}

+ 3 - 1
src/main/java/eu/tankernn/chat/server/command/Help.java

@@ -1,5 +1,7 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
+
 import eu.tankernn.chat.common.MessagePacket;
 import eu.tankernn.chat.common.MessagePacket.MessageType;
 import eu.tankernn.chat.server.Client;
@@ -9,7 +11,7 @@ import eu.tankernn.chat.server.Server;
 public class Help implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		caller.send(new MessagePacket(Server.getCommReg().getHelp(), MessageType.COMMAND));
 	}
 

+ 5 - 4
src/main/java/eu/tankernn/chat/server/command/JoinChannel.java

@@ -1,5 +1,6 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
 import java.util.Optional;
 
 import eu.tankernn.chat.common.MessagePacket;
@@ -12,19 +13,19 @@ import eu.tankernn.chat.server.Server;
 public class JoinChannel implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		if (caller.equals(Server.getLocalClient())) {
 			caller.send("Client-only command.");
 			return;
 		}
-
-		Optional<Channel> maybeChannel = Server.getChannelByName(args[0]);
+		String name = args.pop();
+		Optional<Channel> maybeChannel = Server.getChannelByName(name);
 		Channel selectedChannel = maybeChannel.isPresent() ? maybeChannel.get() : null;
 
 		try {
 			selectedChannel.add(caller);
 			caller.setPrimaryChannel(selectedChannel);
-			caller.send(new MessagePacket("You are now speaking in channel " + args[0] + ".", MessageType.COMMAND));
+			caller.send(new MessagePacket("You are now speaking in channel " + name + ".", MessageType.COMMAND));
 		} catch (NullPointerException ex) {
 			caller.send(new MessagePacket("No such channel!", MessageType.ERROR));
 		}

+ 5 - 3
src/main/java/eu/tankernn/chat/server/command/Kick.java

@@ -1,5 +1,6 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
 import java.util.Optional;
 
 import eu.tankernn.chat.common.MessagePacket;
@@ -11,13 +12,14 @@ import eu.tankernn.chat.server.Server;
 public class Kick implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) {
-		Optional<Client> maybeVictim = Server.getUserByName(args[0]);
+	public void execute(Deque<String> args, Client caller) {
+		String name = args.pop();
+		Optional<Client> maybeVictim = Server.getUserByName(name);
 
 		try {
 			maybeVictim.orElseThrow(NullPointerException::new).disconnect();
 		} catch (NullPointerException ex) {
-			caller.send(new MessagePacket("No user called " + args[0] + "!", MessageType.ERROR));
+			caller.send(new MessagePacket("No user called " + name + "!", MessageType.ERROR));
 		}
 	}
 }

+ 6 - 5
src/main/java/eu/tankernn/chat/server/command/LeaveChannel.java

@@ -1,5 +1,6 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
 import java.util.Optional;
 
 import eu.tankernn.chat.common.MessagePacket;
@@ -12,24 +13,24 @@ import eu.tankernn.chat.server.Server;
 public class LeaveChannel implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) throws Exception {
+	public void execute(Deque<String> args, Client caller) throws Exception {
 		if (caller.equals(Server.getLocalClient())) {
 			caller.send("Client-only command.");
 			return;
 		}
-
-		Optional<Channel> maybeChannel = Server.getChannelByName(args[0]);
+		String name = args.pop();
+		Optional<Channel> maybeChannel = Server.getChannelByName(name);
 		Channel selectedChannel = maybeChannel.isPresent() ? maybeChannel.get() : null;
 
 		try {
 			selectedChannel.remove(caller);
 			if (caller.getPrimaryChannel().equals(selectedChannel))
 				caller.setPrimaryChannel(Server.getChannels().get(0));
-			caller.send(new MessagePacket("You left channel " + args[0] + ".", MessageType.COMMAND));
+			caller.send(new MessagePacket("You left channel " + name + ".", MessageType.COMMAND));
 			caller.send(new MessagePacket("You are now speaking in channel " + caller.getPrimaryChannel().name + ".",
 					MessageType.COMMAND));
 		} catch (NullPointerException ex) {
-			caller.send(new MessagePacket("No channel named " + args[0] + ".", MessageType.ERROR));
+			caller.send(new MessagePacket("No channel named " + name + ".", MessageType.ERROR));
 			return;
 		}
 	}

+ 5 - 3
src/main/java/eu/tankernn/chat/server/command/List.java

@@ -1,5 +1,6 @@
 package eu.tankernn.chat.server.command;
 
+import java.util.Deque;
 import java.util.Optional;
 
 import eu.tankernn.chat.common.MessagePacket;
@@ -12,11 +13,12 @@ import eu.tankernn.chat.server.Server;
 public class List implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		String arr, channelName = null;
 
-		if (args.length >= 1) {
-			Optional<Channel> maybeChannel = Server.getChannelByName(args[0]);
+		if (!args.isEmpty()) {
+			String name = args.pop();
+			Optional<Channel> maybeChannel = Server.getChannelByName(name);
 			Channel selectedChannel = maybeChannel.isPresent() ? maybeChannel.get() : null;
 			try {
 				arr = selectedChannel.listClients("\n");

+ 6 - 9
src/main/java/eu/tankernn/chat/server/command/PrivateMessage.java

@@ -1,38 +1,35 @@
 package eu.tankernn.chat.server.command;
 
-import java.util.Arrays;
+import java.util.Deque;
 import java.util.Optional;
 
 import eu.tankernn.chat.common.MessagePacket;
 import eu.tankernn.chat.common.MessagePacket.MessageType;
 import eu.tankernn.chat.server.Client;
 import eu.tankernn.chat.server.Server;
-import eu.tankernn.chat.util.ArrayUtil;
 
 @CommandInfo(desc = "Sends a private message to a user", name = "pm", permission = "user.pm", minArg = 2)
 public class PrivateMessage implements Command {
 
 	@Override
-	public void execute(String[] args, Client caller) {
+	public void execute(Deque<String> args, Client caller) {
 		Client reciever;
-		Optional<Client> maybeVictim = Server.getUserByName(args[0]);
+		String name = args.pop();
+		Optional<Client> maybeVictim = Server.getUserByName(name);
 
 		if (maybeVictim.isPresent())
 			reciever = maybeVictim.get();
 		else {
-			caller.send(new MessagePacket("No user called " + args[0] + ".", MessageType.ERROR));
+			caller.send(new MessagePacket("No user called " + name + ".", MessageType.ERROR));
 			return;
 		}
 		if (caller.equals(reciever)) {
 			caller.send("Please don't speak with yourself.");
 			return;
-		} else if (reciever == null) {
-			caller.send("No user called " + args[0] + ".");
-			return;
 		}
 
 		MessagePacket mess = new MessagePacket("PM", caller.username,
-				Arrays.toString(ArrayUtil.removeFirst(args)), MessagePacket.MessageType.PM);
+				String.join(" ", args), MessagePacket.MessageType.PM);
 
 		reciever.send(mess);
 		caller.send(mess);

+ 0 - 9
src/main/java/eu/tankernn/chat/util/ArrayUtil.java

@@ -1,9 +0,0 @@
-package eu.tankernn.chat.util;
-
-import java.util.Arrays;
-
-public class ArrayUtil {
-	public static <T> T[] removeFirst(T[] oldArr) {
-		return Arrays.copyOfRange(oldArr, 1, oldArr.length);
-	}
-}

+ 0 - 7
src/test/java/eu/tankernn/chat/server/test/ServerTestCase.java

@@ -4,15 +4,8 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import eu.tankernn.chat.server.CommandRegistry;
-import eu.tankernn.chat.util.ArrayUtil;
 
 public class ServerTestCase {
-	
-	@Test
-	public void testArrayUtils() {
-		Assert.assertArrayEquals(new String[] {"like", "trains"}, ArrayUtil.removeFirst(new String[] {"I", "like", "trains"}));
-	}
-	
 	@Test
 	public void testCommandLoading() {
 		CommandRegistry registry = new CommandRegistry();