Some preparations for a cmd arg rework

These are some preparations so everybody can be ready for a rework of how command arguments is handled.
This commit is contained in:
Magnus Ulf 2015-02-03 19:22:36 +01:00 committed by Olof Larsson
parent ed0b598ffb
commit 842c44e0b5
56 changed files with 1304 additions and 476 deletions

View File

@ -0,0 +1,33 @@
package com.massivecraft.massivecore;
public class PredictateStartsWithIgnoreCase implements Predictate<String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String prefix;
public String getPrefix() { return this.prefix; };
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public static PredictateStartsWithIgnoreCase get(String prefix) { return new PredictateStartsWithIgnoreCase(prefix); }
public PredictateStartsWithIgnoreCase(String prefix)
{
this.prefix = prefix.toLowerCase();
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(String str)
{
if (str == null) return false;
return str.toLowerCase().startsWith(this.getPrefix());
}
}

View File

@ -7,6 +7,7 @@ import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.cmd.arg.ARFloat;
import com.massivecraft.massivecore.cmd.arg.ARSound;
public final class SoundEffect implements Cloneable, Serializable
@ -66,19 +67,18 @@ public final class SoundEffect implements Cloneable, Serializable
soundString = soundString.trim();
String[] parts = soundString.split("[^a-zA-Z0-9_.]+");
Sound sound = ARSound.getSoundFromString(parts[0]);
if (sound == null) throw new IllegalArgumentException("Unknown sound \"" + parts[0] + "\"");
Sound sound = ARSound.get().read(parts[0]);
float volume = 1.0f;
if (parts.length >= 2)
{
volume = Float.parseFloat(parts[1]);
volume = ARFloat.get().read(parts[1]);
}
float pitch = 1.0f;
if (parts.length >= 3)
{
pitch = Float.parseFloat(parts[2]);
pitch = ARFloat.get().read(parts[2]);
}
return SoundEffect.valueOf(sound, volume, pitch);

View File

@ -18,7 +18,7 @@ import org.bukkit.plugin.Plugin;
import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.cmd.arg.ArgReader;
import com.massivecraft.massivecore.cmd.arg.AR;
import com.massivecraft.massivecore.cmd.req.Req;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.collections.MassiveList;
@ -523,9 +523,10 @@ public class MassiveCommand
public int getMaxLevenshteinDistanceForArg(String arg)
{
if (arg == null) return 0;
if (arg.length() <= 1) return 0; // When dealing with 1 character aliases, there is way too many options. So we don't suggest.
if (arg.length() <= 3) return 1; // When dealing with low length aliases, there too many options. So we won't suggest much
if (arg.length() < 8) return 2; // 2 is default.
if (arg.length() <= 4) return 1; // When dealing with low length aliases, there too many options. So we won't suggest much
if (arg.length() <= 7) return 2; // 2 is default.
return 3; // If it were 8 characters or more, we end up here. Because many characters allow for more typos.
}
@ -693,13 +694,13 @@ public class MassiveCommand
return this.getArgs().get(idx);
}
public <T> T arg(int idx, ArgReader<T> argReader) throws MassiveException
public <T> T arg(int idx, AR<T> argReader) throws MassiveException
{
String str = this.arg(idx);
return this.arg(str, argReader);
}
public <T> T arg(int idx, ArgReader<T> argReader, T defaultNotSet) throws MassiveException
public <T> T arg(int idx, AR<T> argReader, T defaultNotSet) throws MassiveException
{
String str = this.arg(idx);
return this.arg(str, argReader, defaultNotSet);
@ -716,13 +717,13 @@ public class MassiveCommand
return Txt.implode(this.getArgs().subList(from, to), " ");
}
public <T> T argConcatFrom(int idx, ArgReader<T> argReader) throws MassiveException
public <T> T argConcatFrom(int idx, AR<T> argReader) throws MassiveException
{
String str = this.argConcatFrom(idx);
return this.arg(str, argReader);
}
public <T> T argConcatFrom(int idx, ArgReader<T> argReader, T defaultNotSet) throws MassiveException
public <T> T argConcatFrom(int idx, AR<T> argReader, T defaultNotSet) throws MassiveException
{
String str = this.argConcatFrom(idx);
return this.arg(str, argReader, defaultNotSet);
@ -730,18 +731,17 @@ public class MassiveCommand
// Core & Other
public <T> T arg(ArgReader<T> argReader) throws MassiveException
public <T> T arg(AR<T> argReader) throws MassiveException
{
return this.arg(null, argReader);
}
public <T> T arg(String str, ArgReader<T> argReader) throws MassiveException
public <T> T arg(String str, AR<T> argReader) throws MassiveException
{
T result = argReader.read(str, this.sender);
return result;
return argReader.read(str, this.sender);
}
public <T> T arg(String str, ArgReader<T> argReader, T defaultNotSet) throws MassiveException
public <T> T arg(String str, AR<T> argReader, T defaultNotSet) throws MassiveException
{
if (str == null) return defaultNotSet;
return this.arg(str, argReader);

View File

@ -0,0 +1,28 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.List;
public class AMain
{
// This class is temporarily here for testing.
public static void main(String[] args)
{
try
{
System.out.println("start");
List<String> input = new ArrayList<String>();
input.add("1");
for(String str : ARAbstract.prepareForSpaces(input))
System.out.println("\"" + str + "\"");
System.out.println("end");
}
catch(Throwable t)
{
t.printStackTrace();
}
}
}

View File

@ -0,0 +1,34 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public interface AR<T>
{
// Human friendly name
public String getTypeName();
// Read
public T read(String arg, CommandSender sender) throws MassiveException;
public T read(CommandSender sender) throws MassiveException;
public T read(String arg) throws MassiveException;
public T read() throws MassiveException;
// Valid (used for arbitary argument order)
public boolean isValid(String arg, CommandSender sender);
// Tab list
// The sender is the one that tried to tab complete.
// The arg is beginning the word they are trying to tab complete.
public Collection<String> getTabList(CommandSender sender, String arg);
public List<String> getTabListFiltered(CommandSender sender, String arg);
// Sometimes we put a space after a tab completion.
// That would however not make sense with all ArgReaders.
// Default is true;
public boolean allowSpaceAfterTab();
}

View File

@ -0,0 +1,230 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.google.common.collect.Lists;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.Txt;
public abstract class ARAbstract<T> implements AR<T>
{
// -------------------------------------------- //
// OVERRIDE: READ
// -------------------------------------------- //
@Override
public T read(CommandSender sender) throws MassiveException
{
return this.read(null, sender);
}
@Override
public T read(String arg) throws MassiveException
{
return this.read(arg, null);
}
@Override
public T read() throws MassiveException
{
return this.read(null, null);
}
// -------------------------------------------- //
// OVERRIDE: VALID
// -------------------------------------------- //
@Override
public boolean isValid(String arg, CommandSender sender)
{
try
{
this.read(arg, sender);
return true;
}
catch (MassiveException ex)
{
return false;
}
}
// -------------------------------------------- //
// OVERRIDE: NAME
// -------------------------------------------- //
@Override
public String getTypeName()
{
int prefixLength = "AR".length();
String name = this.getClass().getSimpleName();
// We don't want the "AR" part
name = name.substring(prefixLength);
// We split at uppercase letters, because most class names are camel-case.
final String[] words = name.split("(?=[A-Z])");
return Txt.implode(words, " ").toLowerCase();
}
// -------------------------------------------- //
// OVERRIDE: TAB
// -------------------------------------------- //
@Override
public boolean allowSpaceAfterTab()
{
return true;
}
@Override
public List<String> getTabListFiltered(CommandSender sender, String arg)
{
// Filter them to start with what the user typed in.
Collection<String> raw = this.getTabList(sender, arg);
if (raw == null) return Collections.emptyList();
List<String> ret = Txt.getStartsWithIgnoreCase(raw, arg);
// Here we do a lot of things related to spaces.
// Because spaces and tab completion desn't go well together.
// In the future we might be able to do something better,
// but MineCraft has its limitations.
ret = prepareForSpaces(ret);
return ret;
}
// -------------------------------------------- //
// PRIVATE: TAB COMPLETE CALCULATIONS
// -------------------------------------------- //
// Should be private. But isn't currently for testing purposes.
static List<String> prepareForSpaces(List<String> suggestions)
{
List<List<String>> suggestionParts = getParts(suggestions);
// This will get the common prefix for all passed in suggestions.
// This will allow us to tab complete somethings with spaces
// if we know they all start with the same value,
// so we don't have to replace all of it.
List<String> prefix = getPrefix(suggestionParts);
// This is all the suggetions without the common prefix.
List<String> ret = withoutPreAndSuffix(suggestionParts, prefix);
// If it isn't empty and there is a prefix...
if ( ! ret.isEmpty() && ! prefix.isEmpty())
{
// ...then we want the first one to have the prefix.
// That prefix is not removed automatically,
// due to how tab completion works.
final String current = ret.get(0);
final String prefixStr = Txt.implode(prefix, " ");
String result = prefixStr;
if ( ! current.isEmpty()) result += " " + current;
ret.set(0, result);
}
return ret;
}
// This things splits up the arguments at spaces.
private static List<List<String>> getParts(List<String> list)
{
List<List<String>> ret = Lists.newArrayList();
for (String str : list)
{
if (str == null) continue;
if (str.isEmpty()) continue;
ret.add(Arrays.asList(str.split("\\s+")));
}
return ret;
}
private static List<String> withoutPreAndSuffix(List<List<String>> suggestionParts, List<String> prefix)
{
List<String> ret = new ArrayList<String>(suggestionParts.size());
boolean includesPrefix = false; // Sometimes a suggestion is equal to the prefix.
for (List<String> suggestion : suggestionParts)
{
if (suggestion.equals(prefix) && !includesPrefix)
{
ret.add("");
includesPrefix = true;
continue;
}
// We remove the prefix because we only want that once.
// But we can't keep things after the first part either
// because of spaces and stuff.
if (suggestion.size() <= prefix.size()) continue;
ret.add(suggestion.get(prefix.size()));
}
return ret;
}
private static List<String> getPrefix(List<List<String>> suggestionParts)
{
List<String> prefix = null;
for (List<String> suggestion : suggestionParts)
{
prefix = getOkay(prefix, suggestion);
}
return prefix;
}
// This method return a new array only including
// the first parts that are equal.
private static List<String> getOkay(List<String> original, List<String> compared)
{
if (original == null) return compared;
final int size = Math.min(original.size(), compared.size());
List<String> ret = new ArrayList<String>(size);
for (int i = 0; i < size; i++)
{
if (compared.get(i) == null || original.get(i) == null) break;
if ( ! compared.get(i).equals(original.get(i))) break;
ret.add(original.get(i));
}
return ret;
}
//TODO: Move this code somewhere else.
// Currently just kept here.
/*
private List<String> addSpaceAtEnd(MassiveCommand command, int argNumber, List<String> suggestions)
{
// This ArgReader must allow space at the end.
if ( ! this.allowSpaceAfterTab()) return suggestions;
// There must be exactly one suggestion.
if (suggestions.size() != 1) return suggestions;
// The maximum size of args to pass in.
int argsSize = command.getOptionalArgs().size() + command.getRequiredArgs().size();
// If it accepts infinite arguments, just make it as high as possible.
if (command.getErrorOnToManyArgs()) argsSize = Integer.MAX_VALUE;
argsSize--; // It now becomes one smaller, so the size matches index.
// So if this was the last arg meaning the user can't to type more in
// We will just stop here.
if (argNumber >= argsSize) return suggestions;
// Get the suggestion.
String suggestion = suggestions.get(0);
// Add the space at the end.
suggestion += " ";
return new MassiveCoreSingleValueList<String>(suggestion);
}*/
}

View File

@ -0,0 +1,42 @@
package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.Txt;
public abstract class ARAbstractException<T> extends ARAbstract<T>
{
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract T valueOf(String arg, CommandSender sender) throws Exception;
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public T read(String arg, CommandSender sender) throws MassiveException
{
try
{
return this.valueOf(arg, sender);
}
catch (Exception ex)
{
throw new MassiveException().addMessage(this.extractErrorMessage(arg, sender, ex));
}
}
// -------------------------------------------- //
// MESSAGE (OVERRIDABLE)
// -------------------------------------------- //
public String extractErrorMessage(String arg, CommandSender sender, Exception ex)
{
return Txt.parse("<b>%s", ex.getMessage());
}
}

View File

@ -0,0 +1,27 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bukkit.command.CommandSender;
public abstract class ARAbstractNumber<T extends Number> extends ARAbstractPrimitive<T>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final List<String> TAB_LIST = Collections.singletonList("1");
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return TAB_LIST;
}
}

View File

@ -2,36 +2,18 @@ package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.Txt;
public abstract class ARAbstractPrimitive<T> extends ArgReaderAbstract<T>
public abstract class ARAbstractPrimitive<T> extends ARAbstractException<T>
{
// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
public abstract String typename();
public abstract T convert(String arg) throws Exception;
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public T read(String arg, CommandSender sender) throws MassiveException
public String extractErrorMessage(String arg, CommandSender sender, Exception ex)
{
T result;
try
{
result = this.convert(arg);
}
catch (Exception e)
{
throw new MassiveException().addMsg("<b>Invalid %s \"<h>%s<b>\".", this.typename(), arg);
}
return result;
return Txt.parse("<b>\"<h>%s\"<b> is not a %s.", arg, this.getTypeName());
}
}

View File

@ -2,6 +2,7 @@ package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
@ -10,7 +11,7 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.Txt;
public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
public abstract class ARAbstractSelect<T> extends ARAbstract<T>
{
// -------------------------------------------- //
// CONSTANT
@ -22,7 +23,6 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
// ABSTRACT
// -------------------------------------------- //
public abstract String typename();
public abstract T select(String str, CommandSender sender) throws MassiveException;
public abstract Collection<String> altNames(CommandSender sender);
public boolean canList(CommandSender sender) { return true; }
@ -39,7 +39,7 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
if (result != null) return result;
MassiveException exception = new MassiveException();
exception.addMsg("<b>No %s matches \"<h>%s<b>\".", this.typename(), arg);
exception.addMsg("<b>No %s matches \"<h>%s<b>\".", this.getTypeName(), arg);
if (this.canList(sender))
{
@ -50,7 +50,7 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
if (names.isEmpty())
{
exception.addMsg("<i>Note: There is no %s available.", this.typename());
exception.addMsg("<i>Note: There is no %s available.", this.getTypeName());
}
else if ( ! matches.isEmpty() && matches.size() < LIST_COUNT_MAX)
{
@ -77,6 +77,7 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
public List<String> getMatchingAltNames(String arg, CommandSender sender, int maxLevenshteinDistance)
{
if (arg == null) return Collections.emptyList(); // For some apparent reason this is required.
arg = arg.toLowerCase();
// Try Levenshtein
@ -94,8 +95,9 @@ public abstract class ARAbstractSelect<T> extends ArgReaderAbstract<T>
public int getMaxLevenshteinDistanceForArg(String arg)
{
if (arg == null) return 0; // For some apparent reason this is required.
if (arg.length() <= 1) return 0; // When dealing with 1 character aliases, there is way too many options.
if (arg.length() < 8) return 1; // 1 is default.
if (arg.length() <= 7) return 1; // 1 is default.
return 2; // If it were 8 characters or more, we end up here. Because many characters allow for more typos.
}

View File

@ -0,0 +1,64 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
public class ARAll<T> extends ARAbstract<Collection<T>>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private ARAllAble<T> innerArgReader;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public ARAll(ARAllAble<T> inner)
{
if (inner == null) throw new IllegalArgumentException("innerArgReader musn't be null");
this.innerArgReader = inner;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
return innerArgReader.getTypeName();
}
@Override
public Collection<T> read(String arg, CommandSender sender) throws MassiveException
{
if ("all".equalsIgnoreCase(arg)) return innerArgReader.getAll();
T innerRet = innerArgReader.read(arg, sender);
List<T> ret = new MassiveList<T>();
ret.add(innerRet);
return ret;
}
@Override
public boolean isValid(String arg, CommandSender sender)
{
return "all".equalsIgnoreCase(arg) || innerArgReader.isValid(arg, sender);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return innerArgReader.getTabList(sender, arg);
}
}

View File

@ -0,0 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
public interface ARAllAble<T> extends AR<T>
{
public Collection<T> getAll();
}

View File

@ -21,12 +21,6 @@ public class ARAspect extends ARAbstractSelect<Aspect>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "aspect";
}
@Override
public Aspect select(String arg, CommandSender sender)
{
@ -45,4 +39,10 @@ public class ARAspect extends ARAbstractSelect<Aspect>
return AspectColl.get().getIds();
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
}

View File

@ -20,21 +20,14 @@ public class ARBiome extends ARAbstractSelect<Biome>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "biome";
}
@Override
public Biome select(String arg, CommandSender sender)
{
arg = getComparable(arg);
String biomestr;
for (Biome biome : Biome.values())
{
biomestr = getComparable(biome.name());
String biomestr = getComparable(biome.name());
if (biomestr.equals(arg))
{
return biome;
@ -64,16 +57,30 @@ public class ARBiome extends ARAbstractSelect<Biome>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
List<String> ret = new ArrayList<String>();
for (Biome biome : Biome.values())
{
ret.add(getComparable(biome.name()));
}
return ret;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(String str)
public static String getComparable(String string)
{
str = str.toLowerCase();
str = str.replace("_", "");
str = str.replace(" ", "");
return str;
if (string == null) return null;
string = string.toLowerCase();
string = string.replace("_", "");
string = string.replace(" ", "");
return string;
}
}

View File

@ -1,7 +1,30 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.util.MUtil;
public class ARBoolean extends ARAbstractPrimitive<Boolean>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final Set<String> TRUE_OPTIONS = MUtil.set(
"y", "ye", "yes",
"on",
"t", "tr", "tru", "true");
public static final Set<String> FALSE_OPTIONS = MUtil.set(
"n", "no",
"of", "off",
"f", "fa", "fal", "fals", "false");
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@ -14,20 +37,43 @@ public class ARBoolean extends ARAbstractPrimitive<Boolean>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "boolean";
return "toggle";
}
@Override
public Boolean convert(String arg) throws Exception
public Boolean valueOf(String arg, CommandSender sender) throws Exception
{
arg = arg.toLowerCase();
if (arg.startsWith("y") || arg.startsWith("t") || arg.startsWith("on") || arg.startsWith("+") || arg.startsWith("1"))
{
return true;
if (TRUE_OPTIONS.contains(arg)) return Boolean.TRUE;
if (FALSE_OPTIONS.contains(arg)) return Boolean.FALSE;
throw new Exception();
}
return false;
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
List<String> ret = new ArrayList<String>();
// Default yes and no.
ret.add("yes");
ret.add("no");
// If it is empty we just want to show yes and no
// else we might want to show other things.
// We can safely add them because it is filtered.
if ( ! arg.isEmpty())
{
ret.add("true");
ret.add("false");
ret.add("on");
ret.add("off");
}
return ret;
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
public class ARByte extends ARAbstractPrimitive<Byte>
import org.bukkit.command.CommandSender;
public class ARByte extends ARAbstractNumber<Byte>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,13 +16,13 @@ public class ARByte extends ARAbstractPrimitive<Byte>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "byte";
return "small number";
}
@Override
public Byte convert(String arg) throws Exception
public Byte valueOf(String arg, CommandSender sender) throws Exception
{
return Byte.parseByte(arg);
}

View File

@ -20,28 +20,19 @@ public class ARChatColor extends ARAbstractSelect<ChatColor>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "chat color";
}
@Override
public ChatColor select(String arg, CommandSender sender)
{
ChatColor ret = null;
arg = getToCompare(arg);
arg = getComparable(arg);
for (ChatColor cc : ChatColor.values())
{
String ccstr = getToCompare(cc.name());
String ccstr = getComparable(cc.name());
if ( ! ccstr.equals(arg)) continue;
ret = cc;
break;
return cc;
}
return ret;
return null;
}
@Override
@ -51,7 +42,20 @@ public class ARChatColor extends ARAbstractSelect<ChatColor>
for (ChatColor cc : ChatColor.values())
{
ret.add(cc.toString()+getToCompare(cc.name()));
ret.add(cc.toString() + getComparable(cc.name()));
}
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
List<String> ret = new ArrayList<String>();
for (ChatColor cc : ChatColor.values())
{
ret.add(getComparable(cc.name()));
}
return ret;
@ -63,12 +67,12 @@ public class ARChatColor extends ARAbstractSelect<ChatColor>
// "DARK_RED" --> "darkred"
// "DARK RED" --> "darkred"
public static String getToCompare(String str)
public static String getComparable(String string)
{
str = str.toLowerCase();
str = str.replace("_", "");
str = str.replace(" ", "");
return str;
string = string.toLowerCase();
string = string.replace("_", "");
string = string.replace(" ", "");
return string;
}
}

View File

@ -19,12 +19,6 @@ public class ARColl extends ARAbstractSelect<Coll<?>>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "coll";
}
@Override
public Coll<?> select(String arg, CommandSender sender)
{
@ -37,4 +31,10 @@ public class ARColl extends ARAbstractSelect<Coll<?>>
return Coll.getNames();
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
}

View File

@ -9,27 +9,27 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
public class ARCombined extends ArgReaderAbstract<List<?>>
public class ARCombined extends ARAbstract<List<?>>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected List<ArgReader<?>> inners;
public List<ArgReader<?>> getInners() { return this.inners; }
protected List<AR<?>> inners;
public List<AR<?>> getInners() { return this.inners; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static ARCombined get(Collection<ArgReader<?>> inners) { return new ARCombined(inners); }
public static ARCombined get(ArgReader<?>... inners) { return new ARCombined(inners); }
public static ARCombined get(Collection<AR<?>> inners) { return new ARCombined(inners); }
public static ARCombined get(AR<?>... inners) { return new ARCombined(inners); }
public ARCombined(Collection<ArgReader<?>> inners)
public ARCombined(Collection<AR<?>> inners)
{
this.inners = new MassiveList<ArgReader<?>>(inners);
this.inners = new MassiveList<AR<?>>(inners);
}
public ARCombined(ArgReader<?>... inners)
public ARCombined(AR<?>... inners)
{
this(Arrays.asList(inners));
}
@ -38,6 +38,14 @@ public class ARCombined extends ArgReaderAbstract<List<?>>
// OVERRIDE
// -------------------------------------------- //
//TODO: How to do this?
/*@Override
public String getTypeName()
{
}*/
@Override
public List<?> read(String arg, CommandSender sender) throws MassiveException
{
@ -54,7 +62,7 @@ public class ARCombined extends ArgReaderAbstract<List<?>>
for (int i = 0; i < parts.size(); i++)
{
String part = parts.get(i);
ArgReader<?> argReader = this.getInners().get(i);
AR<?> argReader = this.getInners().get(i);
Object asdf = argReader.read(part, sender);
@ -65,4 +73,11 @@ public class ARCombined extends ArgReaderAbstract<List<?>>
return ret;
}
//TODO: How to do this?
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return null; // ???
}
}

View File

@ -2,8 +2,12 @@ package com.massivecraft.massivecore.cmd.arg;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import org.bukkit.command.CommandSender;
public class ARDate extends ARAbstractPrimitive<Date>
{
// -------------------------------------------- //
@ -24,15 +28,21 @@ public class ARDate extends ARAbstractPrimitive<Date>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "YYYY-MM-DD date";
}
@Override
public Date convert(String arg) throws Exception
public Date valueOf(String arg, CommandSender sender) throws Exception
{
return DATE_FORMAT.parse(arg);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptySet();
}
}

View File

@ -1,12 +1,14 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.event.EventMassiveCoreDestination;
import com.massivecraft.massivecore.teleport.Destination;
public class ARDestination extends ArgReaderAbstract<Destination>
public class ARDestination extends ARAbstract<Destination>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -36,4 +38,10 @@ public class ARDestination extends ArgReaderAbstract<Destination>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return null;
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bukkit.Difficulty;
import org.bukkit.command.CommandSender;
@ -9,6 +11,12 @@ import com.massivecraft.massivecore.util.MUtil;
public class ARDifficulty extends ARAbstractSelect<Difficulty>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final List<String> ALT_NAMES = Collections.unmodifiableList(MUtil.list("peaceful", "easy", "normal", "hard"));
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@ -20,43 +28,41 @@ public class ARDifficulty extends ARAbstractSelect<Difficulty>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "difficulty";
}
@Override
public Difficulty select(String arg, CommandSender sender)
{
Difficulty ret = null;
arg = arg.toLowerCase();
if (arg.startsWith("p"))
{
ret = Difficulty.PEACEFUL;
return Difficulty.PEACEFUL;
}
else if (arg.startsWith("e"))
{
ret = Difficulty.EASY;
return Difficulty.EASY;
}
else if (arg.startsWith("n"))
{
ret = Difficulty.NORMAL;
return Difficulty.NORMAL;
}
else if (arg.startsWith("h"))
{
ret = Difficulty.HARD;
return Difficulty.HARD;
}
return ret;
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
return MUtil.list("peaceful", "easy", "normal", "hard");
return ALT_NAMES;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
public class ARDouble extends ARAbstractPrimitive<Double>
import org.bukkit.command.CommandSender;
public class ARDouble extends ARAbstractNumber<Double>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,13 +16,13 @@ public class ARDouble extends ARAbstractPrimitive<Double>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "double";
return "number with decimals";
}
@Override
public Double convert(String arg) throws Exception
public Double valueOf(String arg, CommandSender sender) throws Exception
{
return Double.parseDouble(arg);
}

View File

@ -20,24 +20,19 @@ public class AREntityType extends ARAbstractSelect<EntityType>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "entity type";
}
@Override
public EntityType select(String arg, CommandSender sender)
{
arg = getComparable(arg);
// Custom Detection
if (arg.contains("pig") && (arg.contains("man") || arg.contains("zombie"))) return EntityType.PIG_ZOMBIE;
if (arg.contains("pig") && ((arg.contains("man") || arg.contains("zombie")))) return EntityType.PIG_ZOMBIE;
// Algorithmic General Detection
for (EntityType entityType : EntityType.values())
{
if (getComparable(entityType.toString()).equals(arg)) return entityType;
String compare = getComparable(entityType);
if (compare.equals(arg)) return entityType;
}
// Nothing found
@ -50,13 +45,30 @@ public class AREntityType extends ARAbstractSelect<EntityType>
List<String> ret = new ArrayList<String>();
for (EntityType entityType : EntityType.values())
{
ret.add(getComparable(entityType.toString()));
ret.add(getComparable(entityType));
}
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(EntityType entityType)
{
if (entityType == null) return null;
return getComparable(entityType.toString());
}
public static String getComparable(String string)
{
if (string == null) return null;
return string.toLowerCase().replaceAll("[_\\-\\s]+", "");
}

View File

@ -1,6 +1,5 @@
package com.massivecraft.massivecore.cmd.arg;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -28,6 +27,7 @@ public class AREnum<T> extends ARAbstractSelect<T>
public AREnum(Class<T> clazz)
{
if ( ! clazz.isEnum()) throw new IllegalArgumentException("passed clazz param must be an enum");
this.clazz = clazz;
}
@ -36,7 +36,7 @@ public class AREnum<T> extends ARAbstractSelect<T>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return Txt.getNicedEnumString(clazz.getSimpleName());
}
@ -50,63 +50,67 @@ public class AREnum<T> extends ARAbstractSelect<T>
// Algorithmic General Detection
int startswithCount = 0;
T startswith = null;
for (T value : getEnumValues(this.clazz))
T startsWith = null;
for (T value : getEnumValues(clazz))
{
String comparable = getComparable(value.toString());
String comparable = getComparable(value);
if (comparable.equals(arg)) return value;
if (comparable.startsWith(arg))
{
startswith = value;
startswithCount++;
}
}
// If there already were a result
// we have multiple results and stop.
if (startsWith != null) return null;
if (startswithCount == 1)
{
return startswith;
// Else we set the result.
startsWith = value;
}
}
// Nothing found
return null;
return startsWith;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
List<String> ret = new ArrayList<String>();
for (T value : getEnumValues(this.clazz))
for (T value : getEnumValues(clazz))
{
ret.add(getComparable(value.toString()));
ret.add(getComparable(value));
}
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static <T> T[] getEnumValues(Class<T> clazz)
{
if ( ! clazz.isEnum()) throw new IllegalArgumentException("passed clazz param must be an enum");
T[] ret = clazz.getEnumConstants();
if (ret == null) throw new RuntimeException("failed to retrieve enum constants");
return ret;
}
public static String getComparable(Object value)
{
if (value == null) return null;
return getComparable(value.toString());
}
public static String getComparable(String string)
{
if (string == null) return null;
return string.toLowerCase().replaceAll("[_\\-\\s]+", "");
}
@SuppressWarnings("unchecked")
public static <T> T[] getEnumValues(Class<T> clazz)
{
try
{
Method method = clazz.getMethod("values");
Object o = method.invoke(null);
return (T[]) o;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}

View File

@ -1,7 +1,10 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
@ -9,6 +12,12 @@ import com.massivecraft.massivecore.util.MUtil;
public class AREnvironment extends ARAbstractSelect<Environment>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final List<String> ALT_NAMES = Collections.unmodifiableList(MUtil.list("normal", "end", "nether"));
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@ -20,45 +29,64 @@ public class AREnvironment extends ARAbstractSelect<Environment>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "environment";
}
@Override
public Environment select(String arg, CommandSender sender)
{
Environment ret = null;
// "THE_END" --> "end"
arg = arg.toLowerCase();
arg = arg.replace("_", "");
arg = arg.replace("the", "");
arg = getComparable(arg);
if (arg.startsWith("no") || arg.startsWith("d"))
{
// "normal" or "default"
ret = Environment.NORMAL;
return Environment.NORMAL;
}
else if (arg.startsWith("ne"))
{
// "nether"
ret = Environment.NETHER;
return Environment.NETHER;
}
else if (arg.startsWith("e"))
{
// "end"
ret = Environment.THE_END;
return Environment.THE_END;
}
return ret;
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
return MUtil.list("normal", "end", "nether");
return ALT_NAMES;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Collection<String> ret = this.altNames(sender);
// The_end or the_nether
if (StringUtils.startsWithIgnoreCase(arg, "t"))
{
ret.add("the_end");
ret.add("the_nether");
}
return ret;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(String str)
{
str = str.toLowerCase();
str = str.replace(" ", "");
str = str.replace("_", "");
str = str.replace("the", "");
return str;
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
public class ARFloat extends ARAbstractPrimitive<Float>
import org.bukkit.command.CommandSender;
public class ARFloat extends ARAbstractNumber<Float>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,13 +16,13 @@ public class ARFloat extends ARAbstractPrimitive<Float>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "integer";
return "number with decimals";
}
@Override
public Float convert(String arg) throws Exception
public Float valueOf(String arg, CommandSender sender) throws Exception
{
return Float.parseFloat(arg);
}

View File

@ -1,11 +1,13 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
public class ARGameMode extends ARAbstractSelect<GameMode>
{
@ -20,39 +22,55 @@ public class ARGameMode extends ARAbstractSelect<GameMode>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "game mode";
}
@Override
public GameMode select(String arg, CommandSender sender)
{
GameMode ret = null;
arg = getComparable(arg);
arg = arg.toLowerCase();
if (arg.length() < 2) return null; // Some gamemodes have the same beginning character. So we need atleast 2 characters.
if (arg.startsWith("s") || arg.equals("0"))
for (GameMode gm : GameMode.values())
{
ret = GameMode.SURVIVAL;
}
else if (arg.startsWith("c") || arg.equals("1"))
{
ret = GameMode.CREATIVE;
}
else if (arg.startsWith("a") || arg.equals("2"))
{
ret = GameMode.ADVENTURE;
// Comparable
String compare = getComparable(gm);
if (compare.startsWith(arg)) return gm;
}
return ret;
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
return MUtil.list("survival", "creative", "adventure");
List<String> ret = new ArrayList<String>();
for (GameMode gm : GameMode.values())
{
ret.add(Txt.getNicedEnum(gm));
}
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(GameMode gamemode)
{
if (gamemode == null) return null;
return getComparable(gamemode.name());
}
public static String getComparable(String string)
{
if (string == null) return null;
return string.toLowerCase();
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
public class ARInteger extends ARAbstractPrimitive<Integer>
import org.bukkit.command.CommandSender;
public class ARInteger extends ARAbstractNumber<Integer>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,13 +16,13 @@ public class ARInteger extends ARAbstractPrimitive<Integer>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "integer";
return "number";
}
@Override
public Integer convert(String arg) throws Exception
public Integer valueOf(String arg, CommandSender sender) throws Exception
{
return Integer.parseInt(arg);
}

View File

@ -1,31 +1,32 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public class ARList<T> extends ArgReaderAbstract<List<T>>
public class ARList<T> extends ARAbstract<List<T>>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected ArgReader<T> innerArgReader;
public ArgReader<T> getInnerArgReader() { return this.innerArgReader; }
protected AR<T> innerArgReader;
public AR<T> getInnerArgReader() { return this.innerArgReader; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ARList<T> get(ArgReader<T> innerArgReader)
public static <T> ARList<T> get(AR<T> innerArgReader)
{
return new ARList<T>(innerArgReader);
}
public ARList(ArgReader<T> innerArgReader)
public ARList(AR<T> innerArgReader)
{
this.innerArgReader = innerArgReader;
}
@ -34,6 +35,12 @@ public class ARList<T> extends ArgReaderAbstract<List<T>>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
return innerArgReader.getTypeName();
}
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
@Override
public List<T> read(String arg, CommandSender sender) throws MassiveException
@ -56,4 +63,10 @@ public class ARList<T> extends ArgReaderAbstract<List<T>>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.getInnerArgReader().getTabList(sender, arg);
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
public class ARLong extends ARAbstractPrimitive<Long>
import org.bukkit.command.CommandSender;
public class ARLong extends ARAbstractNumber<Long>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -14,13 +16,13 @@ public class ARLong extends ARAbstractPrimitive<Long>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "long";
return "number";
}
@Override
public Long convert(String arg) throws Exception
public Long valueOf(String arg, CommandSender sender) throws Exception
{
return Long.parseLong(arg);
}

View File

@ -1,11 +1,15 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public class ARMaterial extends ArgReaderAbstract<Material>
public class ARMaterial extends ARAbstract<Material>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -24,10 +28,22 @@ public class ARMaterial extends ArgReaderAbstract<Material>
Material ret = Material.matchMaterial(arg);
if (ret == null)
{
MassiveException exception = new MassiveException();
exception.addMsg("<b>No material matches <h>%s<b>.", arg);
exception.addMsg("<i>Suggestion: <aqua>http://www.minecraftwiki.net/wiki/Data_values");
throw exception;
throw new MassiveException()
.addMsg("<b>No material matches <h>%s<b>.", arg)
.addMsg("<i>https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Material.java");
}
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (Material material : Material.values())
{
ret.add(material.name());
}
return ret;

View File

@ -1,11 +1,13 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.TimeDiffUtil;
public class ARMillisDiff extends ArgReaderAbstract<Long>
public class ARMillisDiff extends ARAbstractException<Long>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -19,19 +21,21 @@ public class ARMillisDiff extends ArgReaderAbstract<Long>
// -------------------------------------------- //
@Override
public Long read(String arg, CommandSender sender) throws MassiveException
public String getTypeName()
{
Long ret;
try
{
ret = TimeDiffUtil.millis(arg);
}
catch (Exception e)
{
throw new MassiveException().addMsg("<b>%s", e.getMessage());
return "time amount";
}
return ret;
@Override
public Long valueOf(String arg, CommandSender sender) throws Exception
{
return TimeDiffUtil.millis(arg);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptySet();
}
}

View File

@ -21,12 +21,6 @@ public class ARMultiverse extends ARAbstractSelect<Multiverse>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "multiverse";
}
@Override
public Multiverse select(String arg, CommandSender sender)
{
@ -45,4 +39,10 @@ public class ARMultiverse extends ARAbstractSelect<Multiverse>
return MultiverseColl.get().getIds();
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
}

View File

@ -1,23 +1,26 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveException;
public class ARNullable<T> extends ArgReaderAbstract<T>
public class ARNullable<T> extends ARAbstract<T>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ARNullable<T> get(ArgReader<T> inner)
public static <T> ARNullable<T> get(AR<T> inner)
{
return new ARNullable<T>(inner);
}
public ARNullable(ArgReader<T> inner)
public ARNullable(AR<T> inner)
{
if (inner == null) throw new IllegalArgumentException("inner param is null");
this.inner = inner;
}
@ -25,13 +28,19 @@ public class ARNullable<T> extends ArgReaderAbstract<T>
// FIELDS
// -------------------------------------------- //
protected ArgReader<T> inner;
public ArgReader<T> getInner() { return this.inner; }
protected AR<T> inner;
public AR<T> getInner() { return this.inner; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
return this.getInner().getTypeName();
}
@Override
public T read(String arg, CommandSender sender) throws MassiveException
{
@ -40,4 +49,10 @@ public class ARNullable<T> extends ArgReaderAbstract<T>
return this.getInner().read(arg, sender);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.getInner().getTabList(sender, arg);
}
}

View File

@ -1,6 +1,7 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
@ -11,7 +12,7 @@ import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.ps.PSBuilder;
import com.mysql.jdbc.StringUtils;
public class ARPS extends ArgReaderAbstract<PS>
public class ARPS extends ARAbstract<PS>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -203,6 +204,12 @@ public class ARPS extends ArgReaderAbstract<PS>
return ret.build();
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return null;
}
public static String getValue(String entry, String... prefixes)
{
for (String prefix : prefixes)

View File

@ -1,12 +1,16 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import com.massivecraft.massivecore.MassiveException;
public class ARPermission extends ArgReaderAbstract<Permission>
public class ARPermission extends ARAbstract<Permission>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -22,20 +26,25 @@ public class ARPermission extends ArgReaderAbstract<Permission>
@Override
public Permission read(String arg, CommandSender sender) throws MassiveException
{
Permission ret = null;
for (Permission permission : Bukkit.getPluginManager().getPermissions())
{
if ( ! permission.getName().equals(arg)) continue;
ret = permission;
break;
return permission;
}
if (ret == null)
{
throw new MassiveException().addMsg("<b>No permission with the name \"<h>%s<b>\" was found.", arg);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (Permission perm : Bukkit.getPluginManager().getPermissions())
{
ret.add(perm.getName());
}
return ret;
}

View File

@ -1,9 +1,16 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
public class ARPlayer extends ARSenderIdAbstract<Player>
{
@ -25,8 +32,23 @@ public class ARPlayer extends ARSenderIdAbstract<Player>
@Override
public Player getResultForSenderId(String senderId)
{
if (senderId == null) return null;
// Null check is done in IdUtil :)
return IdUtil.getPlayer(senderId);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String name : IdUtil.getOnlineNames())
{
if ( ! MUtil.isValidPlayerName(name)) continue;
if ( ! Mixin.canSee(sender, name)) continue;
ret.add(name);
}
return ret;
}
}

View File

@ -22,12 +22,6 @@ public class ARPotionEffectType extends ARAbstractSelect<PotionEffectType>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "potion effect type";
}
@Override
public PotionEffectType select(String str, CommandSender sender) throws MassiveException
{
@ -65,6 +59,16 @@ public class ARPotionEffectType extends ARAbstractSelect<PotionEffectType>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(PotionEffectType potionEffectType)
{
if (potionEffectType == null) return null;

View File

@ -1,5 +1,6 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
@ -8,7 +9,7 @@ import org.bukkit.potion.PotionEffectType;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.PotionEffectWrap;
public class ARPotionEffectWrap extends ArgReaderAbstract<PotionEffectWrap>
public class ARPotionEffectWrap extends ARAbstract<PotionEffectWrap>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -77,4 +78,10 @@ public class ARPotionEffectWrap extends ArgReaderAbstract<PotionEffectWrap>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return ARPotionEffectType.get().getTabList(sender, arg);
}
}

View File

@ -1,7 +1,12 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil;
@ -25,8 +30,22 @@ public class ARSender extends ARSenderIdAbstract<CommandSender>
@Override
public CommandSender getResultForSenderId(String senderId)
{
if (senderId == null) return null;
//Null check is done in IdUtil
return IdUtil.getSender(senderId);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String name : IdUtil.getOnlineNames())
{
if ( ! Mixin.canSee(sender, name)) continue;
ret.add(name);
}
return ret;
}
}

View File

@ -1,7 +1,15 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.store.SenderEntity;
import com.massivecraft.massivecore.util.IdUtil;
public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstract<T>
{
@ -41,8 +49,22 @@ public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstrac
@Override
public T getResultForSenderId(String senderId)
{
if (senderId == null) return null;
// Null check is done in SenderColl & IdUtil :)
return this.coll.get(senderId);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String name : IdUtil.getOnlineNames())
{
if ( ! Mixin.canSee(sender, name)) continue;
ret.add(name);
}
return ret;
}
}

View File

@ -1,7 +1,15 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil;
public class ARSenderId extends ARSenderIdAbstract<String>
{
@ -43,4 +51,18 @@ public class ARSenderId extends ARSenderIdAbstract<String>
return senderId;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String id : IdUtil.getOnlineIds())
{
if ( ! Mixin.canSee(sender, id)) continue;
ret.add(id);
}
return ret;
}
}

View File

@ -9,7 +9,7 @@ import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.util.IdUtil;
public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
{
// -------------------------------------------- //
// FIELDS
@ -43,6 +43,13 @@ public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
if (online) return "online player";
else return "player";
}
@Override
public T read(String arg, CommandSender sender) throws MassiveException
{
@ -60,7 +67,7 @@ public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
if (ret == null)
{
// No alternatives found
throw new MassiveException().addMsg("<b>No player matches \"<h>%s<b>\".", arg);
throw new MassiveException().addMsg("<b>No %s matches \"<h>%s<b>\".", this.getTypeName(), arg);
}
// Return Ret
@ -83,7 +90,7 @@ public abstract class ARSenderIdAbstract<T> extends ArgReaderAbstract<T>
for (Collection<String> coll : this.source.getSenderIdCollections())
{
// If the senderId exists ...
if (!coll.contains(senderId)) continue;
if ( ! coll.contains(senderId)) continue;
// ... and the online check passes ...
if (this.online && !Mixin.isOnline(senderId)) continue;

View File

@ -1,35 +1,36 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.mixin.Mixin;
public class ARSet<T> extends ArgReaderAbstract<Set<T>>
public class ARSet<T> extends ARAbstract<Set<T>>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
protected ArgReader<T> innerArgReader;
public ArgReader<T> getInnerArgReader() { return this.innerArgReader; }
private final AR<T> innerArgReader;
public AR<T> getInnerArgReader() { return this.innerArgReader; }
protected boolean warnOnDuplicates;
private final boolean warnOnDuplicates;
public boolean getWarnOnDuplicate() { return warnOnDuplicates; }
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static <T> ARSet<T> get(ArgReader<T> innerArgReader, boolean warnOnDuplicates)
public static <T> ARSet<T> get(AR<T> innerArgReader, boolean warnOnDuplicates)
{
return new ARSet<T>(innerArgReader, warnOnDuplicates);
}
public ARSet(ArgReader<T> innerArgReader, boolean warnOnDuplicates)
public ARSet(AR<T> innerArgReader, boolean warnOnDuplicates)
{
this.innerArgReader = innerArgReader;
this.warnOnDuplicates = warnOnDuplicates;
@ -39,6 +40,12 @@ public class ARSet<T> extends ArgReaderAbstract<Set<T>>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
return innerArgReader.getTypeName();
}
// NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false).
@Override
public Set<T> read(String arg, CommandSender sender) throws MassiveException
@ -61,11 +68,17 @@ public class ARSet<T> extends ArgReaderAbstract<Set<T>>
if (warnOnDuplicates && duplicates)
{
sender.sendMessage(Txt.parse("<i>Some duplicate arguments were removed"));
Mixin.msgOne(sender, "<i>Some duplicate command input were removed.");
}
// Return Ret
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return innerArgReader.getTabList(sender, arg);
}
}

View File

@ -1,11 +1,15 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public class ARSound extends ArgReaderAbstract<Sound>
public class ARSound extends ARAbstract<Sound>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -24,31 +28,52 @@ public class ARSound extends ArgReaderAbstract<Sound>
Sound result = getSoundFromString(arg);
if (result == null)
{
MassiveException errors = new MassiveException();
errors.addMsg("<b>No sound matches \"<h>%s<b>\".", arg);
errors.addMsg("<aqua>https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Sound.java");
throw errors;
throw new MassiveException()
.addMsg("<b>No sound matches \"<h>%s<b>\".", arg)
.addMsg("<aqua>https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Sound.java");
}
return result;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (Sound sound : Sound.values())
{
String name = getComparable(sound);
ret.add(name);
}
return ret;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static Sound getSoundFromString(String string)
public static Sound getSoundFromString(String str)
{
String string1 = getCompareString(string);
if (str == null) return null;
String string1 = getComparable(str);
for (Sound sound : Sound.values())
{
String string2 = getCompareString(sound.name());
if (string1.equals(string2)) return sound;
String compare = getComparable(sound);
if (string1.equals(compare)) return sound;
}
return null;
}
public static String getCompareString(String string)
public static String getComparable(Sound sound)
{
if (sound == null) return null;
return getComparable(sound.name());
}
public static String getComparable(String string)
{
if (string == null) return null;
string = string.toLowerCase();
string = string.replaceAll("[^a-zA-Z0-9]", "");
return string;

View File

@ -1,11 +1,14 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Set;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.SoundEffect;
import com.massivecraft.massivecore.collections.MassiveSet;
public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
public class ARSoundEffect extends ARAbstractException<SoundEffect>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -19,19 +22,28 @@ public class ARSoundEffect extends ArgReaderAbstract<SoundEffect>
// -------------------------------------------- //
@Override
public SoundEffect read(String arg, CommandSender sender) throws MassiveException
public SoundEffect valueOf(String arg, CommandSender sender) throws Exception
{
SoundEffect ret;
return SoundEffect.valueOf(arg);
}
try
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
ret = SoundEffect.valueOf(arg);
}
catch (Exception e)
Set<String> ret = new MassiveSet<String>();
for (String str : ARSound.get().getTabList(sender, arg))
{
throw new MassiveException().addMsg("<b>%s", e.getMessage());
ret.add(str + "-");
}
return ret;
}
@Override
public boolean allowSpaceAfterTab()
{
return false;
}
}

View File

@ -1,53 +0,0 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.SoundEffect;
/**
* @deprecated use ARList
*/
@Deprecated
public class ARSoundEffects extends ArgReaderAbstract<List<SoundEffect>>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ARSoundEffects i = new ARSoundEffects();
public static ARSoundEffects get() { return i; }
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public List<SoundEffect> read(String arg, CommandSender sender) throws MassiveException
{
List<SoundEffect> ret = new ArrayList<SoundEffect>();
List<SoundEffect> result = new ArrayList<SoundEffect>();
arg = arg.trim();
List<String> soundStrings = Arrays.asList(arg.split("\\s+"));
try
{
for (String soundString : soundStrings)
{
result.add(SoundEffect.valueOf(soundString));
}
ret = result;
}
catch (Exception e)
{
throw new MassiveException().addMsg("<b>%s", e.getMessage());
}
return ret;
}
}

View File

@ -1,8 +1,11 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import org.bukkit.command.CommandSender;
public class ARString extends ArgReaderAbstract<String>
public class ARString extends ARAbstract<String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -15,10 +18,22 @@ public class ARString extends ArgReaderAbstract<String>
// OVERRIDE
// -------------------------------------------- //
@Override
public String getTypeName()
{
return "text";
}
@Override
public String read(String arg, CommandSender sender)
{
return arg;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Collections.emptySet();
}
}

View File

@ -7,9 +7,10 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.Multiverse;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.Txt;
public class ARUniverse extends ArgReaderAbstract<String>
public class ARUniverse extends ARAbstract<String>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -25,7 +26,7 @@ public class ARUniverse extends ArgReaderAbstract<String>
// -------------------------------------------- //
protected Multiverse multiverse;
public Multiverse multiverse() { return this.multiverse; }
public Multiverse getMultiverse() { return this.multiverse; }
// -------------------------------------------- //
// OVERRIDE
@ -34,28 +35,29 @@ public class ARUniverse extends ArgReaderAbstract<String>
@Override
public String read(String arg, CommandSender sender) throws MassiveException
{
String result = new String();
if (multiverse.containsUniverse(arg))
{
result = arg;
return arg;
}
else
{
MassiveException exception = new MassiveException();
exception.addMsg("<b>No universe \"<h>%s<b>\" exists in multiverse <h>%s<b>.", arg, this.multiverse.getId());
Collection<String> names = new ArrayList<String>(multiverse.getUniverses());
String format = Txt.parse("<h>%s");
String comma = Txt.parse("<i>, ");
String and = Txt.parse(" <i>or ");
String dot = Txt.parse("<i>.");
exception.addMsg("<i>Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot));
throw exception;
throw new MassiveException()
.addMsg("<b>No universe \"<h>%s<b>\" exists in multiverse <h>%s<b>.", arg, this.multiverse.getId())
.addMsg("<i>Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot));
}
}
return result;
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
// MassiveSet is linked.
return new MassiveSet<String>(multiverse.getUniverses());
}
}

View File

@ -1,12 +1,14 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public class ARWorld extends ArgReaderAbstract<World>
public class ARWorld extends ARAbstract<World>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
@ -24,9 +26,7 @@ public class ARWorld extends ArgReaderAbstract<World>
{
World ret;
String inner = ARWorldId.get().read(arg, sender);
String worldId = inner;
String worldId = ARWorldId.get().read(arg, sender);
ret = Bukkit.getWorld(worldId);
@ -38,4 +38,10 @@ public class ARWorld extends ArgReaderAbstract<World>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return ARWorldId.get().getTabList(sender, arg);
}
}

View File

@ -1,6 +1,7 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.command.CommandSender;
@ -21,7 +22,7 @@ public class ARWorldId extends ARAbstractSelect<String>
// -------------------------------------------- //
@Override
public String typename()
public String getTypeName()
{
return "world";
}
@ -33,13 +34,15 @@ public class ARWorldId extends ARAbstractSelect<String>
for (String worldId : visibleWorldIds)
{
if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
// This was already done above in Mixin.getVisibleWorldIds(sender);
// if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
if (arg.equalsIgnoreCase(worldId)) return worldId;
}
for (String worldId : visibleWorldIds)
{
if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
// This was already done above in Mixin.getVisibleWorldIds(sender);
// if ( ! Mixin.canSeeWorld(sender, worldId)) continue;
for (String worldAlias : Mixin.getWorldAliases(worldId))
{
if (arg.equalsIgnoreCase(worldAlias)) return worldId;
@ -61,4 +64,10 @@ public class ARWorldId extends ARAbstractSelect<String>
return ret;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return Mixin.getVisibleWorldIds(sender);
}
}

View File

@ -1,6 +1,8 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bukkit.WorldType;
import org.bukkit.command.CommandSender;
@ -9,6 +11,12 @@ import com.massivecraft.massivecore.util.MUtil;
public class ARWorldType extends ARAbstractSelect<WorldType>
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public static final List<String> ALT_NAMES = Collections.unmodifiableList(MUtil.list("normal", "flat", "1.1", "largebiomes"));
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
@ -20,55 +28,64 @@ public class ARWorldType extends ARAbstractSelect<WorldType>
// OVERRIDE
// -------------------------------------------- //
@Override
public String typename()
{
return "world type";
}
@Override
public WorldType select(String arg, CommandSender sender)
{
WorldType ret = null;
// "DEFAULT_1_1" --> "11"
// "LARGE_BIOMES" --> "large"
// "Default" --> ""
arg = arg.toLowerCase();
arg = arg.replace("_", "");
arg = arg.replace(".", "");
arg = arg.replace("normal", "");
arg = arg.replace("default", "");
arg = arg.replace("biomes", "");
arg = getComparable(arg);
if (arg.equals(""))
{
// "normal" or "default"
ret = WorldType.NORMAL;
return WorldType.NORMAL;
}
else if (arg.startsWith("flat"))
{
// "flat"
ret = WorldType.FLAT;
return WorldType.FLAT;
}
else if (arg.contains("11"))
{
// "VERSION_1_1"
ret = WorldType.VERSION_1_1;
return WorldType.VERSION_1_1;
}
else if (arg.contains("large"))
{
// "LARGE_BIOMES"
ret = WorldType.LARGE_BIOMES;
return WorldType.LARGE_BIOMES;
}
return ret;
return null;
}
@Override
public Collection<String> altNames(CommandSender sender)
{
return MUtil.list("normal", "flat", "1.1", "largebiomes");
return ALT_NAMES;
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
return this.altNames(sender);
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static String getComparable(String string)
{
// "DEFAULT_1_1" --> "11"
// "LARGE_BIOMES" --> "large"
// "Default" --> ""
string = string.toLowerCase();
string = string.replace("_", "");
string = string.replace(".", "");
string = string.replace("normal", "");
string = string.replace("default", "");
string = string.replace("biomes", "");
return string;
}
}

View File

@ -1,13 +0,0 @@
package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public interface ArgReader<T>
{
public T read(String arg, CommandSender sender) throws MassiveException;
public T read(CommandSender sender) throws MassiveException;
public T read(String arg) throws MassiveException;
public T readArg() throws MassiveException;
}

View File

@ -1,31 +0,0 @@
package com.massivecraft.massivecore.cmd.arg;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
public abstract class ArgReaderAbstract<T> implements ArgReader<T>
{
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public T read(CommandSender sender) throws MassiveException
{
return this.read(null, sender);
}
@Override
public T read(String arg) throws MassiveException
{
return this.read(arg, null);
}
@Override
public T readArg() throws MassiveException
{
return this.read(null, null);
}
}

View File

@ -1,8 +1,14 @@
package com.massivecraft.massivecore.util;
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -11,6 +17,9 @@ import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.Predictate;
import com.massivecraft.massivecore.PredictateStartsWithIgnoreCase;
public class Txt
{
// -------------------------------------------- //
@ -567,6 +576,38 @@ public class Txt
return ret;
}
// -------------------------------------------- //
// FILTER
// -------------------------------------------- //
public static <T> List<T> getFiltered(Collection<T> elements, Predictate<T> predictate)
{
List<T> ret = new ArrayList<T>();
for (T element : elements)
{
if ( ! predictate.apply(element)) continue;
ret.add(element);
}
return ret;
}
public static <T> List<T> getFiltered(T[] elements, Predictate<T> predictate)
{
return getFiltered(Arrays.asList(elements), predictate);
}
public static List<String> getStartsWithIgnoreCase(Collection<String> elements, String prefix)
{
return getFiltered(elements, PredictateStartsWithIgnoreCase.get(prefix));
}
public static List<String> getStartsWithIgnoreCase(String[] elements, String prefix)
{
return getStartsWithIgnoreCase(Arrays.asList(elements), prefix);
}
// -------------------------------------------- //
// Tokenization
// -------------------------------------------- //