From 5bf38ab0aa76c793dee0ef38ce6db800fd8fff5a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 22 Oct 2011 14:39:01 +0200 Subject: [PATCH] Adding match mode for id search to MassiveCraftCore and improving the faction argument readers. --- src/com/massivecraft/factions/FPlayers.java | 15 ----- src/com/massivecraft/factions/Factions.java | 20 ++++++- src/com/massivecraft/factions/P.java | 4 +- .../massivecraft/factions/cmd/FCommand.java | 60 +++++++++++-------- .../zcore/persist/EntityCollection.java | 10 +++- .../zcore/persist/PlayerEntityCollection.java | 1 - .../factions/zcore/util/TextUtil.java | 32 ++++++++++ 7 files changed, 97 insertions(+), 45 deletions(-) diff --git a/src/com/massivecraft/factions/FPlayers.java b/src/com/massivecraft/factions/FPlayers.java index 74429bf1..08d5b94d 100644 --- a/src/com/massivecraft/factions/FPlayers.java +++ b/src/com/massivecraft/factions/FPlayers.java @@ -66,19 +66,4 @@ public class FPlayers extends PlayerEntityCollection } } } - - - // TODO: Intressant.... denna skulle jag kanske behöva undersöka lite mer... lägga till i core? - // En form av match player name... - public FPlayer find(String playername) - { - for (FPlayer fplayer : this.get()) - { - if (fplayer.getId().equalsIgnoreCase(playername) || fplayer.getId().toLowerCase().startsWith(playername.toLowerCase())) - { - return fplayer; - } - } - return null; - } } diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 12500024..78ee5271 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -12,6 +12,7 @@ import org.bukkit.ChatColor; import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.util.MiscUtil; import com.massivecraft.factions.zcore.persist.EntityCollection; +import com.massivecraft.factions.zcore.util.TextUtil; public class Factions extends EntityCollection { @@ -132,7 +133,7 @@ public class Factions extends EntityCollection return errors; } - public Faction findByTag(String str) + public Faction getByTag(String str) { String compStr = MiscUtil.getComparisonString(str); for (Faction faction : this.get()) @@ -145,9 +146,24 @@ public class Factions extends EntityCollection return null; } + public Faction getBestTagMatch(String pattern) + { + Map tag2faction = new HashMap(); + + // TODO: Slow index building + for (Faction faction : this.get()) + { + tag2faction.put(faction.getTag(), faction); + } + + String tag = TextUtil.getWhereLongestCommonStartCI(tag2faction.keySet(), pattern); + if (tag == null) return null; + return tag2faction.get(tag); + } + public boolean isTagTaken(String str) { - return this.findByTag(str) != null; + return this.getByTag(str) != null; } } diff --git a/src/com/massivecraft/factions/P.java b/src/com/massivecraft/factions/P.java index c9e84101..ac6324eb 100644 --- a/src/com/massivecraft/factions/P.java +++ b/src/com/massivecraft/factions/P.java @@ -311,7 +311,7 @@ public class P extends MPlugin public Set getPlayersInFaction(String factionTag) { Set players = new HashSet(); - Faction faction = Factions.i.findByTag(factionTag); + Faction faction = Factions.i.getByTag(factionTag); if (faction != null) { for (FPlayer fplayer : faction.getFPlayers()) @@ -326,7 +326,7 @@ public class P extends MPlugin public Set getOnlinePlayersInFaction(String factionTag) { Set players = new HashSet(); - Faction faction = Factions.i.findByTag(factionTag); + Faction faction = Factions.i.getByTag(factionTag); if (faction != null) { for (FPlayer fplayer : faction.getFPlayersWhereOnline(true)) diff --git a/src/com/massivecraft/factions/cmd/FCommand.java b/src/com/massivecraft/factions/cmd/FCommand.java index 36d0d126..edb95b64 100644 --- a/src/com/massivecraft/factions/cmd/FCommand.java +++ b/src/com/massivecraft/factions/cmd/FCommand.java @@ -148,15 +148,14 @@ public abstract class FCommand extends MCommand

// Argument Readers // -------------------------------------------- // - // ARG AS FPLAYER - public FPlayer argAsFPlayer(int idx, FPlayer def, boolean msg) + // FPLAYER ====================== + public FPlayer strAsFPlayer(String name, FPlayer def, boolean msg) { FPlayer ret = def; - String name = this.argAsString(idx); if (name != null) { - FPlayer fplayer = FPlayers.i.get(name); + FPlayer fplayer = FPlayers.i.get(name); if (fplayer != null) { ret = fplayer; @@ -165,11 +164,15 @@ public abstract class FCommand extends MCommand

if (msg && ret == null) { - this.sendMessage(p.txt.parse("The player \"

%s\" could not be found.", name)); + this.msg("No player \"

%s\" could not be found.", name); } return ret; } + public FPlayer argAsFPlayer(int idx, FPlayer def, boolean msg) + { + return this.strAsFPlayer(this.argAsString(idx), def, msg); + } public FPlayer argAsFPlayer(int idx, FPlayer def) { return this.argAsFPlayer(idx, def, true); @@ -179,15 +182,14 @@ public abstract class FCommand extends MCommand

return this.argAsFPlayer(idx, null); } - // ARG AS BEST FPLAYER MATCH - public FPlayer argAsBestFPlayerMatch(int idx, FPlayer def, boolean msg) + // BEST FPLAYER MATCH ====================== + public FPlayer strAsBestFPlayerMatch(String name, FPlayer def, boolean msg) { FPlayer ret = def; - String name = this.argAsString(idx); if (name != null) { - FPlayer fplayer = FPlayers.i.find(name); + FPlayer fplayer = FPlayers.i.getBestIdMatch(name); if (fplayer != null) { ret = fplayer; @@ -196,11 +198,15 @@ public abstract class FCommand extends MCommand

if (msg && ret == null) { - this.sendMessage(p.txt.parse("The player \"

%s\" could not be found.", name)); + this.msg("No player match found for \"

%s\".", name); } return ret; } + public FPlayer argAsBestFPlayerMatch(int idx, FPlayer def, boolean msg) + { + return this.strAsBestFPlayerMatch(this.argAsString(idx), def, msg); + } public FPlayer argAsBestFPlayerMatch(int idx, FPlayer def) { return this.argAsBestFPlayerMatch(idx, def, true); @@ -210,37 +216,43 @@ public abstract class FCommand extends MCommand

return this.argAsBestFPlayerMatch(idx, null); } - // ARG AS FACTION - public Faction argAsFaction(int idx, Faction def, boolean msg) + // FACTION ====================== + public Faction strAsFaction(String name, Faction def, boolean msg) { Faction ret = def; - String name = this.argAsString(idx); if (name != null) { - // First we search faction names - Faction faction = Factions.i.findByTag(name); + // First we match faction tags + Faction faction = Factions.i.getBestTagMatch(name); + + // Next we match player names + if (faction == null) + { + FPlayer fplayer = FPlayers.i.getBestIdMatch(name); + if (fplayer != null) + { + faction = fplayer.getFaction(); + } + } + if (faction != null) { ret = faction; } - - // Next we search player names - FPlayer fplayer = FPlayers.i.find(name); - if (fplayer != null) - { - ret = fplayer.getFaction(); - } - } if (msg && ret == null) { - this.sendMessage(p.txt.parse("The faction or player \"

%s\" could not be found.", name)); + this.msg("The faction or player \"

%s\" could not be found.", name); } return ret; } + public Faction argAsFaction(int idx, Faction def, boolean msg) + { + return this.strAsFaction(this.argAsString(idx), def, msg); + } public Faction argAsFaction(int idx, Faction def) { return this.argAsFaction(idx, def, true); diff --git a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java index fcfe55e0..d6578beb 100644 --- a/src/com/massivecraft/factions/zcore/persist/EntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/EntityCollection.java @@ -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 { @@ -16,7 +17,7 @@ public abstract class EntityCollection // These must be instantiated in order to allow for different configuration (orders, comparators etc) private Collection entities; - private Map id2entity; + protected Map 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 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 // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java b/src/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java index 81404c7c..95d2df5e 100644 --- a/src/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java +++ b/src/com/massivecraft/factions/zcore/persist/PlayerEntityCollection.java @@ -42,5 +42,4 @@ public abstract class PlayerEntityCollection extends EntityCol } return entities; } - } diff --git a/src/com/massivecraft/factions/zcore/util/TextUtil.java b/src/com/massivecraft/factions/zcore/util/TextUtil.java index 5916ae62..c3f1019f 100644 --- a/src/com/massivecraft/factions/zcore/util/TextUtil.java +++ b/src/com/massivecraft/factions/zcore/util/TextUtil.java @@ -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 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; + } }