From 4f7fd6dd96286ff7fea41bb158cf315a12542869 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sun, 23 Oct 2011 12:07:20 +0200 Subject: [PATCH] Possibly solved the faction creation issue. Changed the best id match to something more reasonable --- src/com/massivecraft/factions/Factions.java | 4 +- .../massivecraft/factions/cmd/CmdCreate.java | 4 +- .../zcore/persist/EntityCollection.java | 37 +++++++++++-------- .../factions/zcore/util/TextUtil.java | 24 ++++++++---- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 85d4250a..420b8d72 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -146,7 +146,7 @@ public class Factions extends EntityCollection return null; } - public Faction getBestTagMatch(String pattern) + public Faction getBestTagMatch(String searchFor) { Map tag2faction = new HashMap(); @@ -156,7 +156,7 @@ public class Factions extends EntityCollection 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); } diff --git a/src/com/massivecraft/factions/cmd/CmdCreate.java b/src/com/massivecraft/factions/cmd/CmdCreate.java index 67fa0ec9..965616eb 100644 --- a/src/com/massivecraft/factions/cmd/CmdCreate.java +++ b/src/com/massivecraft/factions/cmd/CmdCreate.java @@ -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("There was an internal error while trying to create your faction. Please try again."); diff --git a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java index d6578beb..c171886b 100644 --- a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -97,7 +97,7 @@ public abstract class EntityCollection 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 // 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 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 entry : this.id2entity.entrySet()) @@ -251,16 +253,21 @@ public abstract class EntityCollection } } + 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) { } } - } diff --git a/src/com/massivecraft/factions/zcore/util/TextUtil.java b/src/com/massivecraft/factions/zcore/util/TextUtil.java index 8db25be6..a4caee9d 100644 --- a/src/com/massivecraft/factions/zcore/util/TextUtil.java +++ b/src/com/massivecraft/factions/zcore/util/TextUtil.java @@ -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 candidates, String pattern) + public static String getBestStartWithCI(Collection 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; } }