diff --git a/src/com/massivecraft/mcore2/Lang.java b/src/com/massivecraft/mcore2/Lang.java index 950984fb..0f876e9c 100644 --- a/src/com/massivecraft/mcore2/Lang.java +++ b/src/com/massivecraft/mcore2/Lang.java @@ -7,5 +7,6 @@ public class Lang public static final String commandSenderMustBePlayer = "This command can only be used by ingame players."; public static final String commandToFewArgs = "To few arguments. Use like this:"; - public static final String commandToManyArgs = "Strange argument \"

%s\". Use the command like this:"; + public static final String commandToManyArgs = "Strange arguments %s."; + public static final String commandToManyArgs2 = "Use the command like this:"; } diff --git a/src/com/massivecraft/mcore2/MCore.java b/src/com/massivecraft/mcore2/MCore.java index 9161bea0..8b1f484c 100644 --- a/src/com/massivecraft/mcore2/MCore.java +++ b/src/com/massivecraft/mcore2/MCore.java @@ -79,6 +79,8 @@ public class MCore extends JavaPlugin public static Random random = new Random(); + + @Override public void onDisable() { diff --git a/src/com/massivecraft/mcore2/cmd/BukkitGlueCommand.java b/src/com/massivecraft/mcore2/cmd/BukkitGlueCommand.java index f7c49c06..a064b1df 100644 --- a/src/com/massivecraft/mcore2/cmd/BukkitGlueCommand.java +++ b/src/com/massivecraft/mcore2/cmd/BukkitGlueCommand.java @@ -1,12 +1,10 @@ package com.massivecraft.mcore2.cmd; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import com.massivecraft.mcore2.util.Txt; + public class BukkitGlueCommand extends Command { protected MCommand mcommand; @@ -24,8 +22,7 @@ public class BukkitGlueCommand extends Command return false; } - List argList = new ArrayList(Arrays.asList(args)); - this.mcommand.execute(sender, argList); + this.mcommand.execute(sender, Txt.tokenizeArguments(Txt.implode(args, " "))); return true; } } diff --git a/src/com/massivecraft/mcore2/cmd/MCommand.java b/src/com/massivecraft/mcore2/cmd/MCommand.java index 94d74e3a..1f25cf7d 100644 --- a/src/com/massivecraft/mcore2/cmd/MCommand.java +++ b/src/com/massivecraft/mcore2/cmd/MCommand.java @@ -288,7 +288,8 @@ public abstract class MCommand { // Get the to many string slice List theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size()); - msg(Lang.commandToManyArgs, Txt.implode(theToMany, " ")); + msg(Lang.commandToManyArgs, Txt.implodeCommaAndDot(theToMany, Txt.parse("%s"), Txt.parse(", "), Txt.parse(" and "), "")); + msg(Lang.commandToManyArgs2); sender.sendMessage(this.getUseageTemplate()); } return false; diff --git a/src/com/massivecraft/mcore2/util/Txt.java b/src/com/massivecraft/mcore2/util/Txt.java index 14b20660..4ba63d25 100644 --- a/src/com/massivecraft/mcore2/util/Txt.java +++ b/src/com/massivecraft/mcore2/util/Txt.java @@ -199,12 +199,7 @@ public class Txt else return string + repeat(string, times-1); } - public static String implode(final Collection coll, final String glue) - { - return implode(coll.toArray(new Object[0]), glue); - } - - public static String implode(final Object[] list, final String glue) + public static String implode(final Object[] list, final String glue, final String format) { StringBuilder ret = new StringBuilder(); for (int i=0; i coll, final String glue, final String format) + { + return implode(coll.toArray(new Object[0]), glue, format); + } + public static String implode(final Collection coll, final String glue) + { + return implode(coll, glue, null); + } - public static String implodeCommaAndDot(final Collection objects, final String comma, final String and, final String dot) + public static String implodeCommaAndDot(final Collection objects, final String format, final String comma, final String and, final String dot) { if (objects.size() == 0) return ""; - if (objects.size() == 1) return objects.iterator().next().toString(); + if (objects.size() == 1) + { + return implode(objects, comma, format); + } List ourObjects = new ArrayList(objects); String lastItem = ourObjects.get(ourObjects.size()-1).toString(); String nextToLastItem = ourObjects.get(ourObjects.size()-2).toString(); + if (format != null) + { + lastItem = String.format(format, lastItem); + nextToLastItem = String.format(format, nextToLastItem); + } String merge = nextToLastItem+and+lastItem; ourObjects.set(ourObjects.size()-2, merge); ourObjects.remove(ourObjects.size()-1); - return implode(ourObjects, comma)+dot; + return implode(ourObjects, comma, format)+dot; } + + public static String implodeCommaAndDot(final Collection objects, final String comma, final String and, final String dot) + { + return implodeCommaAndDot(objects, null, comma, and, dot); + } + public static String implodeCommaAnd(final Collection objects, final String comma, final String and) { return implodeCommaAndDot(objects, comma, and, ""); @@ -421,4 +449,63 @@ public class Txt } return ret; } + + // -------------------------------------------- // + // Tokenization + // -------------------------------------------- // + + public static List tokenizeArguments(String str) + { + List ret = new ArrayList(); + StringBuilder token = null; + boolean escaping = false; + boolean citing = false; + + for(int i = 0; i < str.length(); i++) + { + char c = str.charAt(i); + if (token == null) + { + token = new StringBuilder(); + } + + if (escaping) + { + escaping = false; + token.append(c); + } + else if (c == '\\') + { + escaping = true; + } + else if (c == '"') + { + if (citing || token.length() > 0) + { + ret.add(token.toString()); + token = null; + } + citing = !citing; + } + else if (citing == false && c == ' ') + { + if (token.length() > 0) + { + ret.add(token.toString()); + token = null; + } + } + else + { + token.append(c); + } + } + + if (token != null) + { + ret.add(token.toString()); + } + + return ret; + } }