A couple of measures to speed up tab completion further.

This commit is contained in:
Olof Larsson 2015-05-12 10:42:52 +02:00
parent 37000854be
commit 30f2460838
7 changed files with 72 additions and 54 deletions

View File

@ -1,13 +1,14 @@
package com.massivecraft.massivecore;
// Inspired by: String#regionMatches(ignoreCase, toffset, other, ooffset, len)
public class PredictateStartsWithIgnoreCase implements Predictate<String>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String prefix;
public String getPrefix() { return this.prefix; };
private final String prefixLower;
private final String prefixUpper;
// -------------------------------------------- //
// CONSTRUCT
@ -16,7 +17,9 @@ public class PredictateStartsWithIgnoreCase implements Predictate<String>
public static PredictateStartsWithIgnoreCase get(String prefix) { return new PredictateStartsWithIgnoreCase(prefix); }
public PredictateStartsWithIgnoreCase(String prefix)
{
this.prefix = prefix.toLowerCase();
if (prefix == null) throw new NullPointerException("prefix");
this.prefixLower = prefix.toLowerCase();
this.prefixUpper = prefix.toUpperCase();
}
// -------------------------------------------- //
@ -27,7 +30,15 @@ public class PredictateStartsWithIgnoreCase implements Predictate<String>
public boolean apply(String str)
{
if (str == null) return false;
return str.toLowerCase().startsWith(this.getPrefix());
int index = this.prefixLower.length();
if (str.length() < index) return false;
while (index-- > 0)
{
char c = str.charAt(index);
if (c == prefixLower.charAt(index)) continue;
if (c != prefixUpper.charAt(index)) return false;
}
return true;
}
}

View File

@ -1,13 +1,9 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.Iterator;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.store.SenderIdSourceMixinAllSenderIds;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
public class ARPlayer extends ARSenderIdAbstract<Player>
{
@ -19,7 +15,7 @@ public class ARPlayer extends ARSenderIdAbstract<Player>
public static ARPlayer get() { return i; }
private ARPlayer()
{
super(SenderIdSourceMixinAllSenderIds.get());
super(SenderIdSourceMixinAllSenderIds.get(), true, true);
}
// -------------------------------------------- //
@ -33,25 +29,4 @@ public class ARPlayer extends ARSenderIdAbstract<Player>
return IdUtil.getPlayer(senderId);
}
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
// Super Ret
Collection<String> ret = super.getTabList(sender, arg);
// Filter Ret
Iterator<String> iter = ret.iterator();
while (iter.hasNext())
{
String name = iter.next();
if ( ! MUtil.isValidPlayerName(name))
{
iter.remove();
}
}
// Return Ret
return ret;
}
}

View File

@ -15,7 +15,7 @@ public class ARSender extends ARSenderIdAbstract<CommandSender>
public static ARSender get() { return i; }
private ARSender()
{
super(SenderIdSourceMixinAllSenderIds.get());
super(SenderIdSourceMixinAllSenderIds.get(), true, false);
}
// -------------------------------------------- //

View File

@ -18,9 +18,15 @@ public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstrac
// CONSTRUCT
// -------------------------------------------- //
private ARSenderEntity(SenderColl<T> coll, boolean online)
private ARSenderEntity(SenderColl<T> coll, boolean onlineOnly, boolean playerOnly)
{
super(coll, online);
super(coll, onlineOnly, playerOnly);
this.coll = coll;
}
private ARSenderEntity(SenderColl<T> coll, boolean onlineOnly)
{
super(coll, onlineOnly);
this.coll = coll;
}
@ -34,7 +40,8 @@ public class ARSenderEntity<T extends SenderEntity<T>> extends ARSenderIdAbstrac
// GET
// -------------------------------------------- //
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean online) { return new ARSenderEntity<T>(coll, online); }
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean onlineOnly, boolean playerOnly) { return new ARSenderEntity<T>(coll, onlineOnly, playerOnly); }
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll, boolean onlineOnly) { return new ARSenderEntity<T>(coll, onlineOnly); }
public static <T extends SenderEntity<T>> ARSenderEntity<T> get(SenderColl<T> coll) { return new ARSenderEntity<T>(coll); }
// -------------------------------------------- //

View File

@ -9,9 +9,14 @@ public class ARSenderId extends ARSenderIdAbstract<String>
// CONSTRUCT
// -------------------------------------------- //
private ARSenderId(SenderIdSource source, boolean online)
private ARSenderId(SenderIdSource source, boolean onlineOnly, boolean playerOnly)
{
super(source, online);
super(source, onlineOnly, playerOnly);
}
private ARSenderId(SenderIdSource source, boolean onlineOnly)
{
super(source, onlineOnly);
}
private ARSenderId(SenderIdSource source)
@ -30,7 +35,8 @@ public class ARSenderId extends ARSenderIdAbstract<String>
// GET
// -------------------------------------------- //
public static ARSenderId get(SenderIdSource source, boolean online) { return new ARSenderId(source, online); }
public static ARSenderId get(SenderIdSource source, boolean onlineOnly, boolean playerOnly) { return new ARSenderId(source, onlineOnly, playerOnly); }
public static ARSenderId get(SenderIdSource source, boolean onlineOnly) { return new ARSenderId(source, onlineOnly); }
public static ARSenderId get(SenderIdSource source) { return new ARSenderId(source); }
// -------------------------------------------- //

View File

@ -1,12 +1,13 @@
package com.massivecraft.massivecore.cmd.arg;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.command.CommandSender;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.store.SenderIdSource;
import com.massivecraft.massivecore.util.IdUtil;
@ -19,16 +20,23 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
// -------------------------------------------- //
protected final SenderIdSource source;
protected final boolean online;
protected final boolean onlineOnly;
protected final boolean playerOnly;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public ARSenderIdAbstract(SenderIdSource source, boolean online)
public ARSenderIdAbstract(SenderIdSource source, boolean onlineOnly, boolean playerOnly)
{
this.source = source;
this.online = online;
this.onlineOnly = onlineOnly;
this.playerOnly = playerOnly;
}
public ARSenderIdAbstract(SenderIdSource source, boolean online)
{
this(source, online, false);
}
public ARSenderIdAbstract(SenderIdSource source)
@ -49,7 +57,7 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
@Override
public String getTypeName()
{
if (online) return "online player";
if (onlineOnly) return "online player";
else return "player";
}
@ -94,28 +102,25 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
@Override
public Collection<String> getTabList(CommandSender sender, String arg)
{
// Create Ret
Set<String> ret = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
// Fill Ret
Set<String> names;
if (online)
if (onlineOnly)
{
names = IdUtil.getOnlineNames();
names = playerOnly ? IdUtil.getOnlinePlayerNames() : IdUtil.getOnlineNames();
List<String> ret = new MassiveList<String>();
for (String name : names)
{
if ( ! Mixin.canSee(sender, name)) continue;
ret.add(name);
}
return ret;
}
else
{
names = IdUtil.getAllNames();
ret.addAll(names);
}
names = playerOnly ? IdUtil.getAllPlayerNames() : IdUtil.getAllNames();
// Return Ret
return ret;
return names;
}
}
// -------------------------------------------- //
@ -137,7 +142,7 @@ public abstract class ARSenderIdAbstract<T> extends ARAbstract<T>
if ( ! coll.contains(senderId)) continue;
// ... and the online check passes ...
if (this.online && !Mixin.isOnline(senderId)) continue;
if (this.onlineOnly && !Mixin.isOnline(senderId)) continue;
// ... and the result is non null ...
T result = this.getResultForSenderId(senderId);

View File

@ -119,6 +119,9 @@ public class IdUtil implements Listener, Runnable
private static Set<String> onlineNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getOnlineNames() { return Collections.unmodifiableSet(onlineNames); }
private static Set<String> onlinePlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getOnlinePlayerNames() { return Collections.unmodifiableSet(onlinePlayerNames); }
private static Set<String> allIds = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllIds() { return Collections.unmodifiableSet(allIds); }
@ -126,6 +129,9 @@ public class IdUtil implements Listener, Runnable
private static Set<String> allNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllNames() { return Collections.unmodifiableSet(allNames); }
private static Set<String> allPlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
public static Set<String> getAllPlayerNames() { return Collections.unmodifiableSet(allPlayerNames); }
// -------------------------------------------- //
// REGISTRY
// -------------------------------------------- //
@ -215,7 +221,9 @@ public class IdUtil implements Listener, Runnable
if (name == null) throw new NullPointerException("name");
onlineNames.remove(name);
onlinePlayerNames.remove(name);
allNames.remove(name);
allPlayerNames.remove(name);
IdData data = nameToData.remove(name);
if (data == null) return null;
@ -243,10 +251,14 @@ public class IdUtil implements Listener, Runnable
if (id != null && name != null)
{
boolean player = MUtil.isValidPlayerName(name);
if (online) onlineIds.add(id);
allIds.add(id);
if (online && player) onlinePlayerNames.add(name);
if (online) onlineNames.add(name);
if (player) allPlayerNames.add(name);
allNames.add(name);
}
}
@ -336,9 +348,11 @@ public class IdUtil implements Listener, Runnable
onlineIds.clear();
onlineNames.clear();
onlinePlayerNames.clear();
allIds.clear();
allNames.clear();
allPlayerNames.clear();
// Load Datas
loadDatas();