Recent adittions.

This commit is contained in:
Olof Larsson 2012-04-26 10:16:23 +02:00
parent 6536631e6d
commit 57bbbddede
5 changed files with 192 additions and 6 deletions

View File

@ -33,7 +33,7 @@ public class HelpCommand extends MCommand
{ {
if (subCommand.visibleTo(sender)) if (subCommand.visibleTo(sender))
{ {
lines.add(subCommand.getUseageTemplate(this.commandChain, true)); lines.add(subCommand.getUseageTemplate(this.commandChain, true, true));
} }
} }

View File

@ -304,16 +304,25 @@ public abstract class MCommand
// Help and Usage information // Help and Usage information
// -------------------------------------------- // // -------------------------------------------- //
public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc) public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc, boolean onlyFirstAlias)
{ {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
ret.append(Txt.parse("<c>")); ret.append(Txt.parse("<c>"));
ret.append('/'); ret.append('/');
boolean first = true;
for (MCommand mc : commandChain) for (MCommand mc : commandChain)
{ {
ret.append(Txt.implode(mc.aliases, ",")); if (first && onlyFirstAlias)
{
ret.append(mc.aliases.get(0));
}
else
{
ret.append(Txt.implode(mc.aliases, ","));
}
ret.append(' '); ret.append(' ');
first = false;
} }
ret.append(Txt.implode(this.aliases, ",")); ret.append(Txt.implode(this.aliases, ","));
@ -356,6 +365,11 @@ public abstract class MCommand
return ret.toString(); return ret.toString();
} }
public String getUseageTemplate(List<MCommand> commandChain, boolean addDesc)
{
return getUseageTemplate(commandChain, addDesc, false);
}
public String getUseageTemplate(boolean addDesc) public String getUseageTemplate(boolean addDesc)
{ {
return getUseageTemplate(this.commandChain, addDesc); return getUseageTemplate(this.commandChain, addDesc);

View File

@ -133,11 +133,18 @@ public class Persist
ArrayList<T> ret = new ArrayList<T>(items.size()); ArrayList<T> ret = new ArrayList<T>(items.size());
// WHERE // WHERE
for (T item : items) if (where == null)
{ {
if (where.apply(item)) ret.addAll(items);
}
else
{
for (T item : items)
{ {
ret.add(item); if (where.apply(item))
{
ret.add(item);
}
} }
} }

View File

@ -0,0 +1,147 @@
package com.massivecraft.mcore2.util;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import com.massivecraft.mcore2.MCore;
public class IntervalUtil
{
// -------------------------------------------- //
// PARSING SIMPLE
// -------------------------------------------- //
public static Double parseDouble(String str, Double def)
{
if (str == null) return def;
try
{
return Double.valueOf(str);
}
catch (Exception e)
{
return def;
}
}
public static Integer parseInteger(String str, Integer def)
{
if (str == null) return def;
try
{
return Integer.valueOf(str);
}
catch (Exception e)
{
return def;
}
}
public static boolean isValidInterval(String interval)
{
return interval.matches("^.+to.+$");
}
// -------------------------------------------- //
// PARSING ADVANCED
// -------------------------------------------- //
public static Entry<Double, Double> parseDoubleInterval(String interval, Double dmin, Double dmax)
{
if (interval == null)
{
return new SimpleEntry<Double, Double>(dmin, dmax);
}
if (interval.contains("to"))
{
String[] parts = interval.split("to");
if (parts.length == 2)
{
Double min = parseDouble(parts[0], dmin);
Double max = parseDouble(parts[1], dmax);
return new SimpleEntry<Double, Double>(min, max);
}
}
Double single = parseDouble(interval, dmin);
return new SimpleEntry<Double, Double>(single, single);
}
public static Entry<Integer, Integer> parseIntegerInterval(String interval, Integer dmin, Integer dmax)
{
if (interval == null)
{
return new SimpleEntry<Integer, Integer>(dmin, dmax);
}
if (interval.contains("to"))
{
String[] parts = interval.split("to");
if (parts.length == 2)
{
Integer min = parseInteger(parts[0], dmin);
Integer max = parseInteger(parts[1], dmax);
return new SimpleEntry<Integer, Integer>(min, max);
}
}
Integer single = parseInteger(interval, dmin);
return new SimpleEntry<Integer, Integer>(single, single);
}
// -------------------------------------------- //
// RANDOM SIMPLE
// -------------------------------------------- //
public static int randomIntegerFromInterval(int min, int max)
{
return min+MCore.random.nextInt(max-min+1);
}
public static int randomIntegerFromInterval(Entry<Integer, Integer> interval)
{
int min = interval.getKey();
int max = interval.getValue();
return randomIntegerFromInterval(min, max);
}
public static double randomDoubleFromInterval(double min, double max)
{
return min+MCore.random.nextDouble()*(max-min);
}
public static double randomDoubleFromInterval(Entry<Double, Double> interval)
{
double min = interval.getKey();
double max = interval.getValue();
return randomDoubleFromInterval(min, max);
}
// -------------------------------------------- //
// RANDOM COMBINED
// -------------------------------------------- //
public static Double randomDoubleFromInterval(String data, Double def)
{
if (isValidInterval(data))
{
Entry<Double, Double> interval = parseDoubleInterval(data, def, def);
return randomDoubleFromInterval(interval);
}
else
{
return parseDouble(data, def);
}
}
public static Integer randomIntegerFromInterval(String data, Integer def)
{
if (isValidInterval(data))
{
Entry<Integer, Integer> interval = parseIntegerInterval(data, def, def);
return randomIntegerFromInterval(interval);
}
else
{
return parseInteger(data, def);
}
}
}

View File

@ -1,6 +1,7 @@
package com.massivecraft.mcore2.util; package com.massivecraft.mcore2.util;
import java.util.*; import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -269,6 +270,23 @@ public class Txt
return ret; return ret;
} }
public static String removeLeadingCommandDust(String string)
{
return string.replaceAll("^[/\\s]+", "");
}
public static Entry<String, String> divideOnFirstSpace(String string)
{
String[] parts = string.split("\\s+", 2);
String first = parts[0];
String second = null;
if (parts.length > 1)
{
second = parts[1];
}
return new SimpleEntry<String, String>(first, second);
}
// -------------------------------------------- // // -------------------------------------------- //
// Material name tools // Material name tools
// -------------------------------------------- // // -------------------------------------------- //