Rework the ARFaction. No best match. Matching is slow and dangerous.

This commit is contained in:
Olof Larsson 2014-10-13 08:11:00 +02:00
parent 6974c82bb6
commit b54293577d
4 changed files with 27 additions and 30 deletions

View File

@ -3,6 +3,7 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.util.MUtil;
@ -50,7 +51,7 @@ public class CmdFactionsMotd extends FactionsCommand
target = Txt.parse(target);
// Removal
if (target != null && MUtil.set("", "r", "remove", "d", "delete", "del", "e", "erase", "none", "null", "nothing").contains(target))
if (target != null && MassiveCore.NOTHING_REMOVE.contains(target))
{
target = null;
}

View File

@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.cmd.arg.ArgReaderAbstract;
import com.massivecraft.massivecore.cmd.arg.ArgResult;
import com.massivecraft.massivecore.util.IdUtil;
@ -28,14 +29,24 @@ public class ARFaction extends ArgReaderAbstract<Faction>
{
ArgResult<Faction> result = new ArgResult<Faction>();
// Nothing/Remove targets Wilderness
if (MassiveCore.NOTHING_REMOVE.contains(str))
{
result.setResult(FactionColl.get().getNone());
return result;
}
// Faction Id Exact
if (FactionColl.get().containsId(str))
{
result.setResult(FactionColl.get().get(str));
if (result.hasResult()) return result;
}
// Faction Name Exact
result.setResult(FactionColl.get().getByName(str));
if (result.hasResult()) return result;
// Faction Name Match
result.setResult(FactionColl.get().getBestNameMatch(str));
if (result.hasResult()) return result;
// MPlayer Name Exact
String id = IdUtil.getId(str);
MPlayer mplayer = MPlayer.get(id);

View File

@ -238,9 +238,9 @@ public class FactionColl extends Coll<Faction>
return errors;
}
public Faction getByName(String str)
public Faction getByName(String name)
{
String compStr = MiscUtil.getComparisonString(str);
String compStr = MiscUtil.getComparisonString(name);
for (Faction faction : this.getAll())
{
if (faction.getComparisonName().equals(compStr))
@ -251,21 +251,6 @@ public class FactionColl extends Coll<Faction>
return null;
}
public Faction getBestNameMatch(String searchFor)
{
Map<String, Faction> name2faction = new HashMap<String, Faction>();
// TODO: Slow index building
for (Faction faction : this.getAll())
{
name2faction.put(ChatColor.stripColor(faction.getName()), faction);
}
String tag = Txt.getBestCIStart(name2faction.keySet(), searchFor);
if (tag == null) return null;
return name2faction.get(tag);
}
public boolean isNameTaken(String str)
{
return this.getByName(str) != null;