Adding match mode for id search to MassiveCraftCore and improving the faction argument readers.

This commit is contained in:
Olof Larsson
2011-10-22 14:39:01 +02:00
parent e6d45a6aa2
commit 5bf38ab0aa
7 changed files with 97 additions and 45 deletions

View File

@ -7,6 +7,7 @@ import java.util.Map.Entry;
import com.google.gson.Gson;
import com.massivecraft.factions.zcore.util.DiscUtil;
import com.massivecraft.factions.zcore.util.TextUtil;
public abstract class EntityCollection<E extends Entity>
{
@ -16,7 +17,7 @@ public abstract class EntityCollection<E extends Entity>
// These must be instantiated in order to allow for different configuration (orders, comparators etc)
private Collection<E> entities;
private Map<String, E> id2entity;
protected Map<String, E> id2entity;
// If the entities are creative they will create a new instance if a non existent id was requested
private boolean creative;
@ -94,6 +95,13 @@ public abstract class EntityCollection<E extends Entity>
return id2entity.get(id) != null;
}
public E getBestIdMatch(String pattern)
{
String id = TextUtil.getWhereLongestCommonStartCI(this.id2entity.keySet(), pattern);
if (id == null) return null;
return this.id2entity.get(id);
}
// -------------------------------------------- //
// CREATE
// -------------------------------------------- //

View File

@ -42,5 +42,4 @@ public abstract class PlayerEntityCollection<E extends Entity> extends EntityCol
}
return entities;
}
}

View File

@ -280,4 +280,36 @@ public class TextUtil
return ""+num+" "+unit+" "+agofromnow;
}
// -------------------------------------------- //
// String comparison
// -------------------------------------------- //
public static int commonStartLength(String a, String b)
{
int len = a.length() > b.length() ? a.length() : b.length();
int i;
for (i = 0; i < len; i++)
{
if (a.charAt(i) != b.charAt(i)) break;
}
return i;
}
public static String getWhereLongestCommonStartCI(Collection<String> candidates, String pattern)
{
String ret = null;
int best = 0;
pattern = pattern.toLowerCase();
for (String candidate : candidates)
{
int csl = commonStartLength(pattern, candidate.toLowerCase());
if (csl > best)
{
best = csl;
ret = candidate;
}
}
return ret;
}
}