Now using an argument tokenizer. Citation mark usage implemented.
This commit is contained in:
parent
57bbbddede
commit
8b290daadc
@ -7,5 +7,6 @@ public class Lang
|
|||||||
|
|
||||||
public static final String commandSenderMustBePlayer = "<b>This command can only be used by ingame players.";
|
public static final String commandSenderMustBePlayer = "<b>This command can only be used by ingame players.";
|
||||||
public static final String commandToFewArgs = "<b>To few arguments. <i>Use like this:";
|
public static final String commandToFewArgs = "<b>To few arguments. <i>Use like this:";
|
||||||
public static final String commandToManyArgs = "<b>Strange argument \"<p>%s<b>\". <i>Use the command like this:";
|
public static final String commandToManyArgs = "<b>Strange arguments %s<b>.";
|
||||||
|
public static final String commandToManyArgs2 = "<i>Use the command like this:";
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,8 @@ public class MCore extends JavaPlugin
|
|||||||
|
|
||||||
public static Random random = new Random();
|
public static Random random = new Random();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable()
|
public void onDisable()
|
||||||
{
|
{
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
package com.massivecraft.mcore2.cmd;
|
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.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import com.massivecraft.mcore2.util.Txt;
|
||||||
|
|
||||||
public class BukkitGlueCommand extends Command
|
public class BukkitGlueCommand extends Command
|
||||||
{
|
{
|
||||||
protected MCommand mcommand;
|
protected MCommand mcommand;
|
||||||
@ -24,8 +22,7 @@ public class BukkitGlueCommand extends Command
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> argList = new ArrayList<String>(Arrays.asList(args));
|
this.mcommand.execute(sender, Txt.tokenizeArguments(Txt.implode(args, " ")));
|
||||||
this.mcommand.execute(sender, argList);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,8 @@ public abstract class MCommand
|
|||||||
{
|
{
|
||||||
// Get the to many string slice
|
// Get the to many string slice
|
||||||
List<String> theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size());
|
List<String> 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("<aqua>%s"), Txt.parse("<b>, "), Txt.parse("<b> and "), ""));
|
||||||
|
msg(Lang.commandToManyArgs2);
|
||||||
sender.sendMessage(this.getUseageTemplate());
|
sender.sendMessage(this.getUseageTemplate());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -199,12 +199,7 @@ public class Txt
|
|||||||
else return string + repeat(string, times-1);
|
else return string + repeat(string, times-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String implode(final Collection<? extends Object> coll, final String glue)
|
public static String implode(final Object[] list, final String glue, final String format)
|
||||||
{
|
|
||||||
return implode(coll.toArray(new Object[0]), glue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String implode(final Object[] list, final String glue)
|
|
||||||
{
|
{
|
||||||
StringBuilder ret = new StringBuilder();
|
StringBuilder ret = new StringBuilder();
|
||||||
for (int i=0; i<list.length; i++)
|
for (int i=0; i<list.length; i++)
|
||||||
@ -213,26 +208,59 @@ public class Txt
|
|||||||
{
|
{
|
||||||
ret.append(glue);
|
ret.append(glue);
|
||||||
}
|
}
|
||||||
ret.append(list[i]);
|
if (format != null)
|
||||||
|
{
|
||||||
|
ret.append(String.format(format, list[i].toString()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret.append(list[i].toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret.toString();
|
return ret.toString();
|
||||||
}
|
}
|
||||||
|
public static String implode(final Object[] list, final String glue)
|
||||||
|
{
|
||||||
|
return implode(list, glue, null);
|
||||||
|
}
|
||||||
|
public static String implode(final Collection<? extends Object> coll, final String glue, final String format)
|
||||||
|
{
|
||||||
|
return implode(coll.toArray(new Object[0]), glue, format);
|
||||||
|
}
|
||||||
|
public static String implode(final Collection<? extends Object> coll, final String glue)
|
||||||
|
{
|
||||||
|
return implode(coll, glue, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static String implodeCommaAndDot(final Collection<? extends Object> objects, final String comma, final String and, final String dot)
|
public static String implodeCommaAndDot(final Collection<? extends Object> objects, final String format, final String comma, final String and, final String dot)
|
||||||
{
|
{
|
||||||
if (objects.size() == 0) return "";
|
if (objects.size() == 0) return "";
|
||||||
if (objects.size() == 1) return objects.iterator().next().toString();
|
if (objects.size() == 1)
|
||||||
|
{
|
||||||
|
return implode(objects, comma, format);
|
||||||
|
}
|
||||||
|
|
||||||
List<Object> ourObjects = new ArrayList<Object>(objects);
|
List<Object> ourObjects = new ArrayList<Object>(objects);
|
||||||
|
|
||||||
String lastItem = ourObjects.get(ourObjects.size()-1).toString();
|
String lastItem = ourObjects.get(ourObjects.size()-1).toString();
|
||||||
String nextToLastItem = ourObjects.get(ourObjects.size()-2).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;
|
String merge = nextToLastItem+and+lastItem;
|
||||||
ourObjects.set(ourObjects.size()-2, merge);
|
ourObjects.set(ourObjects.size()-2, merge);
|
||||||
ourObjects.remove(ourObjects.size()-1);
|
ourObjects.remove(ourObjects.size()-1);
|
||||||
|
|
||||||
return implode(ourObjects, comma)+dot;
|
return implode(ourObjects, comma, format)+dot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String implodeCommaAndDot(final Collection<? extends Object> objects, final String comma, final String and, final String dot)
|
||||||
|
{
|
||||||
|
return implodeCommaAndDot(objects, null, comma, and, dot);
|
||||||
|
}
|
||||||
|
|
||||||
public static String implodeCommaAnd(final Collection<? extends Object> objects, final String comma, final String and)
|
public static String implodeCommaAnd(final Collection<? extends Object> objects, final String comma, final String and)
|
||||||
{
|
{
|
||||||
return implodeCommaAndDot(objects, comma, and, "");
|
return implodeCommaAndDot(objects, comma, and, "");
|
||||||
@ -421,4 +449,63 @@ public class Txt
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// Tokenization
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
public static List<String> tokenizeArguments(String str)
|
||||||
|
{
|
||||||
|
List<String> ret = new ArrayList<String>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user