Browse Source

Apache Commons Lang StringUtils split FTW!

Douglas William Thrift 13 years ago
parent
commit
b03095c831
1 changed files with 72 additions and 51 deletions
  1. 72 51
      src/net/douglasthrift/bigscreenbot/BigScreenBot.java

+ 72 - 51
src/net/douglasthrift/bigscreenbot/BigScreenBot.java

@@ -45,6 +45,8 @@ import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 
+import org.apache.commons.lang3.StringUtils;
+
 import org.jibble.pircbot.Colors;
 import org.jibble.pircbot.IrcException;
 
@@ -157,9 +159,9 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String argument)
             {
-                String[] arguments = argument.split("\\s");
+                String[] arguments = StringUtils.split(argument);
 
-                if (arguments.length == 1 && arguments[0].isEmpty())
+                if (arguments.length == 0)
                 {
                     listBans(channel, sender);
 
@@ -190,12 +192,19 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String arguments)
             {
-                String argument = arguments.split("\\s", 2)[0].toLowerCase();
+                String argument = null;
+                Command command = null;
+ 
+                try
+                {
+                    argument = StringUtils.split(arguments, null, 2)[0].toLowerCase();
 
-                if (argument.startsWith("!"))
-                    argument = argument.substring(1);
+                    if (argument.startsWith("!"))
+                        argument = argument.substring(1);
 
-                Command command = commands.get(argument);
+                    command = commands.get(argument);
+                }
+                catch (ArrayIndexOutOfBoundsException exception) {}
 
                 sendMessage(channel, sender, Colors.BOLD + String.format("%1$-11s %2$-23s %3$-15s %4$s", "command", "arguments", "access", "description") + Colors.NORMAL);
 
@@ -229,9 +238,13 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String arguments)
             {
-                String argument = arguments.split("\\s", 2)[0];
+                String argument;
 
-                if (argument.isEmpty())
+                try
+                {
+                    argument = StringUtils.split(arguments, null, 2)[0];
+                }
+                catch (ArrayIndexOutOfBoundsException exception)
                 {
                     help(channel, sender, admin, "join");
 
@@ -255,56 +268,60 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(final String channel, final String sender, boolean admin, String argument)
             {
-                final String[] arguments = argument.split("\\s", 2);
+                final String[] arguments = StringUtils.split(argument, null, 2);
 
-                if (arguments.length == 1)
-                    if (arguments[0].isEmpty())
-                        new Thread()
+                switch (arguments.length)
+                {
+                case 0:
+                    new Thread()
+                    {
+                        @Override
+                        public void run()
                         {
-                            @Override
-                            public void run()
+                            List<String> devices;
+
+                            synchronized (remote)
                             {
-                                List<String> devices;
+                                devices = remote.listDevices();
+                            }
 
-                                synchronized (remote)
-                                {
-                                    devices = remote.listDevices();
-                                }
+                            if (devices.isEmpty())
+                            {
+                                sendMessage(channel, sender, "there are no devices to pair with");
 
-                                if (devices.isEmpty())
-                                {
-                                    sendMessage(channel, sender, "there are no devices to pair with");
+                                return;
+                            }
 
-                                    return;
-                                }
+                            sendMessage(channel, sender, Colors.BOLD + "devices" + Colors.NORMAL);
 
-                                sendMessage(channel, sender, Colors.BOLD + "devices" + Colors.NORMAL);
+                            for (String device : devices)
+                                sendMessage(channel, sender, device);
+                        }
+                    }.start();
 
-                                for (String device : devices)
-                                    sendMessage(channel, sender, device);
-                            }
-                        }.start();
-                    else
-                        new Thread()
+                    break;
+                case 1:
+                    new Thread()
+                    {
+                        @Override
+                        public void run()
                         {
-                            @Override
-                            public void run()
+                            synchronized (remote)
                             {
-                                synchronized (remote)
+                                try
+                                {
+                                    remote.beginPairDevice(arguments[0]);
+                                }
+                                catch (GeneralSecurityException exception)
                                 {
-                                    try
-                                    {
-                                        remote.beginPairDevice(arguments[0]);
-                                    }
-                                    catch (GeneralSecurityException exception)
-                                    {
-                                        error(channel, sender, exception);
-                                    }
+                                    error(channel, sender, exception);
                                 }
                             }
-                        }.start();
-                else
-                {
+                        }
+                    }.start();
+
+                    break;
+                default:
                 }
             }
         });
@@ -313,9 +330,9 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String argument)
             {
-                String[] arguments = argument.split("\\s", 2);
+                String[] arguments = StringUtils.split(argument, null, 2);
 
-                if (arguments.length == 1 && arguments[0].isEmpty())
+                if (arguments.length == 0)
                 {
                     help(channel, sender, admin, "part");
 
@@ -368,7 +385,7 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String argument)
             {
-                String[] arguments = argument.split("\\s", 2);
+                String[] arguments = StringUtils.split(argument, null, 2);
 
                 if (arguments.length != 2)
                 {
@@ -393,9 +410,9 @@ public class BigScreenBot extends Bot
             @Override
             public void execute(String channel, String sender, boolean admin, String argument)
             {
-                String[] arguments = argument.split("\\s");
+                String[] arguments = StringUtils.split(argument);
 
-                if (arguments.length == 1 && arguments[0].isEmpty())
+                if (arguments.length == 0)
                 {
                     listBans(channel, sender);
 
@@ -501,7 +518,11 @@ public class BigScreenBot extends Bot
 
         message = Colors.removeFormattingAndColors(message);
 
-        String[] arguments = message.split("\\s", 2);
+        String[] arguments = StringUtils.split(message, null, 2);
+
+        if (arguments.length == 0)
+            return;
+
         String argument = arguments[0].toLowerCase();
 
         if (argument.startsWith("!"))