diff --git a/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java b/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java new file mode 100644 index 00000000..ed72c16c --- /dev/null +++ b/src/com/massivecraft/massivecore/PredictateStartsWithIgnoreCase.java @@ -0,0 +1,33 @@ +package com.massivecraft.massivecore; + +public class PredictateStartsWithIgnoreCase implements Predictate +{ + // -------------------------------------------- // + // 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()); + } + +} diff --git a/src/com/massivecraft/massivecore/SoundEffect.java b/src/com/massivecraft/massivecore/SoundEffect.java index a7e64a05..428926d5 100644 --- a/src/com/massivecraft/massivecore/SoundEffect.java +++ b/src/com/massivecraft/massivecore/SoundEffect.java @@ -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); diff --git a/src/com/massivecraft/massivecore/cmd/MassiveCommand.java b/src/com/massivecraft/massivecore/cmd/MassiveCommand.java index 5a95ff76..d66a4ca3 100644 --- a/src/com/massivecraft/massivecore/cmd/MassiveCommand.java +++ b/src/com/massivecraft/massivecore/cmd/MassiveCommand.java @@ -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 arg(int idx, ArgReader argReader) throws MassiveException + public T arg(int idx, AR argReader) throws MassiveException { String str = this.arg(idx); return this.arg(str, argReader); } - public T arg(int idx, ArgReader argReader, T defaultNotSet) throws MassiveException + public T arg(int idx, AR 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 argConcatFrom(int idx, ArgReader argReader) throws MassiveException + public T argConcatFrom(int idx, AR argReader) throws MassiveException { String str = this.argConcatFrom(idx); return this.arg(str, argReader); } - public T argConcatFrom(int idx, ArgReader argReader, T defaultNotSet) throws MassiveException + public T argConcatFrom(int idx, AR 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 arg(ArgReader argReader) throws MassiveException + public T arg(AR argReader) throws MassiveException { return this.arg(null, argReader); } - public T arg(String str, ArgReader argReader) throws MassiveException + public T arg(String str, AR argReader) throws MassiveException { - T result = argReader.read(str, this.sender); - return result; + return argReader.read(str, this.sender); } - public T arg(String str, ArgReader argReader, T defaultNotSet) throws MassiveException + public T arg(String str, AR argReader, T defaultNotSet) throws MassiveException { if (str == null) return defaultNotSet; return this.arg(str, argReader); diff --git a/src/com/massivecraft/massivecore/cmd/arg/AMain.java b/src/com/massivecraft/massivecore/cmd/arg/AMain.java new file mode 100644 index 00000000..3f048690 --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/AMain.java @@ -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 input = new ArrayList(); + + input.add("1"); + + for(String str : ARAbstract.prepareForSpaces(input)) + System.out.println("\"" + str + "\""); + System.out.println("end"); + } + catch(Throwable t) + { + t.printStackTrace(); + } + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/AR.java b/src/com/massivecraft/massivecore/cmd/arg/AR.java new file mode 100644 index 00000000..814ea48b --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/AR.java @@ -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 +{ + // 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 getTabList(CommandSender sender, String arg); + public List 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(); +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstract.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstract.java new file mode 100644 index 00000000..779a0614 --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstract.java @@ -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 implements AR +{ + // -------------------------------------------- // + // 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 getTabListFiltered(CommandSender sender, String arg) + { + // Filter them to start with what the user typed in. + Collection raw = this.getTabList(sender, arg); + if (raw == null) return Collections.emptyList(); + List 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 prepareForSpaces(List suggestions) + { + List> 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 prefix = getPrefix(suggestionParts); + // This is all the suggetions without the common prefix. + List 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> getParts(List list) + { + List> 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 withoutPreAndSuffix(List> suggestionParts, List prefix) + { + List ret = new ArrayList(suggestionParts.size()); + boolean includesPrefix = false; // Sometimes a suggestion is equal to the prefix. + for (List 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 getPrefix(List> suggestionParts) + { + List prefix = null; + + for (List 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 getOkay(List original, List compared) + { + if (original == null) return compared; + + final int size = Math.min(original.size(), compared.size()); + List ret = new ArrayList(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 addSpaceAtEnd(MassiveCommand command, int argNumber, List 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(suggestion); + }*/ + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractException.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractException.java new file mode 100644 index 00000000..c4701e78 --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractException.java @@ -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 extends ARAbstract +{ + // -------------------------------------------- // + // 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("%s", ex.getMessage()); + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractNumber.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractNumber.java new file mode 100644 index 00000000..28768c2c --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractNumber.java @@ -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 extends ARAbstractPrimitive +{ + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final List TAB_LIST = Collections.singletonList("1"); + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return TAB_LIST; + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java index 64f71c46..70b4a43e 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractPrimitive.java @@ -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 extends ArgReaderAbstract -{ - // -------------------------------------------- // - // ABSTRACT - // -------------------------------------------- // - - public abstract String typename(); - public abstract T convert(String arg) throws Exception; - +public abstract class ARAbstractPrimitive extends ARAbstractException +{ // -------------------------------------------- // // 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("Invalid %s \"%s\".", this.typename(), arg); - } - - return result; + return Txt.parse("\"%s\" is not a %s.", arg, this.getTypeName()); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractSelect.java b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractSelect.java index 09679da5..7240fdea 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARAbstractSelect.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAbstractSelect.java @@ -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 extends ArgReaderAbstract +public abstract class ARAbstractSelect extends ARAbstract { // -------------------------------------------- // // CONSTANT @@ -22,8 +23,7 @@ public abstract class ARAbstractSelect extends ArgReaderAbstract // ABSTRACT // -------------------------------------------- // - public abstract String typename(); - public abstract T select(String str, CommandSender sender) throws MassiveException; + public abstract T select(String str, CommandSender sender) throws MassiveException; public abstract Collection altNames(CommandSender sender); public boolean canList(CommandSender sender) { return true; } @@ -35,11 +35,11 @@ public abstract class ARAbstractSelect extends ArgReaderAbstract public T read(String arg, CommandSender sender) throws MassiveException { T result = this.select(arg, sender); - + if (result != null) return result; MassiveException exception = new MassiveException(); - exception.addMsg("No %s matches \"%s\".", this.typename(), arg); + exception.addMsg("No %s matches \"%s\".", this.getTypeName(), arg); if (this.canList(sender)) { @@ -50,7 +50,7 @@ public abstract class ARAbstractSelect extends ArgReaderAbstract if (names.isEmpty()) { - exception.addMsg("Note: There is no %s available.", this.typename()); + exception.addMsg("Note: There is no %s available.", this.getTypeName()); } else if ( ! matches.isEmpty() && matches.size() < LIST_COUNT_MAX) { @@ -68,7 +68,7 @@ public abstract class ARAbstractSelect extends ArgReaderAbstract String comma = Txt.parse(", "); String and = Txt.parse(" or "); String dot = Txt.parse("."); - exception.addMsg("Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot)); + exception.addMsg("Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot)); } } @@ -77,6 +77,7 @@ public abstract class ARAbstractSelect extends ArgReaderAbstract public List 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 extends ArgReaderAbstract 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. } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAll.java b/src/com/massivecraft/massivecore/cmd/arg/ARAll.java new file mode 100644 index 00000000..8de78487 --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAll.java @@ -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 extends ARAbstract> +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private ARAllAble innerArgReader; + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ARAll(ARAllAble 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 read(String arg, CommandSender sender) throws MassiveException + { + if ("all".equalsIgnoreCase(arg)) return innerArgReader.getAll(); + + T innerRet = innerArgReader.read(arg, sender); + + List ret = new MassiveList(); + ret.add(innerRet); + + return ret; + } + + @Override + public boolean isValid(String arg, CommandSender sender) + { + return "all".equalsIgnoreCase(arg) || innerArgReader.isValid(arg, sender); + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return innerArgReader.getTabList(sender, arg); + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAllAble.java b/src/com/massivecraft/massivecore/cmd/arg/ARAllAble.java new file mode 100644 index 00000000..5455b963 --- /dev/null +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAllAble.java @@ -0,0 +1,8 @@ +package com.massivecraft.massivecore.cmd.arg; + +import java.util.Collection; + +public interface ARAllAble extends AR +{ + public Collection getAll(); +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARAspect.java b/src/com/massivecraft/massivecore/cmd/arg/ARAspect.java index 2b17dbb4..d5f3397e 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARAspect.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARAspect.java @@ -20,12 +20,6 @@ public class ARAspect extends ARAbstractSelect // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - - @Override - public String typename() - { - return "aspect"; - } @Override public Aspect select(String arg, CommandSender sender) @@ -38,11 +32,17 @@ public class ARAspect extends ARAbstractSelect { return MassiveCorePerm.USYS_ASPECT_LIST.has(sender, false); } - + @Override public Collection altNames(CommandSender sender) { return AspectColl.get().getIds(); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARBiome.java b/src/com/massivecraft/massivecore/cmd/arg/ARBiome.java index d7bc73ec..68f9c561 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARBiome.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARBiome.java @@ -19,22 +19,15 @@ public class ARBiome extends ARAbstractSelect // -------------------------------------------- // // 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; @@ -63,17 +56,31 @@ public class ARBiome extends ARAbstractSelect return ret; } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + List ret = new ArrayList(); + + 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; } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARBoolean.java b/src/com/massivecraft/massivecore/cmd/arg/ARBoolean.java index f791f0e5..7c3729eb 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARBoolean.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARBoolean.java @@ -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 { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final Set TRUE_OPTIONS = MUtil.set( + "y", "ye", "yes", + "on", + "t", "tr", "tru", "true"); + + public static final Set FALSE_OPTIONS = MUtil.set( + "n", "no", + "of", "off", + "f", "fa", "fal", "fals", "false"); + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -14,20 +37,43 @@ public class ARBoolean extends ARAbstractPrimitive // -------------------------------------------- // @Override - public String typename() + public String getTypeName() { - return "boolean"; + return "toggle"; + } + + @Override + public Boolean valueOf(String arg, CommandSender sender) throws Exception + { + arg = arg.toLowerCase(); + + if (TRUE_OPTIONS.contains(arg)) return Boolean.TRUE; + if (FALSE_OPTIONS.contains(arg)) return Boolean.FALSE; + + throw new Exception(); } @Override - public Boolean convert(String arg) throws Exception + public Collection getTabList(CommandSender sender, String arg) { - arg = arg.toLowerCase(); - if (arg.startsWith("y") || arg.startsWith("t") || arg.startsWith("on") || arg.startsWith("+") || arg.startsWith("1")) + List ret = new ArrayList(); + + // 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()) { - return true; + ret.add("true"); + ret.add("false"); + ret.add("on"); + ret.add("off"); } - return false; - } - + + return ret; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARByte.java b/src/com/massivecraft/massivecore/cmd/arg/ARByte.java index 9d0d3fdb..faee4b7d 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARByte.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARByte.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.cmd.arg; -public class ARByte extends ARAbstractPrimitive +import org.bukkit.command.CommandSender; + +public class ARByte extends ARAbstractNumber { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -14,15 +16,15 @@ public class ARByte extends ARAbstractPrimitive // -------------------------------------------- // @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); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARChatColor.java b/src/com/massivecraft/massivecore/cmd/arg/ARChatColor.java index 0ee06f33..498d1bf9 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARChatColor.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARChatColor.java @@ -19,29 +19,20 @@ public class ARChatColor extends ARAbstractSelect // -------------------------------------------- // // 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 for (ChatColor cc : ChatColor.values()) { - ret.add(cc.toString()+getToCompare(cc.name())); + ret.add(cc.toString() + getComparable(cc.name())); + } + + return ret; + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + List ret = new ArrayList(); + + for (ChatColor cc : ChatColor.values()) + { + ret.add(getComparable(cc.name())); } return ret; @@ -63,12 +67,12 @@ public class ARChatColor extends ARAbstractSelect // "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; } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARColl.java b/src/com/massivecraft/massivecore/cmd/arg/ARColl.java index 9baad924..4bd86635 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARColl.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARColl.java @@ -18,23 +18,23 @@ public class ARColl extends ARAbstractSelect> // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - - @Override - public String typename() - { - return "coll"; - } @Override public Coll select(String arg, CommandSender sender) { return Coll.getMap().get(arg); } - + @Override public Collection altNames(CommandSender sender) { return Coll.getNames(); } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARCombined.java b/src/com/massivecraft/massivecore/cmd/arg/ARCombined.java index c1ed2fef..6029ebed 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARCombined.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARCombined.java @@ -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> +public class ARCombined extends ARAbstract> { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - protected List> inners; - public List> getInners() { return this.inners; } + protected List> inners; + public List> getInners() { return this.inners; } // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static ARCombined get(Collection> inners) { return new ARCombined(inners); } - public static ARCombined get(ArgReader... inners) { return new ARCombined(inners); } + public static ARCombined get(Collection> inners) { return new ARCombined(inners); } + public static ARCombined get(AR... inners) { return new ARCombined(inners); } - public ARCombined(Collection> inners) + public ARCombined(Collection> inners) { - this.inners = new MassiveList>(inners); + this.inners = new MassiveList>(inners); } - public ARCombined(ArgReader... inners) + public ARCombined(AR... inners) { this(Arrays.asList(inners)); } @@ -37,7 +37,15 @@ public class ARCombined extends ArgReaderAbstract> // -------------------------------------------- // // 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> 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); @@ -64,5 +72,12 @@ public class ARCombined extends ArgReaderAbstract> // Return Ret return ret; } + + //TODO: How to do this? + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return null; // ??? + } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARDate.java b/src/com/massivecraft/massivecore/cmd/arg/ARDate.java index e0a33dc8..9f990a9b 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARDate.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARDate.java @@ -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 { // -------------------------------------------- // @@ -24,15 +28,21 @@ public class ARDate extends ARAbstractPrimitive // -------------------------------------------- // @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); } - -} \ No newline at end of file + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return Collections.emptySet(); + } + +} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java b/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java index 3d25e8bc..fd709b12 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java @@ -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 +public class ARDestination extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -35,5 +37,11 @@ public class ARDestination extends ArgReaderAbstract return ret; } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return null; + } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARDifficulty.java b/src/com/massivecraft/massivecore/cmd/arg/ARDifficulty.java index 8b258c06..a6056744 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARDifficulty.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARDifficulty.java @@ -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 { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final List ALT_NAMES = Collections.unmodifiableList(MUtil.list("peaceful", "easy", "normal", "hard")); + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -19,44 +27,42 @@ public class ARDifficulty extends ARAbstractSelect // -------------------------------------------- // // 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 altNames(CommandSender sender) { - return MUtil.list("peaceful", "easy", "normal", "hard"); + return ALT_NAMES; } - + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARDouble.java b/src/com/massivecraft/massivecore/cmd/arg/ARDouble.java index 92c9832f..81c7d412 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARDouble.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARDouble.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.cmd.arg; -public class ARDouble extends ARAbstractPrimitive +import org.bukkit.command.CommandSender; + +public class ARDouble extends ARAbstractNumber { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -14,15 +16,15 @@ public class ARDouble extends ARAbstractPrimitive // -------------------------------------------- // @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); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/AREntityType.java b/src/com/massivecraft/massivecore/cmd/arg/AREntityType.java index debf0f55..f276457d 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/AREntityType.java +++ b/src/com/massivecraft/massivecore/cmd/arg/AREntityType.java @@ -19,12 +19,6 @@ public class AREntityType extends ARAbstractSelect // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - - @Override - public String typename() - { - return "entity type"; - } @Override public EntityType select(String arg, CommandSender sender) @@ -32,12 +26,13 @@ public class AREntityType extends ARAbstractSelect 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 List ret = new ArrayList(); for (EntityType entityType : EntityType.values()) { - ret.add(getComparable(entityType.toString())); + ret.add(getComparable(entityType)); } return ret; } + @Override + public Collection 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]+", ""); } diff --git a/src/com/massivecraft/massivecore/cmd/arg/AREnum.java b/src/com/massivecraft/massivecore/cmd/arg/AREnum.java index ef65bd79..3cca0adb 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/AREnum.java +++ b/src/com/massivecraft/massivecore/cmd/arg/AREnum.java @@ -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 extends ARAbstractSelect public AREnum(Class clazz) { + if ( ! clazz.isEnum()) throw new IllegalArgumentException("passed clazz param must be an enum"); this.clazz = clazz; } @@ -36,11 +36,11 @@ public class AREnum extends ARAbstractSelect // -------------------------------------------- // @Override - public String typename() + public String getTypeName() { return Txt.getNicedEnumString(clazz.getSimpleName()); } - + @Override public T select(String arg, CommandSender sender) { @@ -50,63 +50,67 @@ public class AREnum extends ARAbstractSelect // 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; + + // Else we set the result. + startsWith = value; } } - if (startswithCount == 1) - { - return startswith; - } - // Nothing found - return null; + return startsWith; } @Override public Collection altNames(CommandSender sender) { List ret = new ArrayList(); - 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 getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } // -------------------------------------------- // // UTIL // -------------------------------------------- // - public static String getComparable(String string) - { - return string.toLowerCase().replaceAll("[_\\-\\s]+", ""); - } - - @SuppressWarnings("unchecked") public static T[] getEnumValues(Class 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; - } + 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]+", ""); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/AREnvironment.java b/src/com/massivecraft/massivecore/cmd/arg/AREnvironment.java index 4fc9cd75..2bc067d4 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/AREnvironment.java +++ b/src/com/massivecraft/massivecore/cmd/arg/AREnvironment.java @@ -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 { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final List ALT_NAMES = Collections.unmodifiableList(MUtil.list("normal", "end", "nether")); + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -19,46 +28,65 @@ public class AREnvironment extends ARAbstractSelect // -------------------------------------------- // // 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 altNames(CommandSender sender) { - return MUtil.list("normal", "end", "nether"); + return ALT_NAMES; + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Collection 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; } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARFloat.java b/src/com/massivecraft/massivecore/cmd/arg/ARFloat.java index 1bc2d5d8..ccd5ae91 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARFloat.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARFloat.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.cmd.arg; -public class ARFloat extends ARAbstractPrimitive +import org.bukkit.command.CommandSender; + +public class ARFloat extends ARAbstractNumber { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -14,15 +16,15 @@ public class ARFloat extends ARAbstractPrimitive // -------------------------------------------- // @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); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARGameMode.java b/src/com/massivecraft/massivecore/cmd/arg/ARGameMode.java index e1084ee8..30ee7702 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARGameMode.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARGameMode.java @@ -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 { @@ -19,40 +21,56 @@ public class ARGameMode extends ARAbstractSelect // -------------------------------------------- // // 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 altNames(CommandSender sender) { - return MUtil.list("survival", "creative", "adventure"); + List ret = new ArrayList(); + for (GameMode gm : GameMode.values()) + { + ret.add(Txt.getNicedEnum(gm)); + } + return ret; } + @Override + public Collection 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(); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARInteger.java b/src/com/massivecraft/massivecore/cmd/arg/ARInteger.java index 52424e44..c5fa1ac1 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARInteger.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARInteger.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.cmd.arg; -public class ARInteger extends ARAbstractPrimitive +import org.bukkit.command.CommandSender; + +public class ARInteger extends ARAbstractNumber { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -14,13 +16,13 @@ public class ARInteger extends ARAbstractPrimitive // -------------------------------------------- // @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); } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARList.java b/src/com/massivecraft/massivecore/cmd/arg/ARList.java index 66ed523e..60cd6a54 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARList.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARList.java @@ -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 extends ArgReaderAbstract> +public class ARList extends ARAbstract> { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - protected ArgReader innerArgReader; - public ArgReader getInnerArgReader() { return this.innerArgReader; } + protected AR innerArgReader; + public AR getInnerArgReader() { return this.innerArgReader; } // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static ARList get(ArgReader innerArgReader) + public static ARList get(AR innerArgReader) { return new ARList(innerArgReader); } - public ARList(ArgReader innerArgReader) + public ARList(AR innerArgReader) { this.innerArgReader = innerArgReader; } @@ -34,6 +35,12 @@ public class ARList extends ArgReaderAbstract> // OVERRIDE // -------------------------------------------- // + @Override + public String getTypeName() + { + return innerArgReader.getTypeName(); + } + // NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false). @Override public List read(String arg, CommandSender sender) throws MassiveException @@ -56,4 +63,10 @@ public class ARList extends ArgReaderAbstract> return ret; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.getInnerArgReader().getTabList(sender, arg); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARLong.java b/src/com/massivecraft/massivecore/cmd/arg/ARLong.java index f018715a..ffeb0d2c 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARLong.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARLong.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore.cmd.arg; -public class ARLong extends ARAbstractPrimitive +import org.bukkit.command.CommandSender; + +public class ARLong extends ARAbstractNumber { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -14,15 +16,15 @@ public class ARLong extends ARAbstractPrimitive // -------------------------------------------- // @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); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARMaterial.java b/src/com/massivecraft/massivecore/cmd/arg/ARMaterial.java index 282ae314..06c4dc85 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARMaterial.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARMaterial.java @@ -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 +public class ARMaterial extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -24,13 +28,25 @@ public class ARMaterial extends ArgReaderAbstract Material ret = Material.matchMaterial(arg); if (ret == null) { - MassiveException exception = new MassiveException(); - exception.addMsg("No material matches %s.", arg); - exception.addMsg("Suggestion: http://www.minecraftwiki.net/wiki/Data_values"); - throw exception; + throw new MassiveException() + .addMsg("No material matches %s.", arg) + .addMsg("https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Material.java"); } return ret; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + for (Material material : Material.values()) + { + ret.add(material.name()); + } + + return ret; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARMillisDiff.java b/src/com/massivecraft/massivecore/cmd/arg/ARMillisDiff.java index 0b196b82..68ad595a 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARMillisDiff.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARMillisDiff.java @@ -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 +public class ARMillisDiff extends ARAbstractException { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -19,19 +21,21 @@ public class ARMillisDiff extends ArgReaderAbstract // -------------------------------------------- // @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("%s", e.getMessage()); - } - - return ret; + return "time amount"; } + @Override + public Long valueOf(String arg, CommandSender sender) throws Exception + { + return TimeDiffUtil.millis(arg); + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return Collections.emptySet(); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARMultiverse.java b/src/com/massivecraft/massivecore/cmd/arg/ARMultiverse.java index c4803c8f..b44e7fce 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARMultiverse.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARMultiverse.java @@ -20,12 +20,6 @@ public class ARMultiverse extends ARAbstractSelect // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - - @Override - public String typename() - { - return "multiverse"; - } @Override public Multiverse select(String arg, CommandSender sender) @@ -45,4 +39,10 @@ public class ARMultiverse extends ARAbstractSelect return MultiverseColl.get().getIds(); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java b/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java index 91a23b6b..fcf03e0a 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARNullable.java @@ -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 extends ArgReaderAbstract +public class ARNullable extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static ARNullable get(ArgReader inner) + public static ARNullable get(AR inner) { return new ARNullable(inner); } - public ARNullable(ArgReader inner) + public ARNullable(AR inner) { + if (inner == null) throw new IllegalArgumentException("inner param is null"); this.inner = inner; } @@ -25,13 +28,19 @@ public class ARNullable extends ArgReaderAbstract // FIELDS // -------------------------------------------- // - protected ArgReader inner; - public ArgReader getInner() { return this.inner; } + protected AR inner; + public AR 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 extends ArgReaderAbstract return this.getInner().read(arg, sender); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.getInner().getTabList(sender, arg); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPS.java b/src/com/massivecraft/massivecore/cmd/arg/ARPS.java index 634b924f..642fc148 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPS.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPS.java @@ -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 +public class ARPS extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -202,6 +203,12 @@ public class ARPS extends ArgReaderAbstract return ret.build(); } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return null; + } public static String getValue(String entry, String... prefixes) { diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPermission.java b/src/com/massivecraft/massivecore/cmd/arg/ARPermission.java index ed3fa868..688ebc60 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPermission.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPermission.java @@ -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 +public class ARPermission extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -22,21 +26,26 @@ public class ARPermission extends ArgReaderAbstract @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("No permission with the name \"%s\" was found.", arg); + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + for (Permission perm : Bukkit.getPluginManager().getPermissions()) { - throw new MassiveException().addMsg("No permission with the name \"%s\" was found.", arg); + ret.add(perm.getName()); } return ret; } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java b/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java index 6f1d6409..1bfefe87 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPlayer.java @@ -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 { @@ -25,8 +32,23 @@ public class ARPlayer extends ARSenderIdAbstract @Override public Player getResultForSenderId(String senderId) { - if (senderId == null) return null; + // Null check is done in IdUtil :) return IdUtil.getPlayer(senderId); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(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; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectType.java b/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectType.java index 64df95b6..cba3a608 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectType.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectType.java @@ -21,12 +21,6 @@ public class ARPotionEffectType extends ARAbstractSelect // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // - - @Override - public String typename() - { - return "potion effect type"; - } @Override public PotionEffectType select(String str, CommandSender sender) throws MassiveException @@ -64,6 +58,16 @@ public class ARPotionEffectType extends ARAbstractSelect // Return Ret return ret; } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return this.altNames(sender); + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // public static String getComparable(PotionEffectType potionEffectType) { @@ -76,5 +80,5 @@ public class ARPotionEffectType extends ARAbstractSelect if (string == null) return null; return string.toLowerCase(); } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectWrap.java b/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectWrap.java index 6a2404cd..0c749fc1 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectWrap.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARPotionEffectWrap.java @@ -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 +public class ARPotionEffectWrap extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -76,5 +77,11 @@ public class ARPotionEffectWrap extends ArgReaderAbstract // Return Ret return ret; } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return ARPotionEffectType.get().getTabList(sender, arg); + } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSender.java b/src/com/massivecraft/massivecore/cmd/arg/ARSender.java index e6c4523d..12645de2 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSender.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSender.java @@ -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 @Override public CommandSender getResultForSenderId(String senderId) { - if (senderId == null) return null; + //Null check is done in IdUtil return IdUtil.getSender(senderId); } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + for (String name : IdUtil.getOnlineNames()) + { + if ( ! Mixin.canSee(sender, name)) continue; + ret.add(name); + } + + return ret; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java index b6ac435a..bd146030 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderEntity.java @@ -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> extends ARSenderIdAbstract { @@ -41,8 +49,22 @@ public class ARSenderEntity> 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 getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + for (String name : IdUtil.getOnlineNames()) + { + if ( ! Mixin.canSee(sender, name)) continue; + ret.add(name); + } + + return ret; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java index 6a6ca61b..d3c59a6e 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderId.java @@ -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 { @@ -42,5 +50,19 @@ public class ARSenderId extends ARSenderIdAbstract { return senderId; } - + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); + + for (String id : IdUtil.getOnlineIds()) + { + if ( ! Mixin.canSee(sender, id)) continue; + ret.add(id); + } + + return ret; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java b/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java index 3f6d0bdf..7ca66919 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSenderIdAbstract.java @@ -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 extends ArgReaderAbstract +public abstract class ARSenderIdAbstract extends ARAbstract { // -------------------------------------------- // // FIELDS @@ -43,6 +43,13 @@ public abstract class ARSenderIdAbstract extends ArgReaderAbstract // OVERRIDE // -------------------------------------------- // + @Override + public String getTypeName() + { + if (online) return "online player"; + else return "player"; + } + @Override public T read(String arg, CommandSender sender) throws MassiveException { @@ -59,8 +66,8 @@ public abstract class ARSenderIdAbstract extends ArgReaderAbstract if (ret == null) { - // No alternatives found - throw new MassiveException().addMsg("No player matches \"%s\".", arg); + // No alternatives found + throw new MassiveException().addMsg("No %s matches \"%s\".", this.getTypeName(), arg); } // Return Ret @@ -83,7 +90,7 @@ public abstract class ARSenderIdAbstract extends ArgReaderAbstract for (Collection 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; diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSet.java b/src/com/massivecraft/massivecore/cmd/arg/ARSet.java index b1d82e15..aa31cc50 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSet.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSet.java @@ -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 extends ArgReaderAbstract> +public class ARSet extends ARAbstract> { // -------------------------------------------- // // FIELDS // -------------------------------------------- // - protected ArgReader innerArgReader; - public ArgReader getInnerArgReader() { return this.innerArgReader; } + private final AR innerArgReader; + public AR getInnerArgReader() { return this.innerArgReader; } - protected boolean warnOnDuplicates; + private final boolean warnOnDuplicates; public boolean getWarnOnDuplicate() { return warnOnDuplicates; } // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - public static ARSet get(ArgReader innerArgReader, boolean warnOnDuplicates) + public static ARSet get(AR innerArgReader, boolean warnOnDuplicates) { return new ARSet(innerArgReader, warnOnDuplicates); } - public ARSet(ArgReader innerArgReader, boolean warnOnDuplicates) + public ARSet(AR innerArgReader, boolean warnOnDuplicates) { this.innerArgReader = innerArgReader; this.warnOnDuplicates = warnOnDuplicates; @@ -39,6 +40,12 @@ public class ARSet extends ArgReaderAbstract> // OVERRIDE // -------------------------------------------- // + @Override + public String getTypeName() + { + return innerArgReader.getTypeName(); + } + // NOTE: Must be used with argConcatFrom and setErrorOnTooManyArgs(false). @Override public Set read(String arg, CommandSender sender) throws MassiveException @@ -61,11 +68,17 @@ public class ARSet extends ArgReaderAbstract> if (warnOnDuplicates && duplicates) { - sender.sendMessage(Txt.parse("Some duplicate arguments were removed")); + Mixin.msgOne(sender, "Some duplicate command input were removed."); } // Return Ret return ret; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return innerArgReader.getTabList(sender, arg); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSound.java b/src/com/massivecraft/massivecore/cmd/arg/ARSound.java index 6156b4c1..82ed3f53 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSound.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSound.java @@ -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 +public class ARSound extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -24,34 +28,55 @@ public class ARSound extends ArgReaderAbstract Sound result = getSoundFromString(arg); if (result == null) { - MassiveException errors = new MassiveException(); - errors.addMsg("No sound matches \"%s\".", arg); - errors.addMsg("https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Sound.java"); - throw errors; + throw new MassiveException() + .addMsg("No sound matches \"%s\".", arg) + .addMsg("https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/browse/src/main/java/org/bukkit/Sound.java"); } return result; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new TreeSet(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; } - + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffect.java b/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffect.java index e071efc4..20c8d69f 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffect.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffect.java @@ -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 +public class ARSoundEffect extends ARAbstractException { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -19,19 +22,28 @@ public class ARSoundEffect extends ArgReaderAbstract // -------------------------------------------- // @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); + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + Set ret = new MassiveSet(); - try + for (String str : ARSound.get().getTabList(sender, arg)) { - ret = SoundEffect.valueOf(arg); - } - catch (Exception e) - { - throw new MassiveException().addMsg("%s", e.getMessage()); + ret.add(str + "-"); } + return ret; } + @Override + public boolean allowSpaceAfterTab() + { + return false; + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffects.java b/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffects.java deleted file mode 100644 index 7b277284..00000000 --- a/src/com/massivecraft/massivecore/cmd/arg/ARSoundEffects.java +++ /dev/null @@ -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> -{ - // -------------------------------------------- // - // INSTANCE & CONSTRUCT - // -------------------------------------------- // - - private static ARSoundEffects i = new ARSoundEffects(); - public static ARSoundEffects get() { return i; } - - // -------------------------------------------- // - // OVERRIDE - // -------------------------------------------- // - - @Override - public List read(String arg, CommandSender sender) throws MassiveException - { - List ret = new ArrayList(); - List result = new ArrayList(); - - arg = arg.trim(); - List 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("%s", e.getMessage()); - } - return ret; - } - -} diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARString.java b/src/com/massivecraft/massivecore/cmd/arg/ARString.java index 4c6a1e85..fb6decce 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARString.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARString.java @@ -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 +public class ARString extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -15,10 +18,22 @@ public class ARString extends ArgReaderAbstract // OVERRIDE // -------------------------------------------- // + @Override + public String getTypeName() + { + return "text"; + } + @Override public String read(String arg, CommandSender sender) { return arg; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return Collections.emptySet(); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARUniverse.java b/src/com/massivecraft/massivecore/cmd/arg/ARUniverse.java index 6675b04c..a51c36dd 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARUniverse.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARUniverse.java @@ -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 +public class ARUniverse extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -25,7 +26,7 @@ public class ARUniverse extends ArgReaderAbstract // -------------------------------------------- // 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 @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("No universe \"%s\" exists in multiverse %s.", arg, this.multiverse.getId()); - Collection names = new ArrayList(multiverse.getUniverses()); String format = Txt.parse("%s"); String comma = Txt.parse(", "); String and = Txt.parse(" or "); String dot = Txt.parse("."); - exception.addMsg("Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot)); - throw exception; + throw new MassiveException() + .addMsg("No universe \"%s\" exists in multiverse %s.", arg, this.multiverse.getId()) + .addMsg("Use %s", Txt.implodeCommaAndDot(names, format, comma, and, dot)); } - - return result; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + // MassiveSet is linked. + return new MassiveSet(multiverse.getUniverses()); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARWorld.java b/src/com/massivecraft/massivecore/cmd/arg/ARWorld.java index 1a546c8c..a692b36a 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARWorld.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARWorld.java @@ -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 +public class ARWorld extends ARAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -24,9 +26,7 @@ public class ARWorld extends ArgReaderAbstract { 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 return ret; } + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return ARWorldId.get().getTabList(sender, arg); + } + } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARWorldId.java b/src/com/massivecraft/massivecore/cmd/arg/ARWorldId.java index 42493694..00f76775 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARWorldId.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARWorldId.java @@ -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 // -------------------------------------------- // @Override - public String typename() + public String getTypeName() { return "world"; } @@ -33,13 +34,15 @@ public class ARWorldId extends ARAbstractSelect 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; @@ -60,5 +63,11 @@ public class ARWorldId extends ARAbstractSelect } return ret; } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return Mixin.getVisibleWorldIds(sender); + } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARWorldType.java b/src/com/massivecraft/massivecore/cmd/arg/ARWorldType.java index ca3e763e..18c4aba1 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARWorldType.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARWorldType.java @@ -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 { + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + public static final List ALT_NAMES = Collections.unmodifiableList(MUtil.list("normal", "flat", "1.1", "largebiomes")); + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // @@ -19,56 +27,65 @@ public class ARWorldType extends ARAbstractSelect // -------------------------------------------- // // 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 altNames(CommandSender sender) { - return MUtil.list("normal", "flat", "1.1", "largebiomes"); + return ALT_NAMES; + } + + @Override + public Collection 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; } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ArgReader.java b/src/com/massivecraft/massivecore/cmd/arg/ArgReader.java deleted file mode 100644 index 015a7bd8..00000000 --- a/src/com/massivecraft/massivecore/cmd/arg/ArgReader.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.massivecraft.massivecore.cmd.arg; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.massivecore.MassiveException; - -public interface ArgReader -{ - 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; -} \ No newline at end of file diff --git a/src/com/massivecraft/massivecore/cmd/arg/ArgReaderAbstract.java b/src/com/massivecraft/massivecore/cmd/arg/ArgReaderAbstract.java deleted file mode 100644 index e58d2761..00000000 --- a/src/com/massivecraft/massivecore/cmd/arg/ArgReaderAbstract.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.massivecraft.massivecore.cmd.arg; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.massivecore.MassiveException; - -public abstract class ArgReaderAbstract implements ArgReader -{ - // -------------------------------------------- // - // 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); - } - -} diff --git a/src/com/massivecraft/massivecore/util/Txt.java b/src/com/massivecraft/massivecore/util/Txt.java index 48e1afa9..b136bfc7 100644 --- a/src/com/massivecraft/massivecore/util/Txt.java +++ b/src/com/massivecraft/massivecore/util/Txt.java @@ -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 List getFiltered(Collection elements, Predictate predictate) + { + List ret = new ArrayList(); + + for (T element : elements) + { + if ( ! predictate.apply(element)) continue; + ret.add(element); + } + + return ret; + } + + public static List getFiltered(T[] elements, Predictate predictate) + { + return getFiltered(Arrays.asList(elements), predictate); + } + + public static List getStartsWithIgnoreCase(Collection elements, String prefix) + { + return getFiltered(elements, PredictateStartsWithIgnoreCase.get(prefix)); + } + + public static List getStartsWithIgnoreCase(String[] elements, String prefix) + { + return getStartsWithIgnoreCase(Arrays.asList(elements), prefix); + } + // -------------------------------------------- // // Tokenization // -------------------------------------------- //