PredicateEqualsIgnoreCase and used aliases

This commit is contained in:
BuildTools 2016-01-08 16:09:58 +01:00 committed by Olof Larsson
parent b050c768d8
commit 6746824e34
2 changed files with 56 additions and 3 deletions

View File

@ -0,0 +1,44 @@
package com.massivecraft.massivecore;
// Inspired by: String#regionMatches(ignoreCase, toffset, other, ooffset, len)
public class PredicateEqualsIgnoreCase implements Predicate<String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String strLower;
private final String strUpper;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public static PredicateEqualsIgnoreCase get(String prefix) { return new PredicateEqualsIgnoreCase(prefix); }
public PredicateEqualsIgnoreCase(String str)
{
if (str == null) throw new NullPointerException("str");
this.strLower = str.toLowerCase();
this.strUpper = str.toUpperCase();
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public boolean apply(String str)
{
if (str == null) return false;
int index = this.strLower.length();
if (str.length() != index) return false;
while (index-- > 0)
{
char c = str.charAt(index);
if (c == strLower.charAt(index)) continue;
if (c != strUpper.charAt(index)) return false;
}
return true;
}
}

View File

@ -1,11 +1,14 @@
package com.massivecraft.massivecore.command.editor;
import java.util.Set;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.sender.TypeSender;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
public class EditSettings<O>
@ -93,10 +96,16 @@ public class EditSettings<O>
// TYPE READ UTILITY
// -------------------------------------------- //
public O getUsedOrCommandException(CommandSender sender) throws MassiveException
// No nice constructors for TreeSet :(
public static final Set<String> ALIASES_USED = MUtil.treeset("used", "selected", "chosen");
public O getUsedOrCommandException(CommandSender sender, String arg) throws MassiveException
{
O ret = this.getUsed(sender);
if (ret != null) return ret;
if (arg == null || ALIASES_USED.contains(arg))
{
O ret = this.getUsed(sender);
if (ret != null) return ret;
}
String noun = this.getObjectType().getTypeName();
String aan = Txt.aan(noun);
throw new MassiveException().addMsg("<b>You must select %s %s for use to skip the optional argument.", aan, noun);