Possibly solved the faction creation issue. Changed the best id match to something more reasonable
This commit is contained in:
parent
32624e0339
commit
4f7fd6dd96
@ -146,7 +146,7 @@ public class Factions extends EntityCollection<Faction>
|
||||
return null;
|
||||
}
|
||||
|
||||
public Faction getBestTagMatch(String pattern)
|
||||
public Faction getBestTagMatch(String searchFor)
|
||||
{
|
||||
Map<String, Faction> tag2faction = new HashMap<String, Faction>();
|
||||
|
||||
@ -156,7 +156,7 @@ public class Factions extends EntityCollection<Faction>
|
||||
tag2faction.put(ChatColor.stripColor(faction.getTag()), faction);
|
||||
}
|
||||
|
||||
String tag = TextUtil.getWhereLongestCommonStartCI(tag2faction.keySet(), pattern);
|
||||
String tag = TextUtil.getBestStartWithCI(tag2faction.keySet(), searchFor);
|
||||
if (tag == null) return null;
|
||||
return tag2faction.get(tag);
|
||||
}
|
||||
|
@ -53,13 +53,13 @@ public class CmdCreate extends FCommand
|
||||
sendMessage(tagValidationErrors);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
|
||||
if ( ! payForCommand(Conf.econCostCreate, "to create a new faction", "for creating a new faction")) return;
|
||||
|
||||
Faction faction = Factions.i.create();
|
||||
|
||||
// TODO: Why would this even happen???
|
||||
// TODO: Why would this even happen??? Auto increment clash??
|
||||
if (faction == null)
|
||||
{
|
||||
msg("<b>There was an internal error while trying to create your faction. Please try again.");
|
||||
|
@ -97,7 +97,7 @@ public abstract class EntityCollection<E extends Entity>
|
||||
|
||||
public E getBestIdMatch(String pattern)
|
||||
{
|
||||
String id = TextUtil.getWhereLongestCommonStartCI(this.id2entity.keySet(), pattern);
|
||||
String id = TextUtil.getBestStartWithCI(this.id2entity.keySet(), pattern);
|
||||
if (id == null) return null;
|
||||
return this.id2entity.get(id);
|
||||
}
|
||||
@ -106,12 +106,12 @@ public abstract class EntityCollection<E extends Entity>
|
||||
// CREATE
|
||||
// -------------------------------------------- //
|
||||
|
||||
public E create()
|
||||
public synchronized E create()
|
||||
{
|
||||
return this.create(this.getNextId());
|
||||
}
|
||||
|
||||
public E create(String id)
|
||||
public synchronized E create(String id)
|
||||
{
|
||||
if ( ! this.isIdFree(id)) return null;
|
||||
|
||||
@ -225,21 +225,23 @@ public abstract class EntityCollection<E extends Entity>
|
||||
|
||||
public String getNextId()
|
||||
{
|
||||
String next = Integer.toString(this.nextId);
|
||||
do
|
||||
while ( ! isIdFree(this.nextId) )
|
||||
{
|
||||
this.nextId += 1;
|
||||
} while ( ! isIdFree(Integer.toString(this.nextId)) );
|
||||
|
||||
return next;
|
||||
}
|
||||
return Integer.toString(this.nextId);
|
||||
}
|
||||
|
||||
public boolean isIdFree(String id)
|
||||
{
|
||||
return ! this.id2entity.containsKey(id);
|
||||
}
|
||||
public boolean isIdFree(int id)
|
||||
{
|
||||
return this.isIdFree(Integer.toString(id));
|
||||
}
|
||||
|
||||
protected void fillIds()
|
||||
protected synchronized void fillIds()
|
||||
{
|
||||
this.nextId = 1;
|
||||
for(Entry<String, E> entry : this.id2entity.entrySet())
|
||||
@ -251,16 +253,21 @@ public abstract class EntityCollection<E extends Entity>
|
||||
}
|
||||
}
|
||||
|
||||
protected synchronized void updateNextIdForId(int id)
|
||||
{
|
||||
if (this.nextId < id)
|
||||
{
|
||||
this.nextId = id + 1;
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateNextIdForId(String id)
|
||||
{
|
||||
try
|
||||
{
|
||||
int idAsInt = Integer.parseInt(id);
|
||||
if (this.nextId < idAsInt)
|
||||
{
|
||||
this.nextId = idAsInt + 1;
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
this.updateNextIdForId(idAsInt);
|
||||
}
|
||||
catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -285,7 +285,7 @@ public class TextUtil
|
||||
// String comparison
|
||||
// -------------------------------------------- //
|
||||
|
||||
public static int commonStartLength(String a, String b)
|
||||
/*private static int commonStartLength(String a, String b)
|
||||
{
|
||||
int len = a.length() < b.length() ? a.length() : b.length();
|
||||
int i;
|
||||
@ -294,19 +294,29 @@ public class TextUtil
|
||||
if (a.charAt(i) != b.charAt(i)) break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}*/
|
||||
|
||||
public static String getWhereLongestCommonStartCI(Collection<String> candidates, String pattern)
|
||||
public static String getBestStartWithCI(Collection<String> candidates, String start)
|
||||
{
|
||||
String ret = null;
|
||||
int best = 0;
|
||||
pattern = pattern.toLowerCase();
|
||||
|
||||
start = start.toLowerCase();
|
||||
int minlength = start.length();
|
||||
for (String candidate : candidates)
|
||||
{
|
||||
int csl = commonStartLength(pattern, candidate.toLowerCase());
|
||||
if (csl > best)
|
||||
if (candidate.length() < minlength) continue;
|
||||
if ( ! candidate.toLowerCase().startsWith(start)) continue;
|
||||
|
||||
// The closer to zero the better
|
||||
int lendiff = candidate.length() - minlength;
|
||||
if (lendiff == 0)
|
||||
{
|
||||
best = csl;
|
||||
return candidate;
|
||||
}
|
||||
if (lendiff < best ||best == 0)
|
||||
{
|
||||
best = lendiff;
|
||||
ret = candidate;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user