mirror of
https://github.com/MassiveCraft/Factions.git
synced 2024-12-28 03:27:43 +01:00
Adding match mode for id search to MassiveCraftCore and improving the faction argument readers.
This commit is contained in:
parent
e6d45a6aa2
commit
5bf38ab0aa
@ -66,19 +66,4 @@ public class FPlayers extends PlayerEntityCollection<FPlayer>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Faction>
|
||||
{
|
||||
@ -132,7 +133,7 @@ public class Factions extends EntityCollection<Faction>
|
||||
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<Faction>
|
||||
return null;
|
||||
}
|
||||
|
||||
public Faction getBestTagMatch(String pattern)
|
||||
{
|
||||
Map<String, Faction> tag2faction = new HashMap<String, Faction>();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -311,7 +311,7 @@ public class P extends MPlugin
|
||||
public Set<String> getPlayersInFaction(String factionTag)
|
||||
{
|
||||
Set<String> players = new HashSet<String>();
|
||||
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<String> getOnlinePlayersInFaction(String factionTag)
|
||||
{
|
||||
Set<String> players = new HashSet<String>();
|
||||
Faction faction = Factions.i.findByTag(factionTag);
|
||||
Faction faction = Factions.i.getByTag(factionTag);
|
||||
if (faction != null)
|
||||
{
|
||||
for (FPlayer fplayer : faction.getFPlayersWhereOnline(true))
|
||||
|
@ -148,12 +148,11 @@ public abstract class FCommand extends MCommand<P>
|
||||
// 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);
|
||||
@ -165,11 +164,15 @@ public abstract class FCommand extends MCommand<P>
|
||||
|
||||
if (msg && ret == null)
|
||||
{
|
||||
this.sendMessage(p.txt.parse("<b>The player \"<p>%s<b>\" could not be found.", name));
|
||||
this.msg("<b>No player \"<p>%s<b>\" 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<P>
|
||||
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<P>
|
||||
|
||||
if (msg && ret == null)
|
||||
{
|
||||
this.sendMessage(p.txt.parse("<b>The player \"<p>%s<b>\" could not be found.", name));
|
||||
this.msg("<b>No player match found for \"<p>%s<b>\".", 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<P>
|
||||
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("<b>The faction or player \"<p>%s<b>\" could not be found.", name));
|
||||
this.msg("<b>The faction or player \"<p>%s<b>\" 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);
|
||||
|
@ -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
|
||||
// -------------------------------------------- //
|
||||
|
@ -42,5 +42,4 @@ public abstract class PlayerEntityCollection<E extends Entity> extends EntityCol
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user